295 lines
8.8 KiB
Dart
295 lines
8.8 KiB
Dart
// ignore: depend_on_referenced_packages
|
|
import 'package:collection/collection.dart';
|
|
import 'package:didvan/main.dart';
|
|
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/user.dart';
|
|
import 'package:didvan/models/view/alert_data.dart';
|
|
import 'package:didvan/providers/core.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:didvan/services/notification/firebase_api.dart';
|
|
import 'package:didvan/services/storage/storage.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
|
|
class UserProvider extends CoreProvier {
|
|
late User user;
|
|
bool isAuthenticated = false;
|
|
int _unreadMessageCount = 0;
|
|
|
|
set unreadMessageCount(int value) {
|
|
if (value < 0) {
|
|
return;
|
|
}
|
|
_unreadMessageCount = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
int get unreadMessageCount => _unreadMessageCount;
|
|
|
|
// static final List<MapEntry> _radarMarkQueue = [];
|
|
// static final List<MapEntry> _newsMarkQueue = [];
|
|
// static final List<MapEntry> _studioMarkQueue = [];
|
|
static final List<MapEntry> _statisticMarkQueue = [];
|
|
static final List<Map> _itemMarkQueue = [];
|
|
|
|
Future<String?> setAndGetToken({String? newToken}) async {
|
|
try {
|
|
if (newToken == null) {
|
|
final token = await StorageService.getValue(key: 'token');
|
|
return token;
|
|
}
|
|
await StorageService.setValue(key: 'token', value: newToken);
|
|
return null;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<bool> getUserInfo() async {
|
|
isAuthenticated = true;
|
|
final RequestService service = RequestService(RequestHelper.userInfo);
|
|
await service.httpGet();
|
|
if (service.statusCode == 401 ||
|
|
(service.isSuccess && service.result['user'] == null)) {
|
|
return false;
|
|
}
|
|
if (service.isSuccess) {
|
|
try {
|
|
user = User.fromJson(service.result['user']);
|
|
await StorageService.setValue(
|
|
key: 'notificationTimeRangeStart',
|
|
value: service.result['user']['start'],
|
|
);
|
|
await StorageService.setValue(
|
|
key: 'notificationTimeRangeEnd',
|
|
value: service.result['user']['end'],
|
|
);
|
|
|
|
FirebaseApi()
|
|
.initNotification()
|
|
.then((value) => _registerFirebaseToken());
|
|
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
throw 'Getting user from API failed!';
|
|
}
|
|
|
|
Future<void> _registerFirebaseToken() async {
|
|
if (FirebaseApi.fcmToken != null) {
|
|
final service = RequestService(RequestHelper.firebaseToken, body: {
|
|
'token': FirebaseApi.fcmToken,
|
|
});
|
|
await service.put();
|
|
}
|
|
}
|
|
|
|
Future<bool> setProfilePhoto(dynamic file) async {
|
|
appState = AppState.isolatedBusy;
|
|
final RequestService service =
|
|
RequestService(RequestHelper.updateProfilePhoto);
|
|
await service.multipart(
|
|
file: file,
|
|
method: 'PUT',
|
|
fileName: 'user-profile',
|
|
fieldName: 'photo',
|
|
mediaExtension: 'jpg',
|
|
mediaFormat: 'image',
|
|
);
|
|
if (service.isSuccess) {
|
|
user = user.copyWith(photo: service.result['photo']);
|
|
appState = AppState.idle;
|
|
return true;
|
|
}
|
|
appState = AppState.failed;
|
|
return false;
|
|
}
|
|
|
|
Future<bool> deleteProfilePhoto() async {
|
|
appState = AppState.isolatedBusy;
|
|
final RequestService service =
|
|
RequestService(RequestHelper.updateProfilePhoto);
|
|
await service.delete();
|
|
if (service.isSuccess) {
|
|
user = user.copyWith(photo: null);
|
|
appState = AppState.idle;
|
|
return true;
|
|
}
|
|
appState = AppState.idle;
|
|
return false;
|
|
}
|
|
|
|
Future<bool?> checkUsername(String username) async {
|
|
if (user.username == username) return true;
|
|
final RequestService service = RequestService(
|
|
RequestHelper.checkUsername,
|
|
body: {'username': username},
|
|
);
|
|
await service.post();
|
|
if (service.isSuccess) {
|
|
return service.result['available'];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> editProfile(
|
|
String fullName,
|
|
String? username,
|
|
String? email,
|
|
) async {
|
|
appState = AppState.isolatedBusy;
|
|
final service = RequestService(
|
|
RequestHelper.updateProfile,
|
|
body: {
|
|
'fullName': fullName,
|
|
'email': email,
|
|
'username': username,
|
|
},
|
|
);
|
|
await service.put();
|
|
if (service.isSuccess) {
|
|
user = user.copyWith(
|
|
fullName: fullName,
|
|
email: email,
|
|
username: username,
|
|
photo: user.photo);
|
|
appState = AppState.idle;
|
|
ActionSheetUtils(navigatorKey.currentContext!).pop();
|
|
ActionSheetUtils(navigatorKey.currentContext!).showAlert(
|
|
AlertData(
|
|
message: 'پروفایل با موفقیت ویرایش شد',
|
|
aLertType: ALertType.success,
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
appState = AppState.idle;
|
|
ActionSheetUtils(navigatorKey.currentContext!).showAlert(
|
|
AlertData(
|
|
message: service.errorMessage,
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> changeItemMark(String type, int id, bool? value,
|
|
{String? description}) async {
|
|
_itemMarkQueue.add({
|
|
'type': type,
|
|
'id': id,
|
|
'value': value,
|
|
'description': description,
|
|
});
|
|
Future.delayed(const Duration(milliseconds: 500), () async {
|
|
final lastChange =
|
|
_itemMarkQueue.lastWhereOrNull((item) => item['id'] == id);
|
|
if (lastChange == null) return;
|
|
final service = RequestService(
|
|
RequestHelper.editItemBookmark(type, id),
|
|
body: {'description': lastChange['description']},
|
|
);
|
|
if (lastChange['value'] == true) {
|
|
await service.post();
|
|
} else if (lastChange['value'] == false) {
|
|
await service.delete();
|
|
} else {
|
|
service.put();
|
|
}
|
|
_itemMarkQueue.removeWhere((element) => element['id'] == id);
|
|
});
|
|
}
|
|
|
|
static Future<void> changeItemLiked(
|
|
String type,
|
|
int id,
|
|
bool value,
|
|
) async {
|
|
if (type == 'infography') {
|
|
type = 'banner';
|
|
}
|
|
final service = RequestService(
|
|
RequestHelper.editItemLike(type, id),
|
|
);
|
|
|
|
if (value) {
|
|
await service.post();
|
|
} else {
|
|
await service.delete();
|
|
}
|
|
}
|
|
|
|
// static Future<void> changeRadarMark(int id, bool value) async {
|
|
// _radarMarkQueue.add(MapEntry(id, value));
|
|
// Future.delayed(const Duration(milliseconds: 500), () async {
|
|
// final MapEntry? lastChange =
|
|
// _radarMarkQueue.lastWhereOrNull((item) => item.key == id);
|
|
// if (lastChange == null) return;
|
|
// final service = RequestService(RequestHelper.mark(id, 'radar'));
|
|
// if (lastChange.value) {
|
|
// await service.post();
|
|
// } else {
|
|
// await service.delete();
|
|
// }
|
|
// _radarMarkQueue.removeWhere((element) => element.key == id);
|
|
// });
|
|
// }
|
|
|
|
// static Future<void> changeStudioMark(int id, bool value) async {
|
|
// _studioMarkQueue.add(MapEntry(id, value));
|
|
// Future.delayed(const Duration(milliseconds: 500), () async {
|
|
// final MapEntry? lastChange =
|
|
// _studioMarkQueue.lastWhereOrNull((item) => item.key == id);
|
|
// if (lastChange == null) return;
|
|
// final service = RequestService(RequestHelper.mark(id, 'studio'));
|
|
// if (lastChange.value) {
|
|
// await service.post();
|
|
// } else {
|
|
// await service.delete();
|
|
// }
|
|
// _studioMarkQueue.removeWhere((element) => element.key == id);
|
|
// });
|
|
// }
|
|
|
|
// static Future<void> changeNewsMark(int id, bool value) async {
|
|
// _newsMarkQueue.add(MapEntry(id, value));
|
|
// Future.delayed(const Duration(milliseconds: 500), () async {
|
|
// final MapEntry? lastChange =
|
|
// _newsMarkQueue.lastWhereOrNull((item) => item.key == id);
|
|
// if (lastChange == null) return;
|
|
// final service = RequestService(RequestHelper.mark(id, 'news'));
|
|
// if (lastChange.value) {
|
|
// await service.post();
|
|
// } else {
|
|
// await service.delete();
|
|
// }
|
|
// _newsMarkQueue.removeWhere((element) => element.key == id);
|
|
// });
|
|
// }
|
|
|
|
static Future<void> changeStatisticMark(int id, bool value) async {
|
|
_statisticMarkQueue.add(MapEntry(id, value));
|
|
Future.delayed(const Duration(milliseconds: 500), () async {
|
|
final MapEntry? lastChange =
|
|
_statisticMarkQueue.lastWhereOrNull((item) => item.key == id);
|
|
if (lastChange == null) return;
|
|
final service = RequestService(RequestHelper.mark(id, 'statistic'));
|
|
if (lastChange.value) {
|
|
await service.post();
|
|
} else {
|
|
await service.delete();
|
|
}
|
|
_statisticMarkQueue.removeWhere((element) => element.key == id);
|
|
});
|
|
}
|
|
|
|
// Future<void> getUnreadMessageCount() async {
|
|
// final RequestService service = RequestService(RequestHelper.directs);
|
|
// await service.httpGet();
|
|
// if (service.isSuccess) {
|
|
// _unreadMessageCount = service.result['unread'] ?? 0;
|
|
// }
|
|
// }
|
|
}
|