-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from wbreeze/i35Watch
I35 watch
- Loading branch information
Showing
45 changed files
with
1,057 additions
and
64 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
ShipsClock.xcodeproj/xcshareddata/xcschemes/ShipsClock.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// ShipsClock | ||
// Created by Douglas Lovell on 4/9/22. | ||
// Copyright © 2022 Douglas Lovell | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
|
||
import Foundation | ||
|
||
class ClockModel : ObservableObject { | ||
@Published var timeOfDayInSeconds = 0 | ||
@Published var utcTimeInSeconds = 0 | ||
|
||
private var ticker: Timer? | ||
|
||
private let tickInterval = 1.0 // seconds | ||
|
||
init() { | ||
timeOfDayInSeconds = ClockModel.nextTime() | ||
utcTimeInSeconds = ClockModel.utcTime() | ||
} | ||
|
||
func moveToForeground() { | ||
updateClock() // right away, not at next tick | ||
ticker = Timer.scheduledTimer(withTimeInterval: tickInterval, repeats: true, block: updateTimeCallback) | ||
} | ||
|
||
func moveToBackground() { | ||
ticker?.invalidate() | ||
} | ||
|
||
private func updateTimeCallback(_: Timer) { | ||
updateClock() | ||
} | ||
|
||
func updateClock() { | ||
timeOfDayInSeconds = ClockModel.nextTime() | ||
utcTimeInSeconds = ClockModel.utcTime() | ||
} | ||
|
||
fileprivate static func timeInSeconds(_ cal: Calendar) -> Int { | ||
let now = Date() | ||
let hms = cal.dateComponents([.hour, .minute, .second], from: now) | ||
return timeInSeconds(hms) | ||
} | ||
|
||
fileprivate static func timeInSeconds(_ hms: DateComponents) -> Int { | ||
if let hour = hms.hour, let minute = hms.minute, let second = hms.second { | ||
return (hour * 60 + minute) * 60 + second | ||
} else { | ||
return 0 | ||
} | ||
} | ||
|
||
static func nextTime() -> Int { | ||
return timeInSeconds(Calendar.current) | ||
} | ||
|
||
static func utcTime() -> Int { | ||
var cal = Calendar.current | ||
cal.timeZone = TimeZone.init(secondsFromGMT: 0) ?? TimeZone.current | ||
return timeInSeconds(cal) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// WatchClock WatchKit Extension | ||
// Created by Douglas Lovell on 5/9/22. | ||
// Copyright © 2022 Douglas Lovell | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
|
||
import Foundation | ||
|
||
class WatchClock : ClockModel { | ||
private var bell = ShipsBell() | ||
private var foregroundRinger: TimerRinger | ||
|
||
override init() { | ||
foregroundRinger = TimerRinger(bell: bell) | ||
super.init() | ||
} | ||
|
||
override func moveToForeground() { | ||
foregroundRinger.initializeLastPlayed(forTimeInSeconds: timeOfDayInSeconds) | ||
super.moveToForeground() | ||
} | ||
|
||
override func updateClock() { | ||
super.updateClock() | ||
foregroundRinger.maybeRing(forTimeInSeconds: timeOfDayInSeconds) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...it Extension/Assets.xcassets/Complication.complicationset/Circular.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "watch", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "watch", | ||
"scale" : "2x", | ||
"screen-width" : "<=145" | ||
}, | ||
{ | ||
"idiom" : "watch", | ||
"scale" : "2x", | ||
"screen-width" : ">183" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
}, | ||
"properties" : { | ||
"auto-scaling" : "auto" | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
WatchClock WatchKit Extension/Assets.xcassets/Complication.complicationset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"assets" : [ | ||
{ | ||
"filename" : "Circular.imageset", | ||
"idiom" : "watch", | ||
"role" : "circular" | ||
}, | ||
{ | ||
"filename" : "Extra Large.imageset", | ||
"idiom" : "watch", | ||
"role" : "extra-large" | ||
}, | ||
{ | ||
"filename" : "Graphic Bezel.imageset", | ||
"idiom" : "watch", | ||
"role" : "graphic-bezel" | ||
}, | ||
{ | ||
"filename" : "Graphic Circular.imageset", | ||
"idiom" : "watch", | ||
"role" : "graphic-circular" | ||
}, | ||
{ | ||
"filename" : "Graphic Corner.imageset", | ||
"idiom" : "watch", | ||
"role" : "graphic-corner" | ||
}, | ||
{ | ||
"filename" : "Graphic Extra Large.imageset", | ||
"idiom" : "watch", | ||
"role" : "graphic-extra-large" | ||
}, | ||
{ | ||
"filename" : "Graphic Large Rectangular.imageset", | ||
"idiom" : "watch", | ||
"role" : "graphic-large-rectangular" | ||
}, | ||
{ | ||
"filename" : "Modular.imageset", | ||
"idiom" : "watch", | ||
"role" : "modular" | ||
}, | ||
{ | ||
"filename" : "Utilitarian.imageset", | ||
"idiom" : "watch", | ||
"role" : "utilitarian" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Oops, something went wrong.