From 0164679cceac10602ceda50a8f93f8004fc076b1 Mon Sep 17 00:00:00 2001 From: julian Date: Sat, 12 Nov 2022 16:04:16 -0600 Subject: [PATCH] File system path clean up --- lib/main.dart | 24 ++----- .../advanced_views/debug_view.dart | 23 +++---- .../create_auto_backup_view.dart | 6 +- .../create_backup_view.dart | 6 +- .../edit_auto_backup_view.dart | 6 +- ..._file_system.dart => swb_file_system.dart} | 5 +- .../restore_from_file_view.dart | 9 ++- .../create_auto_backup.dart | 7 +- .../coins/epiccash/epiccash_wallet.dart | 27 ++++---- lib/services/coins/monero/monero_wallet.dart | 12 +--- .../coins/wownero/wownero_wallet.dart | 11 +--- .../flutter_secure_storage_interface.dart | 10 +-- lib/utilities/stack_file_system.dart | 66 +++++++++++++++++++ 13 files changed, 119 insertions(+), 93 deletions(-) rename lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/{stack_file_system.dart => swb_file_system.dart} (95%) create mode 100644 lib/utilities/stack_file_system.dart diff --git a/lib/main.dart b/lib/main.dart index 70f80e8ed..21abd9df7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -53,6 +53,7 @@ import 'package:stackwallet/utilities/db_version_migration.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/theme/color_theme.dart'; import 'package:stackwallet/utilities/theme/dark_colors.dart'; import 'package:stackwallet/utilities/theme/light_colors.dart'; @@ -79,29 +80,11 @@ void main() async { setWindowMaxSize(Size.infinite); } - Directory appDirectory = (await getApplicationDocumentsDirectory()); - if (Platform.isIOS) { - appDirectory = (await getLibraryDirectory()); - } - - if (Logging.isArmLinux) { - appDirectory = Directory("${appDirectory.path}/.stackwallet"); - await appDirectory.create(); - } - - if (Platform.isLinux) { - appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet"); - - if (!appDirectory.existsSync()) { - await appDirectory.create(); - } - } - // FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding); if (!(Logging.isArmLinux || Logging.isTestEnv)) { final isar = await Isar.open( [LogSchema], - directory: appDirectory.path, + directory: (await StackFileSystem.applicationIsarDirectory()).path, inspector: false, ); await Logging.instance.init(isar); @@ -150,7 +133,8 @@ void main() async { Hive.registerAdapter(WalletTypeAdapter()); Hive.registerAdapter(UnspentCoinsInfoAdapter()); - await Hive.initFlutter(appDirectory.path); + await Hive.initFlutter( + (await StackFileSystem.applicationHiveDirectory()).path); await Hive.openBox(DB.boxNameDBInfo); diff --git a/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart b/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart index a3aa925a0..055773ef6 100644 --- a/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart +++ b/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart @@ -7,19 +7,25 @@ import 'package:event_bus/event_bus.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS; +import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS; import 'package:package_info_plus/package_info_plus.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:stackwallet/models/isar/models/log.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/providers/global/debug_service_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; +import 'package:stackwallet/utilities/clipboard_interface.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/flush_bar_type.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; +import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart'; import 'package:stackwallet/widgets/custom_loading_overlay.dart'; @@ -28,15 +34,6 @@ import 'package:stackwallet/widgets/rounded_container.dart'; import 'package:stackwallet/widgets/stack_dialog.dart'; import 'package:stackwallet/widgets/stack_text_field.dart'; import 'package:stackwallet/widgets/textfield_icon_button.dart'; -import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS; -import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS; -import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS; - -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; - -import 'package:stackwallet/utilities/clipboard_interface.dart'; - -import 'package:stackwallet/utilities/util.dart'; class DebugView extends ConsumerStatefulWidget { const DebugView({Key? key}) : super(key: key); @@ -352,10 +349,10 @@ class _DebugViewState extends ConsumerState { BlueTextButton( text: "Save logs to file", onTap: () async { - final systemfile = StackFileSystem(); + final systemfile = SWBFileSystem(); await systemfile.prepareStorage(); - Directory rootPath = - (await getApplicationDocumentsDirectory()); + Directory rootPath = await StackFileSystem + .applicationRootDirectory(); if (Platform.isAndroid) { rootPath = Directory("/storage/emulated/0/"); diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/create_auto_backup_view.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/create_auto_backup_view.dart index 334d50e35..bf8bd40e7 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/create_auto_backup_view.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/create_auto_backup_view.dart @@ -9,7 +9,7 @@ import 'package:stack_wallet_backup/stack_wallet_backup.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/auto_backup_view.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart'; -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/backup_frequency_type_select_sheet.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; @@ -49,7 +49,7 @@ class _EnableAutoBackupViewState extends ConsumerState { late final FocusNode passwordFocusNode; late final FocusNode passwordRepeatFocusNode; - late final StackFileSystem stackFileSystem; + late final SWBFileSystem stackFileSystem; final zxcvbn = Zxcvbn(); String passwordFeedback = @@ -70,7 +70,7 @@ class _EnableAutoBackupViewState extends ConsumerState { @override void initState() { secureStore = ref.read(secureStoreProvider); - stackFileSystem = StackFileSystem(); + stackFileSystem = SWBFileSystem(); fileLocationController = TextEditingController(); passwordController = TextEditingController(); passwordRepeatController = TextEditingController(); diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/create_backup_view.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/create_backup_view.dart index fc4719fe1..b7ee6b4be 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/create_backup_view.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/create_backup_view.dart @@ -7,7 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart'; -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; @@ -41,7 +41,7 @@ class _RestoreFromFileViewState extends State { late final FocusNode passwordFocusNode; late final FocusNode passwordRepeatFocusNode; - late final StackFileSystem stackFileSystem; + late final SWBFileSystem stackFileSystem; final zxcvbn = Zxcvbn(); String passwordFeedback = @@ -61,7 +61,7 @@ class _RestoreFromFileViewState extends State { @override void initState() { - stackFileSystem = StackFileSystem(); + stackFileSystem = SWBFileSystem(); fileLocationController = TextEditingController(); passwordController = TextEditingController(); passwordRepeatController = TextEditingController(); diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/edit_auto_backup_view.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/edit_auto_backup_view.dart index 0be718549..4d3c6ca99 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/edit_auto_backup_view.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/edit_auto_backup_view.dart @@ -9,7 +9,7 @@ import 'package:stack_wallet_backup/stack_wallet_backup.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/auto_backup_view.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart'; -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/backup_frequency_type_select_sheet.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; @@ -48,7 +48,7 @@ class _EditAutoBackupViewState extends ConsumerState { late final FocusNode passwordFocusNode; late final FocusNode passwordRepeatFocusNode; - late final StackFileSystem stackFileSystem; + late final SWBFileSystem stackFileSystem; final zxcvbn = Zxcvbn(); String passwordFeedback = @@ -69,7 +69,7 @@ class _EditAutoBackupViewState extends ConsumerState { @override void initState() { secureStore = ref.read(secureStoreProvider); - stackFileSystem = StackFileSystem(); + stackFileSystem = SWBFileSystem(); fileLocationController = TextEditingController(); passwordController = TextEditingController(); passwordRepeatController = TextEditingController(); diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart similarity index 95% rename from lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart rename to lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart index e57c5493f..82d3fd97d 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart @@ -4,15 +4,16 @@ import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:permission_handler/permission_handler.dart'; +import 'package:stackwallet/utilities/util.dart'; -class StackFileSystem { +class SWBFileSystem { Directory? rootPath; Directory? startPath; String? filePath; String? dirPath; - final bool isDesktop = !(Platform.isAndroid || Platform.isIOS); + final bool isDesktop = Util.isDesktop; Future prepareStorage() async { if (Platform.isAndroid) { diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/restore_from_file_view.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/restore_from_file_view.dart index e2d16db54..c5ccfa6b3 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/restore_from_file_view.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/restore_from_file_view.dart @@ -7,7 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart'; -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/stack_restore_progress_view.dart'; import 'package:stackwallet/route_generator.dart'; import 'package:stackwallet/utilities/assets.dart'; @@ -20,14 +20,13 @@ import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/desktop/desktop_dialog.dart'; +import 'package:stackwallet/widgets/desktop/desktop_dialog_close_button.dart'; import 'package:stackwallet/widgets/desktop/primary_button.dart'; import 'package:stackwallet/widgets/desktop/secondary_button.dart'; import 'package:stackwallet/widgets/loading_indicator.dart'; import 'package:stackwallet/widgets/stack_text_field.dart'; import 'package:tuple/tuple.dart'; -import '../../../../widgets/desktop/desktop_dialog_close_button.dart'; - class RestoreFromFileView extends ConsumerStatefulWidget { const RestoreFromFileView({Key? key}) : super(key: key); @@ -44,13 +43,13 @@ class _RestoreFromFileViewState extends ConsumerState { late final FocusNode passwordFocusNode; - late final StackFileSystem stackFileSystem; + late final SWBFileSystem stackFileSystem; bool hidePassword = true; @override void initState() { - stackFileSystem = StackFileSystem(); + stackFileSystem = SWBFileSystem(); fileLocationController = TextEditingController(); passwordController = TextEditingController(); diff --git a/lib/pages_desktop_specific/home/settings_menu/backup_and_restore/create_auto_backup.dart b/lib/pages_desktop_specific/home/settings_menu/backup_and_restore/create_auto_backup.dart index cccb3b0b7..02d33fb95 100644 --- a/lib/pages_desktop_specific/home/settings_menu/backup_and_restore/create_auto_backup.dart +++ b/lib/pages_desktop_specific/home/settings_menu/backup_and_restore/create_auto_backup.dart @@ -9,14 +9,13 @@ import 'package:flutter_svg/svg.dart'; import 'package:stack_wallet_backup/stack_wallet_backup.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart'; -import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart'; +import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; import 'package:stackwallet/utilities/enums/flush_bar_type.dart'; -import 'package:stackwallet/utilities/enums/log_level_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/format.dart'; import 'package:stackwallet/utilities/logger.dart'; @@ -48,7 +47,7 @@ class _CreateAutoBackup extends ConsumerState { late final SecureStorageInterface secureStore; - late final StackFileSystem stackFileSystem; + late final SWBFileSystem stackFileSystem; late final FocusNode passphraseFocusNode; late final FocusNode passphraseRepeatFocusNode; final zxcvbn = Zxcvbn(); @@ -81,7 +80,7 @@ class _CreateAutoBackup extends ConsumerState { @override void initState() { secureStore = ref.read(secureStoreProvider); - stackFileSystem = StackFileSystem(); + stackFileSystem = SWBFileSystem(); fileLocationController = TextEditingController(); passphraseController = TextEditingController(); diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index 683e26544..1a5b2961f 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -9,7 +9,6 @@ import 'package:flutter_libepiccash/epic_cash.dart'; import 'package:hive/hive.dart'; import 'package:http/http.dart'; import 'package:mutex/mutex.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:stack_wallet_backup/generate_password.dart'; import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/models/node_model.dart'; @@ -31,6 +30,7 @@ import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; import 'package:stackwallet/utilities/test_epic_box_connection.dart'; import 'package:tuple/tuple.dart'; @@ -253,14 +253,16 @@ Future deleteEpicWallet({ required SecureStorageInterface secureStore, }) async { String? config = await secureStore.read(key: '${walletId}_config'); + // TODO: why double check for iOS? if (Platform.isIOS) { - Directory appDir = (await getApplicationDocumentsDirectory()); - if (Platform.isIOS) { - appDir = (await getLibraryDirectory()); - } - if (Platform.isLinux) { - appDir = Directory("${appDir.path}/.stackwallet"); - } + Directory appDir = await StackFileSystem.applicationRootDirectory(); + // todo why double check for ios? + // if (Platform.isIOS) { + // appDir = (await getLibraryDirectory()); + // } + // if (Platform.isLinux) { + // appDir = Directory("${appDir.path}/.stackwallet"); + // } final path = "${appDir.path}/epiccash"; final String name = walletId; @@ -1237,13 +1239,8 @@ class EpicCashWallet extends CoinServiceAPI { } Future currentWalletDirPath() async { - Directory appDir = (await getApplicationDocumentsDirectory()); - if (Platform.isIOS) { - appDir = (await getLibraryDirectory()); - } - if (Platform.isLinux) { - appDir = Directory("${appDir.path}/.stackwallet"); - } + Directory appDir = await StackFileSystem.applicationRootDirectory(); + final path = "${appDir.path}/epiccash"; final String name = _walletId.trim(); return '$path/$name'; diff --git a/lib/services/coins/monero/monero_wallet.dart b/lib/services/coins/monero/monero_wallet.dart index caf1185a5..c35323d53 100644 --- a/lib/services/coins/monero/monero_wallet.dart +++ b/lib/services/coins/monero/monero_wallet.dart @@ -25,7 +25,6 @@ import 'package:flutter_libmonero/monero/monero.dart'; import 'package:flutter_libmonero/view_model/send/output.dart' as monero_output; import 'package:http/http.dart'; import 'package:mutex/mutex.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; @@ -47,6 +46,7 @@ import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; const int MINIMUM_CONFIRMATIONS = 10; @@ -897,14 +897,8 @@ class MoneroWallet extends CoinServiceAPI { required String name, required WalletType type, }) async { - Directory root = (await getApplicationDocumentsDirectory()); - if (Platform.isIOS) { - root = (await getLibraryDirectory()); - } - // - if (Platform.isLinux) { - root = Directory("${root.path}/.stackwallet"); - } + Directory root = await StackFileSystem.applicationRootDirectory(); + final prefix = walletTypeToString(type).toLowerCase(); final walletsDir = Directory('${root.path}/wallets'); final walletDire = Directory('${walletsDir.path}/$prefix/$name'); diff --git a/lib/services/coins/wownero/wownero_wallet.dart b/lib/services/coins/wownero/wownero_wallet.dart index 8f36352d0..e39d13005 100644 --- a/lib/services/coins/wownero/wownero_wallet.dart +++ b/lib/services/coins/wownero/wownero_wallet.dart @@ -26,7 +26,6 @@ import 'package:flutter_libmonero/view_model/send/output.dart' import 'package:flutter_libmonero/wownero/wownero.dart'; import 'package:http/http.dart'; import 'package:mutex/mutex.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; @@ -48,6 +47,7 @@ import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; const int MINIMUM_CONFIRMATIONS = 10; @@ -901,13 +901,8 @@ class WowneroWallet extends CoinServiceAPI { required String name, required WalletType type, }) async { - Directory root = (await getApplicationDocumentsDirectory()); - if (Platform.isIOS) { - root = (await getLibraryDirectory()); - } - if (Platform.isLinux) { - root = Directory("${root.path}/.stackwallet"); - } + Directory root = await StackFileSystem.applicationRootDirectory(); + final prefix = walletTypeToString(type).toLowerCase(); final walletsDir = Directory('${root.path}/wallets'); final walletDire = Directory('${walletsDir.path}/$prefix/$name'); diff --git a/lib/utilities/flutter_secure_storage_interface.dart b/lib/utilities/flutter_secure_storage_interface.dart index 9e8aef95c..2d9b19050 100644 --- a/lib/utilities/flutter_secure_storage_interface.dart +++ b/lib/utilities/flutter_secure_storage_interface.dart @@ -1,9 +1,8 @@ -import 'dart:io'; - import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:isar/isar.dart'; import 'package:stack_wallet_backup/secure_storage.dart'; import 'package:stackwallet/models/isar/models/encrypted_string_value.dart'; +import 'package:stackwallet/utilities/stack_file_system.dart'; abstract class SecureStorageInterface { dynamic get store; @@ -47,14 +46,9 @@ class DesktopSecureStore { DesktopSecureStore(this.handler); Future init() async { - Directory? appDirectory; - if (Platform.isLinux) { - appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet"); - await appDirectory.create(); - } isar = await Isar.open( [EncryptedStringValueSchema], - directory: appDirectory!.path, + directory: (await StackFileSystem.applicationIsarDirectory()).path, inspector: false, name: "desktopStore", ); diff --git a/lib/utilities/stack_file_system.dart b/lib/utilities/stack_file_system.dart new file mode 100644 index 000000000..5177f1973 --- /dev/null +++ b/lib/utilities/stack_file_system.dart @@ -0,0 +1,66 @@ +import 'dart:io'; + +import 'package:path_provider/path_provider.dart'; +import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/util.dart'; + +abstract class StackFileSystem { + static Future applicationRootDirectory() async { + Directory appDirectory; + + // todo: can merge and do same as regular linux home dir? + if (Logging.isArmLinux) { + appDirectory = await getApplicationDocumentsDirectory(); + appDirectory = Directory("${appDirectory.path}/.stackwallet"); + } else if (Platform.isLinux) { + appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet"); + } else if (Platform.isWindows) { + // TODO: windows root .stackwallet dir location + throw Exception("Unsupported platform"); + } else if (Platform.isMacOS) { + // currently run in ipad mode?? + throw Exception("Unsupported platform"); + } else if (Platform.isIOS) { + // todo: check if we need different behaviour here + if (Util.isDesktop) { + appDirectory = await getLibraryDirectory(); + } else { + appDirectory = await getLibraryDirectory(); + } + } else if (Platform.isAndroid) { + appDirectory = await getApplicationDocumentsDirectory(); + } else { + throw Exception("Unsupported platform"); + } + if (!appDirectory.existsSync()) { + await appDirectory.create(recursive: true); + } + return appDirectory; + } + + static Future applicationIsarDirectory() async { + final root = await applicationRootDirectory(); + if (Util.isDesktop) { + final dir = Directory("${root.path}/isar"); + if (!dir.existsSync()) { + await dir.create(); + } + return dir; + } else { + return root; + } + } + + static Future applicationHiveDirectory() async { + final root = await applicationRootDirectory(); + if (Util.isDesktop) { + final dir = Directory("${root.path}/hive"); + if (!dir.existsSync()) { + await dir.create(); + } + return dir; + } else { + return root; + } + } +}