223 lines
5.8 KiB
Dart
223 lines
5.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
|
|
import 'package:fcs/fcs/common/domain/entities/auth_result.dart';
|
|
import 'package:fcs/fcs/common/domain/entities/auth_status.dart';
|
|
import 'package:fcs/fcs/common/domain/entities/setting.dart';
|
|
import 'package:fcs/fcs/common/domain/entities/user.dart';
|
|
import 'package:fcs/fcs/common/helpers/network_connectivity.dart';
|
|
import 'package:fcs/fcs/common/helpers/shared_pref.dart';
|
|
import 'package:fcs/fcs/common/pages/model/base_model.dart';
|
|
import 'package:fcs/fcs/common/services/services.dart';
|
|
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');
|
|
List<BaseModel> models = [];
|
|
|
|
String messagingToken;
|
|
User user;
|
|
PackageInfo packageInfo;
|
|
|
|
set setMessaginToken(token) {
|
|
this.messagingToken = token;
|
|
uploadMsgToken();
|
|
}
|
|
|
|
Setting setting;
|
|
|
|
bool isLoaded = false;
|
|
bool isOnline = false;
|
|
bool isFirstLaunch = false;
|
|
|
|
MainModel() {
|
|
NetworkConnectivity.instance.statusStream.listen((data) {
|
|
bool _isOnline = data["isOnline"];
|
|
if (_isOnline && !this.isOnline) {
|
|
init();
|
|
}
|
|
this.isOnline = _isOnline;
|
|
notifyListeners();
|
|
});
|
|
Services.instance.authService.onAuthStatus().listen((event) async {
|
|
this.user =
|
|
await Services.instance.authService.getUser(refreshIdToken: true);
|
|
_initUser(user);
|
|
if (user != null) {
|
|
uploadMsgToken();
|
|
}
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
bool faqEditable() {
|
|
return this.user != null && this.user.hasSupport();
|
|
}
|
|
|
|
bool paymentMethodsEditable() {
|
|
return this.user != null && this.user.hasSupport();
|
|
}
|
|
|
|
bool termEditable() {
|
|
return this.user != null && this.user.hasSupport();
|
|
}
|
|
|
|
bool contactEditable() {
|
|
return this.user != null && this.user.hasSupport();
|
|
}
|
|
|
|
bool isLogin() {
|
|
return this.user != null;
|
|
}
|
|
|
|
bool isCustomer() {
|
|
return user != null && user.isCustomer();
|
|
}
|
|
|
|
bool isSysAdmin() {
|
|
return this.user != null && this.user.hasSysAdmin();
|
|
}
|
|
|
|
bool isAdmin() {
|
|
return this.user != null && this.user.hasAdmin();
|
|
}
|
|
|
|
init() async {
|
|
await _loadSetting();
|
|
this.isFirstLaunch = await SharedPref.isFirstLaunch();
|
|
this.isFirstLaunch = this.isFirstLaunch ?? true;
|
|
|
|
// _loadUser();
|
|
this.packageInfo = await PackageInfo.fromPlatform();
|
|
}
|
|
|
|
void addModel(BaseModel model) {
|
|
models.add(model);
|
|
}
|
|
|
|
StreamSubscription<User> userListener;
|
|
|
|
void _initUser(User user) {
|
|
if (user != null) {
|
|
if (user.id != null && user.id != "") {
|
|
if (userListener != null) userListener.cancel();
|
|
userListener = Services.instance.authService
|
|
.getUserStream(user.id)
|
|
.listen((event) {
|
|
this.user = event;
|
|
|
|
models.forEach((m) => m.initUser(user));
|
|
notifyListeners();
|
|
});
|
|
}
|
|
models.forEach((m) => m.initUser(user));
|
|
} else {
|
|
models.forEach((m) => m.logout());
|
|
}
|
|
isLoaded = true;
|
|
// if (firebaseMessaging != null) {
|
|
// firebaseMessaging.subscribeToTopic(user.docID);
|
|
// }
|
|
}
|
|
|
|
void _initSetting(Setting setting) {
|
|
models.forEach((m) => m.initSetting(setting));
|
|
}
|
|
|
|
Future<void> _loadSetting() async {
|
|
try {
|
|
Services.instance.authService.getSetting().listen((event) {
|
|
this.setting = event;
|
|
_initSetting(setting);
|
|
notifyListeners();
|
|
});
|
|
} finally {}
|
|
}
|
|
|
|
// void _loadUser() async {
|
|
// try {
|
|
// this.user = await Services.instance.authService.getUser();
|
|
// _initUser(user);
|
|
// } finally {
|
|
// this.isLoaded = true;
|
|
// notifyListeners();
|
|
// }
|
|
// }
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
// if (this.userListener != null) {
|
|
// this.userListener.cancel();
|
|
// }
|
|
// SharedPref.removeUser();
|
|
// this.user = User();
|
|
}
|
|
|
|
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);
|
|
if (authResult != null &&
|
|
authResult.authStatus == AuthStatus.AUTH_VERIFIED) {
|
|
this.user = await Services.instance.authService.getUser();
|
|
}
|
|
return authResult;
|
|
}
|
|
|
|
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 {
|
|
// logout models
|
|
models.forEach((m) => m.logout());
|
|
await removeMsgToken();
|
|
this.user = null;
|
|
notifyListeners();
|
|
|
|
return Services.instance.authService.signout();
|
|
}
|
|
|
|
Future<bool> hasInvite() async {
|
|
return Services.instance.authService.hasInvite();
|
|
}
|
|
|
|
Future<void> signup(String userName) async {
|
|
await Services.instance.authService.signup(userName);
|
|
this.user =
|
|
await Services.instance.authService.getUser(refreshIdToken: true);
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> joinInvite(String userName) async {
|
|
await Services.instance.authService.joinInvite(userName);
|
|
this.user =
|
|
await Services.instance.authService.getUser(refreshIdToken: true);
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> updateProfile(String newUserName) async {
|
|
await Services.instance.authService.updateProfile(newUserName);
|
|
this.user =
|
|
await Services.instance.authService.getUser(refreshIdToken: true);
|
|
notifyListeners();
|
|
}
|
|
}
|