Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I35 watch #38

Merged
merged 18 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 229 additions & 9 deletions ShipsClock.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
LastUpgradeVersion = "1420"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
6 changes: 3 additions & 3 deletions ShipsClock/ClockFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SwiftUI

struct ClockFace: View {
@EnvironmentObject var shipsClock: ShipsClock
@ObservedObject var shipsClock: ShipsClock

var body: some View {
GeometryReader { geometry in
Expand All @@ -20,14 +20,14 @@ struct ClockFace: View {
ClockUTC(radius: currentRadius).environmentObject(self.shipsClock)
ClockSun(radius: currentRadius).environmentObject(self.shipsClock.celestialComputer)
ClockMoon(radius: currentRadius).environmentObject(self.shipsClock.celestialComputer)
ClockHands(radius: currentRadius).environmentObject(self.shipsClock)
ClockHands(clockModel: self.shipsClock, radius: currentRadius)
}.frame(width: currentDiameter, height: currentDiameter, alignment: .top)
}
}
}

struct ClockFace_Previews: PreviewProvider {
static var previews: some View {
ClockFace().environmentObject(ShipsClock())
ClockFace(shipsClock: ShipsClock())
}
}
12 changes: 6 additions & 6 deletions ShipsClock/ClockHands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import SwiftUI

struct ClockHands: View {
@EnvironmentObject var shipsClock: ShipsClock
@ObservedObject var clockModel: ClockModel
var radius : Double

var body: some View {
ZStack {
MinuteHand(
radius: self.radius, timeInSeconds: self.shipsClock.timeOfDayInSeconds)
radius: self.radius, timeInSeconds: clockModel.timeOfDayInSeconds)
HourHand(
radius: self.radius, timeInSeconds: self.shipsClock.timeOfDayInSeconds)
radius: self.radius, timeInSeconds: clockModel.timeOfDayInSeconds)
WatchHand(
radius: self.radius, timeInSeconds: self.shipsClock.timeOfDayInSeconds)
radius: self.radius, timeInSeconds: clockModel.timeOfDayInSeconds)
CenterCircle(radius: self.radius)
}
}
Expand Down Expand Up @@ -118,13 +118,13 @@ struct ClockHands: View {
return Circle()
.size(width: centerSize, height: centerSize)
.offset(x: offset, y: offset)
.fill(Color(UIColor.systemBackground))
.fill(Color(UIColor.clear))
}
}
}

struct ClockHands_Previews: PreviewProvider {
static var previews: some View {
ClockHands(radius: 200.0).environmentObject(ShipsClock())
ClockHands(clockModel: ClockModel(), radius: 200.0)
}
}
76 changes: 76 additions & 0 deletions ShipsClock/ClockModel.swift
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)
}
}
4 changes: 2 additions & 2 deletions ShipsClock/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ContentView: View {

var body: some View {
VStack {
ClockFace()
ClockFace(shipsClock: shipsClock)
SituationDisplay(displayWidth: displaySize)
.environmentObject(shipsClock.locationTracker)
}
Expand All @@ -30,7 +30,7 @@ struct ContentView: View {

var body: some View {
HStack(alignment: .top, spacing: nil) {
ClockFace()
ClockFace(shipsClock: shipsClock)
SituationDisplay(displayWidth: displaySize)
.environmentObject(shipsClock.locationTracker)
}
Expand Down
53 changes: 10 additions & 43 deletions ShipsClock/ShipsClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import Foundation
/*:
Application model for the Ships Clock
*/
class ShipsClock : ObservableObject {
@Published var timeOfDayInSeconds = 0
@Published var utcTimeInSeconds = 0
class ShipsClock : ClockModel {
@Published var locationTracker: LocationTracker
@Published var celestialComputer: CelestialComputer

Expand All @@ -24,14 +22,13 @@ class ShipsClock : ObservableObject {

private let tickInterval = 1.0 // seconds

init() {
timeOfDayInSeconds = ShipsClock.nextTime()
utcTimeInSeconds = ShipsClock.utcTime()
override init() {
foregroundRinger = TimerRinger(bell: bell)
backgroundRinger = NotifierRinger(bell: bell)
let lt = LocationTracker()
locationTracker = lt
celestialComputer = CelestialComputer(locationTracker: lt)
super.init()
}

func prepareForStart() {
Expand All @@ -45,52 +42,22 @@ class ShipsClock : ObservableObject {
backgroundRinger.disableNotifications()
}

func moveToForeground() {
override func moveToForeground() {
super.moveToForeground()
backgroundRinger.disableNotifications()
foregroundRinger.initializeLastPlayed(forTimeInSeconds: ShipsClock.nextTime())
updateClock() // right away, not at next tick
ticker = Timer.scheduledTimer(withTimeInterval: tickInterval, repeats: true, block: updateTimeCallback)
foregroundRinger.initializeLastPlayed(forTimeInSeconds: timeOfDayInSeconds)
locationTracker.activate()
}

func moveToBackground() {
ticker?.invalidate()
override func moveToBackground() {
super.moveToBackground()
backgroundRinger.scheduleBellNotificationsIfAuthorized()
locationTracker.deactivate()
}

private func updateTimeCallback(_: Timer) {
updateClock()
}

private func updateClock() {
timeOfDayInSeconds = ShipsClock.nextTime()
utcTimeInSeconds = ShipsClock.utcTime()
override func updateClock() {
super.updateClock()
celestialComputer.maybeUpdateTheSky(timeOfDayInSeconds: timeOfDayInSeconds)
foregroundRinger.maybeRing(forTimeInSeconds: timeOfDayInSeconds)
}

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
}
}

fileprivate static func timeInSeconds(_ cal: Calendar) -> Int {
let now = Date()
let hms = cal.dateComponents([.hour, .minute, .second], from: now)
return timeInSeconds(hms)
}

private static func nextTime() -> Int {
return timeInSeconds(Calendar.current)
}

private static func utcTime() -> Int {
var cal = Calendar.current
cal.timeZone = TimeZone.init(secondsFromGMT: 0) ?? TimeZone.current
return timeInSeconds(cal)
}
}
40 changes: 40 additions & 0 deletions ShipsClock/WatchClock.swift
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)
}
}
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"
}
}
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
}
}
Loading