Delete obsolete old cache

This commit is contained in:
Ming Ming 2021-09-16 18:24:17 +08:00
parent 6b24f6f98a
commit c16c97ec9f
2 changed files with 67 additions and 25 deletions

View file

@ -0,0 +1,7 @@
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
/// Compatibility helper for v29
class CompatV29 {
/// Clear the old cache
static Future<void> clearDefaultCache() => DefaultCacheManager().emptyCache();
}

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
@ -6,7 +8,9 @@ import 'package:nc_photos/changelog.dart' as changelog;
import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/pref.dart'; import 'package:nc_photos/pref.dart';
import 'package:nc_photos/theme.dart'; import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/compat/v29.dart';
import 'package:nc_photos/widget/home.dart'; import 'package:nc_photos/widget/home.dart';
import 'package:nc_photos/widget/processing_dialog.dart';
import 'package:nc_photos/widget/setup.dart'; import 'package:nc_photos/widget/setup.dart';
import 'package:nc_photos/widget/sign_in.dart'; import 'package:nc_photos/widget/sign_in.dart';
@ -87,38 +91,69 @@ class _SplashState extends State<Splash> {
return lastVersion < k.version; return lastVersion < k.version;
} }
void _handleUpgrade() { void _handleUpgrade() async {
final lastVersion = Pref.inst().getLastVersionOr(k.version); try {
// ... final lastVersion = Pref.inst().getLastVersionOr(k.version);
await _upgrade(lastVersion);
final change = _gatherChangelog(lastVersion); final change = _gatherChangelog(lastVersion);
if (change.isNotEmpty) { if (change.isNotEmpty) {
showDialog( await showDialog(
context: context, context: context,
builder: (context) => AlertDialog( builder: (context) => AlertDialog(
title: Text(L10n.global().changelogTitle), title: Text(L10n.global().changelogTitle),
content: SingleChildScrollView( content: SingleChildScrollView(
child: Text(change), child: Text(change),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
)
],
), ),
actions: <Widget>[ );
TextButton( }
onPressed: () { } finally {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
)
],
),
).whenComplete(() {
_initTimedExit();
Pref.inst().setLastVersion(k.version);
});
} else {
_initTimedExit(); _initTimedExit();
Pref.inst().setLastVersion(k.version); Pref.inst().setLastVersion(k.version);
} }
} }
Future<void> _upgrade(int lastVersion) async {
if (lastVersion < 290) {
await _upgrade29(lastVersion);
}
}
Future<void> _upgrade29(int lastVersion) async {
await _peformUpgrade(() async {
try {
_log.info("[_upgrade29] clearDefaultCache");
await CompatV29.clearDefaultCache();
} catch (e, stackTrace) {
_log.shout(
"[_upgrade29] Failed while clearDefaultCache", e, stackTrace);
// just leave the cache then
}
});
}
Future<void> _peformUpgrade(FutureOr<void> Function() fn) async {
showDialog(
context: context,
builder: (_) => ProcessingDialog(
text: L10n.global().genericProcessingDialogContent,
));
try {
await fn();
} finally {
Navigator.of(context).pop();
}
}
String _gatherChangelog(int from) { String _gatherChangelog(int from) {
if (from < 100) { if (from < 100) {
from *= 10; from *= 10;