From fafafb8c44b7fda14fb4995e854c2d9590b49f68 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 7 Apr 2020 12:49:31 +0200 Subject: [PATCH] refactor ios --- .../Notifications/NotificationService.swift | 84 +++++++++++++------ example/ios/Runner/AppDelegate.swift | 8 +- lib/src/stream_chat.dart | 2 - 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/example/ios/Notifications/NotificationService.swift b/example/ios/Notifications/NotificationService.swift index c9d32623..e7418cdb 100644 --- a/example/ios/Notifications/NotificationService.swift +++ b/example/ios/Notifications/NotificationService.swift @@ -9,8 +9,8 @@ import UserNotifications import StreamChatClient -class NotificationService: UNNotificationServiceExtension { - +final class NotificationService: UNNotificationServiceExtension { + var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? @@ -18,31 +18,34 @@ class NotificationService: UNNotificationServiceExtension { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) - let sharedDefaults = UserDefaults(suiteName: "group.io.stream.flutter") - let apiKey = sharedDefaults!.string(forKey: "KEY_API_KEY")! - let userId = sharedDefaults!.string(forKey: "KEY_USER_ID")! - let token = sharedDefaults!.string(forKey: "KEY_TOKEN")! - - let messageId = bestAttemptContent?.userInfo["message_id"] as! String + guard let sharedDefaults = UserDefaults(suiteName: "group.io.stream.flutter"), + let apiKey = sharedDefaults.string(forKey: "KEY_API_KEY"), + let userId = sharedDefaults.string(forKey: "KEY_USER_ID"), + let token = sharedDefaults.string(forKey: "KEY_TOKEN"), + let messageId = bestAttemptContent?.userInfo["message_id"] as? String else { + return + } Client.config = .init(apiKey: apiKey, logOptions: .error) Client.shared.set(user: User(id: userId), token: token) { res in - if res.isConnected { - Client.shared.message(withId: messageId) { res in - 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) - if let encodedData = try? JSONEncoder.stream.encode(messageWrapper) { - let storedMessages = sharedDefaults?.stringArray(forKey: "messageQueue") ?? [] - let encodedString = String(data: encodedData, encoding: .utf8)! - sharedDefaults?.setValue(storedMessages + [encodedString], forKey: "messageQueue") - - // Modify the notification content here... - self.bestAttemptContent?.title = "[modified] \(self.bestAttemptContent?.title ?? "")" - contentHandler(self.bestAttemptContent ?? request.content) - } - Client.shared.disconnect() + guard res.isConnected else { + return + } + + Client.shared.message(withId: messageId) { res in + if let message = res.value?.message, + let channel = res.value?.channel { + let messageWrapper = MessageWrapper(channel: channel, message: message) + if let encodedData = try? JSONEncoder.stream.encode(messageWrapper), + let encodedString = String(data: encodedData, encoding: .utf8) { + let storedMessages = sharedDefaults.stringArray(forKey: "messageQueue") ?? [] + sharedDefaults.setValue(storedMessages + [encodedString], forKey: "messageQueue") + + // Modify the notification content here... + self.bestAttemptContent?.title = "[modified] \(self.bestAttemptContent?.title ?? "")" + contentHandler(self.bestAttemptContent ?? request.content) } + Client.shared.disconnect() } } } @@ -72,6 +75,23 @@ public struct MessageWrapper: Encodable { 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. public let id: String /// The channel cid. @@ -115,8 +135,24 @@ public struct ChannelWrapper: Encodable { case createdBy = "created_by" case created = "created_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. public let id: String /// A channel type + id. diff --git a/example/ios/Runner/AppDelegate.swift b/example/ios/Runner/AppDelegate.swift index fd5f82d2..3f549155 100644 --- a/example/ios/Runner/AppDelegate.swift +++ b/example/ios/Runner/AppDelegate.swift @@ -9,8 +9,8 @@ import Flutter _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { - if let jsonMessage = sharedDefaults?.stringArray(forKey: "messageQueue") { - UserDefaults.standard.setValue(jsonMessage, forKey: "flutter.messageQueue") + if let messageQueue = sharedDefaults?.stringArray(forKey: "messageQueue") { + UserDefaults.standard.setValue(messageQueue, forKey: "flutter.messageQueue") sharedDefaults?.removeObject(forKey: "messageQueue") } @@ -33,8 +33,8 @@ import Flutter } override func applicationWillEnterForeground(_ application: UIApplication) { - if let jsonMessage = sharedDefaults?.stringArray(forKey: "messageQueue") { - UserDefaults.standard.setValue(jsonMessage, forKey: "flutter.messageQueue") + if let messageQueue = sharedDefaults?.stringArray(forKey: "messageQueue") { + UserDefaults.standard.setValue(messageQueue, forKey: "flutter.messageQueue") sharedDefaults?.removeObject(forKey: "messageQueue") } } diff --git a/lib/src/stream_chat.dart b/lib/src/stream_chat.dart index 24412d52..3d0ffa50 100644 --- a/lib/src/stream_chat.dart +++ b/lib/src/stream_chat.dart @@ -275,8 +275,6 @@ class StreamChatState extends State void dispose() { WidgetsBinding.instance.removeObserver(this); - print('disposeeeeeeeeee'); - _channelsController.close(); _queryChannelsLoadingController.close(); _newMessagesSubscription.cancel();