From 664c5b3759194a3bcf13078897459f2a4757b9a7 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Tue, 29 Oct 2019 23:23:15 +0400 Subject: [PATCH] Localize notification service and widget --- NotificationService/NotificationService.h | 2 +- NotificationService/NotificationService.swift | 6 ++++- NotificationService/Sync.swift | 22 ++++++++++++++++-- Widget/PeerNode.swift | 4 ++-- Widget/TodayViewController.swift | 23 ++++++++++++++++--- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/NotificationService/NotificationService.h b/NotificationService/NotificationService.h index 2903b0bc03..f8e94caf66 100644 --- a/NotificationService/NotificationService.h +++ b/NotificationService/NotificationService.h @@ -6,7 +6,7 @@ NS_ASSUME_NONNULL_BEGIN @interface NotificationServiceImpl : NSObject -- (instancetype)initWithCountIncomingMessage:(void (^)(NSString *, int64_t, DeviceSpecificEncryptionParameters *, int64_t, int32_t))countIncomingMessage; +- (instancetype)initWithCountIncomingMessage:(void (^)(NSString *, int64_t, DeviceSpecificEncryptionParameters *, int64_t, int32_t))countIncomingMessage isLocked:(bool (^)(NSString *))isLocked lockedMessageText:(NSString *(^)(NSString *))lockedMessageText; - (void)updateUnreadCount:(int32_t)unreadCount; - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler; diff --git a/NotificationService/NotificationService.swift b/NotificationService/NotificationService.swift index ee86842c8f..410dc9a59b 100644 --- a/NotificationService/NotificationService.swift +++ b/NotificationService/NotificationService.swift @@ -9,9 +9,13 @@ final class NotificationService: UNNotificationServiceExtension { override init() { var completion: ((Int32) -> Void)? self.impl = NotificationServiceImpl(countIncomingMessage: { rootPath, accountId, encryptionParameters, peerId, messageId in - SyncProviderImpl().addIncomingMessage(withRootPath: rootPath, accountId: accountId, encryptionParameters: encryptionParameters, peerId: peerId, messageId: messageId, completion: { count in + SyncProviderImpl.addIncomingMessage(withRootPath: rootPath, accountId: accountId, encryptionParameters: encryptionParameters, peerId: peerId, messageId: messageId, completion: { count in completion?(count) }) + }, isLocked: { rootPath in + return SyncProviderImpl.isLocked(withRootPath: rootPath) + }, lockedMessageText: { rootPath in + return SyncProviderImpl.lockedMessageText(withRootPath: rootPath) }) super.init() diff --git a/NotificationService/Sync.swift b/NotificationService/Sync.swift index ed2a290777..9565046ad6 100644 --- a/NotificationService/Sync.swift +++ b/NotificationService/Sync.swift @@ -7,6 +7,8 @@ import MessageHistoryMetadataTable import PreferencesTable import PeerTable import PostboxCoding +import AppLockState +import NotificationsPresentationData private let registeredTypes: Void = { declareEncodable(InAppNotificationSettings.self, f: InAppNotificationSettings.init(decoder:)) @@ -23,8 +25,24 @@ private final class ValueBoxLoggerImpl: ValueBoxLogger { } } -final class SyncProviderImpl { - func addIncomingMessage(withRootPath rootPath: String, accountId: Int64, encryptionParameters: DeviceSpecificEncryptionParameters, peerId: Int64, messageId: Int32, completion: @escaping (Int32) -> Void) { +enum SyncProviderImpl { + static func isLocked(withRootPath rootPath: String) -> Bool { + if let data = try? Data(contentsOf: URL(fileURLWithPath: appLockStatePath(rootPath: rootPath))), let state = try? JSONDecoder().decode(LockState.self, from: data), isAppLocked(state: state) { + return true + } else { + return false + } + } + + static func lockedMessageText(withRootPath rootPath: String) -> String { + if let data = try? Data(contentsOf: URL(fileURLWithPath: notificationsPresentationDataPath(rootPath: rootPath))), let value = try? JSONDecoder().decode(NotificationsPresentationData.self, from: data) { + return value.applicationLockedMessageString + } else { + return "You have a new message" + } + } + + static func addIncomingMessage(withRootPath rootPath: String, accountId: Int64, encryptionParameters: DeviceSpecificEncryptionParameters, peerId: Int64, messageId: Int32, completion: @escaping (Int32) -> Void) { Queue.mainQueue().async { let _ = registeredTypes diff --git a/Widget/PeerNode.swift b/Widget/PeerNode.swift index 35ebf79119..0da74c80a2 100644 --- a/Widget/PeerNode.swift +++ b/Widget/PeerNode.swift @@ -117,7 +117,7 @@ final class PeerView: UIView { private let tapped: () -> Void - init(accountPeerId: Int64, peer: WidgetDataPeer, tapped: @escaping () -> Void) { + init(primaryColor: UIColor, accountPeerId: Int64, peer: WidgetDataPeer, tapped: @escaping () -> Void) { self.peer = peer self.tapped = tapped self.avatarView = AvatarView(accountPeerId: accountPeerId, peer: peer, size: avatarSize) @@ -125,7 +125,7 @@ final class PeerView: UIView { self.titleLabel = UILabel() let title = peer.name self.titleLabel.text = title - self.titleLabel.textColor = .black + self.titleLabel.textColor = primaryColor self.titleLabel.font = UIFont.systemFont(ofSize: 11.0) self.titleLabel.lineBreakMode = .byTruncatingTail diff --git a/Widget/TodayViewController.swift b/Widget/TodayViewController.swift index 15e6217188..07fb13c555 100644 --- a/Widget/TodayViewController.swift +++ b/Widget/TodayViewController.swift @@ -14,11 +14,21 @@ class TodayViewController: UIViewController, NCWidgetProviding { private var buildConfig: BuildConfig? + private var primaryColor: UIColor = .black private var appLockedLabel: UILabel? override func viewDidLoad() { super.viewDidLoad() + if #available(iOSApplicationExtension 13.0, *) { + switch self.traitCollection.userInterfaceStyle { + case .dark: + self.primaryColor = .white + default: + break + } + } + let appBundleIdentifier = Bundle.main.bundleIdentifier! guard let lastDotRange = appBundleIdentifier.range(of: ".", options: [.backwards]) else { return @@ -37,11 +47,18 @@ class TodayViewController: UIViewController, NCWidgetProviding { let rootPath = rootPathForBasePath(appGroupUrl.path) + let presentationData: WidgetPresentationData + if let data = try? Data(contentsOf: URL(fileURLWithPath: widgetPresentationDataPath(rootPath: rootPath))), let value = try? JSONDecoder().decode(WidgetPresentationData.self, from: data) { + presentationData = value + } else { + presentationData = WidgetPresentationData(applicationLockedString: "Unlock the app to use widget") + } + if let data = try? Data(contentsOf: URL(fileURLWithPath: appLockStatePath(rootPath: rootPath))), let state = try? JSONDecoder().decode(LockState.self, from: data), isAppLocked(state: state) { let appLockedLabel = UILabel() - appLockedLabel.textColor = .black + appLockedLabel.textColor = self.primaryColor appLockedLabel.font = UIFont.systemFont(ofSize: 16.0) - appLockedLabel.text = "Unlock the app to use widget" + appLockedLabel.text = presentationData.applicationLockedString appLockedLabel.sizeToFit() self.appLockedLabel = appLockedLabel self.view.addSubview(appLockedLabel) @@ -82,7 +99,7 @@ class TodayViewController: UIViewController, NCWidgetProviding { break case let .peers(peers): for peer in peers.peers { - let peerView = PeerView(accountPeerId: peers.accountPeerId, peer: peer, tapped: { [weak self] in + let peerView = PeerView(primaryColor: self.primaryColor, accountPeerId: peers.accountPeerId, peer: peer, tapped: { [weak self] in if let strongSelf = self, let buildConfig = strongSelf.buildConfig { if let url = URL(string: "\(buildConfig.appSpecificUrlScheme)://localpeer?id=\(peer.id)") { strongSelf.extensionContext?.open(url, completionHandler: nil)