WIP
This commit is contained in:
@@ -21,6 +21,8 @@ struct AllWorkoutsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
@State private var showLoginView = false
|
||||
|
||||
let pub = NotificationCenter.default.publisher(for: NSNotification.Name("CreatedNewWorkout"))
|
||||
|
||||
var body: some View {
|
||||
@@ -45,34 +47,45 @@ struct AllWorkoutsView: View {
|
||||
Text("no workouts")
|
||||
}
|
||||
}.onAppear{
|
||||
if needsUpdating {
|
||||
UserStore.shared.login(completion: { success in
|
||||
if success {
|
||||
DataStore.shared.fetchAllData()
|
||||
|
||||
AllWorkoutFetchable().fetch(completion: { result in
|
||||
needsUpdating = false
|
||||
switch result {
|
||||
case .success(let model):
|
||||
DispatchQueue.main.async {
|
||||
self.workouts = model
|
||||
}
|
||||
case .failure(let failure):
|
||||
fatalError("shit broke")
|
||||
}
|
||||
})
|
||||
} else {
|
||||
fatalError("shit broke")
|
||||
}
|
||||
})
|
||||
}
|
||||
maybeUpdateShit()
|
||||
}
|
||||
.sheet(item: $selectedWorkout) { item in
|
||||
let viewModel = WorkoutDetailViewModel(workout: item)
|
||||
WorkoutDetailView(viewModel: viewModel)
|
||||
}
|
||||
.sheet(isPresented: $showLoginView) {
|
||||
LoginView(completion: {
|
||||
self.needsUpdating = true
|
||||
maybeUpdateShit()
|
||||
})
|
||||
}
|
||||
.onReceive(pub) { (output) in
|
||||
self.needsUpdating = true
|
||||
maybeUpdateShit()
|
||||
}
|
||||
}
|
||||
|
||||
func maybeUpdateShit() {
|
||||
if UserStore.shared.token != nil{
|
||||
if needsUpdating {
|
||||
DataStore.shared.fetchAllData()
|
||||
|
||||
AllWorkoutFetchable().fetch(completion: { result in
|
||||
needsUpdating = false
|
||||
switch result {
|
||||
case .success(let model):
|
||||
DispatchQueue.main.async {
|
||||
self.workouts = model
|
||||
}
|
||||
case .failure(_):
|
||||
fatalError("shit broke")
|
||||
}
|
||||
})
|
||||
} else {
|
||||
fatalError("shit broke")
|
||||
}
|
||||
} else {
|
||||
showLoginView = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user