Locally hide notification text when locked

This commit is contained in:
Peter 2019-10-29 23:24:39 +04:00
parent 9e5db9c4dc
commit 1e437ee1a8
4 changed files with 83 additions and 5 deletions

View File

@ -55,6 +55,8 @@ static void reportMemory() {
NSString * _Nullable _rootPath; NSString * _Nullable _rootPath;
DeviceSpecificEncryptionParameters * _Nullable _deviceSpecificEncryptionParameters; DeviceSpecificEncryptionParameters * _Nullable _deviceSpecificEncryptionParameters;
bool _isLockedValue;
NSString *_lockedMessageTextValue;
NSString * _Nullable _baseAppBundleId; NSString * _Nullable _baseAppBundleId;
void (^_contentHandler)(UNNotificationContent *); void (^_contentHandler)(UNNotificationContent *);
UNMutableNotificationContent * _Nullable _bestAttemptContent; UNMutableNotificationContent * _Nullable _bestAttemptContent;
@ -68,7 +70,7 @@ static void reportMemory() {
@implementation NotificationServiceImpl @implementation NotificationServiceImpl
- (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:(nonnull bool (^)(NSString * _Nonnull))isLocked lockedMessageText:(NSString *(^)(NSString *))lockedMessageText {
self = [super init]; self = [super init];
if (self != nil) { if (self != nil) {
#if DEBUG #if DEBUG
@ -89,6 +91,11 @@ static void reportMemory() {
_rootPath = rootPath; _rootPath = rootPath;
if (rootPath != nil) { if (rootPath != nil) {
_deviceSpecificEncryptionParameters = [BuildConfig deviceSpecificEncryptionParameters:rootPath baseAppBundleId:_baseAppBundleId]; _deviceSpecificEncryptionParameters = [BuildConfig deviceSpecificEncryptionParameters:rootPath baseAppBundleId:_baseAppBundleId];
_isLockedValue = isLocked(rootPath);
if (_isLockedValue) {
_lockedMessageTextValue = lockedMessageText(rootPath);
}
} }
} else { } else {
NSAssert(false, @"appGroupUrl == nil"); NSAssert(false, @"appGroupUrl == nil");
@ -309,13 +316,30 @@ static void reportMemory() {
title = [title stringByAppendingString:@" 🔕"]; title = [title stringByAppendingString:@" 🔕"];
} }
_bestAttemptContent.title = title; _bestAttemptContent.title = title;
if (_isLockedValue) {
_bestAttemptContent.subtitle = @"";
if (_lockedMessageTextValue != nil) {
_bestAttemptContent.body = _lockedMessageTextValue;
} else {
_bestAttemptContent.body = @"You have a new message";
}
} else {
_bestAttemptContent.subtitle = subtitle; _bestAttemptContent.subtitle = subtitle;
_bestAttemptContent.body = body; _bestAttemptContent.body = body;
}
} else if ([alert isKindOfClass:[NSString class]]) { } else if ([alert isKindOfClass:[NSString class]]) {
_bestAttemptContent.title = @""; _bestAttemptContent.title = @"";
_bestAttemptContent.subtitle = @""; _bestAttemptContent.subtitle = @"";
if (_isLockedValue) {
if (_lockedMessageTextValue != nil) {
_bestAttemptContent.body = _lockedMessageTextValue;
} else {
_bestAttemptContent.body = @"You have a new message";
}
} else {
_bestAttemptContent.body = alert; _bestAttemptContent.body = alert;
} }
}
NSString *threadIdString = aps[@"thread-id"]; NSString *threadIdString = aps[@"thread-id"];
if ([threadIdString isKindOfClass:[NSString class]]) { if ([threadIdString isKindOfClass:[NSString class]]) {

View File

@ -0,0 +1,11 @@
load("//Config:buck_rule_macros.bzl", "static_library")
static_library(
name = "NotificationsPresentationData",
srcs = glob([
"Sources/**/*.swift",
]),
frameworks = [
"$SDKROOT/System/Library/Frameworks/Foundation.framework",
],
)

View File

@ -0,0 +1,13 @@
import Foundation
public struct NotificationsPresentationData: Codable, Equatable {
public var applicationLockedMessageString: String
public init(applicationLockedMessageString: String) {
self.applicationLockedMessageString = applicationLockedMessageString
}
}
public func notificationsPresentationDataPath(rootPath: String) -> String {
return rootPath + "/notificationsPresentationData.json"
}

View File

@ -4,12 +4,16 @@ import Postbox
import TelegramCore import TelegramCore
import SyncCore import SyncCore
import WidgetItems import WidgetItems
import TelegramPresentationData
import NotificationsPresentationData
final class WidgetDataContext { final class WidgetDataContext {
private var currentAccount: Account? private var currentAccount: Account?
private var currentAccountDisposable: Disposable? private var currentAccountDisposable: Disposable?
private var widgetPresentationDataDisposable: Disposable?
private var notificationPresentationDataDisposable: Disposable?
init(basePath: String, activeAccount: Signal<Account?, NoError>) { init(basePath: String, activeAccount: Signal<Account?, NoError>, presentationData: Signal<PresentationData, NoError>) {
self.currentAccountDisposable = (activeAccount self.currentAccountDisposable = (activeAccount
|> distinctUntilChanged(isEqual: { lhs, rhs in |> distinctUntilChanged(isEqual: { lhs, rhs in
return lhs === rhs return lhs === rhs
@ -42,6 +46,32 @@ final class WidgetDataContext {
let _ = try? FileManager.default.removeItem(atPath: path) let _ = try? FileManager.default.removeItem(atPath: path)
} }
}) })
self.widgetPresentationDataDisposable = (presentationData
|> map { presentationData -> WidgetPresentationData in
return WidgetPresentationData(applicationLockedString: presentationData.strings.Widget_ApplicationLocked)
}
|> distinctUntilChanged).start(next: { value in
let path = widgetPresentationDataPath(rootPath: basePath)
if let data = try? JSONEncoder().encode(value) {
let _ = try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
} else {
let _ = try? FileManager.default.removeItem(atPath: path)
}
})
self.notificationPresentationDataDisposable = (presentationData
|> map { presentationData -> NotificationsPresentationData in
return NotificationsPresentationData(applicationLockedMessageString: presentationData.strings.PUSH_LOCKED_MESSAGE("").0)
}
|> distinctUntilChanged).start(next: { value in
let path = notificationsPresentationDataPath(rootPath: basePath)
if let data = try? JSONEncoder().encode(value) {
let _ = try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
} else {
let _ = try? FileManager.default.removeItem(atPath: path)
}
})
} }
deinit { deinit {