60 lines
2.0 KiB
Dart
60 lines
2.0 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
|
|
import '../login/login/entity/LoginEntity.dart';
|
|
|
|
final box = GetStorage();
|
|
class StoreService<T> extends GetxService {
|
|
|
|
static StoreService get to => Get.find<StoreService>();
|
|
Future<StoreService<T>> init() async {
|
|
await GetStorage.init();
|
|
await resetUserInfo();
|
|
return this;
|
|
}
|
|
|
|
Future<void> save(String? key, dynamic value) => box.write(key!, value);
|
|
T read(String? key) => box.read(key!);
|
|
Future remove(String? key) => box.remove(key!);
|
|
bool hasData(String? key) => box.hasData(key!);
|
|
|
|
|
|
final String _loginUserInfoKey = 'LOGIN_USER_INFO';
|
|
final String _deviceUUID = 'DEVICE_ID';
|
|
final String _languageCode = 'LANGUAGE_CODE';
|
|
final String _userAccount = 'USER_ACCOUNT';
|
|
|
|
|
|
LoginEntity? _loginEntity;
|
|
// LoginEntity get loginEntity => _loginEntity!;
|
|
Future resetUserInfo() async{
|
|
if(hasData(_loginUserInfoKey)){
|
|
_loginEntity = LoginEntity.fromJson(box.read(_loginUserInfoKey));
|
|
}
|
|
}
|
|
Future removeUserInfo() => remove(_loginUserInfoKey);
|
|
|
|
Future saveLogInInfo(LoginEntity entity) async {
|
|
_loginEntity = LoginEntity.fromJson(entity.toJson());
|
|
save(_loginUserInfoKey, entity.toJson());
|
|
if(_loginEntity != null && _loginEntity!.data != null && _loginEntity!.data!.email != null && _loginEntity!.data!.email!.isNotEmpty) {
|
|
save(_userAccount, _loginEntity?.data?.email);
|
|
}
|
|
}
|
|
|
|
// String getDeviceId() => hasData(_deviceUUID!) ? read(_deviceUUID!): "";
|
|
Future saveDeviceId(String uuid) => save(_deviceUUID, uuid);
|
|
|
|
// String getLanguageCode() => hasData(_languageCode) ? read(_languageCode): "";
|
|
Future saveLanguageCode(String code) => save(_languageCode, code);
|
|
|
|
bool get hadToken => _loginEntity !=null && _loginEntity!.data!.token!.isNotEmpty;
|
|
String? get userToken => hadToken ? _loginEntity!.data!.token : "";
|
|
|
|
// String getLastUserAccount() => hasData(_userAccount) ? read(_userAccount): "";
|
|
void removeLastUserAccount() => remove(_userAccount);
|
|
|
|
// String get localUserAccount => getLastUserAccount();
|
|
|
|
}
|