Skip to content

Commit

Permalink
Use safe when latest failed & try-catch Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Dec 7, 2023
1 parent 65b8d18 commit 1ad92ce
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
84 changes: 74 additions & 10 deletions lib/pages/database_download/database_download_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
}

Future<void> downloadFileAndroid() async {
try {
await downloadFileAndroidWith('latest');
} catch(e){
await downloadFileAndroidWith('safe');
}
}
Future<void> downloadFileIOS() async {
try {
await downloadFileIOSWith('latest');
} catch(e){
await downloadFileIOSWith('safe');
}
}

Future<void> downloadFileAndroidWith(String target) async {
Dio dio = Dio();
int oneMega = 1024 * 1024;
int nu = 0;
Expand All @@ -105,7 +120,18 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
tlatest = tnu;
tnu = 0;
}));
await SyncManager.checkSync();
switch(target){
case 'latest': await SyncManager.checkSyncLatest(); break;
case 'safe': await SyncManager.checkSync(); break;
default: {
try {
await downloadFileAndroidWith('latest');
} catch(e){
await downloadFileAndroidWith('safe');
}
return;
}
}
await dio.download(
SyncManager.getLatestDB().getDBDownloadUrl(widget.dbType!),
'${dir.path}/db.sql.7z', onReceiveProgress: (rec, total) {
Expand Down Expand Up @@ -162,10 +188,26 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {

await DataBaseManager.reloadInstance();

if (Settings.useOptimizeDatabase) {
await deleteUnused();

try {
if (Settings.useOptimizeDatabase) {
await deleteUnused();
await indexing();
}
} catch(e1,st1){
Logger.error('[Sync-check] E: $e1\n'
'$st1');
}
try {
if (Settings.useOptimizeDatabase) await deleteUnused();
} catch(e1,st1){
Logger.error('[deleteUnused] E: $e1\n'
'$st1');
}
try {
await indexing();
} catch(e1,st1){
Logger.error('[indexing] E: $e1\n'
'$st1');
}

if (widget.isSync == true) {
Expand All @@ -181,6 +223,7 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
} catch (e, st) {
Logger.error('[DBDownload] E: $e\n'
'$st');
if(target == 'latest') throw e;
}

setState(() {
Expand All @@ -189,7 +232,7 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
});
}

Future<void> downloadFileIOS() async {
Future<void> downloadFileIOSWith(String target) async {
Dio dio = Dio();
int oneMega = 1024 * 1024;
int nu = 0;
Expand All @@ -210,7 +253,18 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
tlatest = tnu;
tnu = 0;
}));
await SyncManager.checkSync();
switch(target){
case 'latest': await SyncManager.checkSyncLatest(); break;
case 'safe': await SyncManager.checkSync(); break;
default: {
try {
await downloadFileIOSWith('latest');
} catch(e){
await downloadFileIOSWith('safe');
}
return;
}
}
await dio.download(
SyncManager.getLatestDB().getDBDownloadUrliOS(widget.dbType!),
'$dir/data.db', onReceiveProgress: (rec, total) {
Expand Down Expand Up @@ -246,9 +300,18 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {

await DataBaseManager.reloadInstance();

if (Settings.useOptimizeDatabase) await deleteUnused();

await indexing();
try {
if (Settings.useOptimizeDatabase) await deleteUnused();
} catch(e1,st1){
Logger.error('[deleteUnused] E: $e1\n'
'$st1');
}
try {
await indexing();
} catch(e1,st1){
Logger.error('[indexing] E: $e1\n'
'$st1');
}

if (widget.isSync == true) {
Navigator.pop(context);
Expand All @@ -263,6 +326,7 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
} catch (e, st) {
Logger.error('[DBDownload] E: $e\n'
'$st');
if(target == 'latest') throw e;
}

setState(() {
Expand All @@ -275,7 +339,7 @@ class DataBaseDownloadPageState extends State<DataBaseDownloadPage> {
var sql = HitomiManager.translate2query(
'${Settings.includeTags} ${Settings.excludeTags.where((e) => e.trim().isNotEmpty).map((e) => '-$e').join(' ')}')
.replaceAll(' AND ExistOnHitomi=1', '');

await (await DataBaseManager.getInstance()).delete('HitomiColumnModel',
'NOT (${sql.substring(sql.indexOf('WHERE') + 6)})', []);
}
Expand Down
1 change: 1 addition & 0 deletions lib/version/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class SyncManager {
} catch (e, st) {
Logger.error('[Sync-check] E: $e\n'
'$st');
throw e;
}
}

Expand Down

0 comments on commit 1ad92ce

Please sign in to comment.