forked from AppFlowy-IO/AppFlowy
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support clearing caches and fix unable to load image (AppFlowy-…
…IO#4809) * feat: support clearing caches * fix: try to clear the image cache when loading failed. * feat: clear cache on mobile * chore: add error log
- Loading branch information
Showing
13 changed files
with
229 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
frontend/appflowy_flutter/lib/shared/appflowy_cache_manager.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:appflowy_backend/log.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
class FlowyCacheManager { | ||
final _caches = <ICache>[]; | ||
|
||
// if you add a new cache, you should register it here. | ||
void registerCache(ICache cache) { | ||
_caches.add(cache); | ||
} | ||
|
||
void unregisterAllCache(ICache cache) { | ||
_caches.clear(); | ||
} | ||
|
||
Future<void> clearAllCache() async { | ||
try { | ||
for (final cache in _caches) { | ||
await cache.clearAll(); | ||
} | ||
|
||
Log.info('Cache cleared'); | ||
} catch (e) { | ||
Log.error(e); | ||
} | ||
} | ||
|
||
Future<int> getCacheSize() async { | ||
try { | ||
int tmpDirSize = 0; | ||
for (final cache in _caches) { | ||
tmpDirSize += await cache.cacheSize(); | ||
} | ||
Log.info('Cache size: $tmpDirSize'); | ||
return tmpDirSize; | ||
} catch (e) { | ||
Log.error(e); | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
abstract class ICache { | ||
Future<int> cacheSize(); | ||
Future<void> clearAll(); | ||
} | ||
|
||
class TemporaryDirectoryCache implements ICache { | ||
@override | ||
Future<int> cacheSize() async { | ||
final tmpDir = await getTemporaryDirectory(); | ||
final tmpDirStat = await tmpDir.stat(); | ||
return tmpDirStat.size; | ||
} | ||
|
||
@override | ||
Future<void> clearAll() async { | ||
final tmpDir = await getTemporaryDirectory(); | ||
await tmpDir.delete(recursive: true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
frontend/appflowy_flutter/lib/shared/custom_image_cache_manager.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import 'package:appflowy/shared/appflowy_cache_manager.dart'; | ||
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; | ||
|
||
class CustomImageCacheManager extends CacheManager with ImageCacheManager { | ||
class CustomImageCacheManager extends CacheManager | ||
with ImageCacheManager | ||
implements ICache { | ||
CustomImageCacheManager._() : super(Config(key)); | ||
|
||
factory CustomImageCacheManager() => _instance; | ||
|
||
static final CustomImageCacheManager _instance = CustomImageCacheManager._(); | ||
|
||
static const key = 'appflowy_image_cache'; | ||
|
||
@override | ||
Future<int> cacheSize() async { | ||
// https://github.com/Baseflow/flutter_cache_manager/issues/239#issuecomment-719475429 | ||
// this package does not provide a way to get the cache size | ||
return 0; | ||
} | ||
|
||
@override | ||
Future<void> clearAll() async { | ||
await emptyCache(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
.../widgets/settings_export_file_widget.dart → ...ts/files/settings_export_file_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...flutter/lib/workspace/presentation/settings/widgets/files/settings_file_cache_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'package:appflowy/generated/flowy_svgs.g.dart'; | ||
import 'package:appflowy/generated/locale_keys.g.dart'; | ||
import 'package:appflowy/shared/appflowy_cache_manager.dart'; | ||
import 'package:appflowy/startup/startup.dart'; | ||
import 'package:appflowy/workspace/presentation/home/toast.dart'; | ||
import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; | ||
import 'package:easy_localization/easy_localization.dart'; | ||
import 'package:flowy_infra_ui/flowy_infra_ui.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class SettingsFileCacheWidget extends StatelessWidget { | ||
const SettingsFileCacheWidget({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Expanded( | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Padding( | ||
padding: const EdgeInsets.symmetric(horizontal: 5.0), | ||
child: FlowyText.medium( | ||
LocaleKeys.settings_files_clearCache.tr(), | ||
fontSize: 13, | ||
overflow: TextOverflow.ellipsis, | ||
), | ||
), | ||
const VSpace(8), | ||
Opacity( | ||
opacity: 0.6, | ||
child: FlowyText( | ||
LocaleKeys.settings_files_clearCacheDesc.tr(), | ||
fontSize: 10, | ||
maxLines: 3, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
const _ClearCacheButton(), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class _ClearCacheButton extends StatelessWidget { | ||
const _ClearCacheButton(); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FlowyIconButton( | ||
hoverColor: Theme.of(context).colorScheme.secondaryContainer, | ||
tooltipText: LocaleKeys.settings_files_clearCache.tr(), | ||
icon: FlowySvg( | ||
FlowySvgs.delete_s, | ||
size: const Size.square(18), | ||
color: Theme.of(context).iconTheme.color, | ||
), | ||
onPressed: () { | ||
NavigatorAlertDialog( | ||
title: LocaleKeys.settings_files_areYouSureToClearCache.tr(), | ||
confirm: () async { | ||
await getIt<FlowyCacheManager>().clearAllCache(); | ||
if (context.mounted) { | ||
showSnackBarMessage( | ||
context, | ||
LocaleKeys.settings_files_clearCacheSuccess.tr(), | ||
); | ||
} | ||
}, | ||
).show(context); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters