Localize notification service and widget

This commit is contained in:
Peter 2019-10-29 23:23:15 +04:00
parent 0fb9b83d49
commit 664c5b3759
5 changed files with 48 additions and 9 deletions

View File

@ -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;

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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)