refactor ios

This commit is contained in:
Salvatore Giordano
2020-04-07 12:49:31 +02:00
parent dc90e38743
commit fafafb8c44
3 changed files with 64 additions and 30 deletions
@@ -9,8 +9,8 @@
import UserNotifications import UserNotifications
import StreamChatClient import StreamChatClient
class NotificationService: UNNotificationServiceExtension { final class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)? var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent? var bestAttemptContent: UNMutableNotificationContent?
@@ -18,31 +18,34 @@ class NotificationService: UNNotificationServiceExtension {
self.contentHandler = contentHandler self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
let sharedDefaults = UserDefaults(suiteName: "group.io.stream.flutter") guard let sharedDefaults = UserDefaults(suiteName: "group.io.stream.flutter"),
let apiKey = sharedDefaults!.string(forKey: "KEY_API_KEY")! let apiKey = sharedDefaults.string(forKey: "KEY_API_KEY"),
let userId = sharedDefaults!.string(forKey: "KEY_USER_ID")! let userId = sharedDefaults.string(forKey: "KEY_USER_ID"),
let token = sharedDefaults!.string(forKey: "KEY_TOKEN")! let token = sharedDefaults.string(forKey: "KEY_TOKEN"),
let messageId = bestAttemptContent?.userInfo["message_id"] as? String else {
let messageId = bestAttemptContent?.userInfo["message_id"] as! String return
}
Client.config = .init(apiKey: apiKey, logOptions: .error) Client.config = .init(apiKey: apiKey, logOptions: .error)
Client.shared.set(user: User(id: userId), token: token) { res in Client.shared.set(user: User(id: userId), token: token) { res in
if res.isConnected { guard res.isConnected else {
Client.shared.message(withId: messageId) { res in return
if let message = res.value?.message, }
let channel = res.value?.channel {
let messageWrapper = MessageWrapper(id: message.id, channel: ChannelWrapper(id: channel.id, cid: channel.cid, type: channel.type, name: channel.name, imageURL: channel.imageURL, lastMessageDate: channel.lastMessageDate, created: channel.created, deleted: channel.deleted, createdBy: channel.createdBy, config: channel.config, frozen: channel.frozen, extraData: channel.extraData), type: message.type, user: message.user, created: message.created, updated: message.updated, text: message.text, command: message.command, args: message.args, attachments: message.attachments, parentId: message.parentId, showReplyInChannel: message.showReplyInChannel, mentionedUsers: message.mentionedUsers, extraData: message.extraData) Client.shared.message(withId: messageId) { res in
if let encodedData = try? JSONEncoder.stream.encode(messageWrapper) { if let message = res.value?.message,
let storedMessages = sharedDefaults?.stringArray(forKey: "messageQueue") ?? [] let channel = res.value?.channel {
let encodedString = String(data: encodedData, encoding: .utf8)! let messageWrapper = MessageWrapper(channel: channel, message: message)
sharedDefaults?.setValue(storedMessages + [encodedString], forKey: "messageQueue") if let encodedData = try? JSONEncoder.stream.encode(messageWrapper),
let encodedString = String(data: encodedData, encoding: .utf8) {
// Modify the notification content here... let storedMessages = sharedDefaults.stringArray(forKey: "messageQueue") ?? []
self.bestAttemptContent?.title = "[modified] \(self.bestAttemptContent?.title ?? "<NoContent>")" sharedDefaults.setValue(storedMessages + [encodedString], forKey: "messageQueue")
contentHandler(self.bestAttemptContent ?? request.content)
} // Modify the notification content here...
Client.shared.disconnect() self.bestAttemptContent?.title = "[modified] \(self.bestAttemptContent?.title ?? "<NoContent>")"
contentHandler(self.bestAttemptContent ?? request.content)
} }
Client.shared.disconnect()
} }
} }
} }
@@ -72,6 +75,23 @@ public struct MessageWrapper: Encodable {
case mentionedUsers = "mentioned_users" case mentionedUsers = "mentioned_users"
} }
init(channel: Channel, message: Message) {
id = message.id
type = message.type
user = message.user
created = message.created
updated = message.updated
text = message.text
command = message.command
args = message.args
attachments = message.attachments
parentId = message.parentId
showReplyInChannel = message.showReplyInChannel
mentionedUsers = message.mentionedUsers
extraData = message.extraData
self.channel = ChannelWrapper(channel: channel)
}
/// A message id. /// A message id.
public let id: String public let id: String
/// The channel cid. /// The channel cid.
@@ -115,8 +135,24 @@ public struct ChannelWrapper: Encodable {
case createdBy = "created_by" case createdBy = "created_by"
case created = "created_at" case created = "created_at"
case deleted = "deleted_at" case deleted = "deleted_at"
case frozen
} }
init(channel: Channel) {
id = channel.id
cid = channel.cid
type = channel.type
name = channel.name
imageURL = channel.imageURL
lastMessageDate = channel.lastMessageDate
created = channel.created
deleted = channel.deleted
createdBy = channel.createdBy
config = channel.config
frozen = channel.frozen
extraData = channel.extraData
}
/// A channel id. /// A channel id.
public let id: String public let id: String
/// A channel type + id. /// A channel type + id.
+4 -4
View File
@@ -9,8 +9,8 @@ import Flutter
_ application: UIApplication, _ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool { ) -> Bool {
if let jsonMessage = sharedDefaults?.stringArray(forKey: "messageQueue") { if let messageQueue = sharedDefaults?.stringArray(forKey: "messageQueue") {
UserDefaults.standard.setValue(jsonMessage, forKey: "flutter.messageQueue") UserDefaults.standard.setValue(messageQueue, forKey: "flutter.messageQueue")
sharedDefaults?.removeObject(forKey: "messageQueue") sharedDefaults?.removeObject(forKey: "messageQueue")
} }
@@ -33,8 +33,8 @@ import Flutter
} }
override func applicationWillEnterForeground(_ application: UIApplication) { override func applicationWillEnterForeground(_ application: UIApplication) {
if let jsonMessage = sharedDefaults?.stringArray(forKey: "messageQueue") { if let messageQueue = sharedDefaults?.stringArray(forKey: "messageQueue") {
UserDefaults.standard.setValue(jsonMessage, forKey: "flutter.messageQueue") UserDefaults.standard.setValue(messageQueue, forKey: "flutter.messageQueue")
sharedDefaults?.removeObject(forKey: "messageQueue") sharedDefaults?.removeObject(forKey: "messageQueue")
} }
} }
-2
View File
@@ -275,8 +275,6 @@ class StreamChatState extends State<StreamChat>
void dispose() { void dispose() {
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
print('disposeeeeeeeeee');
_channelsController.close(); _channelsController.close();
_queryChannelsLoadingController.close(); _queryChannelsLoadingController.close();
_newMessagesSubscription.cancel(); _newMessagesSubscription.cancel();