Files
fcs/lib/pages/main/model/main_model.dart

229 lines
5.9 KiB
Dart
Raw Normal View History

2020-09-04 01:42:58 +06:30
import 'dart:async';
2020-10-07 02:33:06 +06:30
import 'package:fcs/data/services/services.dart';
import 'package:fcs/domain/entities/auth_result.dart';
import 'package:fcs/domain/entities/setting.dart';
import 'package:fcs/domain/entities/user.dart';
import 'package:fcs/helpers/network_connectivity.dart';
import 'package:fcs/helpers/shared_pref.dart';
import 'package:fcs/pages/main/model/base_model.dart';
2020-09-04 01:42:58 +06:30
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
2024-12-27 22:36:48 +06:30
import 'package:package_info_plus/package_info_plus.dart';
2020-09-04 01:42:58 +06:30
class MainModel extends ChangeNotifier {
final log = Logger('MainModel');
2020-09-11 16:14:36 +06:30
List<BaseModel> models = [];
2020-09-04 01:42:58 +06:30
2021-09-10 15:15:20 +06:30
String? messagingToken;
User? user;
2024-10-04 13:55:59 +06:30
User? _fbUser;
2021-09-10 15:15:20 +06:30
PackageInfo? packageInfo;
2020-09-04 01:42:58 +06:30
2020-09-20 05:34:49 +06:30
set setMessaginToken(token) {
this.messagingToken = token;
2024-02-22 17:16:19 +06:30
_uploadMsgToken();
2020-09-20 05:34:49 +06:30
}
2021-09-10 15:15:20 +06:30
Setting? setting;
2020-09-04 01:42:58 +06:30
bool isLoaded = false;
bool isOnline = false;
2020-09-12 03:34:52 +06:30
bool isFirstLaunch = false;
2024-10-04 13:55:59 +06:30
bool isLockOn = false;
bool get isPinLogin => user?.isPinLogin ?? false;
2020-09-04 01:42:58 +06:30
MainModel() {
NetworkConnectivity.instance.statusStream.listen((data) {
2024-12-27 22:36:48 +06:30
bool _isOnline = data != ConnectivityStatus.none;
2020-09-04 01:42:58 +06:30
if (_isOnline && !this.isOnline) {
2020-09-22 03:52:48 +06:30
_init();
2020-09-04 01:42:58 +06:30
}
this.isOnline = _isOnline;
notifyListeners();
});
}
2020-09-12 03:34:52 +06:30
bool faqEditable() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSupport();
2020-09-04 01:42:58 +06:30
}
2020-09-12 03:34:52 +06:30
2020-10-15 03:06:13 +06:30
bool rateEditable() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSupport();
2020-10-15 03:06:13 +06:30
}
2020-09-18 21:33:41 +06:30
bool paymentMethodsEditable() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSupport();
2020-09-18 21:33:41 +06:30
}
2020-09-12 03:34:52 +06:30
bool termEditable() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSupport();
2020-09-04 01:42:58 +06:30
}
2020-09-12 03:34:52 +06:30
bool contactEditable() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSupport();
2020-09-04 01:42:58 +06:30
}
2020-09-11 16:14:36 +06:30
bool isLogin() {
return this.user != null;
2020-09-04 01:42:58 +06:30
}
2020-09-11 16:14:36 +06:30
bool isCustomer() {
2021-09-10 15:15:20 +06:30
return user != null && user!.isCustomer();
2020-09-04 01:42:58 +06:30
}
2020-09-11 16:14:36 +06:30
bool isSysAdmin() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasSysAdmin();
2020-09-04 01:42:58 +06:30
}
bool isAdmin() {
2021-09-10 15:15:20 +06:30
return this.user != null && this.user!.hasAdmin();
2020-09-04 01:42:58 +06:30
}
2020-09-22 03:52:48 +06:30
// userListener should never be closed
2021-09-10 15:15:20 +06:30
StreamSubscription<User?>? userListener;
2020-09-22 03:52:48 +06:30
_init() async {
await _listenSetting();
2021-09-10 15:15:20 +06:30
this.isFirstLaunch = await SharedPref.isFirstLaunch() ?? true;
2020-09-04 01:42:58 +06:30
this.packageInfo = await PackageInfo.fromPlatform();
2024-10-04 13:55:59 +06:30
this.isLockOn = await SharedPref.getPinLockOn() ?? false;
2020-09-04 01:42:58 +06:30
2024-02-22 17:16:19 +06:30
userListener?.cancel();
2020-09-22 03:52:48 +06:30
userListener =
Services.instance.authService.getUserStream().listen((_user) {
2024-02-22 17:16:19 +06:30
bool isFirstTime = user == null && _user != null;
bool diffPrivilege =
_user != null && (user == null || user!.diffPrivileges(_user));
bool loggingOut = user != null && _user == null;
2024-10-04 13:55:59 +06:30
if (_user != null) {
if (!_user.isPinLogin) {
_fbUser = _user;
}
}
if ((_fbUser?.id == _user?.id)) {
_user?.pinToken = null;
}
2024-02-22 17:16:19 +06:30
user = _user;
2020-09-22 03:52:48 +06:30
if (_user != null) {
2024-02-22 17:16:19 +06:30
for (final m in models) {
m.initUser(_user);
if (diffPrivilege) {
m.privilegeChanged();
}
2020-09-22 03:52:48 +06:30
}
2024-02-22 17:16:19 +06:30
}
2024-10-04 13:55:59 +06:30
2024-02-22 17:16:19 +06:30
if (loggingOut) {
for (final m in models) {
m.logout();
2020-09-22 03:52:48 +06:30
}
2020-09-17 06:02:48 +06:30
}
2024-02-22 17:16:19 +06:30
if (isFirstTime) {
_uploadMsgToken();
}
2020-09-22 03:52:48 +06:30
isLoaded = true;
notifyListeners();
});
2020-09-13 21:49:39 +06:30
}
2020-09-04 01:42:58 +06:30
2020-09-22 03:52:48 +06:30
void addModel(BaseModel model) {
models.add(model);
2020-09-11 16:14:36 +06:30
}
2020-09-04 01:42:58 +06:30
2020-09-22 03:52:48 +06:30
Future<void> _listenSetting() async {
2020-09-04 01:42:58 +06:30
try {
2020-09-07 16:05:28 +06:30
Services.instance.authService.getSetting().listen((event) {
2020-09-10 02:13:22 +06:30
this.setting = event;
Services.setting = event;
2020-09-22 03:52:48 +06:30
models.forEach((m) => m.initSetting(setting));
2020-09-10 02:13:22 +06:30
notifyListeners();
2020-09-07 16:05:28 +06:30
});
2020-09-04 01:42:58 +06:30
} finally {}
}
bool isSupport() {
if (packageInfo == null || setting == null) return false;
2021-09-10 15:15:20 +06:30
return int.parse(packageInfo!.buildNumber) >= setting!.supportBuildNum;
2020-09-04 01:42:58 +06:30
}
Future<AuthResult> sendSms(String phoneNumber,
{String? forceResendingToken}) {
return Services.instance.authService.sendSmsCodeToPhoneNumber(phoneNumber,
forceResendingToken: forceResendingToken);
2020-09-04 01:42:58 +06:30
}
Future<AuthResult> signin(String smsCode) async {
AuthResult authResult =
await Services.instance.authService.signInWithSmsCode(smsCode);
return authResult;
}
2024-02-22 17:16:19 +06:30
Future<void> _uploadMsgToken() {
2024-02-23 17:05:51 +06:30
if (messagingToken == null || user == null) return Future.value();
2021-09-10 15:15:20 +06:30
return Services.instance.userService.uploadMsgToken(messagingToken!);
2020-09-20 05:34:49 +06:30
}
2024-02-22 17:16:19 +06:30
Future<void> _removeMsgToken() {
2024-02-23 17:05:51 +06:30
if (messagingToken == null || user == null) return Future.value();
2021-09-10 15:15:20 +06:30
return Services.instance.userService.removeMsgToken(messagingToken!);
2020-09-20 05:34:49 +06:30
}
Future<void> signout() async {
2020-10-22 04:14:53 +06:30
try {
2024-02-23 17:05:51 +06:30
await Services.instance.authService.signoutStart();
2024-02-22 17:16:19 +06:30
await _removeMsgToken();
2024-02-23 17:05:51 +06:30
for (var i = 0; i < models.length; i++) {
models[i].initUser(null);
models[i].logout();
}
await Services.instance.authService.signoutEnd();
2024-02-22 17:16:19 +06:30
} catch (e) {
2024-02-23 17:05:51 +06:30
log.info("signout:${e.toString()}");
2024-02-22 17:16:19 +06:30
}
2020-09-04 01:42:58 +06:30
}
2020-09-13 21:49:39 +06:30
Future<bool> hasInvite() async {
return Services.instance.authService.hasInvite();
}
2020-09-04 01:42:58 +06:30
Future<void> signup(String userName) async {
await Services.instance.authService.signup(userName);
2020-09-13 21:49:39 +06:30
}
2020-09-17 06:02:48 +06:30
Future<void> joinInvite(String userName) async {
await Services.instance.authService.joinInvite(userName);
notifyListeners();
}
2020-10-11 02:17:23 +06:30
Future<void> updateProfileName(String newUserName) async {
await Services.instance.authService.updateProfileName(newUserName);
notifyListeners();
}
Future<void> updatePreferredCurrency(String currency) async {
await Services.instance.authService.updatePreferredCurrency(currency);
2020-09-04 01:42:58 +06:30
notifyListeners();
}
2024-03-05 17:09:04 +06:30
Future<void> deleteAccount() async {
return await Services.instance.authService.deleteAccount();
}
2024-10-04 13:55:59 +06:30
Future<void> pinLogin({required String fcsID, required String pin}) async {
await Services.instance.authService
.pinLogin(fcsID: fcsID, pin: pin, currentUserId: _fbUser?.id ?? '');
}
Future<void> logoutPinAccount() async {
await Services.instance.authService.logoutPinAccount();
}
2020-09-04 01:42:58 +06:30
}