Yimaru-Mobile/lib/services/image_downloader_service.dart
BisratHailu 7f6a5f1d34 -feat(progress): Integrat my progress section with api.
-fix(localization): Localization state issue fixed.
-fix(profile_image): Fix profile image downloading issue.
2026-06-01 01:34:15 +03:00

58 lines
1.5 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import '../app/app.locator.dart';
import '../models/refresh_object.dart';
import '../ui/common/enmus.dart';
import 'api_service.dart';
import 'dio_service.dart';
class ImageDownloaderService {
// Dependency injection
final _service = locator<DioService>();
final _apiService = locator<ApiService>();
final Dio _dio = Dio();
// Image downloader
Future<String?> downloader(String? networkImage) async {
try {
File? image;
String? profileImage = networkImage;
final Directory appDir = await getApplicationDocumentsDirectory();
Map<String, dynamic> data = {'reference': profileImage};
Map<String, dynamic> response = await _apiService.refreshObject(data);
if (response['status'] == ResponseStatus.success) {
RefreshObject object = response['data'] as RefreshObject;
profileImage = object.url;
}
if (profileImage != null) {
final Response profileImageResponse = await _dio.get<List<int>>(
profileImage,
options: Options(
responseType: ResponseType.bytes,
),
);
final localImagePath = join(appDir.path, 'profile.jpg');
image = File(localImagePath);
image.writeAsBytes(profileImageResponse.data);
return image.path;
}
return null;
} catch (e) {
return null;
}
}
}