2026-08-10 14:12:45 -07:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2022-05-28 22:35:45 -05:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2025-03-14 08:50:26 +05:30
|
|
|
import 'package:immich_mobile/domain/services/user.service.dart';
|
|
|
|
|
import 'package:immich_mobile/providers/infrastructure/user.provider.dart';
|
2025-09-12 18:56:00 -04:00
|
|
|
import 'package:immich_mobile/utils/debug_print.dart';
|
2022-05-28 22:35:45 -05:00
|
|
|
|
2026-08-10 14:12:45 -07:00
|
|
|
part 'upload_profile_image.provider.freezed.dart';
|
2022-05-28 22:35:45 -05:00
|
|
|
|
2026-08-10 14:12:45 -07:00
|
|
|
enum UploadProfileStatus { idle, loading, success, failure }
|
2022-05-28 22:35:45 -05:00
|
|
|
|
2026-08-10 14:12:45 -07:00
|
|
|
@freezed
|
|
|
|
|
abstract class UploadProfileImageState with _$UploadProfileImageState {
|
|
|
|
|
const factory UploadProfileImageState({required UploadProfileStatus status, required String profileImagePath}) =
|
|
|
|
|
_UploadProfileImageState;
|
2022-05-28 22:35:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
class UploadProfileImageNotifier extends StateNotifier<UploadProfileImageState> {
|
2025-03-14 08:50:26 +05:30
|
|
|
UploadProfileImageNotifier(this._userService)
|
2025-07-29 00:34:03 +05:30
|
|
|
: super(const UploadProfileImageState(profileImagePath: '', status: UploadProfileStatus.idle));
|
2022-05-28 22:35:45 -05:00
|
|
|
|
2025-03-14 08:50:26 +05:30
|
|
|
final UserService _userService;
|
2022-06-26 03:46:51 +09:00
|
|
|
|
2026-02-22 07:02:33 +01:00
|
|
|
Future<bool> upload(XFile file, {String? fileName}) async {
|
2022-05-28 22:35:45 -05:00
|
|
|
state = state.copyWith(status: UploadProfileStatus.loading);
|
|
|
|
|
|
2026-07-30 01:56:46 -07:00
|
|
|
final profileImagePath = await _userService.createProfileImage(fileName ?? file.name, await file.readAsBytes());
|
2022-05-28 22:35:45 -05:00
|
|
|
|
2025-03-14 08:50:26 +05:30
|
|
|
if (profileImagePath != null) {
|
2025-09-12 18:56:00 -04:00
|
|
|
dPrint(() => "Successfully upload profile image");
|
2025-07-29 00:34:03 +05:30
|
|
|
state = state.copyWith(status: UploadProfileStatus.success, profileImagePath: profileImagePath);
|
2022-05-28 22:35:45 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state = state.copyWith(status: UploadProfileStatus.failure);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 08:07:22 +05:30
|
|
|
final uploadProfileImageProvider = StateNotifierProvider<UploadProfileImageNotifier, UploadProfileImageState>(
|
2026-07-30 01:56:46 -07:00
|
|
|
(ref) => UploadProfileImageNotifier(ref.watch(userServiceProvider)),
|
2022-07-13 07:23:48 -05:00
|
|
|
);
|