iOS桌面小组件-可支持自定义的小组件

·  阅读 948

长按小组件-编辑小组件-选择卡片类型


import WidgetKit
import SwiftUI
import Intents

struct Provider: IntentTimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), configuration: SelectCharacterIntent())
    }

    func getSnapshot(for configuration: SelectCharacterIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), configuration: configuration)
        completion(entry)
    }

    func getTimeline(for configuration: SelectCharacterIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, configuration: configuration)
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: SelectCharacterIntent
}

struct WidgetExtensionEntryView : View {
    var entry: Provider.Entry

    var selectCharList: [String] {
        entry.configuration.SelectedCardType?.map{
            $0.identifier
        } as! [String]
    }
    
    var body: some View {
        Text(entry.date, style: .time)
//        Text(entry.configuration.BigListOne?.identifier ?? "")
        Text(selectCharList.joined())
        
//        Text(entry.configuration.parameter ?? "")
    }
}

@main
struct WidgetExtension: Widget {
    let kind: String = "WidgetExtension"

    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: SelectCharacterIntent.self, provider: Provider()) { entry in
            WidgetExtensionEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

struct WidgetExtension_Previews: PreviewProvider {
    static var previews: some View {
        WidgetExtensionEntryView(entry: SimpleEntry(date: Date(), configuration: SelectCharacterIntent()))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    }
}

复制代码

WX20210513-160521@2x.png

WX20210513-160534@2x.png

WX20210513-160551@2x.png


import Intents

class IntentHandler: INExtension, SelectCharacterIntentHandling {
    
  /// 点击选择的卡片类型数组
    /// - Parameters:
    ///   - intent: intent description
    ///   - completion: completion description
    func provideSelectedCardTypeOptionsCollection(for intent: SelectCharacterIntent, with completion: @escaping (INObjectCollection<Cardtype>?, Error?) -> Swift.Void) {
        let currentList = intent.SelectedCardType?.map{
            $0.identifier
        }
        
        var cardList = [Cardtype]()
        for idx in 0...9 {
            let card = Cardtype(identifier: "\(idx)",
                            display: "display --- \(idx)",
                            subtitle: "subtitle --- \(idx)",
                            image: nil)
            if !(currentList?.contains("\(idx)") ?? false) {
                cardList.append(card)
            }
        }
        completion(INObjectCollection(items: cardList) , nil)
    }

    /// 默认已选择好的卡片类型
    /// - Parameter intent: 当前处理的组件intent
    /// - Returns: 返回已选择好的卡片类型数组
    func defaultSelectedCardType(for intent: SelectCharacterIntent) -> [Cardtype]? {
        var cardList = [Cardtype]()
        for idx in 0...9 {
            let card = Cardtype(identifier: "\(idx)",
                            display: "display --- \(idx)",
                            subtitle: "subtitle --- \(idx)",
                            image: nil)
            cardList.append(card)
        }
        return cardList
    }
    
    override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.
        
        return self
    }
    
}

复制代码

Simulator Screen Shot - iPod touch (7th generation) - 2021-05-13 at 16.06.24.png

分类:
iOS
标签:
分类:
iOS
标签:
收藏成功!
已添加到「」, 点击更改