Skip to content

Commit

Permalink
Fix watchOS time machine view and watchOS 9 crash
Browse files Browse the repository at this point in the history
  • Loading branch information
daneden committed Sep 21, 2023
1 parent 303dd27 commit 70a7f38
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 58 deletions.
29 changes: 27 additions & 2 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
}
},
"%@" : {

"localizations" : {
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "%@"
}
}
}
},
"%@ (%@)" : {
"localizations" : {
Expand Down Expand Up @@ -141,7 +148,14 @@
}
},
"%@ Location required" : {

"localizations" : {
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "%@ Posizione richiesta"
}
}
}
},
"%@ of daylight" : {
"localizations" : {
Expand Down Expand Up @@ -231,6 +245,17 @@
}
}
},
"%lld" : {
"comment" : "Number of days in the past or future for Time Travel on Apple Watch",
"localizations" : {
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "%lld"
}
}
}
},
"%lld days in the %@" : {
"localizations" : {
"en" : {
Expand Down
7 changes: 0 additions & 7 deletions Solstice/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
}
},
"idiom" : "universal"
},
{
"color" : {
"platform" : "watchos",
"reference" : "systemYellowColor"
},
"idiom" : "watch"
}
],
"info" : {
Expand Down
41 changes: 23 additions & 18 deletions Solstice/Detail View/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,33 @@ struct DetailView<Location: ObservableLocation>: View {
Solar(for: timeMachine.date, coordinate: location.coordinate)
}

var toolbarItemPlacement: ToolbarItemPlacement {
if #available(watchOS 10, *) {
return .topBarTrailing
} else {
return .automatic
}
}

@ToolbarContentBuilder
var toolbarItems: some ToolbarContent {
#if os(watchOS)
if #available(watchOS 10, *) {
ToolbarItem(id: "timeMachineToggle", placement: .topBarTrailing) {
Button {
timeMachine.controlsVisible.toggle()
} label: {
Label("Time Travel", systemImage: "clock.arrow.2.circlepath")
.symbolEffect(.pulse, isActive: timeMachine.isOn)
ToolbarItem(id: "timeMachineToggle", placement: toolbarItemPlacement) {
Button {
timeMachine.controlsVisible.toggle()
} label: {
Label("Time Travel", systemImage: "clock.arrow.2.circlepath")
}
.sheet(isPresented: $timeMachine.controlsVisible) {
Form {
TimeMachineView()
}
.sheet(isPresented: $timeMachine.controlsVisible) {
Form {
TimeMachineView()
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button {
timeMachine.controlsVisible.toggle()
} label: {
Label("Close", systemImage: "xmark")
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button {
timeMachine.controlsVisible.toggle()
} label: {
Label("Close", systemImage: "xmark")
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions Solstice/Extensions/Solar++.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ extension Solar {
let comparator = date.isToday ? yesterday : Solar(coordinate: self.coordinate)!
let difference = daylightDuration - comparator.daylightDuration
let differenceString = Duration.seconds(abs(difference)).formatted(.units(maximumUnitCount: 2))
var string = (daylightDuration - comparator.daylightDuration).localizedString

let moreOrLess = difference >= 0 ? NSLocalizedString("more", comment: "More daylight middle of sentence") : NSLocalizedString("less", comment: "Less daylight middle of sentence")

Expand All @@ -74,7 +73,6 @@ extension Solar {
}

let comparatorDate = comparator.date
let comparatorDateString = formatter.string(from: comparatorDate)

return LocalizedStringKey("\(differenceString) \(moreOrLess) daylight \(baseDateString) compared to \(formatter.string(from: comparatorDate))")
}
Expand Down
6 changes: 2 additions & 4 deletions Solstice/SolsticeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@
//

import SwiftUI
import UserNotifications
import StoreKit

@main
struct SolsticeApp: App {
@Environment(\.scenePhase) var phase
@StateObject private var currentLocation = CurrentLocation()
@StateObject private var timeMachine = TimeMachine()

private let persistenceController = PersistenceController.shared

var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(currentLocation)
.environmentObject(timeMachine)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.task {
for await result in Transaction.updates {
switch result {
case .verified(let transaction):
print("Transaction verified in listener")

await transaction.finish()

// Update the user's purchases...
case .unverified:
print("Transaction unverified")
}
Expand Down
37 changes: 30 additions & 7 deletions Solstice/TimeMachineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ struct TimeMachineView: View {

@ViewBuilder
var controls: some View {
if #available(watchOS 10, iOS 15, macOS 13, *) {
DatePicker(selection: $timeMachine.targetDate, displayedComponents: .date) {
Text("\(Image(systemName: "clock.arrow.2.circlepath")) Time Travel")
}
#if os(watchOS)
.datePickerStyle(.wheel)
#endif
#if !os(watchOS)
DatePicker(selection: $timeMachine.targetDate, displayedComponents: .date) {
Text("\(Image(systemName: "clock.arrow.2.circlepath")) Time Travel")
}
#endif

#if os(iOS) || os(macOS)
Slider(value: timeMachine.offset,
Expand All @@ -55,6 +52,32 @@ struct TimeMachineView: View {
.tint(Color(UIColor.systemFill))
#endif
.foregroundStyle(.secondary)
#elseif os(watchOS)
Stepper(
value: timeMachine.offset,
in: -365...365,
step: 1
) {
Text("\(Int(timeMachine.offset.wrappedValue))", comment: "Number of days in the past or future for Time Travel on Apple Watch")
.font(.largeTitle)
}
.foregroundStyle(.secondary)

VStack(alignment: .leading) {
Text(timeMachine.date, style: .date)
Text("\(Int(abs(timeMachine.offset.wrappedValue))) days in the \(timeMachine.offset.wrappedValue >= 0 ? Text("future") : Text("past"))")
.font(.footnote)
.foregroundStyle(.secondary)
}

Button {
withAnimation {
timeMachine.offset.wrappedValue = 0
}
} label: {
Label("Reset", systemImage: "gobackward")
}
.disabled(timeMachine.offset.wrappedValue == 0)
#endif
}
}
Expand Down
35 changes: 17 additions & 18 deletions watchOS/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct ContentView: View {
@Environment(\.scenePhase) var scenePhase
@EnvironmentObject var currentLocation: CurrentLocation
@StateObject var timeMachine = TimeMachine()
@EnvironmentObject var timeMachine: TimeMachine

private let timer = Timer.publish(every: 60, on: RunLoop.main, in: .common).autoconnect()

Expand All @@ -27,27 +27,26 @@ struct ContentView: View {
fatalError()
}
}
.environmentObject(timeMachine)
.navigationTitle(Text(verbatim: "Solstice"))
.onChange(of: scenePhase) { _ in
timeMachine.referenceDate = Date()
if currentLocation.isAuthorized,
scenePhase != .background {
currentLocation.requestLocation()
}
.navigationTitle(Text(verbatim: "Solstice"))
.onChange(of: scenePhase) { _ in
timeMachine.referenceDate = Date()
if currentLocation.isAuthorized,
scenePhase != .background {
currentLocation.requestLocation()
}
.onReceive(timer) { _ in
timeMachine.referenceDate = Date()
if currentLocation.isAuthorized {
currentLocation.requestLocation()
}
}
.onReceive(timer) { _ in
timeMachine.referenceDate = Date()
if currentLocation.isAuthorized {
currentLocation.requestLocation()
}
}
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
#Preview {
ContentView()
.environmentObject(TimeMachine.preview)
.environmentObject(CurrentLocation())
}

0 comments on commit 70a7f38

Please sign in to comment.