From da47b58404d456567d1e7be3386424d336ae49b0 Mon Sep 17 00:00:00 2001 From: perol Date: Sun, 28 Mar 2021 17:21:58 +0800 Subject: [PATCH] support load data by app group --- ios/Runner.xcodeproj/project.pbxproj | 5 +++++ ios/Runner/Runner.entitlements | 4 +++- ios/Runner/WidgetkitPlugin.swift | 12 +++++++----- ios/SSSGridman/SSSGridman.swift | 16 ++++++++-------- ios/SSSGridmanExtension.entitlements | 10 ++++++++++ lib/lighting/lighting_store.dart | 9 +++++++-- lib/widgetkit_plugin.dart | 2 +- 7 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 ios/SSSGridmanExtension.entitlements diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 23f924eff..15ebfd1a4 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -78,6 +78,7 @@ 7C8B5972260F680600098D5C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 7CC54C3726103351009033DF /* Runner.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Runner.entitlements; sourceTree = ""; }; 7CD2072C2610741D00677759 /* WidgetkitPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetkitPlugin.swift; sourceTree = ""; }; + 7CD2074626107D5600677759 /* SSSGridmanExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SSSGridmanExtension.entitlements; sourceTree = ""; }; 9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = ""; }; 9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = ""; }; 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -157,6 +158,7 @@ 97C146E51CF9000F007C117D = { isa = PBXGroup; children = ( + 7CD2074626107D5600677759 /* SSSGridmanExtension.entitlements */, 9740EEB11CF90186004384FC /* Flutter */, 97C146F01CF9000F007C117D /* Runner */, 7C8B596C260F680500098D5C /* SSSGridman */, @@ -515,6 +517,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_ENTITLEMENTS = SSSGridmanExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = L9F8ZWCR8Z; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -548,6 +551,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_ENTITLEMENTS = SSSGridmanExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = L9F8ZWCR8Z; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -578,6 +582,7 @@ CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_ENTITLEMENTS = SSSGridmanExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = L9F8ZWCR8Z; GCC_C_LANGUAGE_STANDARD = gnu11; diff --git a/ios/Runner/Runner.entitlements b/ios/Runner/Runner.entitlements index 2eb7e333a..c4fa7d47f 100644 --- a/ios/Runner/Runner.entitlements +++ b/ios/Runner/Runner.entitlements @@ -3,6 +3,8 @@ com.apple.security.application-groups - + + group.pixez + diff --git a/ios/Runner/WidgetkitPlugin.swift b/ios/Runner/WidgetkitPlugin.swift index afec3278a..0c7dc321c 100644 --- a/ios/Runner/WidgetkitPlugin.swift +++ b/ios/Runner/WidgetkitPlugin.swift @@ -13,7 +13,7 @@ public class WidgetkitPlugin { binaryMessenger: controller.binaryMessenger) batteryChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in - if call.method == "getBatteryLevel" { + if call.method == "notify" { notify() result(nil) return @@ -23,10 +23,12 @@ public class WidgetkitPlugin { } private static func notify() { - if let data = UserDefaults(suiteName: "com.perol.pixez")!.string(forKey: "flutter.app_widget_data") { - let host = UserDefaults(suiteName: "com.perol.pixez")!.string(forKey: "flutter.picture_source") ?? "i.pximg.net" - let time = UserDefaults(suiteName: "com.perol.pixez")!.integer(forKey: "flutter.app_widget_time") - let userDefault = UserDefaults(suiteName: "com.perol.pixez") + if let data = UserDefaults.standard.string(forKey: "flutter.app_widget_data"){ + let host = UserDefaults.standard.string(forKey: "flutter.picture_source") ?? "i.pximg.net" + let time = UserDefaults.standard.integer(forKey: "flutter.app_widget_time") + print(host) + print(time) + let userDefault = UserDefaults(suiteName: "group.pixez") userDefault?.setValue(host, forKey: "widgetkit.picture_source") userDefault?.setValue(data, forKey: "widgetkit.app_widget_data") userDefault?.setValue(time, forKey: "widgetkit.app_widget_time") diff --git a/ios/SSSGridman/SSSGridman.swift b/ios/SSSGridman/SSSGridman.swift index 095d8a0b4..07ba98029 100644 --- a/ios/SSSGridman/SSSGridman.swift +++ b/ios/SSSGridman/SSSGridman.swift @@ -46,12 +46,12 @@ struct Provider: IntentTimelineProvider { func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline) -> ()) { var entries: [SimpleEntry] = [] print("time line start") - UserDefaults.standard.string(forKey: <#T##String#>) - print(UserDefaults(suiteName: "com.perol.pixez")!.string(forKey: "flutter.app_widget_data")) + let userDefault = UserDefaults(suiteName: "group.pixez") + print(userDefault!.string(forKey: "widgetkit.app_widget_data")) - if let data = UserDefaults(suiteName: "com.perol.pixez")!.string(forKey: "flutter.app_widget_data") { - let host = UserDefaults(suiteName: "com.perol.pixez")!.string(forKey: "flutter.picture_source") ?? "i.pximg.net" - let time = UserDefaults(suiteName: "com.perol.pixez")!.integer(forKey: "flutter.app_widget_time") + if let data = userDefault!.string(forKey: "widgetkit.app_widget_data") { + let host = userDefault!.string(forKey: "widgetkit.picture_source") ?? "i.pximg.net" + let time = userDefault!.integer(forKey: "widgetkit.app_widget_time") print(host) print(time) let decoder = JSONDecoder() @@ -75,10 +75,10 @@ struct Provider: IntentTimelineProvider { }.resume() if(time<10){ let count = time+1 - UserDefaults(suiteName: "com.perol.pixez")!.setValue(count, forKey: "flutter.app_widget_time") + userDefault!.setValue(count, forKey: "widgetkit.app_widget_time") }else{ - UserDefaults(suiteName: "com.perol.pixez")!.removeObject(forKey: "flutter.app_widget_time") - UserDefaults(suiteName: "com.perol.pixez")!.removeObject(forKey: "flutter.app_widget_data") + userDefault!.removeObject(forKey: "widgetkit.app_widget_time") + userDefault!.removeObject(forKey: "widgetkit.app_widget_data") } }else{ let currentDate = Date() diff --git a/ios/SSSGridmanExtension.entitlements b/ios/SSSGridmanExtension.entitlements new file mode 100644 index 000000000..c4fa7d47f --- /dev/null +++ b/ios/SSSGridmanExtension.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.application-groups + + group.pixez + + + diff --git a/lib/lighting/lighting_store.dart b/lib/lighting/lighting_store.dart index cf8e85a29..8f6e6b80c 100644 --- a/lib/lighting/lighting_store.dart +++ b/lib/lighting/lighting_store.dart @@ -23,6 +23,7 @@ import 'package:pixez/models/illust.dart'; import 'package:pixez/models/recommend.dart'; import 'package:pixez/network/api_client.dart'; import 'package:pixez/page/picture/illust_store.dart'; +import 'package:pixez/widgetkit_plugin.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; part 'lighting_store.g.dart'; @@ -88,8 +89,12 @@ abstract class _LightingStoreBase with Store { nextUrl = recommend.nextUrl; iStores.clear(); iStores.addAll(recommend.illusts.map((e) => IllustStore(e.id, e))); - if (userSetting.prefs.getString("app_widget_data") == null) - userSetting.prefs.setString("app_widget_data", jsonEncode(recommend)); + if (userSetting.prefs.getString("app_widget_data") == null) { + await userSetting.prefs + .setString("app_widget_data", jsonEncode(recommend)); + + } + WidgetkitPlugin.notify(); controller?.refreshCompleted(); return true; } catch (e) { diff --git a/lib/widgetkit_plugin.dart b/lib/widgetkit_plugin.dart index 1d25fb827..226f103bb 100644 --- a/lib/widgetkit_plugin.dart +++ b/lib/widgetkit_plugin.dart @@ -1,6 +1,6 @@ import 'package:flutter/services.dart'; -class WeissPlugin { +class WidgetkitPlugin { static const platform = const MethodChannel('com.perol.dev/widgetkit'); static Future notify() async {