如何让我ClockKit产生不仅仅是100时间表条目的更多?

问题描述 投票:2回答:1

我想创建一个ClockKit复杂,一个人的下一个轮班开始时提供的数据,但正在产生或产生往往不够不够时间表条目,所以有时候,数据是有一定的时间后,不准确的。

我试图调试,并得出结论认为正在创建仅100时间表条目,好为1:40小时日期在每个方向上,而不是sufficent我的应用程序。我已阅读文档和扩展的时间表的方法,但发现它只能使用一定数量的每日倍。

我在print(String(limit) + " After")包括getTimelineEntries(complication:date:limit:handler)找到它是怎么产生许多。 Output of print(String(limit) + " After") or before where applicable

我能做些什么使我的并发症从00:00延长到23:59为它的时间表?另外,为什么不时间轴自动在其超越未来最入门的延伸?这似乎违反直觉苹果的意图的并发症。

我下面包括我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)
    }

}
swift watchkit apple-watch-complication clockkit
1个回答
1
投票

安排backgroundRefresh例如1小时进行未来做一个extendTimeline

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

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

© www.soinside.com 2019 - 2024. All rights reserved.