[mob][photos] Write method to reload magic cache from start
This commit is contained in:
parent
71f1494444
commit
e8e26ee523
1 changed files with 35 additions and 15 deletions
|
@ -132,6 +132,7 @@ class MagicCacheService {
|
|||
Future<List<MagicCache>?> getMagicCache() async {
|
||||
final jsonString = prefs.getString(_key);
|
||||
if (jsonString == null) {
|
||||
_logger.info("No $_key in shared preferences");
|
||||
return null;
|
||||
}
|
||||
return MagicCache.decodeJsonToList(jsonString);
|
||||
|
@ -141,6 +142,40 @@ class MagicCacheService {
|
|||
await prefs.remove(_key);
|
||||
}
|
||||
|
||||
Future<List<GenericSearchResult>> getMagicGenericSearchResult() async {
|
||||
final magicCaches = await getMagicCache();
|
||||
if (magicCaches == null) {
|
||||
_logger.info("No magic cache found");
|
||||
return [];
|
||||
}
|
||||
final List<GenericSearchResult> genericSearchResults = [];
|
||||
for (MagicCache magicCache in magicCaches) {
|
||||
final genericSearchResult = await magicCache.toGenericSearchResult();
|
||||
genericSearchResults.add(genericSearchResult);
|
||||
}
|
||||
return genericSearchResults;
|
||||
}
|
||||
|
||||
Future<void> reloadMagicCaches() async {
|
||||
_logger.info("Reloading magic caches");
|
||||
final randomPromptsData = MagicCacheService.instance.getRandomPrompts();
|
||||
final promptResults = <Map<String, List<int>>>[];
|
||||
final magicCaches = <MagicCache>[];
|
||||
|
||||
for (var randomPromptData in randomPromptsData) {
|
||||
promptResults.add(
|
||||
await MagicCacheService.instance
|
||||
.getMatchingFileIDsForPromptData(randomPromptData),
|
||||
);
|
||||
}
|
||||
for (var promptResult in promptResults) {
|
||||
magicCaches
|
||||
.add(MagicCache(promptResult.keys.first, promptResult.values.first));
|
||||
}
|
||||
|
||||
await MagicCacheService.instance.updateMagicCache(magicCaches);
|
||||
}
|
||||
|
||||
///Generates from 0 to max unique random numbers
|
||||
List<int> _generateUniqueRandomNumbers(int max, int count) {
|
||||
final numbers = <int>[];
|
||||
|
@ -154,19 +189,4 @@ class MagicCacheService {
|
|||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
Future<List<GenericSearchResult>> getMagicGenericSearchResult() async {
|
||||
await Future.delayed(const Duration(seconds: 10));
|
||||
final magicCaches = await getMagicCache();
|
||||
if (magicCaches == null) {
|
||||
_logger.info("No magic cache found");
|
||||
return [];
|
||||
}
|
||||
final List<GenericSearchResult> genericSearchResults = [];
|
||||
for (MagicCache magicCache in magicCaches) {
|
||||
final genericSearchResult = await magicCache.toGenericSearchResult();
|
||||
genericSearchResults.add(genericSearchResult);
|
||||
}
|
||||
return genericSearchResults;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue