Skip to content

Commit

Permalink
♻️ Refactor login/tasks choice to be based on data available
Browse files Browse the repository at this point in the history
  • Loading branch information
phatblat committed Nov 10, 2020
1 parent 34ded05 commit 5300d34
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 26 deletions.
8 changes: 8 additions & 0 deletions RealmTaskTracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
1EEDB6F50D54FBCB4F940AB6 /* Pods_RealmTaskTrackerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 907EC3DD01D524CB0E53AFD1 /* Pods_RealmTaskTrackerTests.framework */; };
C23183EA1465C3F46EE5F23A /* Pods_RealmTaskTracker_RealmTaskTrackerUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 691FDBE374D4822ADF4844BE /* Pods_RealmTaskTracker_RealmTaskTrackerUITests.framework */; };
C855642007EFF193A25ECFC6 /* Pods_RealmTaskTracker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0C4EACB8FCC6598BDBEA5CF8 /* Pods_RealmTaskTracker.framework */; };
F85DB7D0255A391600E621BB /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85DB7CF255A391500E621BB /* MainView.swift */; };
F85DB7DC255A3A2E00E621BB /* ActivityIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85DB7DB255A3A2E00E621BB /* ActivityIndicator.swift */; };
F86A0D942550F873003D3E60 /* LogoutButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86A0D932550F873003D3E60 /* LogoutButton.swift */; };
F887347E2544A44B002B5B31 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = F887347D2544A44B002B5B31 /* Constants.swift */; };
F8BC89A1251F9CB900566416 /* Project.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8BC89A0251F9CB900566416 /* Project.swift */; };
Expand Down Expand Up @@ -56,6 +58,8 @@
C39ABC7D2CA919D8E68C04EE /* Pods-RealmTaskTrackerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RealmTaskTrackerTests.debug.xcconfig"; path = "Target Support Files/Pods-RealmTaskTrackerTests/Pods-RealmTaskTrackerTests.debug.xcconfig"; sourceTree = "<group>"; };
F3A5DFEA6B870BD302B36665 /* Pods-RealmTaskTrackerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RealmTaskTrackerTests.release.xcconfig"; path = "Target Support Files/Pods-RealmTaskTrackerTests/Pods-RealmTaskTrackerTests.release.xcconfig"; sourceTree = "<group>"; };
F5AA5B0820CE66B16B48ED07 /* Pods-RealmTaskTracker.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RealmTaskTracker.release.xcconfig"; path = "Target Support Files/Pods-RealmTaskTracker/Pods-RealmTaskTracker.release.xcconfig"; sourceTree = "<group>"; };
F85DB7CF255A391500E621BB /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = "<group>"; };
F85DB7DB255A3A2E00E621BB /* ActivityIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityIndicator.swift; sourceTree = "<group>"; };
F86A0D932550F873003D3E60 /* LogoutButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoutButton.swift; sourceTree = "<group>"; };
F887347D2544A44B002B5B31 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
F8BC89A0251F9CB900566416 /* Project.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Project.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -208,10 +212,12 @@
F8E141562511A64A003B4289 /* Views */ = {
isa = PBXGroup;
children = (
F85DB7DB255A3A2E00E621BB /* ActivityIndicator.swift */,
F8E1419B2512BFC3003B4289 /* AddTaskView.swift */,
F8E141622511A8AA003B4289 /* LoadingView.swift */,
F8E140CD2511935F003B4289 /* LoginView.swift */,
F86A0D932550F873003D3E60 /* LogoutButton.swift */,
F85DB7CF255A391500E621BB /* MainView.swift */,
F8E141A6251312CC003B4289 /* TaskRow.swift */,
F8E1416A2511C537003B4289 /* TasksView.swift */,
F8E141932512B296003B4289 /* TextAlert.swift */,
Expand Down Expand Up @@ -475,6 +481,8 @@
buildActionMask = 2147483647;
files = (
F8E140CE2511935F003B4289 /* LoginView.swift in Sources */,
F85DB7DC255A3A2E00E621BB /* ActivityIndicator.swift in Sources */,
F85DB7D0255A391600E621BB /* MainView.swift in Sources */,
F8E141632511A8AA003B4289 /* LoadingView.swift in Sources */,
F8E140CC2511935F003B4289 /* App.swift in Sources */,
F8E141A7251312CC003B4289 /* TaskRow.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion RealmTaskTracker/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct App: SwiftUI.App {
var body: some Scene {
WindowGroup {
LoginView()
MainView()
.environmentObject(AppState())
}
}
Expand Down
9 changes: 6 additions & 3 deletions RealmTaskTracker/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ extension AppState {
let emailPassAuth = app.emailPasswordAuth
emailPassAuth.registerUser(email: username, password: password) { (error: Error?) in
guard error == nil else {
print("Signup failed: \(error!)")
completionHandler(.failure(error!))
return
DispatchQueue.main.async {
self.shouldIndicateActivity = false
}
print("Signup failed: \(error!)")
completionHandler(.failure(error!))
return
}

print("Signup successful!")
Expand Down
27 changes: 27 additions & 0 deletions RealmTaskTracker/Views/ActivityIndicator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ActivityIndicator.swift
// RealmTaskTracker
//
// Created by Ben Chatelain on 11/9/20.
//

import SwiftUI

// MARK: UIActivityIndicatorView Wrapper

/// Simple activity indicator to telegraph that the app is active in the background.
struct ActivityIndicator: UIViewRepresentable {
func makeUIView(context: Context) -> some UIView {
return UIActivityIndicatorView(style: .large)
}

func updateUIView(_ uiView: UIViewType, context: Context) {
(uiView as! UIActivityIndicatorView).startAnimating()
}
}

struct ActivityIndicator_Previews: PreviewProvider {
static var previews: some View {
ActivityIndicator()
}
}
43 changes: 22 additions & 21 deletions RealmTaskTracker/Views/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,25 @@ struct LoginView: View {
@State private var message = "Version: \(Constants.appVersion)"

var body: some View {
NavigationView {
LoadingView(isShowing: $state.shouldIndicateActivity) {
VStack {
if let error = error {
Text("Error: \(error.localizedDescription)")
}
Text("Please enter a username and password.")
.padding()
Form {
TextField("Username", text: $username)
SecureField("Password", text: $password)
Button("Sign In", action: signIn)
.disabled(state.shouldIndicateActivity)
Button("Sign Up", action: signUp)
.disabled(state.shouldIndicateActivity)
}
NavigationLink(destination: TasksView(), isActive: $signedIn) { EmptyView() }
Text(message)
}
VStack {
if let error = error {
Text("Error: \(error.localizedDescription)")
}
.navigationBarTitle("Login")

Text("Please enter a username and password.")
.padding()

Form {
TextField("Username", text: $username)
SecureField("Password", text: $password)

Button("Sign In", action: signIn)
.disabled(state.shouldIndicateActivity)

Button("Sign Up", action: signUp)
.disabled(state.shouldIndicateActivity)
}
Text(message)
}
}
}
Expand All @@ -49,7 +47,9 @@ extension LoginView {
state.signUp(username: username, password: password) { result in
switch result {
case .failure(let error):
self.error = error
message = "Signup failed: \(error.localizedDescription)"
print(message)
case .success():
print("Signup successful!")
signedIn = true
Expand All @@ -61,8 +61,9 @@ extension LoginView {
state.signIn(username: username, password: password) { result in
switch result {
case .failure(let error):
print("Login failed: \(error)")
self.error = error
message = "Login failed: \(error.localizedDescription)"
print(message)
case .success(_):
print("Login succeeded!")
signedIn = true
Expand Down
38 changes: 38 additions & 0 deletions RealmTaskTracker/Views/MainView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MainView.swift
// RealmTaskTracker
//
// Created by Ben Chatelain on 11/9/20.
//

import SwiftUI

// MARK: Main View
/// The main screen that determines whether to present the LoginView or the TasksView for the one group in the realm.
struct MainView: View {
/// The state of this application.
// @ObservedObject var state = AppState()
@EnvironmentObject var state: AppState

var body: some View {
NavigationView {
ZStack {
// If a realm is open for a logged in user, show the TasksView, otherwise show the LoginView
if let tasks = state.tasks {
TasksView(tasks: tasks)
.navigationBarTitle("Tasks")
.disabled(state.shouldIndicateActivity)
} else {
LoginView()
.navigationBarTitle("Login")
}

// If the app is doing work in the background,
// overlay an ActivityIndicator
if state.shouldIndicateActivity {
ActivityIndicator()
}
}
}
}
}
2 changes: 1 addition & 1 deletion RealmTaskTracker/Views/TasksView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct TasksView: View {
@EnvironmentObject var state: AppState

/// All tasks.
// @ObservedObject var tasks: RealmSwift.List<Task>
@ObservedObject var tasks: RealmSwift.List<Task>

@State private var showingLogoutAlert = false
@State private var showingActionSheet = false
Expand Down

0 comments on commit 5300d34

Please sign in to comment.