From 5cff4357eec7673d1003855b0965d18cb3c94754 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:31:28 +0530 Subject: [PATCH] [mob] ServiceLocator for updateService --- mobile/lib/core/network/network.dart | 3 +-- mobile/lib/main.dart | 19 ++++++++----------- mobile/lib/service_locator.dart | 15 ++++++++++++++- mobile/lib/services/update_service.dart | 14 ++++++-------- mobile/lib/ui/home/landing_page_widget.dart | 6 +++--- .../notification/update/change_log_page.dart | 6 +++--- mobile/lib/ui/payment/subscription.dart | 3 +-- .../ui/payment/subscription_plan_widget.dart | 4 ++-- .../lib/ui/settings/about_section_widget.dart | 13 ++++++------- mobile/lib/ui/settings/app_update_dialog.dart | 3 ++- .../settings/backup/free_space_options.dart | 5 ++--- .../settings/debug/debug_section_widget.dart | 4 ++-- .../ui/settings/social_section_widget.dart | 4 ++-- mobile/lib/ui/tabs/home_widget.dart | 12 ++++++------ .../gallery/gallery_app_bar_widget.dart | 3 +-- 15 files changed, 59 insertions(+), 55 deletions(-) diff --git a/mobile/lib/core/network/network.dart b/mobile/lib/core/network/network.dart index 076fb2584b..ded98a12bc 100644 --- a/mobile/lib/core/network/network.dart +++ b/mobile/lib/core/network/network.dart @@ -14,9 +14,8 @@ class NetworkClient { late Dio _dio; late Dio _enteDio; - Future init() async { + Future init(PackageInfo packageInfo) async { await FkUserAgent.init(); - final packageInfo = await PackageInfo.fromPlatform(); final endpoint = Configuration.instance.getHttpEndpoint(); _dio = Dio( BaseOptions( diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index f355f6f6e8..b121a103ac 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -12,6 +12,7 @@ import "package:flutter/services.dart"; import "package:flutter_displaymode/flutter_displaymode.dart"; import 'package:logging/logging.dart'; import "package:media_kit/media_kit.dart"; +import "package:package_info_plus/package_info_plus.dart"; import 'package:path_provider/path_provider.dart'; import 'package:photos/app.dart'; import "package:photos/audio_session_handler.dart"; @@ -47,7 +48,6 @@ import 'package:photos/services/remote_sync_service.dart'; import 'package:photos/services/search_service.dart'; import 'package:photos/services/sync_service.dart'; import 'package:photos/services/trash_sync_service.dart'; -import 'package:photos/services/update_service.dart'; import 'package:photos/services/user_remote_flag_service.dart'; import 'package:photos/services/user_service.dart'; import 'package:photos/ui/tools/app_lock.dart'; @@ -176,7 +176,7 @@ Future _runInBackground(String taskId) async { [ _homeWidgetSync(), () async { - UpdateService.instance.showUpdateNotification().ignore(); + updateService.showUpdateNotification().ignore(); await _sync('bgSync'); }(), ], @@ -212,7 +212,8 @@ Future _init(bool isBackground, {String via = ''}) async { _isProcessRunning = true; _logger.info("Initializing... inBG =$isBackground via: $via"); final SharedPreferences preferences = await SharedPreferences.getInstance(); - await _logFGHeartBeatInfo(); + final PackageInfo packageInfo = await PackageInfo.fromPlatform(); + await _logFGHeartBeatInfo(preferences); _logger.info("_logFGHeartBeatInfo done"); unawaited(_scheduleHeartBeat(preferences, isBackground)); NotificationService.instance.init(preferences); @@ -234,10 +235,11 @@ Future _init(bool isBackground, {String via = ''}) async { _logger.info("Configuration done"); _logger.info("NetworkClient init"); - await NetworkClient.instance.init(); + await NetworkClient.instance.init(packageInfo); _logger.info("NetworkClient init done"); - ServiceLocator.instance.init(preferences, NetworkClient.instance.enteDio); + ServiceLocator.instance + .init(preferences, NetworkClient.instance.enteDio, packageInfo); _logger.info("UserService init"); await UserService.instance.init(); @@ -255,10 +257,6 @@ Future _init(bool isBackground, {String via = ''}) async { await UserRemoteFlagService.instance.init(); _logger.info("UserRemoteFlagService init done"); - _logger.info("UpdateService init"); - await UpdateService.instance.init(); - _logger.info("UpdateService init done"); - _logger.info("BillingService init"); BillingService.instance.init(); _logger.info("BillingService init done"); @@ -459,9 +457,8 @@ Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { } } -Future _logFGHeartBeatInfo() async { +Future _logFGHeartBeatInfo(SharedPreferences prefs) async { final bool isRunningInFG = await _isRunningInForeground(); - final prefs = await SharedPreferences.getInstance(); await prefs.reload(); final lastFGTaskHeartBeatTime = prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0; final String lastRun = lastFGTaskHeartBeatTime == 0 diff --git a/mobile/lib/service_locator.dart b/mobile/lib/service_locator.dart index ee829e961c..3261aac612 100644 --- a/mobile/lib/service_locator.dart +++ b/mobile/lib/service_locator.dart @@ -2,22 +2,26 @@ import "package:dio/dio.dart"; 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/services/storage_bonus_service.dart"; +import "package:photos/services/update_service.dart"; import "package:photos/utils/local_settings.dart"; import "package:shared_preferences/shared_preferences.dart"; class ServiceLocator { late final SharedPreferences prefs; late final Dio enteDio; + late final PackageInfo packageInfo; // instance ServiceLocator._privateConstructor(); static final ServiceLocator instance = ServiceLocator._privateConstructor(); - init(SharedPreferences prefs, Dio enteDio) { + init(SharedPreferences prefs, Dio enteDio, PackageInfo packageInfo) { this.prefs = prefs; this.enteDio = enteDio; + this.packageInfo = packageInfo; } } @@ -50,3 +54,12 @@ StorageBonusService get storageBonusService { ); return _storageBonusService!; } + +UpdateService? _updateService; +UpdateService get updateService { + _updateService ??= UpdateService( + ServiceLocator.instance.prefs, + ServiceLocator.instance.packageInfo, + ); + return _updateService!; +} diff --git a/mobile/lib/services/update_service.dart b/mobile/lib/services/update_service.dart index 7b53c2e246..b58bf6fb22 100644 --- a/mobile/lib/services/update_service.dart +++ b/mobile/lib/services/update_service.dart @@ -11,21 +11,19 @@ import 'package:tuple/tuple.dart'; import 'package:url_launcher/url_launcher_string.dart'; class UpdateService { - UpdateService._privateConstructor(); - - static final UpdateService instance = UpdateService._privateConstructor(); static const kUpdateAvailableShownTimeKey = "update_available_shown_time_key"; static const changeLogVersionKey = "update_change_log_key"; static const currentChangeLogVersion = 24; LatestVersionInfo? _latestVersion; final _logger = Logger("UpdateService"); - late PackageInfo _packageInfo; - late SharedPreferences _prefs; + final PackageInfo _packageInfo; + final SharedPreferences _prefs; - Future init() async { - _packageInfo = await PackageInfo.fromPlatform(); - _prefs = await SharedPreferences.getInstance(); + UpdateService(SharedPreferences prefs, PackageInfo packageInfo) + : _prefs = prefs, + _packageInfo = packageInfo { + debugPrint("UpdateService constructor called"); } Future showChangeLog() async { diff --git a/mobile/lib/ui/home/landing_page_widget.dart b/mobile/lib/ui/home/landing_page_widget.dart index 16604cfca9..e341746ac6 100644 --- a/mobile/lib/ui/home/landing_page_widget.dart +++ b/mobile/lib/ui/home/landing_page_widget.dart @@ -9,7 +9,7 @@ import 'package:photos/core/configuration.dart'; import 'package:photos/ente_theme_data.dart'; import "package:photos/generated/l10n.dart"; import "package:photos/l10n/l10n.dart"; -import 'package:photos/services/update_service.dart'; +import "package:photos/service_locator.dart"; import 'package:photos/ui/account/email_entry_page.dart'; import 'package:photos/ui/account/login_page.dart'; import 'package:photos/ui/account/password_entry_page.dart'; @@ -224,7 +224,7 @@ class _LandingPageWidgetState extends State { } Future _navigateToSignUpPage() async { - UpdateService.instance.hideChangeLog().ignore(); + updateService.hideChangeLog().ignore(); Widget page; if (Configuration.instance.getEncryptedToken() == null) { page = const EmailEntryPage(); @@ -254,7 +254,7 @@ class _LandingPageWidgetState extends State { } void _navigateToSignInPage() { - UpdateService.instance.hideChangeLog().ignore(); + updateService.hideChangeLog().ignore(); Widget page; if (Configuration.instance.getEncryptedToken() == null) { page = const LoginPage(); diff --git a/mobile/lib/ui/notification/update/change_log_page.dart b/mobile/lib/ui/notification/update/change_log_page.dart index 01d91a8b2b..6c5ed621a9 100644 --- a/mobile/lib/ui/notification/update/change_log_page.dart +++ b/mobile/lib/ui/notification/update/change_log_page.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import "package:photos/generated/l10n.dart"; -import 'package:photos/services/update_service.dart'; +import "package:photos/service_locator.dart"; import 'package:photos/theme/ente_theme.dart'; import 'package:photos/ui/components/buttons/button_widget.dart'; import 'package:photos/ui/components/divider_widget.dart'; @@ -64,7 +64,7 @@ class _ChangeLogPageState extends State { labelText: S.of(context).continueLabel, icon: Icons.arrow_forward_outlined, onTap: () async { - await UpdateService.instance.hideChangeLog(); + await updateService.hideChangeLog(); if (mounted && Navigator.of(context).canPop()) { Navigator.of(context).pop(); } @@ -80,7 +80,7 @@ class _ChangeLogPageState extends State { icon: Icons.favorite_rounded, iconColor: enteColorScheme.primary500, onTap: () async { - await UpdateService.instance.launchReviewUrl(); + await updateService.launchReviewUrl(); }, ), const SizedBox(height: 8), diff --git a/mobile/lib/ui/payment/subscription.dart b/mobile/lib/ui/payment/subscription.dart index c30a1c67dd..750fac0ae7 100644 --- a/mobile/lib/ui/payment/subscription.dart +++ b/mobile/lib/ui/payment/subscription.dart @@ -1,12 +1,11 @@ import 'package:flutter/cupertino.dart'; import 'package:photos/core/configuration.dart'; import "package:photos/service_locator.dart"; -import 'package:photos/services/update_service.dart'; import "package:photos/ui/payment/store_subscription_page.dart"; import 'package:photos/ui/payment/stripe_subscription_page.dart'; StatefulWidget getSubscriptionPage({bool isOnBoarding = false}) { - if (UpdateService.instance.isIndependentFlavor()) { + if (updateService.isIndependentFlavor()) { return StripeSubscriptionPage(isOnboarding: isOnBoarding); } if (flagService.enableStripe && _isUserCreatedPostStripeSupport()) { diff --git a/mobile/lib/ui/payment/subscription_plan_widget.dart b/mobile/lib/ui/payment/subscription_plan_widget.dart index cc08c15f19..d5723e4a86 100644 --- a/mobile/lib/ui/payment/subscription_plan_widget.dart +++ b/mobile/lib/ui/payment/subscription_plan_widget.dart @@ -2,7 +2,7 @@ import "package:flutter/foundation.dart"; import 'package:flutter/material.dart'; import "package:flutter/scheduler.dart"; import "package:flutter_animate/flutter_animate.dart"; -import "package:photos/services/update_service.dart"; +import "package:photos/service_locator.dart"; import "package:photos/theme/colors.dart"; import "package:photos/theme/ente_theme.dart"; import 'package:photos/utils/data_util.dart'; @@ -163,7 +163,7 @@ class _Price extends StatelessWidget { final priceDouble = double.parse(priceWithoutCurrency); final pricePerMonth = priceDouble / 12; final pricePerMonthString = pricePerMonth.toStringAsFixed(2); - final bool isPlayStore = UpdateService.instance.isPlayStoreFlavor(); + final bool isPlayStore = updateService.isPlayStoreFlavor(); return Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ diff --git a/mobile/lib/ui/settings/about_section_widget.dart b/mobile/lib/ui/settings/about_section_widget.dart index 01df5544a9..24f98e5a7e 100644 --- a/mobile/lib/ui/settings/about_section_widget.dart +++ b/mobile/lib/ui/settings/about_section_widget.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; import "package:photos/generated/l10n.dart"; -import 'package:photos/services/update_service.dart'; -import 'package:photos/theme/ente_theme.dart'; -import 'package:photos/ui/common/web_page.dart'; +import "package:photos/service_locator.dart"; +import "package:photos/theme/ente_theme.dart"; +import "package:photos/ui/common/web_page.dart"; import 'package:photos/ui/components/captioned_text_widget.dart'; import 'package:photos/ui/components/expandable_menu_item_widget.dart'; import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart'; @@ -51,7 +51,7 @@ class AboutSectionWidget extends StatelessWidget { url: "https://ente.io/terms", ), sectionOptionSpacing, - UpdateService.instance.isIndependent() + updateService.isIndependent() ? Column( children: [ MenuItemWidget( @@ -65,8 +65,7 @@ class AboutSectionWidget extends StatelessWidget { final dialog = createProgressDialog(context, S.of(context).checking); await dialog.show(); - final shouldUpdate = - await UpdateService.instance.shouldUpdate(); + final shouldUpdate = await updateService.shouldUpdate(); await dialog.hide(); if (shouldUpdate) { // ignore: unawaited_futures @@ -75,7 +74,7 @@ class AboutSectionWidget extends StatelessWidget { context: context, builder: (BuildContext context) { return AppUpdateDialog( - UpdateService.instance.getLatestVersionInfo(), + updateService.getLatestVersionInfo(), ); }, barrierColor: Colors.black.withOpacity(0.85), diff --git a/mobile/lib/ui/settings/app_update_dialog.dart b/mobile/lib/ui/settings/app_update_dialog.dart index c9e612201a..c4c6608db8 100644 --- a/mobile/lib/ui/settings/app_update_dialog.dart +++ b/mobile/lib/ui/settings/app_update_dialog.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import "package:photos/generated/l10n.dart"; +import "package:photos/service_locator.dart"; import 'package:photos/services/update_service.dart'; import 'package:photos/theme/ente_theme.dart'; import "package:photos/ui/components/buttons/button_widget.dart"; @@ -82,7 +83,7 @@ class _AppUpdateDialogState extends State { ], ); final shouldForceUpdate = - UpdateService.instance.shouldForceUpdate(widget.latestVersionInfo!); + updateService.shouldForceUpdate(widget.latestVersionInfo!); return PopScope( canPop: !shouldForceUpdate, child: AlertDialog( diff --git a/mobile/lib/ui/settings/backup/free_space_options.dart b/mobile/lib/ui/settings/backup/free_space_options.dart index 1c900edb77..b65a1a16c8 100644 --- a/mobile/lib/ui/settings/backup/free_space_options.dart +++ b/mobile/lib/ui/settings/backup/free_space_options.dart @@ -8,7 +8,6 @@ import "package:photos/models/duplicate_files.dart"; import "package:photos/service_locator.dart"; import "package:photos/services/deduplication_service.dart"; import "package:photos/services/sync_service.dart"; -import "package:photos/services/update_service.dart"; import 'package:photos/theme/ente_theme.dart'; import 'package:photos/ui/components/buttons/button_widget.dart'; import 'package:photos/ui/components/buttons/icon_button_widget.dart'; @@ -238,7 +237,7 @@ class _FreeUpSpaceOptionsScreenState extends State { S.of(context).youHaveSuccessfullyFreedUp(formatBytes(status.size)), firstButtonLabel: S.of(context).rateUs, firstButtonOnTap: () async { - await UpdateService.instance.launchReviewUrl(); + await updateService.launchReviewUrl(); }, firstButtonType: ButtonType.primary, secondButtonLabel: S.of(context).ok, @@ -282,7 +281,7 @@ class _FreeUpSpaceOptionsScreenState extends State { ), firstButtonLabel: S.of(context).rateUs, firstButtonOnTap: () async { - await UpdateService.instance.launchReviewUrl(); + await updateService.launchReviewUrl(); }, firstButtonType: ButtonType.primary, secondButtonLabel: S.of(context).ok, diff --git a/mobile/lib/ui/settings/debug/debug_section_widget.dart b/mobile/lib/ui/settings/debug/debug_section_widget.dart index 6a3b887524..3a7634287b 100644 --- a/mobile/lib/ui/settings/debug/debug_section_widget.dart +++ b/mobile/lib/ui/settings/debug/debug_section_widget.dart @@ -1,9 +1,9 @@ import 'package:flutter/material.dart'; import 'package:photos/core/configuration.dart'; +import "package:photos/service_locator.dart"; import 'package:photos/services/ignored_files_service.dart'; import 'package:photos/services/local_sync_service.dart'; import 'package:photos/services/sync_service.dart'; -import 'package:photos/services/update_service.dart'; import 'package:photos/theme/ente_theme.dart'; import 'package:photos/ui/components/captioned_text_widget.dart'; import 'package:photos/ui/components/expandable_menu_item_widget.dart'; @@ -36,7 +36,7 @@ class DebugSectionWidget extends StatelessWidget { trailingIcon: Icons.chevron_right_outlined, trailingIconIsMuted: true, onTap: () async { - await UpdateService.instance.resetChangeLog(); + await updateService.resetChangeLog(); _showKeyAttributesDialog(context); }, ), diff --git a/mobile/lib/ui/settings/social_section_widget.dart b/mobile/lib/ui/settings/social_section_widget.dart index 0b29760c13..b9329eec7f 100644 --- a/mobile/lib/ui/settings/social_section_widget.dart +++ b/mobile/lib/ui/settings/social_section_widget.dart @@ -2,7 +2,7 @@ import "dart:io"; import 'package:flutter/material.dart'; import "package:photos/generated/l10n.dart"; -import 'package:photos/services/update_service.dart'; +import "package:photos/service_locator.dart"; import 'package:photos/theme/ente_theme.dart'; import 'package:photos/ui/components/captioned_text_widget.dart'; import 'package:photos/ui/components/expandable_menu_item_widget.dart'; @@ -24,7 +24,7 @@ class SocialSectionWidget extends StatelessWidget { Widget _getSectionOptions(BuildContext context) { final List options = []; - final result = UpdateService.instance.getRateDetails(); + final result = updateService.getRateDetails(); final String ratePlace = result.item1; final String rateUrl = result.item2; options.addAll( diff --git a/mobile/lib/ui/tabs/home_widget.dart b/mobile/lib/ui/tabs/home_widget.dart index 1922227316..14131a3b1d 100644 --- a/mobile/lib/ui/tabs/home_widget.dart +++ b/mobile/lib/ui/tabs/home_widget.dart @@ -26,12 +26,12 @@ import 'package:photos/events/user_logged_out_event.dart'; import "package:photos/generated/l10n.dart"; import 'package:photos/models/collection/collection_items.dart'; 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/update_service.dart'; import 'package:photos/services/user_service.dart'; import 'package:photos/states/user_details_state.dart'; import 'package:photos/theme/colors.dart'; @@ -194,7 +194,7 @@ class _HomeWidgetState extends State { }, ); _initDeepLinks(); - UpdateService.instance.shouldShowUpdateNotification().then((value) { + updateService.shouldShowUpdateNotification().then((value) { Future.delayed(Duration.zero, () { if (value) { showDialog( @@ -202,12 +202,12 @@ class _HomeWidgetState extends State { context: context, builder: (BuildContext context) { return AppUpdateDialog( - UpdateService.instance.getLatestVersionInfo(), + updateService.getLatestVersionInfo(), ); }, barrierColor: Colors.black.withOpacity(0.85), ); - UpdateService.instance.resetUpdateAvailableShownTime(); + updateService.resetUpdateAvailableShownTime(); } }); }); @@ -540,7 +540,7 @@ class _HomeWidgetState extends State { } showChangeLog(BuildContext context) async { - final bool show = await UpdateService.instance.showChangeLog(); + final bool show = await updateService.showChangeLog(); if (!show || !Configuration.instance.isLoggedIn()) { return; } @@ -566,7 +566,7 @@ class _HomeWidgetState extends State { }, ); // Do not show change dialog again - UpdateService.instance.hideChangeLog().ignore(); + updateService.hideChangeLog().ignore(); } void _onDidReceiveNotificationResponse( diff --git a/mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart b/mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart index fb9c05cf0a..8f4f3771a6 100644 --- a/mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart +++ b/mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart @@ -24,7 +24,6 @@ import 'package:photos/models/selected_files.dart'; import 'package:photos/service_locator.dart'; import 'package:photos/services/collections_service.dart'; import 'package:photos/services/sync_service.dart'; -import 'package:photos/services/update_service.dart'; import 'package:photos/ui/actions/collection/collection_sharing_actions.dart'; import "package:photos/ui/cast/auto.dart"; import "package:photos/ui/cast/choose.dart"; @@ -274,7 +273,7 @@ class _GalleryAppBarWidgetState extends State { body: S.of(context).youHaveSuccessfullyFreedUp(formatBytes(status.size)), firstButtonLabel: S.of(context).rateUs, firstButtonOnTap: () async { - await UpdateService.instance.launchReviewUrl(); + await updateService.launchReviewUrl(); }, firstButtonType: ButtonType.primary, secondButtonLabel: S.of(context).ok,