Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Isolate store and push integrations

Neeraj Gupta98d26a527274
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index a194041f9b..845fc8c666 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -8,7 +8,6 @@ import 'package:ente_crypto/ente_crypto.dart';
import "package:ente_pure_utils/ente_pure_utils.dart";
import "package:ente_rust/ente_rust.dart";
import "package:ffmpeg_kit_flutter/ffmpeg_kit_config.dart";
-import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import "package:flutter/gestures.dart";
import 'package:flutter/material.dart';
@@ -149,7 +148,8 @@ Future<void> _runInForeground(
initialMediaExtensionAction: initialMediaExtensionAction,
),
lockScreen: const LockScreen(),
- enabled: await Configuration.instance.shouldShowLockScreen() ||
+ enabled:
+ await Configuration.instance.shouldShowLockScreen() ||
localSettings.isOnGuestView(),
locale: locale,
lightTheme: lightThemeData,
@@ -310,8 +310,9 @@ Future<void> _runMinimally(String taskId, TimeLogger tlog) async {
hasGrantedMLConsent &&
localSettings.isMLLocalIndexingEnabled) {
PersonService.init(entityService, MLDataDB.instance, prefs);
- _logger
- .info("[BG TASK] person service initialized for memories recompute");
+ _logger.info(
+ "[BG TASK] person service initialized for memories recompute",
+ );
}
await _homeWidgetSync(true);
@@ -468,11 +469,9 @@ Future<void> _init(
}
if (Platform.isIOS) {
- PushService.instance.init().then((_) {
- FirebaseMessaging.onBackgroundMessage(
- _firebaseMessagingBackgroundHandler,
- );
- }).ignore();
+ PushService.instance
+ .init(onBackgroundPush: _handleBackgroundPush)
+ .ignore();
}
_logger.info("PushService/HomeWidget done $tlog");
unawaited(MLService.instance.init());
@@ -533,8 +532,9 @@ void logLocalSettings() {
VideoPreviewService.instance.isVideoStreamingEnabled,
};
- final formattedSettings =
- settings.entries.map((e) => '${e.key}: ${e.value}').join(', ');
+ final formattedSettings = settings.entries
+ .map((e) => '${e.key}: ${e.value}')
+ .join(', ');
_logger.info('Local settings - $formattedSettings');
}
@@ -625,7 +625,7 @@ Future<bool> _isRunningInForeground() async {
(currentTime - kFGTaskDeathTimeoutInMicroseconds);
}
-Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
+Future<void> _handleBackgroundPush(Object message) async {
final bool isRunningInFG = await _isRunningInForeground(); // hb
final bool isInForeground = AppLifecycleService.instance.isForeground;
if (isRunningInFG) {
diff --git a/mobile/apps/photos/lib/services/account/billing_service.dart b/mobile/apps/photos/lib/services/account/billing_service.dart
index 7913ac1995..6171e4c867 100644
--- a/mobile/apps/photos/lib/services/account/billing_service.dart
+++ b/mobile/apps/photos/lib/services/account/billing_service.dart
@@ -4,13 +4,13 @@ import 'package:ente_pure_utils/ente_pure_utils.dart';
import 'package:flutter/material.dart';
// import 'package:flutter/foundation.dart';
// import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';
-import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:logging/logging.dart';
import "package:photos/gateways/billing/billing_gateway.dart";
import 'package:photos/gateways/billing/models/billing_plan.dart';
import 'package:photos/gateways/billing/models/subscription.dart';
import 'package:photos/models/user_details.dart';
import "package:photos/service_locator.dart";
+import "package:photos/services/account/purchase_update_listener.dart";
import 'package:photos/ui/family/family_plan_page.dart';
import 'package:photos/utils/dialog_util.dart';
@@ -43,23 +43,10 @@ class BillingService {
// await FlutterInappPurchase.instance.initConnection;
// FlutterInappPurchase.instance.clearTransactionIOS();
// }
- InAppPurchase.instance.purchaseStream.listen((purchases) {
- if (_isOnSubscriptionPage) {
- return;
- }
- for (final purchase in purchases) {
- if (purchase.status == PurchaseStatus.purchased) {
- verifySubscription(
- purchase.productID,
- purchase.verificationData.serverVerificationData,
- ).then((response) {
- InAppPurchase.instance.completePurchase(purchase);
- });
- } else if (Platform.isIOS && purchase.pendingCompletePurchase) {
- InAppPurchase.instance.completePurchase(purchase);
- }
- }
- });
+ listenForPurchaseUpdates(
+ isOnSubscriptionPage: () => _isOnSubscriptionPage,
+ verifySubscription: verifySubscription,
+ );
}
void clearCache() {
diff --git a/mobile/apps/photos/lib/services/account/purchase_update_listener.dart b/mobile/apps/photos/lib/services/account/purchase_update_listener.dart
new file mode 100644
index 0000000000..ccdd18cd44
--- /dev/null
+++ b/mobile/apps/photos/lib/services/account/purchase_update_listener.dart
@@ -0,0 +1,27 @@
+import "dart:io";
+
+import "package:in_app_purchase/in_app_purchase.dart";
+
+void listenForPurchaseUpdates({
+ required bool Function() isOnSubscriptionPage,
+ required Future<void> Function(String productID, String verificationData)
+ verifySubscription,
+}) {
+ InAppPurchase.instance.purchaseStream.listen((purchases) {
+ if (isOnSubscriptionPage()) {
+ return;
+ }
+ for (final purchase in purchases) {
+ if (purchase.status == PurchaseStatus.purchased) {
+ verifySubscription(
+ purchase.productID,
+ purchase.verificationData.serverVerificationData,
+ ).then((response) {
+ InAppPurchase.instance.completePurchase(purchase);
+ });
+ } else if (Platform.isIOS && purchase.pendingCompletePurchase) {
+ InAppPurchase.instance.completePurchase(purchase);
+ }
+ }
+ });
+}
diff --git a/mobile/apps/photos/lib/services/push_service.dart b/mobile/apps/photos/lib/services/push_service.dart
index bb4d88fc3f..ed99c02dad 100644
--- a/mobile/apps/photos/lib/services/push_service.dart
+++ b/mobile/apps/photos/lib/services/push_service.dart
@@ -11,6 +11,8 @@ import 'package:photos/service_locator.dart';
import 'package:photos/services/sync/sync_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
+typedef BackgroundPushHandler = Future<void> Function(Object message);
+
class PushService {
static const kFCMPushToken = "fcm_push_token";
static const kLastFCMTokenUpdationTime = "fcm_push_token_updation_time";
@@ -27,14 +29,20 @@ class PushService {
PushService._privateConstructor();
- Future<void> init() async {
+ Future<void> init({BackgroundPushHandler? onBackgroundPush}) async {
_prefs = await SharedPreferences.getInstance();
await Firebase.initializeApp();
+ if (onBackgroundPush != null) {
+ FirebaseMessaging.onBackgroundMessage(
+ onBackgroundPush,
+ );
+ }
if (_foregroundMessageSubscription != null) {
await _foregroundMessageSubscription!.cancel();
}
- _foregroundMessageSubscription =
- FirebaseMessaging.onMessage.listen((RemoteMessage message) {
+ _foregroundMessageSubscription = FirebaseMessaging.onMessage.listen((
+ RemoteMessage message,
+ ) {
_logger.info("Got a message whilst in the foreground!");
_handleForegroundPushMessage(message);
});
@@ -45,8 +53,9 @@ class PushService {
if (Configuration.instance.hasConfiguredAccount()) {
await _configurePushToken();
} else {
- _signedInSubscription =
- Bus.instance.on<SignedInEvent>().listen((_) async {
+ _signedInSubscription = Bus.instance.on<SignedInEvent>().listen((
+ _,
+ ) async {
// ignore: unawaited_futures
_configurePushToken();
});
@@ -60,8 +69,8 @@ class PushService {
final String? fcmToken = await FirebaseMessaging.instance.getToken();
final shouldForceRefreshServerToken =
DateTime.now().microsecondsSinceEpoch -
- (_prefs.getInt(kLastFCMTokenUpdationTime) ?? 0) >
- kFCMTokenUpdationIntervalInMicroSeconds;
+ (_prefs.getInt(kLastFCMTokenUpdationTime) ?? 0) >
+ kFCMTokenUpdationIntervalInMicroSeconds;
if (fcmToken != null &&
(_prefs.getString(kFCMPushToken) != fcmToken ||
shouldForceRefreshServerToken)) {
@@ -105,7 +114,10 @@ class PushService {
}
}
- static bool shouldSync(RemoteMessage message) {
+ static bool shouldSync(Object message) {
+ if (message is! RemoteMessage) {
+ return false;
+ }
return message.data.containsKey(kPushAction) &&
message.data[kPushAction] == kSync;
}