initCache function
Future<Box>
initCache(
[ - Duration autoDeleteDuration = const Duration(days: 1)
])
Implementation
Future<Box> initCache(
[Duration autoDeleteDuration = const Duration(days: 1)]) async {
const boxName = 'story_screen_cache';
final Directory directory = await getApplicationDocumentsDirectory();
Hive.init(directory.path);
final box = await Hive.openBox<dynamic>(boxName);
final List<String> deleteList = [];
for (final cacheKey in box.keys) {
final data =
box.get(cacheKey) != null ? jsonDecode(box.get(cacheKey)) : null;
if (data == null) {
deleteList.add(cacheKey);
} else {
final result = _CachedStory.fromJson(data);
if (result.path != null && result.createdDate != null) {
if (DateTime.now().difference(result.createdDate!) >=
autoDeleteDuration) {
File(result.path!).delete();
deleteList.add(cacheKey);
}
} else {
deleteList.add(cacheKey);
}
}
}
box.deleteAll(deleteList);
return box;
}