2022-05-28 20:35:45 -07:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
2024-05-02 13:59:14 -07:00
|
|
|
import 'package:immich_mobile/services/user.service.dart';
|
2022-05-28 20:35:45 -07:00
|
|
|
|
|
|
|
enum UploadProfileStatus {
|
|
|
|
idle,
|
|
|
|
loading,
|
|
|
|
success,
|
|
|
|
failure,
|
|
|
|
}
|
|
|
|
|
|
|
|
class UploadProfileImageState {
|
|
|
|
// enum
|
|
|
|
final UploadProfileStatus status;
|
|
|
|
final String profileImagePath;
|
|
|
|
UploadProfileImageState({
|
|
|
|
required this.status,
|
|
|
|
required this.profileImagePath,
|
|
|
|
});
|
|
|
|
|
|
|
|
UploadProfileImageState copyWith({
|
|
|
|
UploadProfileStatus? status,
|
|
|
|
String? profileImagePath,
|
|
|
|
}) {
|
|
|
|
return UploadProfileImageState(
|
|
|
|
status: status ?? this.status,
|
|
|
|
profileImagePath: profileImagePath ?? this.profileImagePath,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
final result = <String, dynamic>{};
|
|
|
|
|
|
|
|
result.addAll({'status': status.index});
|
|
|
|
result.addAll({'profileImagePath': profileImagePath});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
factory UploadProfileImageState.fromMap(Map<String, dynamic> map) {
|
|
|
|
return UploadProfileImageState(
|
|
|
|
status: UploadProfileStatus.values[map['status'] ?? 0],
|
|
|
|
profileImagePath: map['profileImagePath'] ?? '',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String toJson() => json.encode(toMap());
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
factory UploadProfileImageState.fromJson(String source) =>
|
|
|
|
UploadProfileImageState.fromMap(json.decode(source));
|
2022-05-28 20:35:45 -07:00
|
|
|
|
|
|
|
@override
|
2022-06-25 11:46:51 -07:00
|
|
|
String toString() =>
|
|
|
|
'UploadProfileImageState(status: $status, profileImagePath: $profileImagePath)';
|
2022-05-28 20:35:45 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) return true;
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
return other is UploadProfileImageState &&
|
|
|
|
other.status == status &&
|
|
|
|
other.profileImagePath == profileImagePath;
|
2022-05-28 20:35:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => status.hashCode ^ profileImagePath.hashCode;
|
|
|
|
}
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
class UploadProfileImageNotifier
|
|
|
|
extends StateNotifier<UploadProfileImageState> {
|
|
|
|
UploadProfileImageNotifier(this._userSErvice)
|
2022-07-13 05:23:48 -07:00
|
|
|
: super(
|
|
|
|
UploadProfileImageState(
|
|
|
|
profileImagePath: '',
|
|
|
|
status: UploadProfileStatus.idle,
|
|
|
|
),
|
|
|
|
);
|
2022-05-28 20:35:45 -07:00
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
final UserService _userSErvice;
|
|
|
|
|
2022-05-28 20:35:45 -07:00
|
|
|
Future<bool> upload(XFile file) async {
|
|
|
|
state = state.copyWith(status: UploadProfileStatus.loading);
|
|
|
|
|
2022-06-25 11:46:51 -07:00
|
|
|
var res = await _userSErvice.uploadProfileImage(file);
|
2022-05-28 20:35:45 -07:00
|
|
|
|
|
|
|
if (res != null) {
|
2024-02-14 01:48:59 -07:00
|
|
|
debugPrint("Successfully upload profile image");
|
2022-06-25 11:46:51 -07:00
|
|
|
state = state.copyWith(
|
2022-07-13 05:23:48 -07:00
|
|
|
status: UploadProfileStatus.success,
|
|
|
|
profileImagePath: res.profileImagePath,
|
|
|
|
);
|
2022-05-28 20:35:45 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = state.copyWith(status: UploadProfileStatus.failure);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final uploadProfileImageProvider =
|
2022-06-25 11:46:51 -07:00
|
|
|
StateNotifierProvider<UploadProfileImageNotifier, UploadProfileImageState>(
|
2022-07-13 05:23:48 -07:00
|
|
|
((ref) => UploadProfileImageNotifier(ref.watch(userServiceProvider))),
|
|
|
|
);
|