Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #97 from MXCzkEVM/account_management_feat
Browse files Browse the repository at this point in the history
fix: UI/UX & bug for account numbering
  • Loading branch information
reasje authored Oct 18, 2023
2 parents 409cc14 + 49327f7 commit 120dc34
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 11 deletions.
3 changes: 2 additions & 1 deletion assets/flutter_i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,6 @@
"view_private_key_notice": "Warning: Never disclose this key. Anyone with your private keys can steal any assets held in your account.",
"removing_account": "Removing account",
"removing_account_warning": "Are you sure you want to remove this account?",
"remove": "Remove"
"remove": "Remove",
"duplicate_account_import_notice": "The account you are trying to import is a duplicate"
}
13 changes: 12 additions & 1 deletion lib/features/common/account/account_cache_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ class AccountCacheRepository extends GlobalCacheRepository {
List<Account> get accountItems => accounts.value;
Account get accountItem => account.value!;

void addAccount(Account item) => accounts.value = [...accounts.value, item];
void addAccount(Account item, {int? index}) {
if (index == null) {
accounts.value = [...accounts.value, item];
} else {
final newList = accounts.value;
newList.insert(index, item);
accounts.value = newList;
}
}

void removeAccount(Account item) => accounts.value =
accounts.value.where((e) => e.address != item.address).toList();

void updateAccount(Account item) => accounts.value = accounts.value.map((e) {
if (item.address == account.value!.address) {
account.value = item;
Expand All @@ -67,6 +77,7 @@ class AccountCacheRepository extends GlobalCacheRepository {
}
return e;
}).toList();

void resetAccounts() => accounts.value = [];

void setXsdConversionRate(double value) => xsdConversionRate.value = value;
Expand Down
6 changes: 3 additions & 3 deletions lib/features/common/account/account_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class AccountUseCase extends ReactiveUseCase {
update(account, item);
}

void addAccount(Account item) async {
_accountCacheRepository.addAccount(item);
void addAccount(Account item, {int? index}) async {
_accountCacheRepository.addAccount(item, index: index);
final items = _accountCacheRepository.accountItems;
update(account, item);
update(accounts, items);
Expand All @@ -47,7 +47,7 @@ class AccountUseCase extends ReactiveUseCase {
final items = _accountCacheRepository.accountItems;
update(accounts, items);
}

bool isAccountSelected(Account item) {
return (item.address == account.value!.address);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SettingsPresenter extends CompletePresenter<SettingsState> {
final index = _accountUserCase.findAccountsLastIndex();

final newAccount = await _authUseCase.addNewAccount(index);
_accountUserCase.addAccount(newAccount);
_accountUserCase.addAccount(newAccount, index: index);
loadCache();

notify(() => state.isLoading = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:datadashwallet/common/common.dart';
import 'package:datadashwallet/core/core.dart';
import 'package:datadashwallet/features/settings/settings.dart';
import 'package:datadashwallet/features/settings/subfeatures/accounts/show_accounts_dialog.dart';
import 'package:datadashwallet/features/settings/subfeatures/accounts/subfeatures/account_details/import_account_page.dart';
import 'package:datadashwallet/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ class ImportAccountPage extends HookConsumerWidget {
translate('x_not_empty')
.replaceFirst('{0}', translate('private_key')));
if (res != null) return res;
return Validation.checkEthereumPrivateKey(
context, value ?? '');

final isPrivateKey =
Validation.checkEthereumPrivateKey(context, value ?? '');
if (isPrivateKey != null) return isPrivateKey;

return presenter.checkDuplicate(value ?? '');
},
onChanged: (value) {
presenter.changeAbleToSave(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ class ImportAccountPresenter extends CompletePresenter<ImportAccountState> {

final TextEditingController privateKeyController = TextEditingController();

@override
void initState() {
super.initState();

listen(_accountUserCase.accounts, (value) {
notify(() => state.accounts = value);
});
}

void onSave() async {
loading = true;
try {
final index = _accountUserCase.findAccountsLastIndex();
final index = state.accounts.length;
final privateKey = privateKeyController.text;

final newAccount =
Expand All @@ -41,6 +50,18 @@ class ImportAccountPresenter extends CompletePresenter<ImportAccountState> {
}
}

String? checkDuplicate(String privateKey) {
if (privateKey.isEmpty) return translate('invalid_format');

final foundIndex = state.accounts
.indexWhere((element) => element.privateKey == privateKey);

if (foundIndex != -1) {
return translate('duplicate_account_import_notice')!;
}
return null;
}

void changeAbleToSave(bool value) {
notify(() => state.ableToSave = value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:mxc_logic/mxc_logic.dart';
class ImportAccountState with EquatableMixin {
bool ableToSave = false;
bool isLoading = false;
List<Account> accounts = [];

@override
List<Object?> get props => [ableToSave, isLoading];
List<Object?> get props => [ableToSave, isLoading, accounts];
}

0 comments on commit 120dc34

Please sign in to comment.