Skip to content

Commit

Permalink
widgetkit notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Notsfsssf committed Mar 28, 2021
1 parent da47b58 commit 7685db4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
31 changes: 23 additions & 8 deletions ios/SSSGridman/SSSGridman.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ struct Recommand: Codable {
let illusts: [Illust]
}

struct User: Codable {
let name: String
}

// MARK: - Illust
struct Illust: Codable {
let imageUrls: ImageUrls
let id: Int
let title: String
let user:User


enum CodingKeys: String, CodingKey {
case imageUrls = "image_urls"
case id
case title
case user
}
}

Expand All @@ -35,20 +44,18 @@ struct ImageUrls: Codable {

struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), i: UIImage(named: "ic_launcher-playstore"))
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), i: UIImage(named: "ic_launcher-playstore"),title: "",userName: "")
}

func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration, i: UIImage(named: "ic_launcher-playstore"))
let entry = SimpleEntry(date: Date(), configuration: configuration, i: UIImage(named: "ic_launcher-playstore"),title: "",userName: "")
completion(entry)
}

func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
print("time line start")
let userDefault = UserDefaults(suiteName: "group.pixez")
print(userDefault!.string(forKey: "widgetkit.app_widget_data"))

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")
Expand All @@ -64,7 +71,7 @@ struct Provider: IntentTimelineProvider {
URLSession.shared.dataTask(with: request){ (data, response, error) in
if let image = UIImage(data: data!){
let currentDate = Date()
let entry = SimpleEntry(date: currentDate, configuration: configuration, i: image)
let entry = SimpleEntry(date: currentDate, configuration: configuration, i: image,title:recommand.illusts[randomIndex].title,userName: recommand.illusts[randomIndex].user.name)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
Expand All @@ -82,7 +89,7 @@ struct Provider: IntentTimelineProvider {
}
}else{
let currentDate = Date()
let entry = SimpleEntry(date: currentDate, configuration: configuration, i: UIImage(named: "ic_launcher-playstore"))
let entry = SimpleEntry(date: currentDate, configuration: configuration, i: UIImage(named: "ic_launcher-playstore"),title: "",userName: "")
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
Expand All @@ -96,13 +103,21 @@ struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let i:UIImage?
let title:String
let userName:String
}

struct SSSGridmanEntryView : View {
var entry: Provider.Entry

var body: some View {
Image(uiImage: entry.i!).resizable().scaledToFill()
ZStack{
Image(uiImage: entry.i!).resizable().scaledToFill()
VStack{
Text("\(entry.title)")
Text("\(entry.userName)")
}
}
}

}
Expand All @@ -122,7 +137,7 @@ struct SSSGridman: Widget {

struct SSSGridman_Previews: PreviewProvider {
static var previews: some View {
SSSGridmanEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), i: UIImage(named: "ic_launcher-playstore")))
SSSGridmanEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), i: UIImage(named: "ic_launcher-playstore"),title: "Title",userName: "My Load"))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
3 changes: 1 addition & 2 deletions lib/lighting/lighting_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ abstract class _LightingStoreBase with Store {
if (userSetting.prefs.getString("app_widget_data") == null) {
await userSetting.prefs
.setString("app_widget_data", jsonEncode(recommend));

WidgetkitPlugin.notify();
}
WidgetkitPlugin.notify();
controller?.refreshCompleted();
return true;
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions lib/page/hello/hello_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:pixez/page/hello/recom/recom_spotlight_page.dart';
import 'package:pixez/page/hello/setting/setting_page.dart';
import 'package:pixez/page/preview/preview_page.dart';
import 'package:pixez/page/search/search_page.dart';
import 'package:pixez/widgetkit_plugin.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni_links/uni_links.dart';

Expand Down Expand Up @@ -64,6 +65,7 @@ class _HelloPageState extends State<HelloPage> {
});
initLinksStream();
initPlatformState();
WidgetkitPlugin.notify();
}

Future<void> initPlatformState() async {
Expand Down
5 changes: 5 additions & 0 deletions lib/widgetkit_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import 'dart:io';

import 'package:flutter/services.dart';

class WidgetkitPlugin {
static const platform = const MethodChannel('com.perol.dev/widgetkit');

static Future<void> notify() async {
if(!Platform.isIOS){
return;
}
return await platform.invokeMethod("notify");
}
}

0 comments on commit 7685db4

Please sign in to comment.