From 2dfa0c9e9bb600c21704cbadef7ec4e11135b449 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 20 Feb 2023 10:49:25 +0100 Subject: [PATCH] Data creation is moved to the presets --- LeCountdown/Views/ContentView.swift | 50 +++++++++---------- .../Views/Countdown/NewCountdownView.swift | 13 +++-- LeCountdown/Views/DialView.swift | 2 +- LeCountdown/Views/HomeView.swift | 40 ++++++++++----- LeCountdown/Views/NewDataView.swift | 9 ++-- LeCountdown/Views/PresetsView.swift | 44 ++++++++++++++-- .../Views/Stopwatch/NewStopwatchView.swift | 21 +++++++- 7 files changed, 127 insertions(+), 52 deletions(-) diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index ceeac13..dceb474 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -143,19 +143,19 @@ struct ContentView: View { PermissionAlertView() } .sheet(isPresented: $boringContext.isShowingNewData, content: { - self._newView(isPresented: $boringContext.isShowingNewData) - .environment(\.managedObjectContext, viewContext) +// self._newView(isPresented: $boringContext.isShowingNewData) +// .environment(\.managedObjectContext, viewContext) }) .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Button { - self.boringContext.isShowingNewData = true - } label: { - HStack { - Image(systemName: "plus") - } - } - } +// ToolbarItem(placement: .navigationBarTrailing) { +// Button { +// self.boringContext.isShowingNewData = true +// } label: { +// HStack { +// Image(systemName: "plus") +// } +// } +// } ToolbarItem(placement: .navigationBarLeading) { Button { withAnimation { @@ -232,20 +232,20 @@ struct ContentView: View { // MARK: - Subviews - @ViewBuilder - fileprivate func _newView(isPresented: Binding) -> some View { - switch T.self { - case is Countdown.Type: - NewCountdownView(isPresented: isPresented) - case is Alarm.Type: - NewAlarmView(isPresented: isPresented) - case is Stopwatch.Type: - NewStopwatchView(isPresented: isPresented) - default: - NewDataView(isPresented: isPresented) - } - - } +// @ViewBuilder +// fileprivate func _newView(isPresented: Binding) -> some View { +// switch T.self { +// case is Countdown.Type: +// NewCountdownView(isPresented: isPresented, tabSelection: self.tabse) +// case is Alarm.Type: +// NewAlarmView(isPresented: isPresented) +// case is Stopwatch.Type: +// NewStopwatchView(isPresented: isPresented, tabSelection: <#Binding#>) +// default: +// NewDataView(isPresented: isPresented, tabSelection: <#Binding#>) +// } +// +// } // MARK: - Business diff --git a/LeCountdown/Views/Countdown/NewCountdownView.swift b/LeCountdown/Views/Countdown/NewCountdownView.swift index 4d1f187..0d6ec7e 100644 --- a/LeCountdown/Views/Countdown/NewCountdownView.swift +++ b/LeCountdown/Views/Countdown/NewCountdownView.swift @@ -14,9 +14,15 @@ struct NewCountdownView : View { @Environment(\.managedObjectContext) private var viewContext @Binding var isPresented: Bool + var tabSelection: Binding + init(isPresented: Binding, tabSelection: Binding) { + _isPresented = isPresented + self.tabSelection = tabSelection + } + var body: some View { - CountdownEditView(isPresented: $isPresented) + CountdownEditView(isPresented: $isPresented, tabSelection: self.tabSelection) .environment(\.managedObjectContext, viewContext) .navigationTitle("New countdown") } @@ -61,9 +67,10 @@ struct CountdownEditView : View { @Environment(\.isPresented) var envIsPresented - init(isPresented: Binding, countdown: Countdown? = nil) { + init(isPresented: Binding, countdown: Countdown? = nil, tabSelection: Binding? = nil) { _isPresented = isPresented self.countdown = countdown + self.tabSelection = tabSelection } init(isPresented: Binding, preset: Preset, tabSelection: Binding) { @@ -320,7 +327,7 @@ struct CountdownEditView : View { struct NewCountdownView_Previews: PreviewProvider { static var previews: some View { - NewCountdownView(isPresented: .constant(true)) + NewCountdownView(isPresented: .constant(true), tabSelection: .constant(0)) .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } } diff --git a/LeCountdown/Views/DialView.swift b/LeCountdown/Views/DialView.swift index c7953ca..fbd264f 100644 --- a/LeCountdown/Views/DialView.swift +++ b/LeCountdown/Views/DialView.swift @@ -117,7 +117,7 @@ struct DialView: View { AlarmEditView(alarm: alarm, isPresented: isPresented) .environment(\.managedObjectContext, viewContext) case let stopwatch as Stopwatch: - StopwatchEditView(stopwatch: stopwatch, isPresented: isPresented) + StopwatchEditView(isPresented: isPresented, stopwatch: stopwatch) .environment(\.managedObjectContext, viewContext) default: Text("missing edit view") diff --git a/LeCountdown/Views/HomeView.swift b/LeCountdown/Views/HomeView.swift index 6986cad..733496c 100644 --- a/LeCountdown/Views/HomeView.swift +++ b/LeCountdown/Views/HomeView.swift @@ -11,31 +11,45 @@ struct CompactHomeView: View { @Environment(\.managedObjectContext) private var viewContext - @State private var tabSelection: Int = 1 + @FetchRequest( + sortDescriptors: [NSSortDescriptor(keyPath: \AbstractTimer.order, ascending: true)], + animation: .default) + private var timers: FetchedResults + + @State private var tabSelection: Int = 0 var body: some View { - NavigationStack { TabView(selection: $tabSelection) { - PresetsView(tabSelection: $tabSelection) - .environment(\.managedObjectContext, viewContext) - .tabItem { Label("Presets", systemImage: "globe") } + NavigationStack { + PresetsView(tabSelection: $tabSelection) + .environment(\.managedObjectContext, viewContext) + .tabItem { Label("Presets", systemImage: "globe") } .tag(0) - ContentView() - .environment(\.managedObjectContext, viewContext) - .environmentObject(Conductor.maestro) - .tabItem { Label("Home", systemImage: "clock.fill") } + } + NavigationStack { + ContentView() + .environment(\.managedObjectContext, viewContext) + .environmentObject(Conductor.maestro) + .tabItem { Label("Home", systemImage: "clock.fill") } .tag(1) - ActivitiesView() - .environment(\.managedObjectContext, viewContext) - .tabItem { Label("Stats", systemImage: "chart.bar.fill") } + } + NavigationStack { + ActivitiesView() + .environment(\.managedObjectContext, viewContext) + .tabItem { Label("Stats", systemImage: "chart.bar.fill") } .tag(2) + } } .tabViewStyle(.page) + .onAppear { + if self.timers.count > 0 { + self.tabSelection = 1 + } + } .onOpenURL { _ in self.tabSelection = 1 } - } } } diff --git a/LeCountdown/Views/NewDataView.swift b/LeCountdown/Views/NewDataView.swift index fd7b6b1..5c1f976 100644 --- a/LeCountdown/Views/NewDataView.swift +++ b/LeCountdown/Views/NewDataView.swift @@ -28,7 +28,8 @@ struct NewDataView: View { @Environment(\.managedObjectContext) private var viewContext @Binding var isPresented: Bool - + var tabSelection: Binding + @State var selection: Int = 0 var body: some View { @@ -46,10 +47,10 @@ struct NewDataView: View { .padding(.horizontal) TabView(selection: $selection) { - NewCountdownView(isPresented: $isPresented) + NewCountdownView(isPresented: $isPresented, tabSelection: self.tabSelection) .tag(0) .environment(\.managedObjectContext, viewContext) - NewStopwatchView(isPresented: $isPresented) + NewStopwatchView(isPresented: $isPresented, tabSelection: self.tabSelection) .tag(1) .environment(\.managedObjectContext, viewContext) }.tabViewStyle(.page(indexDisplayMode: .never)) @@ -61,7 +62,7 @@ struct NewDataView: View { struct NewDataView_Previews: PreviewProvider { static var previews: some View { - NewDataView(isPresented: .constant(true)) + NewDataView(isPresented: .constant(true), tabSelection: .constant(0)) .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } } diff --git a/LeCountdown/Views/PresetsView.swift b/LeCountdown/Views/PresetsView.swift index 2e473a6..81ad05f 100644 --- a/LeCountdown/Views/PresetsView.swift +++ b/LeCountdown/Views/PresetsView.swift @@ -153,6 +153,9 @@ struct PresetsView: View { @State var isPresented: Bool = false + @State var isShowingNewCountdown = false + @State var isShowingNewStopwatch = false + var tabSelection: Binding fileprivate func _columnCount() -> Int { @@ -175,9 +178,34 @@ struct PresetsView: View { ScrollView { - Text("You can edit the duration, sound and label before adding") - .padding() - .font(.callout) + VStack(alignment: .leading, spacing: 0.0) { + Button { + self.isShowingNewCountdown = true + } label: { + Text(".create countdown") + .font(.system(.title, weight: .heavy)) + Spacer() + }.frame(height: 40.0) + + Button { + self.isShowingNewStopwatch = true + } label: { + Text(".create stopwatch") + .font(.system(.title, weight: .heavy)) + Spacer() + }.frame(height: 40.0) + + Text("You can ask Siri to create and launch countdowns and stopwatches") + .font(.callout) + .padding(.vertical) + + Text("Presets") + .font(.system(.title, weight: .heavy)) + + Text("You can edit the duration, sound and label before adding") + .font(.callout) + + }.padding(.horizontal) LazyVGrid( columns: self._columns(), @@ -209,11 +237,19 @@ struct PresetsView: View { Spacer() } + .sheet(isPresented: $isShowingNewStopwatch, content: { + NewStopwatchView(isPresented: $isShowingNewStopwatch, tabSelection: self.tabSelection) + .environment(\.managedObjectContext, viewContext) + }) + .sheet(isPresented: $isShowingNewCountdown, content: { + NewCountdownView(isPresented: $isShowingNewCountdown, tabSelection: self.tabSelection) + .environment(\.managedObjectContext, viewContext) + }) .sheet(isPresented: $isPresented, content: { CountdownEditView(isPresented: $isPresented, preset: self.model.selectedPreset, tabSelection: self.tabSelection) .environment(\.managedObjectContext, viewContext) }) - .navigationTitle("Presets") + .navigationTitle("Create") } } diff --git a/LeCountdown/Views/Stopwatch/NewStopwatchView.swift b/LeCountdown/Views/Stopwatch/NewStopwatchView.swift index 22fbe64..599b35f 100644 --- a/LeCountdown/Views/Stopwatch/NewStopwatchView.swift +++ b/LeCountdown/Views/Stopwatch/NewStopwatchView.swift @@ -14,9 +14,15 @@ struct NewStopwatchView: View { @Environment(\.managedObjectContext) private var viewContext @Binding var isPresented: Bool + var tabSelection: Binding + + init(isPresented: Binding, tabSelection: Binding) { + _isPresented = isPresented + self.tabSelection = tabSelection + } var body: some View { - StopwatchEditView(isPresented: $isPresented) + StopwatchEditView(isPresented: $isPresented, tabSelection: self.tabSelection) .environment(\.managedObjectContext, viewContext) .navigationTitle("New stopwatch") } @@ -52,10 +58,18 @@ struct StopwatchEditView: View { @State var _isAdding: Bool = false @Environment(\.isPresented) var envIsPresented + + var tabSelection: Binding? = nil @FetchRequest(sortDescriptors: []) private var timers: FetchedResults + init(isPresented: Binding, stopwatch: Stopwatch? = nil, tabSelection: Binding? = nil) { + _isPresented = isPresented + self.stopwatch = stopwatch + self.tabSelection = tabSelection + } + var body: some View { NavigationStack { Rectangle() @@ -270,6 +284,9 @@ struct StopwatchEditView: View { } else { dismiss() } + + self.tabSelection?.wrappedValue = 1 + } fileprivate func _delete() { @@ -300,7 +317,7 @@ struct StopwatchEditView: View { struct NewStopwatchView_Previews: PreviewProvider { static var previews: some View { - NewStopwatchView(isPresented: .constant(true)) + NewStopwatchView(isPresented: .constant(true), tabSelection: .constant(0)) .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }