diff --git a/mobile/lib/services/magic_cache_service.dart b/mobile/lib/services/magic_cache_service.dart index e02f07b77a..aba9d61533 100644 --- a/mobile/lib/services/magic_cache_service.dart +++ b/mobile/lib/services/magic_cache_service.dart @@ -1,5 +1,5 @@ +import "dart:async"; import "dart:convert"; -import 'dart:math'; import "package:logging/logging.dart"; import "package:photos/models/file/file.dart"; @@ -77,22 +77,23 @@ class MagicCacheService { Future _updateCacheIfNeeded() async { final jsonFile = await RemoteAssetsService.instance .getAssetIfUpdated(_kMagicPromptsDataUrl); - if (jsonFile == null) {} + if (jsonFile != null) {} } - Future>> getMatchingFileIDsForPromptData( - Map promptData, + Future> getMatchingFileIDsForPromptData( + Map promptData, ) async { final result = await SemanticSearchService.instance.getMatchingFileIDs( promptData["prompt"] as String, promptData["minimumScore"] as double, ); - return {promptData["title"] as String: result}; + return result; } Future updateMagicCache() async { - final magicCaches = []; + final magicPromptsData = await _loadMagicPrompts(); + final magicCaches = await nonEmptyMagicResults(magicPromptsData); await prefs.setString( _key, MagicCache.encodeListToJson(magicCaches), @@ -126,17 +127,42 @@ class MagicCacheService { return genericSearchResults; } - ///Generates from 0 to max non-repeating random numbers - List _generateUniqueRandomNumbers(int max, int count) { - final numbers = []; - for (int i = 1; i <= count;) { - final randomNumber = Random().nextInt(max + 1); - if (numbers.contains(randomNumber)) { - continue; + Future> _loadMagicPrompts() async { + final file = + await RemoteAssetsService.instance.getAsset(_kMagicPromptsDataUrl); + + final json = jsonDecode(await file.readAsString()); + return json["prompts"]; + } + + ///Returns random non-empty magic results from magicPromptsData + ///Length is capped at [limit], can be less than [limit] if there are not enough + ///non-empty results + Future> nonEmptyMagicResults( + List magicPromptsData, + ) async { + const limit = 4; + final results = []; + final randomIndexes = List.generate( + magicPromptsData.length, + (index) => index, + growable: false, + )..shuffle(); + for (final index in randomIndexes) { + final files = + await getMatchingFileIDsForPromptData(magicPromptsData[index]); + if (files.isNotEmpty) { + results.add( + MagicCache( + magicPromptsData[index]["title"] as String, + files, + ), + ); + } + if (results.length >= limit) { + break; } - numbers.add(randomNumber); - i++; } - return numbers; + return results; } } diff --git a/mobile/lib/services/remote_assets_service.dart b/mobile/lib/services/remote_assets_service.dart index 7828a121bf..b9bed09b50 100644 --- a/mobile/lib/services/remote_assets_service.dart +++ b/mobile/lib/services/remote_assets_service.dart @@ -21,13 +21,13 @@ class RemoteAssetsService { Future getAsset(String remotePath, {bool refetch = false}) async { final path = await _getLocalPath(remotePath); final file = File(path); - if (await file.exists() && !refetch) { + if (file.existsSync() && !refetch) { _logger.info("Returning cached file for $remotePath"); return file; } else { final tempFile = File(path + ".temp"); await _downloadFile(remotePath, tempFile.path); - await tempFile.rename(path); + tempFile.renameSync(path); return File(path); } }