Jkr*_*ist 3 ios swift widgetkit swiftui
我有一个代码:
struct ContentView: View {
let entry: LessonWidgetEntry
private static let url: URL = URL(string: "widgetUrl")!
var body: some View {
VStack {
switch entry.state {
case .none:
ProgramNotStartedView()
case .currentLesson(let lesson):
LessonView(lesson: lesson, imageName: entry.program?.imageName)
case .lessonCompleted(let lesson):
LessonCompletedView(lesson: lesson)
case .programCompleted:
ProgramCompletedView()
}
}.widgetURL(ContentView.url)
}
}
Run Code Online (Sandbox Code Playgroud)
在午夜 LessonCompletedView 应该更改为 LessonView,但我不知道如何做到这一点。关于如何从小部件更改午夜视图的任何想法?
entry.state...但在这个例子中我使用了一个简化版本):struct SimpleEntry: TimelineEntry {
let date: Date
let lesson: Lesson
}
Run Code Online (Sandbox Code Playgroud)
struct SimpleProvider: TimelineProvider {
...
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let currentDate = Date()
let midnight = Calendar.current.startOfDay(for: currentDate)
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
let entries = [
SimpleEntry(date: currentDate, lesson: Lesson()) // pass the lesson here
]
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}
Run Code Online (Sandbox Code Playgroud)
在 TimelineProvider 中,您可以通过任何您想要的课程(取决于当天或上一课 - 这取决于您)。您还可以将一个变量传递给一个条目,指示课程是否已完成。
通过设置.after(nextMidnight)策略,您可以指示何时重新加载时间轴(以及窗口小部件视图)。
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |