Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Share notification preferences with iOS extension (#14173)

aman-pilotf16fa099bfcd
diff --git a/mobile/apps/photos/ios/NotificationServiceExtension/NotificationKeyStore.swift b/mobile/apps/photos/ios/NotificationServiceExtension/NotificationKeyStore.swift
index 40a3ca63ce..af9f5722c6 100644
--- a/mobile/apps/photos/ios/NotificationServiceExtension/NotificationKeyStore.swift
+++ b/mobile/apps/photos/ios/NotificationServiceExtension/NotificationKeyStore.swift
@@ -14,9 +14,9 @@ enum NotificationKeyStore {
static let accessGroup = "group.io.ente.frame.NotificationServiceExtension"
static let service = "io.ente.frame.notifications"
#if DEBUG
- static let account = "recipient.debug"
+ static let account = "recipient.debug"
#else
- static let account = "recipient"
+ static let account = "recipient"
#endif
enum StoreError: Error {
@@ -29,12 +29,32 @@ enum NotificationKeyStore {
private static var resetPendingKey: String { "\(account).resetPending" }
private static var preparedKey: String { "flutter.\(account).notificationKeyPrepared" }
+ private static let notificationPreferenceKeys = [
+ "notifications_enabled_shared_photos",
+ "notifications_enabled_social",
+ ]
+
+ static func sharePreferences() {
+ for key in notificationPreferenceKeys {
+ let value = UserDefaults.standard.object(forKey: "flutter.\(key)") as? Bool ?? true
+ preferences.set(value, forKey: "\(account).\(key)")
+ }
+ }
+
+ static func clearPreferences() {
+ for key in notificationPreferenceKeys {
+ preferences.removeObject(forKey: "\(account).\(key)")
+ }
+ }
+
private static var query: [String: Any] {
- [kSecClass as String: kSecClassGenericPassword,
+ [
+ kSecClass as String: kSecClassGenericPassword,
kSecAttrAccessGroup as String: accessGroup,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
- kSecAttrSynchronizable as String: false]
+ kSecAttrSynchronizable as String: false,
+ ]
}
static func read() throws -> NotificationRecipient? {
@@ -54,7 +74,8 @@ enum NotificationKeyStore {
}
static func readForPreparation() throws -> NotificationRecipient? {
- if preferences.bool(forKey: resetPendingKey) || !UserDefaults.standard.bool(forKey: preparedKey) {
+ if preferences.bool(forKey: resetPendingKey) || !UserDefaults.standard.bool(forKey: preparedKey)
+ {
try clear()
}
do {
@@ -71,7 +92,8 @@ enum NotificationKeyStore {
guard record.isValid else { throw StoreError.invalidRecord }
_ = try readForPreparation()
let data = try JSONEncoder().encode(record)
- var status = SecItemUpdate(query as CFDictionary, [kSecValueData as String: data] as CFDictionary)
+ var status = SecItemUpdate(
+ query as CFDictionary, [kSecValueData as String: data] as CFDictionary)
if status == errSecItemNotFound {
var attributes = query
attributes[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
diff --git a/mobile/apps/photos/ios/Runner/NotificationKeyPlugin.swift b/mobile/apps/photos/ios/Runner/NotificationKeyPlugin.swift
index b71a75241f..e8b48bd64a 100644
--- a/mobile/apps/photos/ios/Runner/NotificationKeyPlugin.swift
+++ b/mobile/apps/photos/ios/Runner/NotificationKeyPlugin.swift
@@ -11,7 +11,8 @@ final class NotificationKeyPlugin: NSObject, FlutterPlugin {
}
static func register(with registrar: FlutterPluginRegistrar) {
- let channel = FlutterMethodChannel(name: NotificationKeyStore.service, binaryMessenger: registrar.messenger())
+ let channel = FlutterMethodChannel(
+ name: NotificationKeyStore.service, binaryMessenger: registrar.messenger())
registrar.addMethodCallDelegate(NotificationKeyPlugin(), channel: channel)
}
@@ -21,7 +22,8 @@ final class NotificationKeyPlugin: NSObject, FlutterPlugin {
let arguments = call.arguments as? [String: Any] ?? [:]
if call.method == "getPrivateKey" || call.method == "setPrivateKey" {
guard let token = arguments["sessionToken"] as? String,
- UserDefaults.standard.string(forKey: "flutter.token") == token else {
+ UserDefaults.standard.string(forKey: "flutter.token") == token
+ else {
throw NotificationKeyStore.StoreError.invalidRecord
}
}
@@ -37,8 +39,12 @@ final class NotificationKeyPlugin: NSObject, FlutterPlugin {
try NotificationKeyStore.write(privateKey: privateKey)
response = nil
case "clear":
+ NotificationKeyStore.clearPreferences()
try NotificationKeyStore.clear()
response = nil
+ case "sharePreferences":
+ NotificationKeyStore.sharePreferences()
+ response = nil
default:
DispatchQueue.main.async { result(FlutterMethodNotImplemented) }
return
diff --git a/mobile/apps/photos/lib/services/notification_service.dart b/mobile/apps/photos/lib/services/notification_service.dart
index f8c30417ad..3799e171cf 100644
--- a/mobile/apps/photos/lib/services/notification_service.dart
+++ b/mobile/apps/photos/lib/services/notification_service.dart
@@ -2,6 +2,7 @@ import "dart:async";
import 'dart:io';
import "package:android_intent_plus/android_intent.dart";
+import "package:flutter/services.dart";
import "package:flutter/widgets.dart";
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import "package:flutter_timezone/flutter_timezone.dart";
@@ -40,6 +41,7 @@ class NotificationService {
void init(SharedPreferences preferences) {
_preferences = preferences;
unawaited(preferences.remove("notification_permission_granted"));
+ unawaited(_shareNotificationPreferencesWithNSE());
}
Future<void> handlePushNotification(
@@ -277,11 +279,14 @@ class NotificationService {
return result ?? true;
}
- Future<void> setShouldShowNotificationsForSharedPhotosAndAlbums(bool value) {
- return _preferences.setBool(
+ Future<void> setShouldShowNotificationsForSharedPhotosAndAlbums(
+ bool value,
+ ) async {
+ await _preferences.setBool(
keyShouldShowNotificationsForSharedPhotos,
value,
);
+ await _shareNotificationPreferencesWithNSE();
}
bool shouldShowSocialNotifications() {
@@ -289,8 +294,20 @@ class NotificationService {
return result ?? true;
}
- Future<void> setShouldShowSocialNotifications(bool value) {
- return _preferences.setBool(keyShouldShowSocialNotifications, value);
+ Future<void> setShouldShowSocialNotifications(bool value) async {
+ await _preferences.setBool(keyShouldShowSocialNotifications, value);
+ await _shareNotificationPreferencesWithNSE();
+ }
+
+ Future<void> _shareNotificationPreferencesWithNSE() async {
+ if (!Platform.isIOS) return;
+ try {
+ await const MethodChannel(
+ 'io.ente.frame.notifications',
+ ).invokeMethod<void>('sharePreferences');
+ } on PlatformException catch (e, s) {
+ _logger.warning('Could not share notification preferences', e, s);
+ }
}
Future<void> showNotification(