gpt4 book ai didi

swift - 如何让 ClockKit 生成超过 100 个时间线条目?

转载 作者:可可西里 更新时间:2023-11-01 01:56:52 36 4
gpt4 key购买 nike

我正在尝试创建一个 ClockKit 复杂功能,为一个人的下一个轮类开始时间提供数据,但没有足够的时间线条目生成或生成得不够频繁,因此有时,数据在一定时间后不准确。

我已尝试调试并得出结论,仅创建了 100 个时间轴条目,适合每个方向 1:40 小时的日期,这对我的应用程序来说不够。我已经阅读了文档和延长时间线的方法,但发现它每天只能使用一定次数。

我在我的 getTimelineEntries(complication:date:limit:handler) 中加入了 print(String(limit) + "After") 以查看它生成了多少。 Output of print(String(limit) + " After") or before where applicable

我该怎么做才能使我的并发症的时间线从 00:00 延长到 23:59?另外,为什么时间线不会在超过最 future 的条目时自动延长?这似乎与 Apple 对并发症的意图有悖常理。

我在下面包含了我的 ComplicationController.swift。

//
// ComplicationController.swift
// Watch Bell Schedule Extension
//
// Created by Joseph on 8/23/18.
// Copyright © 2018 juniorRubyist. All rights reserved.
//

import ClockKit


class ComplicationController: NSObject, CLKComplicationDataSource {

// MARK: - Timeline Configuration

func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
handler([.forward, .backward])
}

func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date().addingTimeInterval(-256200))
}

func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date().addingTimeInterval(256200))
}

func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
handler(.showOnLockScreen)
}

// MARK: - Timeline Population

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
let date = Date()
let outputFormat = DateFormatter()
outputFormat.locale = Locale(identifier:"en_US")
outputFormat.dateFormat = "e"
let override = 0
let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
outputFormat.dateFormat = "hh:mm"

switch complication.family {
case .utilitarianLarge:
let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time)) 🏫 \(nextPeriodObj.name)"
} else {
compText = "🔕 None Today"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)

case .utilitarianSmall, .utilitarianSmallFlat:
let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time))"
} else {
compText = "🔕"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)

case .modularLarge:
let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
let headerText, body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
headerText = "Bell Schedule"
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
headerText = "No more bells."
body1Text = ""
body2Text = ""
}

complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
complicationTemplate.headerTextProvider.tintColor = TitanColors.red
complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)

case .modularSmall, .circularSmall, .extraLarge:

let body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
body1Text = "NO"
body2Text = "BELL"
}

if complication.family == .modularSmall {
let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)
} else if complication.family == .circularSmall {
let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)
} else if complication.family == .extraLarge {
let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
handler(timelineEntry)
}
}
}

func getTimelineEntries(for complication: CLKComplication, before originalDate: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
print(String(limit) + " Before")
var entries = [CLKComplicationTimelineEntry]()
for i in (1...(limit + 1)).reversed() {
var date = originalDate
date.addTimeInterval(TimeInterval(-1 * (60 * i)))
let outputFormat = DateFormatter()
outputFormat.locale = Locale(identifier:"en_US")
outputFormat.dateFormat = "e"
let override = 0
let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
outputFormat.dateFormat = "hh:mm"

switch complication.family {
case .utilitarianLarge:
let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time)) 🏫 \(nextPeriodObj.name)"
} else {
compText = "🔕 None Today"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .utilitarianSmall, .utilitarianSmallFlat:
let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time))"
} else {
compText = "🔕"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .modularLarge:
let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
let headerText, body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
headerText = "Bell Schedule"
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
headerText = "No more bells."
body1Text = ""
body2Text = ""
}

complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
complicationTemplate.headerTextProvider.tintColor = TitanColors.red
complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .modularSmall, .circularSmall, .extraLarge:

let body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
body1Text = "NO"
body2Text = "BELL"
}

if complication.family == .modularSmall {
let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
} else if complication.family == .circularSmall {
let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
} else if complication.family == .extraLarge {
let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
}
}
}
handler(entries)
}

func getTimelineEntries(for complication: CLKComplication, after originalDate: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
var entries = [CLKComplicationTimelineEntry]()
print(String(limit) + " After")
for i in 1...(limit + 1) {
var date = originalDate
date.addTimeInterval(TimeInterval(60 * i))
let outputFormat = DateFormatter()
outputFormat.locale = Locale(identifier:"en_US")
outputFormat.dateFormat = "e"
let override = 0
let currentSchedule = currentSch((outputFormat.string(from: date)), unless: override)
let nextPeriodObj = nextPeriod(on: currentSchedule, at: date)
outputFormat.dateFormat = "hh:mm"

switch complication.family {
case .utilitarianLarge:
let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time)) 🏫 \(nextPeriodObj.name)"
} else {
compText = "🔕 None Today"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .utilitarianSmall, .utilitarianSmallFlat:
let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
let compText: String
if nextPeriodObj != Period(" ", 0, 0) {
compText = "🔔 \(outputFormat.string(from: nextPeriodObj.time))"
} else {
compText = "🔕"
}
complicationTemplate.textProvider = CLKSimpleTextProvider(text: compText)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .modularLarge:
let complicationTemplate = CLKComplicationTemplateModularLargeStandardBody()
let headerText, body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
headerText = "Bell Schedule"
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
headerText = "No more bells."
body1Text = ""
body2Text = ""
}

complicationTemplate.headerTextProvider = CLKSimpleTextProvider(text: headerText)
complicationTemplate.headerTextProvider.tintColor = TitanColors.red
complicationTemplate.body1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.body2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)

case .modularSmall, .circularSmall, .extraLarge:

let body1Text, body2Text: String

if nextPeriodObj != Period(" ", 0, 0) {
body1Text = "\(nextPeriodObj.name)"
body2Text = "\(outputFormat.string(from: nextPeriodObj.time))"
} else {
body1Text = "NO"
body2Text = "BELL"
}

if complication.family == .modularSmall {
let complicationTemplate = CLKComplicationTemplateModularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
} else if complication.family == .circularSmall {
let complicationTemplate = CLKComplicationTemplateCircularSmallStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
} else if complication.family == .extraLarge {
let complicationTemplate = CLKComplicationTemplateExtraLargeStackText()
complicationTemplate.line1TextProvider = CLKSimpleTextProvider(text: body1Text)
complicationTemplate.line1TextProvider.tintColor = TitanColors.red
complicationTemplate.line2TextProvider = CLKSimpleTextProvider(text: body2Text)

let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: complicationTemplate)
entries.append(timelineEntry)
}
}
}
handler(entries)
}

// MARK: - Placeholder Templates

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// This method will be called once per supported complication, and the results will be cached
handler(nil)
}

}

最佳答案

安排一个 backgroundRefresh 例如 future 一小时做一个extendTimeline .

要安排后台刷新,请在您的 ExtensionDelegate 中的 applicationDidFinishLaunching 中运行它,不要忘记在每次刷新时重新安排它。

让 minutesToRefresh = 60
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date().addingTimeInterval(minutesToRefresh * 60), userInfo: nil, scheduledCompletion: scheduledCompletion)

关于swift - 如何让 ClockKit 生成超过 100 个时间线条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52107521/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com