Skip to content

Commit

Permalink
Reset app option
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Mar 8, 2023
1 parent 8ac1693 commit 01568fd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/accounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ abstract class _ActiveAccount with Store {
final prefs = await SharedPreferences.getInstance();
final coin = prefs.getInt('coin') ?? 0;
var id = prefs.getInt('account') ?? 0;
setActiveAccount(coin, id);
if (WarpApi.checkAccount(coin, id))
setActiveAccount(coin, id);
checkAndUpdate();
}

Expand Down
48 changes: 33 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,17 @@ class ZWalletAppState extends State<ZWalletApp> {
WarpApi.mempoolRun(unconfirmedBalancePort.sendPort.nativePort);

final c = coins.first;
if (!isMobile() && File(c.dbFullPath).existsSync()) {
if (!WarpApi.decryptDb(c.dbFullPath, '')) {
final passwd = await getDbPasswd(context, c.dbFullPath);
if (passwd != null) {
settings.dbPasswd = passwd;
}
do {
if (isMobile()) break; // db encryption is only for desktop
if (!File(c.dbFullPath).existsSync()) break; // fresh install
if (WarpApi.decryptDb(c.dbFullPath, '')) break; // not encrypted

final reset = await getDbPasswd(context, c.dbFullPath);
if (reset) { // user didn't input the passwd and wants to reset
await clearApp(context);
}
}
else break;
} while(true);

for (var c in coins) {
_setProgress(0.2 * (c.coin+1), 'Initializing ${c.ticker}');
Expand Down Expand Up @@ -977,8 +980,9 @@ void resetApp() {
WarpApi.truncateData();
}

Future<String?> getDbPasswd(BuildContext context, String dbPath) async {
Future<bool> getDbPasswd(BuildContext context, String dbPath) async {
final s = S.of(context);
final navigator = navigatorKey.currentState!;
final passwdController = TextEditingController();
final checkPasswd = (String? v) {
final valid = WarpApi.decryptDb(dbPath, passwdController.text);
Expand All @@ -987,7 +991,7 @@ Future<String?> getDbPasswd(BuildContext context, String dbPath) async {
};
final formKey = GlobalKey<FormState>();

final confirmed = await showDialog<bool>(
final reset = await showDialog<bool>(
context: context,
barrierColor: Colors.black,
barrierDismissible: false,
Expand All @@ -1000,14 +1004,28 @@ Future<String?> getDbPasswd(BuildContext context, String dbPath) async {
decoration: InputDecoration(labelText: s.databasePassword),
controller: passwdController,
validator: checkPasswd,
onSaved: (v) { settings.dbPasswd = v!; },
obscureText: true,
),
])))),
actions: confirmButtons(context, () {
if (formKey.currentState!.validate())
Navigator.of(context).pop(true);
}, okLabel: s.ok),
);
actions:
<ElevatedButton>[
ElevatedButton.icon(
icon: Icon(Icons.lock_reset),
label: Text(s.reset),
onPressed: () => navigator.pop(true)),
ElevatedButton.icon(
icon: Icon(Icons.done),
label: Text(s.ok),
onPressed: () {
final fs = formKey.currentState!;
if (fs.validate()) {
fs.save();
navigator.pop(false);
}
})
]);
}) ?? false;
return confirmed ? passwdController.text : null;

return reset;
}
14 changes: 14 additions & 0 deletions lib/reset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:warp_api/types.dart';
import 'package:warp_api/warp_api.dart';
import 'coin/coins.dart';
import 'generated/l10n.dart';
import 'main.dart';
import 'package:path/path.dart' as p;
Expand Down Expand Up @@ -230,3 +231,16 @@ Future<void> showRestartMessage() async {
)
);
}

Future<void> clearApp(BuildContext context) async {
final reset = await showDialog<bool>(context: context, barrierDismissible: false, builder:
(context) => AlertDialog(
title: Text('Reset Database'),
content: Text('Are you sure you want to DELETE ALL your data?'),
actions: confirmButtons(context, () => Navigator.of(context).pop(true)),
)) ?? false;
if (reset) {
final c = coins.first;
File(c.dbDir).deleteSync(recursive: true);
}
}
2 changes: 1 addition & 1 deletion native/zcash-sync
4 changes: 4 additions & 0 deletions packages/warp_api_ffi/lib/warp_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ class WarpApi {
return unwrapResultString(address);
}

static bool checkAccount(int coin, int account) {
return account != 0 && warp_api_lib.check_account(coin, account) != 0;
}

static void setActiveAccount(int coin, int account) {
warp_api_lib.set_active(coin);
warp_api_lib.set_active_account(coin, account);
Expand Down
25 changes: 25 additions & 0 deletions packages/warp_api_ffi/lib/warp_api_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,21 @@ class NativeLibrary {
late final _dart_truncate_sync_data _truncate_sync_data =
_truncate_sync_data_ptr.asFunction<_dart_truncate_sync_data>();

int check_account(
int coin,
int account,
) {
return _check_account(
coin,
account,
);
}

late final _check_account_ptr =
_lookup<ffi.NativeFunction<_c_check_account>>('check_account');
late final _dart_check_account _check_account =
_check_account_ptr.asFunction<_dart_check_account>();

void delete_account(
int coin,
int account,
Expand Down Expand Up @@ -2008,6 +2023,16 @@ typedef _c_truncate_sync_data = ffi.Void Function();

typedef _dart_truncate_sync_data = void Function();

typedef _c_check_account = ffi.Int8 Function(
ffi.Uint8 coin,
ffi.Uint32 account,
);

typedef _dart_check_account = int Function(
int coin,
int account,
);

typedef _c_delete_account = ffi.Void Function(
ffi.Uint8 coin,
ffi.Uint32 account,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.3.5+397
version: 1.3.5+398

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 01568fd

Please sign in to comment.