Last active
July 12, 2021 19:23
-
-
Save xavierchanth/e4acab56d1063d7e8300a46f46ccc24a to your computer and use it in GitHub Desktop.
Null safety implementation of AtClientService Wrapper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:at_client_mobile/at_client_mobile.dart'; | |
| import 'package:at_client/at_client.dart'; | |
| import 'package:at_commons/at_commons.dart'; | |
| import 'package:path_provider/path_provider.dart'; | |
| import 'package:at_birdhouse/utils/constants.dart';//TODO YOUR constants file here | |
| class ClientService { | |
| ClientService._internal(); | |
| static final ClientService _singleton = ClientService._internal(); | |
| factory ClientService.getInstance() => _singleton; | |
| Map<String?, AtClientService> _atClientServiceMap = {}; | |
| String? path; | |
| //* Post onboard variables | |
| String? _atsign; | |
| AtClientService? _atClientServiceInstance; | |
| AtClientImpl? _atClientInstance; | |
| //* AtClientService Getters | |
| AtClientService _getAtClientServiceForAtSign({String? atsign}) => | |
| _atClientServiceInstance ??= | |
| _atClientServiceMap[atsign] ?? AtClientService(); | |
| AtClientImpl _getAtClientForAtsign({String? atsign}) => _atClientInstance ??= | |
| _getAtClientServiceForAtSign(atsign: atsign).atClient!; | |
| //* Onboarding process | |
| Future<AtClientPreference> getAtClientPreference({String? cramSecret}) async { | |
| path ??= (await getApplicationSupportDirectory()).path; | |
| return AtClientPreference() | |
| ..isLocalStoreRequired = true | |
| ..commitLogPath = path | |
| ..cramSecret = cramSecret | |
| ..namespace = AtConstants.NAMESPACE | |
| ..syncStrategy = SyncStrategy.IMMEDIATE | |
| ..rootDomain = AtConstants.ROOT_DOMAIN | |
| ..rootPort = AtConstants.ROOT_PORT | |
| ..hiveStoragePath = path; | |
| } | |
| void postOnboard(Map<String?, AtClientService> value, String? atsign) { | |
| _atClientServiceMap = value; | |
| _atsign = atsign; | |
| _getAtClientForAtsign(atsign: _atsign); | |
| sync(); | |
| } | |
| //* GETTERS (should only be called after onboarding) | |
| String get atsign => _atsign!; | |
| AtClientService get atClientServiceInstance => _atClientServiceInstance!; | |
| AtClientImpl get atClientInstance => _atClientInstance!; | |
| //* VERBS | |
| Future<void> sync() async { | |
| await _getAtClientForAtsign().getSyncManager()!.sync(); | |
| } | |
| Future<String> get(AtKey atKey) async => | |
| (await _getAtClientForAtsign().get(atKey)).value; | |
| Future<bool> put(AtKey atKey, String value) async => | |
| await _getAtClientForAtsign().put(atKey, value); | |
| Future<bool?> delete(AtKey atKey) async => | |
| await _getAtClientForAtsign().delete(atKey); | |
| Future<List<AtKey>> getAtKeys({String? regex, String? sharedBy}) async { | |
| regex ??= AtConstants.NAMESPACE_REGEX; | |
| return await _getAtClientForAtsign() | |
| .getAtKeys(regex: regex, sharedBy: sharedBy); | |
| } | |
| Future<bool> notify( | |
| AtKey atKey, String value, OperationEnum operation) async { | |
| return await _getAtClientForAtsign().notify(atKey, value, operation); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OnboardingScreen extends StatefulWidget { | |
| OnboardingScreen({Key key}) : super(key: key); | |
| static final id = "onboarding_screen"; | |
| @override | |
| _OnboardingScreenState createState() => _OnboardingScreenState(); | |
| } | |
| class _OnboardingScreenState extends State<OnboardingScreen> { | |
| String atSign; | |
| ClientService clientService = ClientService.getInstance(); | |
| var atClientPreference; | |
| var _logger = AtSignLogger('@birdhouse'); | |
| @override | |
| void initState() { | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: Scaffold( | |
| resizeToAvoidBottomInset: false, | |
| appBar: AppBar( | |
| backgroundColor: AppColors.BLUE, | |
| title: Text('Home'), | |
| ), | |
| // appBar: AppBar( | |
| // title: const Text('Plugin example app'), | |
| // ), | |
| body: Builder( | |
| builder: (context) => Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| Center( | |
| child: TextButton( | |
| onPressed: () async { | |
| atClientPreference = | |
| await clientService.getAtClientPreference(); | |
| Onboarding( | |
| appAPIKey: AtConstants.APP_API_KEY, | |
| context: context, | |
| atClientPreference: atClientPreference, | |
| domain: AtConstants.ROOT_DOMAIN, | |
| appColor: AppColors.BLUE, | |
| onboard: clientService.postOnboard, | |
| onError: (error) { | |
| _logger.severe('Onboarding throws $error error'); | |
| }, | |
| nextScreen: BasketScreen(), | |
| ); | |
| }, | |
| child: Text("Let's Go!")), | |
| ), | |
| SizedBox( | |
| height: 10, | |
| ), | |
| TextButton( | |
| onPressed: () async { | |
| KeyChainManager _keyChainManager = | |
| KeyChainManager.getInstance(); | |
| var _atSignsList = | |
| await _keyChainManager.getAtSignListFromKeychain(); | |
| _atSignsList?.forEach((element) { | |
| _keyChainManager.deleteAtSignFromKeychain(element); | |
| }); | |
| ScaffoldMessenger.of(context).showSnackBar(SnackBar( | |
| content: Text( | |
| 'Keychain cleaned', | |
| textAlign: TextAlign.center, | |
| ))); | |
| }, | |
| child: Text( | |
| "Reset Keychain", | |
| style: TextStyle(color: Colors.blueGrey), | |
| )) | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment