From 7182336a0d6db18605e8afdc5ac9f4575ef3826b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 21 Dec 2024 22:19:27 +0800 Subject: [PATCH] :sparkles: iOS quick reply (finished) --- ios/Podfile | 6 +---- ios/Podfile.lock | 6 ++++- ios/Runner/NotifyDelegate.swift | 40 ++++++++++++++++++++++----------- lib/main.dart | 17 -------------- lib/providers/channel.dart | 16 ------------- lib/providers/widget.dart | 3 --- pubspec.yaml | 2 +- 7 files changed, 34 insertions(+), 56 deletions(-) diff --git a/ios/Podfile b/ios/Podfile index 8f9ec71..a0e1296 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -38,13 +38,10 @@ target 'Runner' do target 'SolarNotifyService' do inherit! :search_paths - use_frameworks! - use_modular_headers! - pod 'home_widget', :path => '.symlinks/plugins/home_widget/ios' - pod 'shared_preferences_foundation', :path => '.symlinks/plugins/shared_preferences_foundation/darwin' pod 'Kingfisher', '~> 8.0' + pod 'Alamofire' end target 'SolarWidgetExtension' do @@ -53,7 +50,6 @@ target 'Runner' do use_modular_headers! pod 'home_widget', :path => '.symlinks/plugins/home_widget/ios' - pod 'shared_preferences_foundation', :path => '.symlinks/plugins/shared_preferences_foundation/darwin' pod 'Kingfisher', '~> 8.0' end diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 052aaf2..93a7b58 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -1,4 +1,5 @@ PODS: + - Alamofire (5.10.2) - connectivity_plus (0.0.1): - Flutter - FlutterMacOS @@ -221,6 +222,7 @@ PODS: - Flutter DEPENDENCIES: + - Alamofire - connectivity_plus (from `.symlinks/plugins/connectivity_plus/darwin`) - croppy (from `.symlinks/plugins/croppy/ios`) - device_info_plus (from `.symlinks/plugins/device_info_plus/ios`) @@ -257,6 +259,7 @@ DEPENDENCIES: SPEC REPOS: trunk: + - Alamofire - DKImagePickerController - DKPhotoGallery - Firebase @@ -343,6 +346,7 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/workmanager/ios" SPEC CHECKSUMS: + Alamofire: 7193b3b92c74a07f85569e1a6c4f4237291e7496 connectivity_plus: 18382e7311ba19efcaee94442b23b32507b20695 croppy: b6199bc8d56bd2e03cc11609d1c47ad9875c1321 device_info_plus: bf2e3232933866d73fe290f2942f2156cdd10342 @@ -394,6 +398,6 @@ SPEC CHECKSUMS: WebRTC-SDK: 79942c006ea64f6fb48d7da8a4786dfc820bc1db workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6 -PODFILE CHECKSUM: c287205af8b370f21f61cbff93ce745f0157a3c8 +PODFILE CHECKSUM: 9b244e02f87527430136c8d21cbdcf1cd586b6bc COCOAPODS: 1.16.2 diff --git a/ios/Runner/NotifyDelegate.swift b/ios/Runner/NotifyDelegate.swift index a61f3b2..f6288df 100644 --- a/ios/Runner/NotifyDelegate.swift +++ b/ios/Runner/NotifyDelegate.swift @@ -7,6 +7,7 @@ import Foundation import home_widget +import Alamofire class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { @@ -19,21 +20,34 @@ class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate { let channelId = metadata["channel_id"] as? Int let eventId = metadata["event_id"] as? Int + let replyToken = metadata["reply_token"] as? String - if #available(iOS 17, *) { - Task { - await HomeWidgetBackgroundWorker.run( - url: URL(string: "solink:///chat/reply")?.appending(queryItems: [ - URLQueryItem(name: "channel_id", value: String(channelId ?? 0)), - URLQueryItem(name: "event_id", value: String(eventId ?? 0)), - URLQueryItem(name: "text", value: textResponse.userText) - ]), - appGroup: "group.solsynth.solian" - ) - } - } else { - // Fallback on earlier versions + if (channelId == nil || eventId == nil || replyToken == nil) { + return; } + + let serverUrl = "https://api.sn.solsynth.dev" + let url = "\(serverUrl)/cgi/im/quick/\(channelId!)/reply/\(eventId!)?replyToken=\(replyToken!)" + + let parameters: [String: Any] = [ + "type": "messages.new", + "body": [ + "text": textResponse.userText, + "algorithm": "plain" + ] + ] + + AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default) + .validate() + .responseString { response in + switch response.result { + case .success(_): + break + case .failure(let error): + print("Failed to send chat reply message: \(error)") + break + } + } } completionHandler() diff --git a/lib/main.dart b/lib/main.dart index 9a2a3d3..29ea782 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -57,23 +57,6 @@ void appBackgroundDispatcher() { }); } -@pragma("vm:entry-point") -FutureOr appInteractiveBackgroundDispatcher(Uri? data) async { - print('Interactive background dispatcher called with $data'); - switch (data?.path) { - case "/chat/reply": - final channelId = data?.queryParameters['channel_id']; - final eventId = data?.queryParameters['event_id']; - final message = data?.queryParameters['text']; - if (channelId != null && eventId != null && (message?.isNotEmpty ?? false)) { - await chatReplyMessage(channelId, eventId, message!); - } - break; - default: - break; - } -} - void main() async { WidgetsFlutterBinding.ensureInitialized(); await EasyLocalization.ensureInitialized(); diff --git a/lib/providers/channel.dart b/lib/providers/channel.dart index 3f9f3ec..e934c72 100644 --- a/lib/providers/channel.dart +++ b/lib/providers/channel.dart @@ -140,19 +140,3 @@ class ChatChannelProvider extends ChangeNotifier { super.dispose(); } } - -Future chatReplyMessage(channelId, eventId, String message) async { - print('Chat reply message called with $channelId $eventId $message'); - try { - final snc = await SnNetworkProvider.createOffContextClient(); - await snc.post('/cgi/im/quick/$channelId/reply/$eventId', data: { - 'type': 'messages.new', - 'body': { - 'text': message, - 'algorithm': 'plain', - }, - }); - } catch (err) { - print('Failed to send chat reply message: $err'); - } -} diff --git a/lib/providers/widget.dart b/lib/providers/widget.dart index 430ef5b..1af4452 100644 --- a/lib/providers/widget.dart +++ b/lib/providers/widget.dart @@ -5,7 +5,6 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:home_widget/home_widget.dart'; -import 'package:surface/main.dart'; import 'package:surface/providers/sn_network.dart'; import 'package:surface/types/post.dart'; @@ -17,8 +16,6 @@ class HomeWidgetProvider { if (!kIsWeb && Platform.isIOS) { await HomeWidget.setAppGroupId("group.solsynth.solian"); } - - await HomeWidget.registerInteractivityCallback(appInteractiveBackgroundDispatcher); } Future saveWidgetData(String id, dynamic data, {bool update = true}) async { diff --git a/pubspec.yaml b/pubspec.yaml index 3f07b1a..1fde005 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 2.1.1+35 +version: 2.1.1+36 environment: sdk: ^3.5.4