diff --git a/lib/checker/checker.dart b/lib/checker/checker.dart index 31e31be91..2f4a90a73 100644 --- a/lib/checker/checker.dart +++ b/lib/checker/checker.dart @@ -7,6 +7,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; import 'package:violet/database/query.dart'; import 'package:violet/log/log.dart'; +import 'package:violet/settings/path.dart'; class VioletChecker { static Future checkArticleDatabase() async { @@ -18,17 +19,8 @@ class VioletChecker { // 0. get database path // var dbPath = Platform.isAndroid - ? '${(await getApplicationDocumentsDirectory()).path}/data/data.db' - : '${await getDatabasesPath()}/data.db'; - if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dbPath = '${home}/.violet/data.db'; - } + ? '${(await DefaultPathProvider.getBaseDirectory())}/data/data.db' + : '${(await DefaultPathProvider.getBaseDirectory())}/data.db'; // // 1. check file exists @@ -106,19 +98,7 @@ class VioletChecker { // // 0. get database path // - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = DefaultPathProvider.getBaseDirectory(); var dbPath = '${dir}/user.db'; // diff --git a/lib/component/hitomi/hitomi.dart b/lib/component/hitomi/hitomi.dart index 15d73ee5e..b8b1cf21d 100644 --- a/lib/component/hitomi/hitomi.dart +++ b/lib/component/hitomi/hitomi.dart @@ -11,6 +11,7 @@ import 'package:violet/algorithm/distance.dart'; import 'package:violet/component/hitomi/displayed_tag.dart'; import 'package:violet/component/hitomi/tag_translate.dart'; import 'package:violet/script/script_manager.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/variables.dart'; @@ -27,18 +28,7 @@ class HitomiManager { static int? getArticleCount(String classification, String name) { if (tagmap == null) { final subdir = Platform.isAndroid ? '/data' : ''; - late final path; - if(Platform.isAndroid || Platform.isIOS){ - path = File('${Variables.applicationDocumentsDirectory}$subdir/index.json'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - path = File('${home}/.violet$subdir/index.json'); - } + final path = File('${Variables.applicationDocumentsDirectory}$subdir/index.json'); final text = path.readAsStringSync(); tagmap = jsonDecode(text); } @@ -101,21 +91,8 @@ class HitomiManager { tagmap = jsonDecode(await file.readAsString()); } else { final subdir = Platform.isAndroid ? '/data' : ''; - late final directory; - late final path; - if(Platform.isAndroid || Platform.isIOS){ - directory = await getApplicationDocumentsDirectory(); - path = File('${directory.path}$subdir/index.json'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - path = File('${directory}$subdir/index.json'); - } + final directory = (await (DefaultPathProvider.getDocumentsDirectory())); + final path = File('${directory}$subdir/index.json'); final text = path.readAsStringSync(); tagmap = jsonDecode(text); } diff --git a/lib/component/hitomi/indexs.dart b/lib/component/hitomi/indexs.dart index bdb488a5f..830f78b6e 100644 --- a/lib/component/hitomi/indexs.dart +++ b/lib/component/hitomi/indexs.dart @@ -10,6 +10,7 @@ import 'package:tuple/tuple.dart'; import 'package:violet/algorithm/distance.dart'; import 'package:violet/component/hitomi/tag_translate.dart'; import 'package:violet/log/log.dart'; +import 'package:violet/settings/path.dart'; // This is used for estimation similiar Aritst/Group/Uplaoder with each others. class HitomiIndexs { @@ -40,19 +41,7 @@ class HitomiIndexs { static late Map relatedTag; static Future init() async { - late final directory; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - directory = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - } + final directory = await DefaultPathProvider.getDocumentsDirectory(); final subdir = Platform.isAndroid ? '/data' : ''; // No data on first run. diff --git a/lib/component/hitomi/series_finder.dart b/lib/component/hitomi/series_finder.dart index 7e23aff91..5eb1fe7fc 100644 --- a/lib/component/hitomi/series_finder.dart +++ b/lib/component/hitomi/series_finder.dart @@ -9,25 +9,13 @@ import 'package:tuple/tuple.dart'; import 'package:violet/component/hitomi/hitomi.dart'; import 'package:violet/component/hitomi/title_cluster.dart'; import 'package:violet/database/query.dart'; +import 'package:violet/settings/path.dart'; class SeriesFinder { static Future doFind1() async { final subdir = Platform.isAndroid ? '/data' : ''; - late final directory; - late final path; - if(Platform.isAndroid || Platform.isIOS){ - directory = await getApplicationDocumentsDirectory(); - path = File('${directory.path}$subdir/index.json'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - path = '${directory}$subdir/index.json'; - } + final directory = await DefaultPathProvider.getDocumentsDirectory(); + final path = File('${directory}$subdir/index.json'); final text = path.readAsStringSync(); final tagmap = jsonDecode(text); final artists = tagmap['artist'] as Map; diff --git a/lib/database/database.dart b/lib/database/database.dart index aae44be3e..8fa98a377 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -7,6 +7,7 @@ import 'package:flutter/material.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; +import 'package:violet/settings/path.dart'; class DataBaseManager { String? dbPath; @@ -33,19 +34,9 @@ class DataBaseManager { if (Platform.environment.containsKey('FLUTTER_TEST')) { dbPath = join(Directory.current.path, 'test/db/data.db'); } else { - if(Platform.isAndroid || Platform.isIOS){ - dbPath = Platform.isAndroid - ? '${(await getApplicationDocumentsDirectory()).path}/data/data.db' - : '${await getDatabasesPath()}/data.db'; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dbPath = '${home}/.violet/data.db'; - } + dbPath = Platform.isAndroid + ? '${(await DefaultPathProvider.getBaseDirectory())}/data/data.db' + : '${(await DefaultPathProvider.getBaseDirectory())}/data.db'; } _instance = create(dbPath); await _instance!.open(); diff --git a/lib/database/user/user.dart b/lib/database/user/user.dart index 2d070b1af..5ba33104d 100644 --- a/lib/database/user/user.dart +++ b/lib/database/user/user.dart @@ -6,24 +6,15 @@ import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:violet/database/database.dart'; +import 'package:violet/settings/path.dart'; class CommonUserDatabase extends DataBaseManager { static DataBaseManager? _instance; static Future getInstance() async { if (_instance == null) { - var home = ''; - if(Platform.isAndroid || Platform.isIOS){ - var dir = await getApplicationDocumentsDirectory(); - _instance = DataBaseManager.create('${dir.path}/user.db'); - } else if(Platform.isLinux){ - Platform.environment.forEach((key, value) => { - if(key == 'HOME'){ - home = value - } - }); - _instance = DataBaseManager.create('${home}/.violet/user.db'); - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); + _instance = DataBaseManager.create('${dir}/user.db'); await _instance!.open(); } return _instance!; diff --git a/lib/log/act_log.dart b/lib/log/act_log.dart index 81a94cd1e..1a6009a30 100644 --- a/lib/log/act_log.dart +++ b/lib/log/act_log.dart @@ -9,6 +9,7 @@ import 'package:crypto/crypto.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'package:synchronized/synchronized.dart' as sync; +import 'package:violet/settings/path.dart'; enum ActLogType { none, @@ -109,19 +110,7 @@ class ActLogger { if (Platform.environment.containsKey('FLUTTER_TEST')) { logFile = File('act-log.txt'); } else { - late final dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + final dir = await DefaultPathProvider.getDocumentsDirectory(); logFile = File(join(dir, 'act-log.txt')); } @@ -150,21 +139,7 @@ class ActLogger { } static Future exportLog() async { - late final ext; - if(Platform.isAndroid || Platform.isIOS){ - var d = Platform.isIOS - ? await getApplicationSupportDirectory() - : await getExternalStorageDirectory(); - ext = d!.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - key = value; - } - }); - ext = '${home}/.violet'; - } + final ext = await DefaultPathProvider.getExportDirectory(); final extpath = '${ext}/act-log.txt'; await logFile.copy(extpath); } diff --git a/lib/log/log.dart b/lib/log/log.dart index 4c20cf7e0..a56d5bfb4 100644 --- a/lib/log/log.dart +++ b/lib/log/log.dart @@ -7,6 +7,7 @@ import 'package:intl/intl.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'package:synchronized/synchronized.dart' as sync; +import 'package:violet/settings/path.dart'; class LogEvent { DateTime? dateTime; @@ -48,19 +49,8 @@ class Logger { if (Platform.environment.containsKey('FLUTTER_TEST')) { logFile = File('log.txt'); } else { - if(Platform.isAndroid || Platform.isIOS){ - final dir = await getApplicationDocumentsDirectory(); - logFile = File(join(dir.path, 'log.txt')); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - final dir = '${home}/.violet'; - logFile = File(join(dir, 'log.txt')); - } + final dir = await DefaultPathProvider.getDocumentsDirectory(); + logFile = File(join(dir, 'log.txt')); } if (!await logFile.exists()) { @@ -121,12 +111,10 @@ class Logger { } static Future exportLog() async { - final ext = Platform.isIOS - ? await getApplicationSupportDirectory() - : await getExternalStorageDirectory(); + final ext = await DefaultPathProvider.getExportDirectory(); // final dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss"); // final extpath = '${ext.path}/log-${dateFormat.format(DateTime.now())}.log'; - final extpath = '${ext!.path}/log.txt'; + final extpath = '${ext}/log.txt'; await logFile.copy(extpath); } } diff --git a/lib/pages/database_download/database_download_page.dart b/lib/pages/database_download/database_download_page.dart index 8c49812aa..53c6d6020 100644 --- a/lib/pages/database_download/database_download_page.dart +++ b/lib/pages/database_download/database_download_page.dart @@ -21,6 +21,7 @@ import 'package:violet/locale/locale.dart'; import 'package:violet/log/log.dart'; import 'package:violet/pages/database_download/decompress.dart'; import 'package:violet/platform/misc.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/variables.dart'; import 'package:violet/version/sync.dart'; @@ -62,32 +63,10 @@ class DataBaseDownloadPageState extends State { final prefs = await MultiPreferences.getInstance(); if ((await prefs.getInt('db_exists')) == 1) { var dbPath = Platform.isAndroid - ? '${(await getApplicationDocumentsDirectory()).path}/data/data.db' - : '${await getDatabasesPath()}/data.db'; - if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dbPath = '${home}/.violet/data.db'; - } + ? '${(await DefaultPathProvider.getBaseDirectory())}/data/data.db' + : '${(await DefaultPathProvider.getBaseDirectory())}/data.db'; if (await File(dbPath).exists()) await File(dbPath).delete(); - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } - if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = '${(await DefaultPathProvider.getDocumentsDirectory())}'; if (await Directory('${dir}/data').exists()) { await Directory('${dir}/data').delete(recursive: true); } @@ -140,9 +119,9 @@ class DataBaseDownloadPageState extends State { int tnu = 0; try { - var dir = await getApplicationDocumentsDirectory(); - if (await File('${dir.path}/db.sql.7z').exists()) { - await File('${dir.path}/db.sql.7z').delete(); + var dir = await DefaultPathProvider.getBaseDirectory(); + if (await File('${dir}/db.sql.7z').exists()) { + await File('${dir}/db.sql.7z').delete(); } switch(target){ case 'latest': await SyncManager.checkSyncLatest(_throw); break; @@ -166,7 +145,7 @@ class DataBaseDownloadPageState extends State { })); await dio.download( SyncManager.getLatestDB().getDBDownloadUrl(widget.dbType!), - '${dir.path}/db.sql.7z', onReceiveProgress: (rec, total) { + '${dir}/db.sql.7z', onReceiveProgress: (rec, total) { nu += rec - latest; tnu += rec - latest; latest = rec; @@ -193,23 +172,23 @@ class DataBaseDownloadPageState extends State { }); final p7zip = P7zip(); - if (await Directory('${dir.path}/data2').exists()) { - await Directory('${dir.path}/data2').delete(recursive: true); + if (await Directory('${dir}/data2').exists()) { + await Directory('${dir}/data2').delete(recursive: true); } await p7zip.decompress( - ['${dir.path}/db.sql.7z'], - path: '${dir.path}/data2', + ['${dir}/db.sql.7z'], + path: '${dir}/data2', ); Variables.databaseDecompressed = true; - if (await Directory('${dir.path}/data').exists()) { - await Directory('${dir.path}/data').delete(recursive: true); + if (await Directory('${dir}/data').exists()) { + await Directory('${dir}/data').delete(recursive: true); } - await Directory('${dir.path}/data2').rename('${dir.path}/data'); - if (await Directory('${dir.path}/data2').exists()) { - await Directory('${dir.path}/data2').delete(recursive: true); + await Directory('${dir}/data2').rename('${dir}/data'); + if (await Directory('${dir}/data2').exists()) { + await Directory('${dir}/data2').delete(recursive: true); } - await File('${dir.path}/db.sql.7z').delete(); + await File('${dir}/db.sql.7z').delete(); final prefs = await MultiPreferences.getInstance(); await prefs.setInt('db_exists', 1); @@ -266,7 +245,7 @@ class DataBaseDownloadPageState extends State { int tnu = 0; try { - var dir = await getDatabasesPath(); + var dir = await DefaultPathProvider.getBaseDirectory(); if (await File('$dir/data.db').exists()) { await File('$dir/data.db').delete(); } @@ -369,15 +348,7 @@ class DataBaseDownloadPageState extends State { int tnu = 0; try { - var dir = ''; - if(Platform.isLinux){ - Platform.environment.forEach((key, value) => { - if(key == 'HOME'){ - dir = value - } - }); - dir = '${dir}/.violet'; - } + var dir = await DefaultPathProvider.getBaseDirectory(); if (await File('$dir/data.db').exists()) { await File('$dir/data.db').delete(); } @@ -675,19 +646,7 @@ class DataBaseDownloadPageState extends State { 'class': classes, }; - late final directory; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - directory = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - } + final directory = await DefaultPathProvider.getDocumentsDirectory(); final path1 = File('${directory}/index.json'); path1.writeAsString(jsonEncode(index)); diff --git a/lib/pages/download/download_page.dart b/lib/pages/download/download_page.dart index 6e82d6ec5..c8ce0a92f 100644 --- a/lib/pages/download/download_page.dart +++ b/lib/pages/download/download_page.dart @@ -33,6 +33,7 @@ import 'package:violet/pages/segment/filter_page.dart'; import 'package:violet/pages/segment/filter_page_controller.dart'; import 'package:violet/pages/segment/platform_navigator.dart'; import 'package:violet/script/script_manager.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; import 'package:violet/widgets/search_bar.dart'; @@ -124,19 +125,8 @@ class _DownloadPageState extends ThemeSwitchableState /// to /// /var/mobile/Containers/Data/Application//Documents - late final newPath; - if(Platform.isAndroid || Platform.isIOS){ - newPath = (await getApplicationDocumentsDirectory()).path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - newPath = '${home}/.violet'; - } else { newPath = ''; } - + final newPath = await DefaultPathProvider.getBaseDirectory(); + for (var item in items) { if (item.files() == null) continue; diff --git a/lib/pages/download/download_routine.dart b/lib/pages/download/download_routine.dart index b7298e713..688a8457f 100644 --- a/lib/pages/download/download_routine.dart +++ b/lib/pages/download/download_routine.dart @@ -13,6 +13,7 @@ import 'package:violet/component/downloadable.dart'; import 'package:violet/component/hentai_download_manager.dart'; import 'package:violet/database/user/download.dart'; import 'package:violet/downloader/isolate_downloader.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; class DownloadRoutine { @@ -87,7 +88,7 @@ class DownloadRoutine { } Future extractFilePath() async { - final inner = (await getApplicationDocumentsDirectory()).path; + final inner = (await DefaultPathProvider.getDocumentsDirectory()); final files = tasks! .map((e) => join( Settings.useInnerStorage ? inner : Settings.downloadBasePath, @@ -123,7 +124,7 @@ class DownloadRoutine { var basepath = Settings.downloadBasePath; if (Settings.useInnerStorage) { - basepath = (await getApplicationDocumentsDirectory()).path; + basepath = (await DefaultPathProvider.getDocumentsDirectory()); } downloader.appendTasks(tasks!.map((e) { @@ -150,7 +151,7 @@ class DownloadRoutine { var basepath = Settings.downloadBasePath; if (Settings.useInnerStorage) { - basepath = (await getApplicationDocumentsDirectory()).path; + basepath = (await DefaultPathProvider.getDocumentsDirectory()); } final filenames = tasks! @@ -190,7 +191,7 @@ class DownloadRoutine { var basepath = Settings.downloadBasePath; if (Settings.useInnerStorage) { - basepath = (await getApplicationDocumentsDirectory()).path; + basepath = (await DefaultPathProvider.getDocumentsDirectory()); } downloader.appendTasks(invalidIndex.map((e) => tasks![e]).map((e) { diff --git a/lib/pages/main/info/lab/bookmark/bookmarks.dart b/lib/pages/main/info/lab/bookmark/bookmarks.dart index a3d69bb9a..f2c64dee7 100644 --- a/lib/pages/main/info/lab/bookmark/bookmarks.dart +++ b/lib/pages/main/info/lab/bookmark/bookmarks.dart @@ -13,6 +13,7 @@ import 'package:violet/pages/main/info/lab/bookmark/bookmarks_article_list.dart' import 'package:violet/pages/main/info/lab/bookmark/bookmarks_records.dart'; import 'package:violet/pages/segment/platform_navigator.dart'; import 'package:violet/server/violet.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; @@ -173,19 +174,7 @@ class _BookmarkPageState extends State { DateTime.parse(data.datetime())); - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); var dbraw = await openDatabase('${dir}/user.db'); await dbraw.transaction((txn) async { final batch = txn.batch(); diff --git a/lib/pages/main/info/lab_page.dart b/lib/pages/main/info/lab_page.dart index e5311ad14..7852a16da 100644 --- a/lib/pages/main/info/lab_page.dart +++ b/lib/pages/main/info/lab_page.dart @@ -32,6 +32,7 @@ import 'package:violet/pages/segment/platform_navigator.dart'; import 'package:violet/pages/settings/log_page.dart'; import 'package:violet/server/violet.dart'; import 'package:violet/server/wsalt.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; @@ -348,20 +349,8 @@ class _LaboratoryPageState extends State { 'prefs.json', jsonEncode(Map.fromEntries(values_itr.toList(growable: false)))); - late final dir; - if(Platform.isAndroid || Platform.isIOS) { - dir = await getApplicationDocumentsDirectory(); - await VioletServer.uploadFile('${dir.path}/user.db'); - } else if(Platform.isLinux) { - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - await VioletServer.uploadFile('${dir}/user.db'); - } + final dir = await DefaultPathProvider.getDocumentsDirectory(); + await VioletServer.uploadFile('${dir}/user.db'); await Future.delayed(const Duration(milliseconds: 500)); await VioletServer.uploadFile(ActLogger.logFile.path); }, diff --git a/lib/pages/main/main_page.dart b/lib/pages/main/main_page.dart index 431d154a7..b866a4e40 100644 --- a/lib/pages/main/main_page.dart +++ b/lib/pages/main/main_page.dart @@ -39,6 +39,7 @@ import 'package:violet/pages/segment/double_tap_to_top.dart'; import 'package:violet/pages/segment/platform_navigator.dart'; import 'package:violet/pages/splash/splash_page.dart'; import 'package:violet/platform/android_external_storage_directory.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; import 'package:violet/variables.dart'; @@ -534,19 +535,7 @@ class _VersionAreaWidgetState extends State<_VersionAreaWidget> { return; } - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); try { await ((await openDatabase('${dir}/data/data.db')).close()); @@ -566,21 +555,9 @@ class _VersionAreaWidgetState extends State<_VersionAreaWidget> { ))) .then((value) async { HitomiIndexs.init(); - late final directory; - late final path; - if(Platform.isAndroid || Platform.isIOS){ - directory = await getApplicationDocumentsDirectory(); - path = File('${directory.path}/data/index.json'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - path = File('${directory}/index.json'); - } + final subdir = Platform.isAndroid ? '/data' : ''; + final directory = await DefaultPathProvider.getDocumentsDirectory(); + final path = File('${directory}$subdir/index.json'); final text = path.readAsStringSync(); HitomiManager.tagmap = jsonDecode(text); await DataBaseManager.reloadInstance(); diff --git a/lib/pages/settings/restore_bookmark.dart b/lib/pages/settings/restore_bookmark.dart index 9c2fd0f45..a129b7170 100644 --- a/lib/pages/settings/restore_bookmark.dart +++ b/lib/pages/settings/restore_bookmark.dart @@ -10,6 +10,7 @@ import 'package:sqflite/sqflite.dart'; import 'package:violet/database/user/bookmark.dart'; import 'package:violet/database/user/record.dart'; import 'package:violet/log/log.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; import 'package:violet/widgets/toast.dart'; @@ -60,18 +61,7 @@ class _RestoreBookmarkPageState extends State { } // 북마크 작품 처리 - var dir; - if(Platform.isAndroid || Platform.isIOS){ - dir = await getApplicationDocumentsDirectory(); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); var dbraw = await openDatabase('${dir}/user.db'); await dbraw.transaction((txn) async { final batch = txn.batch(); diff --git a/lib/pages/settings/settings_page.dart b/lib/pages/settings/settings_page.dart index adf2cfb71..ca7e93fc2 100644 --- a/lib/pages/settings/settings_page.dart +++ b/lib/pages/settings/settings_page.dart @@ -61,6 +61,7 @@ import 'package:violet/pages/settings/version_page.dart'; import 'package:violet/pages/splash/splash_page.dart'; import 'package:violet/platform/misc.dart'; import 'package:violet/server/violet.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; import 'package:violet/variables.dart'; @@ -1231,19 +1232,7 @@ class _SettingsPageState extends State return; } - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); try { await ((await openDatabase('${dir}/data/data.db')) .close()); @@ -1261,20 +1250,8 @@ class _SettingsPageState extends State .then( (value) async { HitomiIndexs.init(); - late final directory; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - directory = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - } - final path = File('${directory.path}/data/index.json'); + final directory = await DefaultPathProvider.getDocumentsDirectory(); + final path = File('${directory}/data/index.json'); final text = path.readAsStringSync(); HitomiManager.tagmap = jsonDecode(text); await DataBaseManager.reloadInstance(); @@ -1966,23 +1943,9 @@ class _SettingsPageState extends State } final pickedFile = File(pickedFilePath); - late final db; - if(Platform.isAndroid || Platform.isIOS){ - db = Platform.isIOS - ? await getApplicationSupportDirectory() - : (await getApplicationDocumentsDirectory()); - - await pickedFile.copy('${db.path}/user.db'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - db = '${home}/.violet'; - await pickedFile.copy('${db}/user.db'); - } + final db = await DefaultPathProvider.getSupportDirectory(); + + await pickedFile.copy('${db}/user.db'); await Bookmark.getInstance(); flutterToast.showToast( @@ -2004,23 +1967,8 @@ class _SettingsPageState extends State title: Text(Translations.of(context).trans('exportingbookmark')), trailing: const Icon(Icons.keyboard_arrow_right), onTap: () async { - late final dir; - late final bookmarkDatabaseFile; - if(Platform.isAndroid || Platform.isIOS) { - dir = Platform.isIOS - ? await getApplicationSupportDirectory() - : (await getApplicationDocumentsDirectory()); - bookmarkDatabaseFile = File('${dir.path}/user.db'); - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - bookmarkDatabaseFile = File('${dir}/user.db'); - } + final dir = await DefaultPathProvider.getSupportDirectory(); + final bookmarkDatabaseFile = File('${dir}/user.db'); if (Platform.isAndroid) { await PlatformMiscMethods.instance.exportFile( diff --git a/lib/pages/settings/tag_rebuild_page.dart b/lib/pages/settings/tag_rebuild_page.dart index 4b24dd75b..5db154a99 100644 --- a/lib/pages/settings/tag_rebuild_page.dart +++ b/lib/pages/settings/tag_rebuild_page.dart @@ -9,6 +9,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:violet/component/hitomi/hitomi.dart'; import 'package:violet/database/query.dart'; import 'package:violet/locale/locale.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/style/palette.dart'; @@ -354,19 +355,7 @@ class _TagRebuildPageState extends State { final subdir = Platform.isAndroid ? '/data' : ''; - late final directory; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - directory = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - directory = '${home}/.violet'; - } + final directory = await DefaultPathProvider.getDocumentsDirectory(); final path1 = File('${directory}$subdir/index.json'); if (path1.existsSync()) path1.deleteSync(); path1.writeAsString(jsonEncode(index)); diff --git a/lib/pages/splash/splash_page.dart b/lib/pages/splash/splash_page.dart index 8d433e6d5..8b6026397 100644 --- a/lib/pages/splash/splash_page.dart +++ b/lib/pages/splash/splash_page.dart @@ -32,6 +32,7 @@ import 'package:violet/other/named_color.dart'; import 'package:violet/pages/database_download/database_download_page.dart'; import 'package:violet/pages/settings/settings_page.dart'; import 'package:violet/script/script_manager.dart'; +import 'package:violet/settings/path.dart'; import 'package:violet/settings/settings.dart'; import 'package:violet/variables.dart'; import 'package:violet/version/sync.dart'; @@ -569,19 +570,7 @@ class _SplashPageState extends State { } } if (widget.switching) { - var dir; - if(Platform.isAndroid || Platform.isIOS){ - var d = await getApplicationDocumentsDirectory(); - dir = d.path; - } else if(Platform.isLinux){ - var home = ''; - Platform.environment.forEach((key, value) { - if(key == 'HOME'){ - home = value; - } - }); - dir = '${home}/.violet'; - } + var dir = await DefaultPathProvider.getDocumentsDirectory(); try { await ((await openDatabase('${dir}/data/data.db')).close()); await deleteDatabase('${dir}/data/data.db'); diff --git a/lib/variables.dart b/lib/variables.dart index 6071f2c4e..cdb007621 100644 --- a/lib/variables.dart +++ b/lib/variables.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'dart:math'; import 'package:path_provider/path_provider.dart'; +import 'package:violet/settings/path.dart'; class Variables { static late String applicationDocumentsDirectory; @@ -12,7 +13,7 @@ class Variables { static Future init() async { applicationDocumentsDirectory = - (await getApplicationDocumentsDirectory()).path; + (await DefaultPathProvider.getDocumentsDirectory()); } static double statusBarHeight = 0;