Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix: make people and albums widget better

Prateek Sunalfcef3fd4e52a
diff --git a/mobile/lib/db/entities_db.dart b/mobile/lib/db/entities_db.dart
index d4becaf744..fba74d98a5 100644
--- a/mobile/lib/db/entities_db.dart
+++ b/mobile/lib/db/entities_db.dart
@@ -77,6 +77,20 @@ extension EntitiesDB on FilesDB {
);
}
+ Future<List<LocalEntityData>> getCertainEntities(
+ EntityType type,
+ List<String> ids,
+ ) async {
+ final db = await sqliteAsyncDB;
+ final List<Map<String, dynamic>> maps = await db.getAll(
+ 'SELECT * FROM entities WHERE type = ? AND id IN (${List.filled(ids.length, '?').join(',')})',
+ [type.name, ...ids],
+ );
+ return List.generate(maps.length, (i) {
+ return LocalEntityData.fromJson(maps[i]);
+ });
+ }
+
Future<List<LocalEntityData>> getEntities(EntityType type) async {
final db = await sqliteAsyncDB;
final List<Map<String, dynamic>> maps = await db.getAll(
diff --git a/mobile/lib/services/album_home_widget_service.dart b/mobile/lib/services/album_home_widget_service.dart
index 1f09aa1e7d..3039ca5dd1 100644
--- a/mobile/lib/services/album_home_widget_service.dart
+++ b/mobile/lib/services/album_home_widget_service.dart
@@ -28,7 +28,6 @@ class AlbumHomeWidgetService {
static const String IOS_CLASS_NAME = "EnteAlbumWidget";
static const String ALBUMS_CHANGED_KEY = "albumsChanged.widget";
static const String ALBUMS_STATUS_KEY = "albumsStatusKey.widget";
- static const String TOTAL_ALBUMS_KEY = "totalAlbums";
static const int MAX_ALBUMS_LIMIT = 50;
// Singleton pattern
@@ -185,8 +184,6 @@ class AlbumHomeWidgetService {
int collectionId,
BuildContext context,
) async {
- await _refreshAlbumsWidget();
-
final collection =
CollectionsService.instance.getCollectionByID(collectionId);
if (collection == null) {
@@ -214,7 +211,7 @@ class AlbumHomeWidgetService {
return;
}
- await routeToPage(
+ routeToPage(
context,
DetailPage(
DetailPageConfiguration(
@@ -224,7 +221,8 @@ class AlbumHomeWidgetService {
),
),
forceCustomPageRoute: true,
- );
+ ).ignore();
+ await _refreshAlbumsWidget();
}
// Private methods
diff --git a/mobile/lib/services/entity_service.dart b/mobile/lib/services/entity_service.dart
index 9226eafe7f..296d0e9962 100644
--- a/mobile/lib/services/entity_service.dart
+++ b/mobile/lib/services/entity_service.dart
@@ -41,6 +41,13 @@ class EntityService {
return "entity_last_sync_time_" + type.typeToString();
}
+ Future<List<LocalEntityData>> getCertainEntities(
+ EntityType type,
+ List<String> ids,
+ ) async {
+ return await _db.getCertainEntities(type, ids);
+ }
+
Future<List<LocalEntityData>> getEntities(EntityType type) async {
return await _db.getEntities(type);
}
diff --git a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart b/mobile/lib/services/machine_learning/face_ml/person/person_service.dart
index b9fad6e7d5..5ab093b5cf 100644
--- a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart
+++ b/mobile/lib/services/machine_learning/face_ml/person/person_service.dart
@@ -71,6 +71,19 @@ class PersonService {
});
}
+ Future<List<PersonEntity>> getCertainPersons(List<String> ids) async {
+ final entities =
+ await entityService.getCertainEntities(EntityType.cgroup, ids);
+ return entities
+ .map(
+ (e) => PersonEntity(
+ e.id,
+ PersonData.fromJson(json.decode(e.data)),
+ ),
+ )
+ .toList();
+ }
+
Future<List<PersonEntity>> getPersons() async {
final entities = await entityService.getEntities(EntityType.cgroup);
return entities
diff --git a/mobile/lib/services/memory_home_widget_service.dart b/mobile/lib/services/memory_home_widget_service.dart
index 108162f186..f03ccbf634 100644
--- a/mobile/lib/services/memory_home_widget_service.dart
+++ b/mobile/lib/services/memory_home_widget_service.dart
@@ -21,7 +21,6 @@ class MemoryHomeWidgetService {
static const String IOS_CLASS_NAME = "EnteMemoryWidget";
static const String MEMORY_STATUS_KEY = "memoryStatusKey.widget";
static const String MEMORY_CHANGED_KEY = "memoryChanged.widget";
- static const String TOTAL_MEMORIES_KEY = "totalMemories";
static const int MAX_MEMORIES_LIMIT = 50;
// Singleton pattern
@@ -149,11 +148,13 @@ class MemoryHomeWidgetService {
}
Future<void> onLaunchFromWidget(int generatedId, BuildContext context) async {
+ memoriesCacheService
+ .goToMemoryFromGeneratedFileID(
+ context,
+ generatedId,
+ )
+ .ignore();
await _refreshMemoriesWidget();
- await memoriesCacheService.goToMemoryFromGeneratedFileID(
- context,
- generatedId,
- );
}
// Private methods
diff --git a/mobile/lib/services/people_home_widget_service.dart b/mobile/lib/services/people_home_widget_service.dart
index 5fbe28f7cf..c958988bf2 100644
--- a/mobile/lib/services/people_home_widget_service.dart
+++ b/mobile/lib/services/people_home_widget_service.dart
@@ -28,7 +28,6 @@ class PeopleHomeWidgetService {
static const String IOS_CLASS_NAME = "EntePeopleWidget";
static const String PEOPLE_STATUS_KEY = "peopleStatusKey.widget";
static const String PEOPLE_CHANGED_KEY = "peopleChanged.widget";
- static const String TOTAL_PEOPLE_KEY = "totalPeople";
static const int MAX_PEOPLE_LIMIT = 50;
// Singleton pattern
@@ -52,21 +51,10 @@ class PeopleHomeWidgetService {
}
Future<void> setSelectedPeople(List<String> selectedPeople) async {
- final previousSelection = getSelectedPeople();
await _prefs.setStringList(SELECTED_PEOPLE_KEY, selectedPeople);
- if (previousSelection != null) {
- final oldSet = previousSelection.toSet();
- final newSet = selectedPeople.toSet();
-
- if (oldSet.containsAll(newSet) && newSet.containsAll(oldSet)) {
- _logger.info("People selection unchanged, no update needed");
- return;
- }
- }
-
_logger.info("People selection changed, updating widget");
- await updatePeopleChanged(true);
+ await checkPeopleChanged();
}
String? getPeopleLastHash() {
@@ -77,13 +65,6 @@ class PeopleHomeWidgetService {
await _prefs.setString(PEOPLE_LAST_HASH_KEY, hash);
}
- Future<int> countHomeWidgets() async {
- return await HomeWidgetService.instance.countHomeWidgets(
- ANDROID_CLASS_NAME,
- IOS_CLASS_NAME,
- );
- }
-
Future<void> initPeopleHomeWidget() async {
await HomeWidgetService.instance.computeLock.synchronized(() async {
if (await _hasAnyBlockers()) {
@@ -94,9 +75,11 @@ class PeopleHomeWidgetService {
final bool forceFetchNewPeople = await _shouldUpdateWidgetCache();
if (forceFetchNewPeople) {
- await _loadAndRenderPeople();
+ _logger.info("Initializing people widget: updating people cache");
+ await _updatePeopleWidgetCache();
await updatePeopleChanged(false);
} else {
+ _logger.info("Initializing people widget: syncing existing people");
await _refreshPeopleWidget();
}
});
@@ -113,6 +96,15 @@ class PeopleHomeWidgetService {
await _refreshWidget(message: "PeopleHomeWidget cleared & updated");
}
+ bool getPeopleChanged() {
+ return _prefs.getBool(PEOPLE_CHANGED_KEY) ?? false;
+ }
+
+ Future<void> updatePeopleChanged(bool value) async {
+ _logger.info("Updating people changed flag to $value");
+ await _prefs.setBool(PEOPLE_CHANGED_KEY, value);
+ }
+
WidgetStatus getPeopleStatus() {
return WidgetStatus.values.firstWhereOrNull(
(v) => v.index == (_prefs.getInt(PEOPLE_STATUS_KEY) ?? 0),
@@ -120,19 +112,10 @@ class PeopleHomeWidgetService {
WidgetStatus.notSynced;
}
- bool getPeopleChanged() {
- return _prefs.getBool(PEOPLE_CHANGED_KEY) ?? false;
- }
-
Future<void> updatePeopleStatus(WidgetStatus value) async {
await _prefs.setInt(PEOPLE_STATUS_KEY, value.index);
}
- Future<void> updatePeopleChanged(bool value) async {
- _logger.info("Updating people changed flag to $value");
- await _prefs.setBool(PEOPLE_CHANGED_KEY, value);
- }
-
Future<void> checkPendingPeopleSync() async {
if (await _hasAnyBlockers()) {
_logger.warning("Widget update blocked by existing conditions");
@@ -150,9 +133,11 @@ class PeopleHomeWidgetService {
final peopleIds = await _getEffectiveSelectedPeopleIds();
final currentHash = await _calculateHash(peopleIds);
final lastHash = getPeopleLastHash();
- if (peopleIds.isEmpty || currentHash == lastHash) {
+
+ if (lastHash != null && currentHash == lastHash) {
return false;
}
+
return true;
});
@@ -164,13 +149,18 @@ class PeopleHomeWidgetService {
await initPeopleHomeWidget();
}
+ Future<int> countHomeWidgets() async {
+ return await HomeWidgetService.instance.countHomeWidgets(
+ ANDROID_CLASS_NAME,
+ IOS_CLASS_NAME,
+ );
+ }
+
Future<void> onLaunchFromWidget(
int fileId,
String personId,
BuildContext context,
) async {
- await _refreshPeopleWidget();
-
final file = await FilesDB.instance.getFile(fileId);
if (file == null) {
_logger.warning("Cannot launch widget: file with ID $fileId not found");
@@ -199,7 +189,7 @@ class PeopleHomeWidgetService {
);
final files = clusterFiles.entries.expand((e) => e.value).toList();
- await routeToPage(
+ routeToPage(
context,
DetailPage(
DetailPageConfiguration(
@@ -209,7 +199,8 @@ class PeopleHomeWidgetService {
),
),
forceCustomPageRoute: true,
- );
+ ).ignore();
+ await _refreshPeopleWidget();
}
Future<String> _calculateHash(List<String> peopleIds) async {
@@ -233,16 +224,9 @@ class PeopleHomeWidgetService {
// Check if selected people exist
final peopleIds = await _getEffectiveSelectedPeopleIds();
- try {
- for (final id in peopleIds) {
- final person = await PersonService.instance.getPerson(id);
- if (person == null) {
- _logger.warning("Person not found for id: $id");
- return true;
- }
- }
- } catch (e) {
- _logger.warning("Error looking up people: $e");
+ final hash = await _calculateHash(peopleIds);
+ if (peopleIds.isEmpty || hash.isEmpty) {
+ _logger.warning("No selected people found or hash is empty");
return true;
}
@@ -298,21 +282,16 @@ class PeopleHomeWidgetService {
final peopleIds = await _getEffectiveSelectedPeopleIds();
final Map<String, (String, Iterable<EnteFile>)> peopleFiles = {};
- for (final id in peopleIds) {
- final person = await PersonService.instance.getPerson(id);
- if (person == null) {
- _logger.warning("Person not found for id: $id");
- continue;
- }
-
- final clusterFiles =
- await SearchService.instance.getClusterFilesForPersonID(id);
+ final persons = await PersonService.instance.getCertainPersons(peopleIds);
+ for (final person in persons) {
+ final clusterFiles = await SearchService.instance
+ .getClusterFilesForPersonID(person.remoteID);
final files = clusterFiles.entries.expand((e) => e.value).toList();
if (files.isEmpty) {
_logger.warning("No files found for person: ${person.data.name}");
continue;
}
- peopleFiles[id] = (person.data.name, files);
+ peopleFiles[person.remoteID] = (person.data.name, files);
}
return peopleFiles;
@@ -339,7 +318,7 @@ class PeopleHomeWidgetService {
_logger.info("Home Widget updated: ${message ?? "standard update"}");
}
- Future<void> _loadAndRenderPeople() async {
+ Future<void> _updatePeopleWidgetCache() async {
final peopleIds = await _getEffectiveSelectedPeopleIds();
// TODO: Add logic to directly get random people files from database
final peopleWithFiles = await _getPeople();
diff --git a/mobile/lib/ui/settings/widgets/people_widget_settings.dart b/mobile/lib/ui/settings/widgets/people_widget_settings.dart
index a0d0abbaaa..266a54c7fa 100644
--- a/mobile/lib/ui/settings/widgets/people_widget_settings.dart
+++ b/mobile/lib/ui/settings/widgets/people_widget_settings.dart
@@ -71,8 +71,6 @@ class _PeopleWidgetSettingsState extends State<PeopleWidgetSettings> {
_selectedPeople.personIds.toList(),
);
Navigator.pop(context);
- await PeopleHomeWidgetService.instance
- .checkPeopleChanged();
},
);
},