From 787f2908dac083f797303d2a408c20dc651cb55d Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:44:34 +0530 Subject: [PATCH] [mob] ServiceLocator for EntityService --- mobile/lib/main.dart | 7 +------ mobile/lib/service_locator.dart | 15 +++++++++++++++ mobile/lib/services/entity_service.dart | 17 +++++------------ mobile/lib/services/location_service.dart | 12 ++++++------ .../face_ml/face_recognition_service.dart | 4 ++-- mobile/lib/services/storage_bonus_service.dart | 5 ++++- mobile/lib/services/update_service.dart | 2 +- mobile/lib/ui/tabs/home_widget.dart | 3 +-- 8 files changed, 35 insertions(+), 30 deletions(-) diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index b121a103ac..4a3b6a9511 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -29,7 +29,6 @@ import "package:photos/service_locator.dart"; import 'package:photos/services/app_lifecycle_service.dart'; import 'package:photos/services/billing_service.dart'; import 'package:photos/services/collections_service.dart'; -import "package:photos/services/entity_service.dart"; import 'package:photos/services/favorites_service.dart'; import "package:photos/services/filedata/filedata_service.dart"; import 'package:photos/services/home_widget_service.dart'; @@ -245,10 +244,6 @@ Future _init(bool isBackground, {String via = ''}) async { await UserService.instance.init(); _logger.info("UserService init done"); - _logger.info("EntityService init"); - await EntityService.instance.init(); - _logger.info("EntityService init done"); - _logger.info("LocationService init"); LocationService.instance.init(preferences); _logger.info("LocationService init done"); @@ -308,7 +303,7 @@ Future _init(bool isBackground, {String via = ''}) async { _logger.info("MachineLearningController done"); unawaited(MLService.instance.init()); PersonService.init( - EntityService.instance, + entityService, MLDataDB.instance, preferences, ); diff --git a/mobile/lib/service_locator.dart b/mobile/lib/service_locator.dart index 3261aac612..a1fd77ae93 100644 --- a/mobile/lib/service_locator.dart +++ b/mobile/lib/service_locator.dart @@ -3,6 +3,8 @@ import "package:ente_cast/ente_cast.dart"; import "package:ente_cast_normal/ente_cast_normal.dart"; import "package:ente_feature_flag/ente_feature_flag.dart"; import "package:package_info_plus/package_info_plus.dart"; +import "package:photos/gateways/entity_gw.dart"; +import "package:photos/services/entity_service.dart"; import "package:photos/services/storage_bonus_service.dart"; import "package:photos/services/update_service.dart"; import "package:photos/utils/local_settings.dart"; @@ -26,6 +28,7 @@ class ServiceLocator { } FlagService? _flagService; + FlagService get flagService { _flagService ??= FlagService( ServiceLocator.instance.prefs, @@ -35,6 +38,7 @@ FlagService get flagService { } CastService? _castService; + CastService get castService { _castService ??= CastServiceImpl(); return _castService!; @@ -56,6 +60,7 @@ StorageBonusService get storageBonusService { } UpdateService? _updateService; + UpdateService get updateService { _updateService ??= UpdateService( ServiceLocator.instance.prefs, @@ -63,3 +68,13 @@ UpdateService get updateService { ); return _updateService!; } + +EntityService? _entityService; + +EntityService get entityService { + _entityService ??= EntityService( + ServiceLocator.instance.prefs, + EntityGateway(ServiceLocator.instance.enteDio), + ); + return _entityService!; +} diff --git a/mobile/lib/services/entity_service.dart b/mobile/lib/services/entity_service.dart index 713ba9717e..b8b84406a9 100644 --- a/mobile/lib/services/entity_service.dart +++ b/mobile/lib/services/entity_service.dart @@ -5,7 +5,6 @@ import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; import "package:photos/core/configuration.dart"; -import "package:photos/core/network/network.dart"; import "package:photos/db/entities_db.dart"; import "package:photos/db/files_db.dart"; import "package:photos/gateways/entity_gw.dart"; @@ -20,19 +19,13 @@ import 'package:shared_preferences/shared_preferences.dart'; class EntityService { static const int fetchLimit = 500; final _logger = Logger((EntityService).toString()); + final SharedPreferences _prefs; + final EntityGateway _gateway; final _config = Configuration.instance; - late SharedPreferences _prefs; - late EntityGateway _gateway; - late FilesDB _db; + late final FilesDB _db = FilesDB.instance; - EntityService._privateConstructor(); - - static final EntityService instance = EntityService._privateConstructor(); - - Future init() async { - _prefs = await SharedPreferences.getInstance(); - _db = FilesDB.instance; - _gateway = EntityGateway(NetworkClient.instance.enteDio); + EntityService(this._prefs, this._gateway) { + debugPrint("EntityService constructor"); } String _getEntityKeyPrefix(EntityType type) { diff --git a/mobile/lib/services/location_service.dart b/mobile/lib/services/location_service.dart index f929dca525..d0edee2f16 100644 --- a/mobile/lib/services/location_service.dart +++ b/mobile/lib/services/location_service.dart @@ -13,7 +13,7 @@ import "package:photos/models/file/file.dart"; import "package:photos/models/local_entity_data.dart"; import "package:photos/models/location/location.dart"; import 'package:photos/models/location_tag/location_tag.dart'; -import "package:photos/services/entity_service.dart"; +import "package:photos/service_locator.dart"; import "package:photos/services/remote_assets_service.dart"; import "package:shared_preferences/shared_preferences.dart"; @@ -36,7 +36,7 @@ class LocationService { } Future>> _getStoredLocationTags() async { - final data = await EntityService.instance.getEntities(EntityType.location); + final data = await entityService.getEntities(EntityType.location); return data.map( (e) => LocalEntity(LocationTag.fromJson(json.decode(e.data)), e.id), ); @@ -89,8 +89,8 @@ class LocationService { bSquare: b * b, centerPoint: centerPoint, ); - await EntityService.instance - .addOrUpdate(EntityType.location, locationTag.toJson()); + await entityService.addOrUpdate( + EntityType.location, locationTag.toJson()); Bus.instance.fire(LocationTagUpdatedEvent(LocTagEventType.add)); } catch (e, s) { _logger.severe("Failed to add location tag", e, s); @@ -177,7 +177,7 @@ class LocationService { name: name, ); - await EntityService.instance.addOrUpdate( + await entityService.addOrUpdate( EntityType.location, updatedLoationTag.toJson(), id: locationTagEntity.id, @@ -198,7 +198,7 @@ class LocationService { Future deleteLocationTag(String locTagEntityId) async { try { - await EntityService.instance.deleteEntry( + await entityService.deleteEntry( locTagEntityId, ); Bus.instance.fire( diff --git a/mobile/lib/services/machine_learning/face_ml/face_recognition_service.dart b/mobile/lib/services/machine_learning/face_ml/face_recognition_service.dart index 4c42cd3fdc..b92c395062 100644 --- a/mobile/lib/services/machine_learning/face_ml/face_recognition_service.dart +++ b/mobile/lib/services/machine_learning/face_ml/face_recognition_service.dart @@ -7,7 +7,7 @@ import "package:photos/core/event_bus.dart"; import "package:photos/events/diff_sync_complete_event.dart"; import "package:photos/events/people_changed_event.dart"; import "package:photos/models/api/entity/type.dart"; -import "package:photos/services/entity_service.dart"; +import "package:photos/service_locator.dart"; import "package:photos/services/machine_learning/face_ml/face_detection/detection.dart"; import "package:photos/services/machine_learning/face_ml/face_detection/face_detection_service.dart"; import "package:photos/services/machine_learning/face_ml/face_embedding/face_embedding_service.dart"; @@ -61,7 +61,7 @@ class FaceRecognitionService { return; } _isSyncing = true; - await EntityService.instance.syncEntity(EntityType.cgroup); + await entityService.syncEntity(EntityType.cgroup); if (_shouldSyncPeople) { await PersonService.instance.reconcileClusters(); Bus.instance.fire(PeopleChangedEvent(type: PeopleEventType.syncDone)); diff --git a/mobile/lib/services/storage_bonus_service.dart b/mobile/lib/services/storage_bonus_service.dart index 7881fd11ef..4b6d5daf19 100644 --- a/mobile/lib/services/storage_bonus_service.dart +++ b/mobile/lib/services/storage_bonus_service.dart @@ -1,4 +1,5 @@ import "package:dio/dio.dart"; +import "package:flutter/foundation.dart"; import "package:photos/gateways/storage_bonus_gw.dart"; import "package:photos/models/api/storage_bonus/storage_bonus.dart"; import "package:shared_preferences/shared_preferences.dart"; @@ -11,7 +12,9 @@ class StorageBonusService { final String _showStorageBonusTapCount = "showStorageBonus.tap_count"; StorageBonusService(this.prefs, Dio enteDio) - : gateway = StorageBonusGateway(enteDio); + : gateway = StorageBonusGateway(enteDio) { + debugPrint("StorageBonusService constructor"); + } // returns true if _showStorageBonusTapCount value is less than minTapCountBeforeHidingBanner bool shouldShowStorageBonus() { diff --git a/mobile/lib/services/update_service.dart b/mobile/lib/services/update_service.dart index b58bf6fb22..e8d105ef15 100644 --- a/mobile/lib/services/update_service.dart +++ b/mobile/lib/services/update_service.dart @@ -23,7 +23,7 @@ class UpdateService { UpdateService(SharedPreferences prefs, PackageInfo packageInfo) : _prefs = prefs, _packageInfo = packageInfo { - debugPrint("UpdateService constructor called"); + debugPrint("UpdateService constructor"); } Future showChangeLog() async { diff --git a/mobile/lib/ui/tabs/home_widget.dart b/mobile/lib/ui/tabs/home_widget.dart index 14131a3b1d..53c0b47190 100644 --- a/mobile/lib/ui/tabs/home_widget.dart +++ b/mobile/lib/ui/tabs/home_widget.dart @@ -29,7 +29,6 @@ import 'package:photos/models/selected_files.dart'; import "package:photos/service_locator.dart"; import 'package:photos/services/app_lifecycle_service.dart'; import 'package:photos/services/collections_service.dart'; -import "package:photos/services/entity_service.dart"; import 'package:photos/services/local_sync_service.dart'; import "package:photos/services/notification_service.dart"; import 'package:photos/services/user_service.dart'; @@ -377,7 +376,7 @@ class _HomeWidgetState extends State { return const LandingPageWidget(); } if (!LocalSyncService.instance.hasGrantedPermissions()) { - EntityService.instance.syncEntities(); + entityService.syncEntities(); return const GrantPermissionsWidget(); } if (!LocalSyncService.instance.hasCompletedFirstImport()) {