diff --git a/lib/screen/onboarding_page.dart b/lib/screen/onboarding_page.dart index 6d4bb2173..5f7acdd78 100644 --- a/lib/screen/onboarding_page.dart +++ b/lib/screen/onboarding_page.dart @@ -10,10 +10,12 @@ import 'dart:async'; import 'package:autonomy_flutter/common/injector.dart'; import 'package:autonomy_flutter/screen/app_router.dart'; import 'package:autonomy_flutter/service/account_service.dart'; +import 'package:autonomy_flutter/service/address_service.dart'; import 'package:autonomy_flutter/service/configuration_service.dart'; import 'package:autonomy_flutter/service/deeplink_service.dart'; import 'package:autonomy_flutter/service/metric_client_service.dart'; import 'package:autonomy_flutter/util/log.dart'; +import 'package:autonomy_flutter/util/wallet_utils.dart'; import 'package:autonomy_flutter/view/back_appbar.dart'; import 'package:autonomy_flutter/view/primary_button.dart'; import 'package:autonomy_flutter/view/responsive.dart'; @@ -71,8 +73,39 @@ class _OnboardingPageState extends State } } + /* + - When user did onboarded: + Case 1: user kill app and reopen, we will have restore in background: + Restore mean that we need sync data from keychain and cloud database (that modified in other device) + In this case, we will restore from keychain and cloud in background. + We have to force update because we need to make sure that we have the latest data from cloud + + - When user did not onboarded: we will check if user has primary address in keychain + Case 2: Primary Address not found: new user, create default persona: + + Case 3: Primary address found: user has backup, we will wait until restore from cloud + */ Future _createAccountOrRestoreIfNeeded() async { - await injector().restoreIfNeeded(); + final isDoneOnboarding = + injector().isDoneNewOnboarding(); + if (isDoneOnboarding) { + // existing user + // do this in background, don't await restore + unawaited(injector().restore()); + } else { + final primaryAddress = + await injector().getPrimaryAddress(); + if (primaryAddress == null) { + // new user + final persona = await injector().createDefaultPersona(); + await persona.insertNextAddress(WalletType.Tezos); + await persona.insertNextAddress(WalletType.Ethereum); + } else { + // restore user + // in this case, we have to wait until restore is done + await injector().restoreIfNeeded(); + } + } if (!mounted) { return; } diff --git a/lib/service/account_service.dart b/lib/service/account_service.dart index 9e3890976..182546dce 100644 --- a/lib/service/account_service.dart +++ b/lib/service/account_service.dart @@ -5,6 +5,8 @@ // that can be found in the LICENSE file. // +// ignore_for_file: lines_longer_than_80_chars + import 'dart:async'; import 'dart:io'; @@ -113,6 +115,10 @@ abstract class AccountService { Future updateAddressPersona(WalletAddress walletAddress); + Future> restore(); + + Future createDefaultPersona(); + Future restoreIfNeeded(); Future> getAllViewOnlyAddresses(); @@ -724,13 +730,19 @@ class AccountServiceImpl extends AccountService { } @override - Future restoreIfNeeded() async { - final iapService = injector(); + Future> restore() async { final auditService = injector(); final migrationUtil = MigrationUtil(_cloudDB, auditService); await androidRestoreKeys(); await migrationUtil.migrationFromKeychain(); final personas = await _cloudDB.personaDao.getPersonas(); + return personas; + } + + @override + Future restoreIfNeeded() async { + await restore(); + final personas = await _cloudDB.personaDao.getPersonas(); final hasPersona = personas.isNotEmpty; if (!hasPersona) { @@ -793,9 +805,18 @@ class AccountServiceImpl extends AccountService { .initIfDefaultAccount()); } + final iapService = injector(); unawaited(iapService.restore()); } + @override + Future createDefaultPersona() async { + final persona = await createPersona(isDefault: true); + await persona.insertNextAddress(WalletType.Tezos); + await persona.insertNextAddress(WalletType.Ethereum); + return persona; + } + @override Future getAddressPersona(String address) async => await _cloudDB.addressDao.findByAddress(address);