From ea1c06a519237df868c614297874692e14bad1e8 Mon Sep 17 00:00:00 2001 From: Renaud Jenny Date: Wed, 14 Oct 2020 18:38:21 +0100 Subject: [PATCH 1/3] feat(Widget): add the Speak button directly to the Widget --- TellTimeWidget/TellTimeWidget.swift | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/TellTimeWidget/TellTimeWidget.swift b/TellTimeWidget/TellTimeWidget.swift index 9d15737..6efd2cb 100644 --- a/TellTimeWidget/TellTimeWidget.swift +++ b/TellTimeWidget/TellTimeWidget.swift @@ -91,7 +91,10 @@ struct TellTimeWidgetView: View { } private var smallView: some View { - Text(time).padding() + VStack { + Text(time).padding() + Button("Speak", action: {}) + } } @ViewBuilder @@ -99,12 +102,20 @@ struct TellTimeWidgetView: View { if design == .lewis { HStack { clock - digital + VStack { + digital + Spacer() + Button("Speak", action: {}) + } }.padding() } else { HStack { clock - Text(time) + VStack { + Text(time) + Spacer() + Button("Speak", action: {}) + } }.padding() } } @@ -113,7 +124,12 @@ struct TellTimeWidgetView: View { VStack { clock Spacer() - Text(time) + HStack { + Spacer() + Text(time) + Spacer() + Button("Speak", action: {}) + } Spacer() }.padding() } From 0651fc0b04f28031d6b6e0261299ce11f0fed513 Mon Sep 17 00:00:00 2001 From: Renaud Jenny Date: Thu, 15 Oct 2020 21:57:28 +0100 Subject: [PATCH 2/3] feat(Widget): add the Speaker button to directly tell the time ...when the app opens --- TellTimeWidget/TellTimeWidget.swift | 19 ++++++++++++++----- telltime/App/TellTimeUKApp.swift | 10 +++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/TellTimeWidget/TellTimeWidget.swift b/TellTimeWidget/TellTimeWidget.swift index 6efd2cb..2d59526 100644 --- a/TellTimeWidget/TellTimeWidget.swift +++ b/TellTimeWidget/TellTimeWidget.swift @@ -93,7 +93,6 @@ struct TellTimeWidgetView: View { private var smallView: some View { VStack { Text(time).padding() - Button("Speak", action: {}) } } @@ -104,8 +103,6 @@ struct TellTimeWidgetView: View { clock VStack { digital - Spacer() - Button("Speak", action: {}) } }.padding() } else { @@ -114,7 +111,9 @@ struct TellTimeWidgetView: View { VStack { Text(time) Spacer() - Button("Speak", action: {}) + Link(destination: URL(string: "speak")!) { + speakButton + } } }.padding() } @@ -128,7 +127,9 @@ struct TellTimeWidgetView: View { Spacer() Text(time) Spacer() - Button("Speak", action: {}) + Link(destination: URL(string: "speak")!) { + speakButton + } } Spacer() }.padding() @@ -181,6 +182,14 @@ struct TellTimeWidgetView: View { } } } + + private var speakButton: some View { + Image(systemName: "speaker.2") + .foregroundColor(.white) + .padding() + .cornerRadius(8) + .background(Color.red.cornerRadius(8)) + } } @main diff --git a/telltime/App/TellTimeUKApp.swift b/telltime/App/TellTimeUKApp.swift index 66f952e..201d2c9 100644 --- a/telltime/App/TellTimeUKApp.swift +++ b/telltime/App/TellTimeUKApp.swift @@ -22,7 +22,15 @@ struct TellTimeUKApp: SwiftUI.App { var body: some Scene { WindowGroup { - TellTimeView().environmentObject(store) + TellTimeView() + .environmentObject(store) + .onOpenURL(perform: { url in + switch url.absoluteString { + case "speak": + store.send(.tts(.tellTime(Date()))) + default: break + } + }) } } } From 7a9f6b4b0506062d2b28cbd35cad86a2bfd17890 Mon Sep 17 00:00:00 2001 From: Renaud Jenny Date: Mon, 19 Oct 2020 17:39:32 +0100 Subject: [PATCH 3/3] feat(Widget): open Widget will change the time and style of the clock --- TellTimeWidget/TellTimeWidget.swift | 42 +++++++++++++++++++++++++---- telltime/App/TellTimeUKApp.swift | 16 ++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/TellTimeWidget/TellTimeWidget.swift b/TellTimeWidget/TellTimeWidget.swift index 2d59526..472ecc2 100644 --- a/TellTimeWidget/TellTimeWidget.swift +++ b/TellTimeWidget/TellTimeWidget.swift @@ -104,18 +104,22 @@ struct TellTimeWidgetView: View { VStack { digital } - }.padding() + } + .padding() + .widgetURL(url()) } else { HStack { clock VStack { Text(time) Spacer() - Link(destination: URL(string: "speak")!) { + Link(destination: url(speak: true)) { speakButton } } - }.padding() + } + .padding() + .widgetURL(url()) } } @@ -127,12 +131,14 @@ struct TellTimeWidgetView: View { Spacer() Text(time) Spacer() - Link(destination: URL(string: "speak")!) { + Link(destination: url(speak: true)) { speakButton } } Spacer() - }.padding() + } + .padding() + .widgetURL(url()) } private var formattedTime: String { @@ -190,6 +196,19 @@ struct TellTimeWidgetView: View { .cornerRadius(8) .background(Color.red.cornerRadius(8)) } + + private func url(speak: Bool = false) -> URL { + var urlComponents = URLComponents() + urlComponents.host = "renaud.jenny.telltime" + urlComponents.queryItems = [ + URLQueryItem(name: "clockStyle", value: "\(design.clockStyle.id)"), + URLQueryItem(name: "speak", value: "\(speak)") + ] + guard let url = urlComponents.url else { + fatalError("Cannot build the URL from the Widget") + } + return url + } } @main @@ -209,6 +228,19 @@ struct TellTimeWidget: Widget { } } +extension Design { + var clockStyle: ClockStyle { + switch self { + case .unknown: return .classic + case .classic: return .classic + case .artNouveau: return .artNouveau + case .drawing: return .drawing + case .steampunk: return .steampunk + case .lewis: return .classic + } + } +} + struct TellTimeWidget_Previews: PreviewProvider { static var previews: some View { Group { diff --git a/telltime/App/TellTimeUKApp.swift b/telltime/App/TellTimeUKApp.swift index 201d2c9..ad2f09a 100644 --- a/telltime/App/TellTimeUKApp.swift +++ b/telltime/App/TellTimeUKApp.swift @@ -1,5 +1,6 @@ import SwiftUI import SwiftTTSCombine +import SwiftClockUI @main struct TellTimeUKApp: SwiftUI.App { @@ -25,10 +26,19 @@ struct TellTimeUKApp: SwiftUI.App { TellTimeView() .environmentObject(store) .onOpenURL(perform: { url in - switch url.absoluteString { - case "speak": + store.send(.changeDate(Date())) + guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else { + return + } + guard + let clockStyleValue = urlComponents.queryItems?.first(where: { $0.name == "clockStyle" })?.value, + let clockStyle = ClockStyle.allCases.first(where: { String($0.id) == clockStyleValue }) + else { + return + } + store.send(.configuration(.changeClockStyle(clockStyle))) + if urlComponents.queryItems?.first(where: { $0.name == "speak" })?.value == "true" { store.send(.tts(.tellTime(Date()))) - default: break } }) }