121 lines
3.9 KiB
Dart
121 lines
3.9 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:taafee_mobile/common/widgets/toast.dart';
|
|
import 'package:taafee_mobile/core/local_storage/local_storage.dart';
|
|
import 'package:taafee_mobile/core/routing/routing_manager.dart';
|
|
import 'package:taafee_mobile/features/auth/data_layer/source/auth_service.dart';
|
|
import 'package:taafee_mobile/features/splash/data%20layer/source/splash_source.dart';
|
|
import 'package:rx_future/rx_future.dart';
|
|
|
|
import '../../auth/data_layer/model/user.dart';
|
|
import '../data layer/model/params.dart';
|
|
|
|
class SplashController extends GetxController {
|
|
LocalStorage storage = LocalStorage();
|
|
AuthService authService = AuthService();
|
|
SplashSource splashSource = SplashSource();
|
|
RxBool showErrorResult = false.obs;
|
|
|
|
RxFuture<void> paramsState = RxFuture(null);
|
|
Future<void> getParams({
|
|
void Function(Object)? onConnectionError,
|
|
void Function()? onSessionTerminated,
|
|
required Future<bool> Function(bool) versionAlert,
|
|
}) async {
|
|
await paramsState.observe((value) async {
|
|
return await splashSource.getParams(onConnectionError: onConnectionError);
|
|
}, onSuccess: (value) async {
|
|
bool result = await checkVersion(versionAlert: versionAlert);
|
|
if (result == true) {
|
|
await checkToken(onSessionTerminated: onSessionTerminated);
|
|
}
|
|
}, onError: (err) {
|
|
showErrorResult.value = true;
|
|
showErrorResult.refresh();
|
|
});
|
|
}
|
|
|
|
///user token state///
|
|
RxFuture<User> userTokenState = RxFuture(User.zero());
|
|
Future<void> checkToken(
|
|
{void Function()? onError, void Function()? onSessionTerminated}) async {
|
|
showErrorResult.value = false;
|
|
showErrorResult.refresh();
|
|
bool firstTimeOpened = storage.getfirstTimeOpened();
|
|
String? token = storage.getToken();
|
|
bool isGuest = storage.getIsGuest() ?? false;
|
|
if (firstTimeOpened == true) {
|
|
RoutingManager.offAll(RouteName.onboarding);
|
|
|
|
return;
|
|
} else {
|
|
if (token != null) {
|
|
if (!isGuest) {
|
|
userTokenState.observe(
|
|
(value) async {
|
|
return await authService.showUser(onConnectionError: (err) {
|
|
Toast.showToast('you_have_no_internet_connection'.tr);
|
|
});
|
|
},
|
|
onSuccess: (value) {
|
|
storage.saveUser(value);
|
|
RoutingManager.offAll(RouteName.superHome);
|
|
return;
|
|
},
|
|
onError: (error) {
|
|
if (error.toString() == 'you_are_not_authorized') {
|
|
Toast.showToast('this_session_is_terminated'.tr);
|
|
onSessionTerminated?.call();
|
|
} else {
|
|
showErrorResult.value = true;
|
|
showErrorResult.refresh();
|
|
}
|
|
|
|
print('err is${error.toString()}');
|
|
|
|
onError?.call();
|
|
},
|
|
);
|
|
} else {
|
|
storage.saveUser(User(
|
|
id: 0,
|
|
firstName: 'guest',
|
|
lastName: '',
|
|
chatUserId: 0,
|
|
email: '',
|
|
avatarImage: null));
|
|
storage.saveIsGuest(true);
|
|
RoutingManager.off(RouteName.superHome);
|
|
}
|
|
} else {
|
|
RoutingManager.offAll(RouteName.login);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<bool> checkVersion(
|
|
{required Future<bool> Function(bool forced) versionAlert}) async {
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
String appVersion = packageInfo.version;
|
|
return true;
|
|
if (Platform.isAndroid) {
|
|
if (appVersion != Params.currentAndroidVersion) {
|
|
return await versionAlert(Params.forceUpdateAndroid);
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
if (Platform.isIOS) {
|
|
if (appVersion != Params.currentIosVersion) {
|
|
return await versionAlert(Params.forceUpdateIos);
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|