Line data Source code
1 : import 'package:pal/src/database/entity/in_app_user_entity.dart'; 2 : import 'package:pal/src/database/repository/in_app_user_repository.dart'; 3 : import 'package:pal/src/services/client/in_app_user/in_app_user_client_storage.dart'; 4 : 5 : abstract class InAppUserClientService { 6 : Future<InAppUserEntity> getOrCreate(); 7 : 8 : Future<InAppUserEntity> onConnect(String inAppUserId); 9 : 10 : Future<InAppUserEntity> update(bool inAppUserId); 11 : 12 : Future<InAppUserEntity> onDisconnect(); 13 : 14 3 : factory InAppUserClientService.build( 15 : InAppUserRepository inAppUserRepository, {final InAppUserStorageClientManager inAppUserStorageClientManager}) => 16 3 : _ClientInAppUserHttpService(inAppUserRepository, inAppUserStorageClientManager: inAppUserStorageClientManager); 17 : } 18 : 19 : class _ClientInAppUserHttpService implements InAppUserClientService { 20 : final InAppUserRepository _inAppUserRepository; 21 : final InAppUserStorageClientManager _clientInAppUserStorageManager; 22 : 23 3 : _ClientInAppUserHttpService(this._inAppUserRepository, {final InAppUserStorageClientManager inAppUserStorageClientManager}) 24 2 : : this._clientInAppUserStorageManager = inAppUserStorageClientManager ?? InAppUserStorageClientManager.build(); 25 : 26 : @override 27 2 : Future<InAppUserEntity> getOrCreate() async { 28 : InAppUserEntity inAppUser = 29 6 : await this._clientInAppUserStorageManager.readInAppUser(); 30 : if (inAppUser != null) { 31 : return inAppUser; 32 : } 33 : 34 4 : inAppUser = await this._inAppUserRepository.create(InAppUserEntity(disabledHelpers: false)); 35 3 : await this._clientInAppUserStorageManager.storeInAppUser(inAppUser); 36 : return inAppUser; 37 : } 38 : 39 : @override 40 1 : Future<InAppUserEntity> onConnect(final String inAppUserId) async { 41 : InAppUserEntity inAppUser = 42 3 : await this._clientInAppUserStorageManager.readInAppUser(); 43 1 : if (inAppUser == null || !inAppUser.anonymous) { 44 : return inAppUser; 45 : } 46 : 47 4 : inAppUser = await this._inAppUserRepository.update(InAppUserEntity( 48 1 : id: inAppUser.id, 49 : inAppId: inAppUserId 50 : )); 51 : 52 3 : await this._clientInAppUserStorageManager.clearInAppUser(); 53 3 : await this._clientInAppUserStorageManager.storeInAppUser(inAppUser); 54 : return inAppUser; 55 : } 56 : 57 : @override 58 1 : Future<InAppUserEntity> update(final bool disabledHelpers) async { 59 : InAppUserEntity inAppUser = 60 3 : await this._clientInAppUserStorageManager.readInAppUser(); 61 : 62 : if (inAppUser == null) { 63 : return inAppUser; 64 : } 65 : 66 4 : inAppUser = await this._inAppUserRepository.update(InAppUserEntity( 67 1 : id: inAppUser.id, 68 : disabledHelpers: disabledHelpers 69 : )); 70 : 71 3 : await this._clientInAppUserStorageManager.clearInAppUser(); 72 3 : await this._clientInAppUserStorageManager.storeInAppUser(inAppUser); 73 : return inAppUser; 74 : } 75 : 76 : @override 77 1 : Future<InAppUserEntity> onDisconnect() async { 78 : InAppUserEntity inAppUser = 79 3 : await this._clientInAppUserStorageManager.readInAppUser(); 80 1 : if (inAppUser == null || inAppUser.anonymous) { 81 : return inAppUser; 82 : } 83 : 84 3 : await this._clientInAppUserStorageManager.clearInAppUser(); 85 : 86 1 : inAppUser = await this 87 1 : ._inAppUserRepository 88 2 : .create(InAppUserEntity(disabledHelpers: false)); 89 : 90 3 : await this._clientInAppUserStorageManager.storeInAppUser(inAppUser); 91 : 92 : return inAppUser; 93 : } 94 : }