Adding TabView

release
Laurent 3 years ago
parent 1a8bcf6a4d
commit b258a21fe8
  1. 26
      LeCountdown/LeCountdownApp.swift
  2. 4
      LeCountdown/Views/Alarm/NewAlarmView.swift
  3. 55
      LeCountdown/Views/ContentView.swift
  4. 4
      LeCountdown/Views/Countdown/CountdownDialView.swift
  5. 10
      LeCountdown/Views/Countdown/NewCountdownView.swift
  6. 4
      LeCountdown/Views/Stopwatch/NewStopwatchView.swift

@ -22,14 +22,24 @@ struct LeCountdownApp: App {
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() TabView {
.environmentObject(BoringContext()) ContentView<Countdown>()
.environment(\.managedObjectContext, persistenceController.container.viewContext) .environment(\.managedObjectContext, persistenceController.container.viewContext)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in .tabItem { Label("Countdown", systemImage: "timer") }
self._willEnterForegroundNotification() ContentView<Stopwatch>()
}.onAppear { .environment(\.managedObjectContext, persistenceController.container.viewContext)
self._onAppear() .tabItem { Label("Stopwatch", systemImage: "stopwatch") }
} ContentView<Alarm>()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Alarm", systemImage: "alarm") }
RecordsView().environment(\.managedObjectContext, persistenceController.container.viewContext)
.tabItem { Label("Stats", systemImage: "chart.bar.fill") }
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
self._willEnterForegroundNotification()
}.onAppear {
self._onAppear()
}
} }
} }

@ -18,7 +18,7 @@ struct NewAlarmView: View {
var body: some View { var body: some View {
AlarmEditView(isPresented: $isPresented) AlarmEditView(isPresented: $isPresented)
.environment(\.managedObjectContext, viewContext) .environment(\.managedObjectContext, viewContext)
.navigationTitle("New countdown") .navigationTitle("New alarm")
} }
} }
@ -127,7 +127,7 @@ struct AlarmEditView: View {
} }
} }
} }
.navigationTitle("Edit countdown") .navigationTitle("Edit alarm")
} }
} }

@ -17,16 +17,16 @@ class BoringContext : ObservableObject {
} }
struct ContentView: View { struct ContentView<T : AbstractTimer>: View {
@EnvironmentObject var boringContext: BoringContext @StateObject var boringContext: BoringContext = BoringContext()
@Environment(\.managedObjectContext) private var viewContext @Environment(\.managedObjectContext) private var viewContext
@FetchRequest( @FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \AbstractTimer.order, ascending: true)], sortDescriptors: [NSSortDescriptor(keyPath: \T.order, ascending: true)],
animation: .default) animation: .default)
private var timers: FetchedResults<AbstractTimer> private var timers: FetchedResults<T>
fileprivate let itemSpacing: CGFloat = 10.0 fileprivate let itemSpacing: CGFloat = 10.0
@ -35,7 +35,7 @@ struct ContentView: View {
GridItem(spacing: 10.0), GridItem(spacing: 10.0),
] ]
var timersArray: [AbstractTimer] { var timersArray: [T] {
return Array(timers) return Array(timers)
} }
@ -61,7 +61,7 @@ struct ContentView: View {
} }
}.padding(itemSpacing) }.padding(itemSpacing)
.navigationTitle("Youpi") .navigationTitle("\(String(describing: T.self))")
.alert(boringContext.error?.localizedDescription ?? "missing error", isPresented: $boringContext.showDefaultAlert) { .alert(boringContext.error?.localizedDescription ?? "missing error", isPresented: $boringContext.showDefaultAlert) {
Button("OK", role: .cancel) { } Button("OK", role: .cancel) { }
} }
@ -69,23 +69,23 @@ struct ContentView: View {
PermissionAlertView() PermissionAlertView()
} }
.sheet(isPresented: $boringContext.isShowingNewData, content: { .sheet(isPresented: $boringContext.isShowingNewData, content: {
NewCountdownView(isPresented: $boringContext.isShowingNewData) .environment(\.managedObjectContext, viewContext) self._newView(isPresented: $boringContext.isShowingNewData)
}) .environment(\.managedObjectContext, viewContext)
.sheet(isPresented: $boringContext.isShowingNewData, content: {
NewStopwatchView(isPresented: $boringContext.isShowingNewData) .environment(\.managedObjectContext, viewContext)
})
.sheet(isPresented: $boringContext.isShowingNewData, content: {
NewAlarmView(isPresented: $boringContext.isShowingNewData) .environment(\.managedObjectContext, viewContext)
}) })
// .sheet(isPresented: $boringContext.isShowingNewData, content: {
// NewStopwatchView(isPresented: $boringContext.isShowingNewData) .environment(\.managedObjectContext, viewContext)
// })
// .sheet(isPresented: $boringContext.isShowingNewData, content: {
// NewAlarmView(isPresented: $boringContext.isShowingNewData) .environment(\.managedObjectContext, viewContext)
// })
.toolbar { .toolbar {
ToolbarItemGroup(placement: .bottomBar) {
MainToolbarView(isShowingNewData: $boringContext.isShowingNewData)
}
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink { Button {
RecordsView() self.boringContext.isShowingNewData = true
} label: { } label: {
Image(systemName: "chart.bar.fill") HStack {
Image(systemName: "plus")
}
} }
} }
ToolbarItem(placement: .navigationBarLeading) { ToolbarItem(placement: .navigationBarLeading) {
@ -103,6 +103,21 @@ struct ContentView: View {
} }
@ViewBuilder
fileprivate func _newView(isPresented: Binding<Bool>) -> 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:
Text("missing new view")
}
}
fileprivate func _reorder(from: IndexSet, to: Int) { fileprivate func _reorder(from: IndexSet, to: Int) {
var timers: [AbstractTimer] = self.timersArray var timers: [AbstractTimer] = self.timersArray
timers.move(fromOffsets: from, toOffset: to) timers.move(fromOffsets: from, toOffset: to)
@ -207,6 +222,6 @@ fileprivate extension Countdown {
struct ContentView_Previews: PreviewProvider { struct ContentView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) ContentView<Countdown>().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
} }
} }

@ -9,8 +9,6 @@ import SwiftUI
struct CountdownDialView: View { struct CountdownDialView: View {
@EnvironmentObject var environment: Conductor
@ObservedObject var countdown: Countdown @ObservedObject var countdown: Countdown
var body: some View { var body: some View {
@ -21,7 +19,7 @@ struct CountdownDialView: View {
Spacer() Spacer()
} }
if let dateInterval = environment.notificationDates[countdown.stringId] { if let dateInterval = Conductor.maestro.notificationDates[countdown.stringId] {
Text(dateInterval.end, style: .timer) Text(dateInterval.end, style: .timer)
Button { Button {
CountdownScheduler.master.cancelCurrentNotifications(countdown: countdown) CountdownScheduler.master.cancelCurrentNotifications(countdown: countdown)

@ -205,7 +205,9 @@ struct CountdownEditView : View {
if !self.nameString.isEmpty { if !self.nameString.isEmpty {
if let activity = cd.activity, let currentActivityName = activity.name, self.nameString != currentActivityName { let trimmed = self.nameString.trimmingCharacters(in: .whitespacesAndNewlines)
if let activity = cd.activity, let currentActivityName = activity.name, trimmed != currentActivityName {
switch self._rename { switch self._rename {
case .none: case .none:
@ -213,13 +215,13 @@ struct CountdownEditView : View {
return return
case .some(let rename): case .some(let rename):
if rename { if rename {
activity.name = self.nameString activity.name = trimmed
} else { } else {
cd.activity = CoreDataRequests.getOrCreateActivity(name: self.nameString) cd.activity = CoreDataRequests.getOrCreateActivity(name: trimmed)
} }
} }
} else { } else {
cd.activity = CoreDataRequests.getOrCreateActivity(name: self.nameString) cd.activity = CoreDataRequests.getOrCreateActivity(name: trimmed)
} }
} }

@ -18,7 +18,7 @@ struct NewStopwatchView: View {
var body: some View { var body: some View {
StopwatchEditView(isPresented: $isPresented) StopwatchEditView(isPresented: $isPresented)
.environment(\.managedObjectContext, viewContext) .environment(\.managedObjectContext, viewContext)
.navigationTitle("New countdown") .navigationTitle("New stopwatch")
} }
} }
@ -127,7 +127,7 @@ struct StopwatchEditView: View {
} }
} }
} }
.navigationTitle("Edit countdown") .navigationTitle("Edit stopwatch")
} }
} }

Loading…
Cancel
Save