82 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
	
		
		
			
		
	
	
			82 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
	
| 
								 | 
							
								/// Created by Haoyi on 6/3/21
							 | 
						||
| 
								 | 
							
								part of "../app_property.dart";
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								extension AccountPropertyExtension on AppProperty {
							 | 
						||
| 
								 | 
							
								  void setAccountSaasUser(SaasUser saasUser) {
							 | 
						||
| 
								 | 
							
								    final data = jsonEncode(saasUser);
							 | 
						||
| 
								 | 
							
								    setString(PropertyKeys.accountSaasUser, data);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  void setAccountDevice(DeviceInfo deviceInfo) {
							 | 
						||
| 
								 | 
							
								    final data = jsonEncode(deviceInfo);
							 | 
						||
| 
								 | 
							
								    setString(PropertyKeys.accountDevice, data);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  void setAccountProfile(AccountProfile accountProfile) {
							 | 
						||
| 
								 | 
							
								    final data = jsonEncode(accountProfile);
							 | 
						||
| 
								 | 
							
								    setString(PropertyKeys.accountProfile, data);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // refer updateLocalProfile
							 | 
						||
| 
								 | 
							
								  void setDirtyAccountProfile(AccountProfile accountProfile) {
							 | 
						||
| 
								 | 
							
								    final data = jsonEncode(accountProfile.copyWith(dirty: true));
							 | 
						||
| 
								 | 
							
								    setString(PropertyKeys.accountProfile, data);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  Future<DeviceInfo?> getAccountDevice() async {
							 | 
						||
| 
								 | 
							
								    final deviceString = await getString(PropertyKeys.accountDevice, defValue: '');
							 | 
						||
| 
								 | 
							
								    if (deviceString == "") {
							 | 
						||
| 
								 | 
							
								      return null;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    final map = jsonDecode(deviceString);
							 | 
						||
| 
								 | 
							
								    return DeviceInfo.fromJson(map);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  Future<Account> loadAccount() async {
							 | 
						||
| 
								 | 
							
								    final accountBundle = await loadValuesByTag(PropertyTags.account).catchError((error) {
							 | 
						||
| 
								 | 
							
								      Log.v("loadValuesByTag is empty, $error");
							 | 
						||
| 
								 | 
							
								      return PropertyBundle.empty();
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								    SaasUser? saasUser;
							 | 
						||
| 
								 | 
							
								    DeviceInfo? device;
							 | 
						||
| 
								 | 
							
								    AccountProfile? accountProfile;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    final saasUserString = accountBundle.getString(PropertyKeys.accountSaasUser);
							 | 
						||
| 
								 | 
							
								    if (DartExt.isNotBlank(saasUserString)) {
							 | 
						||
| 
								 | 
							
								      final map = jsonDecode(saasUserString!);
							 | 
						||
| 
								 | 
							
								      saasUser = SaasUser.fromJson(map);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    final accountDeviceString = accountBundle.getString(PropertyKeys.accountDevice);
							 | 
						||
| 
								 | 
							
								    if (DartExt.isNotBlank(accountDeviceString)) {
							 | 
						||
| 
								 | 
							
								      final map = jsonDecode(accountDeviceString!);
							 | 
						||
| 
								 | 
							
								      device = DeviceInfo.fromJson(map);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    final accountProfileString = accountBundle.getString(PropertyKeys.accountProfile);
							 | 
						||
| 
								 | 
							
								    if (DartExt.isNotBlank(accountProfileString)) {
							 | 
						||
| 
								 | 
							
								      final map = jsonDecode(accountProfileString!);
							 | 
						||
| 
								 | 
							
								      accountProfile = AccountProfile.fromJson(map);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return Account.restore(saasUser: saasUser, device: device, accountProfile: accountProfile);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  Future<int> getLatestReportDeviceTimestamp() async {
							 | 
						||
| 
								 | 
							
								    return await getInt(PropertyKeys.latestReportDeviceTimestamp, defValue: 0);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  void setLatestReportDeviceTimestamp(int timestamp) async {
							 | 
						||
| 
								 | 
							
								    setInt(PropertyKeys.latestReportDeviceTimestamp, timestamp);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  Future<String> getAnonymousSecretKey() async {
							 | 
						||
| 
								 | 
							
								    String? secret = await getString(PropertyKeys.anonymousSecretKey, defValue: "");
							 | 
						||
| 
								 | 
							
								    if (secret == '') {
							 | 
						||
| 
								 | 
							
								      secret = IdUtils.uuidV4();
							 | 
						||
| 
								 | 
							
								      await setString(PropertyKeys.anonymousSecretKey, secret);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    return secret;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 |