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

181 lines
4.7 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';
import 'package:package_info/package_info.dart';
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
2020-09-20 05:34:49 +06:30
String messagingToken;
2020-09-04 01:42:58 +06:30
User user;
PackageInfo packageInfo;
2020-09-20 05:34:49 +06:30
set setMessaginToken(token) {
this.messagingToken = token;
uploadMsgToken();
}
2020-09-12 03:34:52 +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;
2020-09-04 01:42:58 +06:30
MainModel() {
NetworkConnectivity.instance.statusStream.listen((data) {
bool _isOnline = data["isOnline"];
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() {
2020-09-13 21:49:39 +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() {
return this.user != null && this.user.hasSupport();
}
2020-09-18 21:33:41 +06:30
bool paymentMethodsEditable() {
return this.user != null && this.user.hasSupport();
}
2020-09-12 03:34:52 +06:30
bool termEditable() {
2020-09-13 21:49:39 +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() {
2020-09-13 21:49:39 +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() {
return user != null && user.isCustomer();
2020-09-04 01:42:58 +06:30
}
2020-09-11 16:14:36 +06:30
bool isSysAdmin() {
return this.user != null && this.user.hasSysAdmin();
2020-09-04 01:42:58 +06:30
}
bool isAdmin() {
return this.user != null && this.user.hasAdmin();
}
2020-09-22 03:52:48 +06:30
// userListener should never be closed
StreamSubscription<User> userListener;
_init() async {
await _listenSetting();
2020-09-12 03:34:52 +06:30
this.isFirstLaunch = await SharedPref.isFirstLaunch();
this.isFirstLaunch = this.isFirstLaunch ?? true;
2020-09-04 01:42:58 +06:30
this.packageInfo = await PackageInfo.fromPlatform();
2020-09-22 03:52:48 +06:30
if (userListener != null) userListener.cancel();
userListener =
Services.instance.authService.getUserStream().listen((_user) {
if (_user != null) {
models.forEach((m) => m.initUser(_user));
// call diffPrivileges if privilege changed or first time login
if (this.user == null || _user.diffPrivileges(this.user)) {
models.forEach((m) => m.privilegeChanged());
}
if (this.user == null) {
uploadMsgToken();
}
} else {
if (this.user != null) {
models.forEach((m) => m.logout());
}
2020-09-17 06:02:48 +06:30
}
2020-09-22 03:52:48 +06:30
this.user = _user;
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;
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;
return int.parse(packageInfo.buildNumber) >= setting.supportBuildNum;
}
Future<AuthResult> sendSms(String phoneNumber) {
return Services.instance.authService.sendSmsCodeToPhoneNumber(phoneNumber);
}
Future<AuthResult> signin(String smsCode) async {
AuthResult authResult =
await Services.instance.authService.signInWithSmsCode(smsCode);
return authResult;
}
2020-09-20 05:34:49 +06:30
Future<void> uploadMsgToken() {
if (messagingToken == null || user == null) return null;
return Services.instance.userService.uploadMsgToken(messagingToken);
}
Future<void> removeMsgToken() {
if (messagingToken == null || user == null) return null;
return Services.instance.userService.removeMsgToken(messagingToken);
}
Future<void> signout() async {
2020-10-22 04:14:53 +06:30
try {
await removeMsgToken();
} catch (e) {}
2020-09-22 03:52:48 +06:30
await Services.instance.authService.signout();
models.forEach((m) => m.logout());
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();
}
}