Skip to content

Commit

Permalink
Merge pull request #22 from renaudjenny/21-add-speak-button-on-differ…
Browse files Browse the repository at this point in the history
…ent-widgets

21 add speak button on different widgets
  • Loading branch information
renaudjenny authored Oct 19, 2020
2 parents 372b166 + 7a9f6b4 commit 983bc91
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
71 changes: 64 additions & 7 deletions TellTimeWidget/TellTimeWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,54 @@ struct TellTimeWidgetView: View {
}

private var smallView: some View {
Text(time).padding()
VStack {
Text(time).padding()
}
}

@ViewBuilder
private var mediumView: some View {
if design == .lewis {
HStack {
clock
digital
}.padding()
VStack {
digital
}
}
.padding()
.widgetURL(url())
} else {
HStack {
clock
Text(time)
}.padding()
VStack {
Text(time)
Spacer()
Link(destination: url(speak: true)) {
speakButton
}
}
}
.padding()
.widgetURL(url())
}
}

private var largeView: some View {
VStack {
clock
Spacer()
Text(time)
HStack {
Spacer()
Text(time)
Spacer()
Link(destination: url(speak: true)) {
speakButton
}
}
Spacer()
}.padding()
}
.padding()
.widgetURL(url())
}

private var formattedTime: String {
Expand Down Expand Up @@ -165,6 +188,27 @@ struct TellTimeWidgetView: View {
}
}
}

private var speakButton: some View {
Image(systemName: "speaker.2")
.foregroundColor(.white)
.padding()
.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
Expand All @@ -184,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 {
Expand Down
20 changes: 19 additions & 1 deletion telltime/App/TellTimeUKApp.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftUI
import SwiftTTSCombine
import SwiftClockUI

@main
struct TellTimeUKApp: SwiftUI.App {
Expand All @@ -22,7 +23,24 @@ struct TellTimeUKApp: SwiftUI.App {

var body: some Scene {
WindowGroup {
TellTimeView().environmentObject(store)
TellTimeView()
.environmentObject(store)
.onOpenURL(perform: { url in
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())))
}
})
}
}
}

0 comments on commit 983bc91

Please sign in to comment.