WIP
This commit is contained in:
@@ -8,10 +8,19 @@
|
||||
import Foundation
|
||||
|
||||
class UserStore: ObservableObject {
|
||||
static let userDefaultsRegisteredUserKey = "registeredUserKey"
|
||||
static let shared = UserStore()
|
||||
|
||||
@Published public private(set) var registeredUser: RegisteredUser?
|
||||
|
||||
init(registeredUser: RegisteredUser? = nil) {
|
||||
self.registeredUser = registeredUser
|
||||
if let data = UserDefaults.standard.data(forKey: UserStore.userDefaultsRegisteredUserKey),
|
||||
let model = try? JSONDecoder().decode(RegisteredUser.self, from: data) {
|
||||
self.registeredUser = model
|
||||
}
|
||||
}
|
||||
|
||||
public var token: String? {
|
||||
guard let token = registeredUser?.token else {
|
||||
return nil
|
||||
@@ -19,19 +28,28 @@ class UserStore: ObservableObject {
|
||||
return "Token \(token)"
|
||||
}
|
||||
|
||||
func login(completion: @escaping (Bool)-> Void) {
|
||||
let postData = ["email": "user1@user1.com", "password":"test12345"]
|
||||
func login(postData: [String: Any], completion: @escaping (Bool)-> Void) {
|
||||
LoginFetchable(postData: postData).fetch(completion: { result in
|
||||
switch result {
|
||||
case .success(let model):
|
||||
self.registeredUser = model
|
||||
completion(true)
|
||||
DispatchQueue.main.async {
|
||||
self.registeredUser = model
|
||||
let data = try! JSONEncoder().encode(model)
|
||||
UserDefaults.standard.set(data, forKey: UserStore.userDefaultsRegisteredUserKey)
|
||||
completion(true)
|
||||
}
|
||||
case .failure(let failure):
|
||||
completion(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func logout() {
|
||||
self.registeredUser = nil
|
||||
UserDefaults.standard.set(nil, forKey: UserStore.userDefaultsRegisteredUserKey)
|
||||
NotificationCenter.default.post(name: NSNotification.Name("CreatedNewWorkout"), object: nil, userInfo: nil)
|
||||
}
|
||||
|
||||
func setFakeUser() {
|
||||
self.registeredUser = PreviewWorkout.parseRegisterdUser()
|
||||
}
|
||||
|
||||
@@ -49,6 +49,17 @@ struct AccountView: View {
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
|
||||
Button("Logout", action: {
|
||||
userStore.logout()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.white)
|
||||
.background(.red)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.padding()
|
||||
.onAppear{
|
||||
|
||||
@@ -24,9 +24,16 @@ struct AddExerciseView: View {
|
||||
var selectedWorkout: ((ExerciseExercise) -> Void)
|
||||
@State var createWorkoutItemPickerViewModel: CreateWorkoutItemPickerViewModel?
|
||||
@State var createWorkoutItemPickerViewType: CreateWorkoutItemPickerViewType?
|
||||
@State var searchString: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
exerciseView()
|
||||
.padding(.top)
|
||||
|
||||
TextField("Filter", text: $searchString)
|
||||
.padding()
|
||||
|
||||
HStack {
|
||||
muscleView()
|
||||
.frame(maxWidth: .infinity)
|
||||
@@ -38,9 +45,6 @@ struct AddExerciseView: View {
|
||||
}
|
||||
.padding(.top)
|
||||
.frame(height: 44)
|
||||
|
||||
exerciseView()
|
||||
.padding(.top)
|
||||
}
|
||||
.onAppear{
|
||||
if #function.hasPrefix("__preview") {
|
||||
@@ -190,25 +194,27 @@ struct AddExerciseView: View {
|
||||
List() {
|
||||
ForEach(filteredExercises.indices, id: \.self) { i in
|
||||
let obj = filteredExercises[i]
|
||||
VStack {
|
||||
Text(obj.name)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
if obj.side.count > 0 {
|
||||
Text(obj.side)
|
||||
if searchString.isEmpty || obj.name.lowercased().contains(searchString.lowercased()) {
|
||||
VStack {
|
||||
Text(obj.name)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
if obj.side.count > 0 {
|
||||
Text(obj.side)
|
||||
.font(.footnote)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
Text(obj.equipmentRequired)
|
||||
.font(.footnote)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Text(obj.muscleGroups)
|
||||
.font(.footnote)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
Text(obj.equipmentRequired)
|
||||
.font(.footnote)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Text(obj.muscleGroups)
|
||||
.font(.footnote)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
selectedWorkout(obj)
|
||||
dismiss()
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
selectedWorkout(obj)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,6 @@ struct CompletedWorkoutView: View {
|
||||
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2)))
|
||||
.cornerRadius(8)
|
||||
|
||||
|
||||
Spacer()
|
||||
|
||||
Button("Upload", action: {
|
||||
|
||||
@@ -17,33 +17,6 @@ struct CreateWorkoutMainView: View {
|
||||
TextField("Title", text: $viewModel.title)
|
||||
.padding()
|
||||
|
||||
HStack {
|
||||
Button("Add Superset", action: {
|
||||
viewModel.addNewSuperset()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.blue)
|
||||
.background(.yellow)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
|
||||
Divider()
|
||||
|
||||
Button("Done", action: {
|
||||
viewModel.uploadWorkout()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.white)
|
||||
.background(.blue)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.frame(height: 44)
|
||||
|
||||
List() {
|
||||
ForEach($viewModel.superSets, id: \.id) { superset in
|
||||
Section {
|
||||
@@ -86,6 +59,35 @@ struct CreateWorkoutMainView: View {
|
||||
}
|
||||
.listRowSeparator(.hidden)
|
||||
}
|
||||
|
||||
HStack {
|
||||
Button("Add Superset", action: {
|
||||
viewModel.addNewSuperset()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.blue)
|
||||
.background(.yellow)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
|
||||
Divider()
|
||||
|
||||
Button("Done", action: {
|
||||
viewModel.uploadWorkout()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.white)
|
||||
.background(.blue)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
.disabled(viewModel.title.isEmpty)
|
||||
}
|
||||
.frame(height: 44)
|
||||
.padding(.bottom)
|
||||
}
|
||||
.sheet(isPresented: $showAddExercise) {
|
||||
AddExerciseView(selectedWorkout: { exercise in
|
||||
|
||||
@@ -22,26 +22,25 @@ struct CreateWorkoutSupersetActionsView: View {
|
||||
Text("Add exercise")
|
||||
.padding()
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.foregroundColor(.white)
|
||||
.background(.green)
|
||||
.cornerRadius(10)
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.buttonStyle(BorderlessButtonStyle())
|
||||
|
||||
// Button(action: {
|
||||
// if let selectedCreateWorkoutSuperSet = selectedCreateWorkoutSuperSet {
|
||||
// viewModel.delete(superset: selectedCreateWorkoutSuperSet)
|
||||
// viewModel.objectWillChange.send()
|
||||
// }
|
||||
// }) {
|
||||
// Text("Delete superset")
|
||||
// .padding()
|
||||
// }
|
||||
// .frame(maxWidth: .infinity)
|
||||
// .foregroundColor(.white)
|
||||
// .background(.red)
|
||||
// .cornerRadius(10)
|
||||
// .frame(maxWidth: .infinity, alignment: .center)
|
||||
Button(action: {
|
||||
viewModel.delete(superset: workoutSuperSet)
|
||||
viewModel.objectWillChange.send()
|
||||
|
||||
}) {
|
||||
Text("Delete superset")
|
||||
.padding()
|
||||
}
|
||||
.foregroundColor(.white)
|
||||
.background(.red)
|
||||
.cornerRadius(10)
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.buttonStyle(BorderlessButtonStyle())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
73
Werkout_ios/Views/Login/LoginView.swift
Normal file
73
Werkout_ios/Views/Login/LoginView.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// LoginView.swift
|
||||
// Werkout_ios
|
||||
//
|
||||
// Created by Trey Tartt on 6/25/23.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct LoginView: View {
|
||||
@State var email: String = ""
|
||||
@State var password: String = ""
|
||||
@Environment(\.dismiss) var dismiss
|
||||
let completion: (() -> Void)
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Login")
|
||||
.font(.title)
|
||||
|
||||
TextField("Email", text: $email)
|
||||
.autocapitalization(.none)
|
||||
.frame(height: 55)
|
||||
.textFieldStyle(PlainTextFieldStyle())
|
||||
.padding([.horizontal], 4)
|
||||
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2)))
|
||||
.cornerRadius(8)
|
||||
|
||||
TextField("Password", text: $password)
|
||||
.autocapitalization(.none)
|
||||
.frame(height: 55)
|
||||
.textFieldStyle(PlainTextFieldStyle())
|
||||
.padding([.horizontal], 4)
|
||||
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2)))
|
||||
.cornerRadius(8)
|
||||
|
||||
Button("Login", action: {
|
||||
login()
|
||||
})
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.frame(height: 44)
|
||||
.foregroundColor(.blue)
|
||||
.background(.yellow)
|
||||
.cornerRadius(8)
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
func login() {
|
||||
let postData = [
|
||||
"email": email,
|
||||
"password": password
|
||||
]
|
||||
UserStore.shared.login(postData: postData, completion: { success in
|
||||
if success {
|
||||
completion()
|
||||
dismiss()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct LoginView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
LoginView(completion: {
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user