π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Write function that selected 4 prompts from list of prompts at random and returns them as MagicCache objects
ashilkn11666eeb3310
diff --git a/mobile/lib/services/magic_cache_service.dart b/mobile/lib/services/magic_cache_service.dartindex 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<void> _updateCacheIfNeeded() async {final jsonFile = await RemoteAssetsService.instance.getAssetIfUpdated(_kMagicPromptsDataUrl);- if (jsonFile == null) {}+ if (jsonFile != null) {}}- Future<Map<String, List<int>>> getMatchingFileIDsForPromptData(- Map<String, Object> promptData,+ Future<List<int>> getMatchingFileIDsForPromptData(+ Map<String, dynamic> 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<void> updateMagicCache() async {- final magicCaches = <MagicCache>[];+ 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<int> _generateUniqueRandomNumbers(int max, int count) {- final numbers = <int>[];- for (int i = 1; i <= count;) {- final randomNumber = Random().nextInt(max + 1);- if (numbers.contains(randomNumber)) {- continue;+ Future<List<dynamic>> _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<List<MagicCache>> nonEmptyMagicResults(+ List<dynamic> magicPromptsData,+ ) async {+ const limit = 4;+ final results = <MagicCache>[];+ 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.dartindex 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<File> 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);}}