Skip to content

Commit f67fb5c

Browse files
committed
Handled no network error case in workspaces screen and implemented retry to load the workspaces
1 parent fae47f4 commit f67fb5c

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

GoInfoGame/GoInfoGame/Network/APIError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum APIError: LocalizedError {
1414
case noData
1515
case decodingFailed(String)
1616
case custom(String)
17+
case noNetworkConnection
1718

1819
// HTTP/Server-side errors
1920
case badRequest
@@ -62,6 +63,8 @@ enum APIError: LocalizedError {
6263
return "Unexpected error: \(description)"
6364
case .custom(let message):
6465
return message
66+
case .noNetworkConnection:
67+
return "No network connection."
6568
}
6669
}
6770
}

GoInfoGame/GoInfoGame/Network/ApiManager.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ class ApiManager {
131131
completion(.failure(APIError.custom("No data returned")))
132132
return
133133
}
134-
if let error = error {
134+
if let error = error as? NSError {
135+
if error.code == -1009 {
136+
completion(.failure(.noNetworkConnection))
137+
return
138+
}
135139
print("Request failed with error: \(error.localizedDescription)")
136140
completion(.failure(APIError.custom("Request failed with error: \(error.localizedDescription)")))
137141
return

GoInfoGame/GoInfoGame/UI/InitialView/InitialView.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,40 @@ struct WorkspacesListView: View {
105105
}
106106
}
107107
} else if viewModel.workspaces == nil {
108-
VStack {
109-
Text("Loading workspaces available for you... Please make sure you have location service enabled.")
110-
.font(.custom("Lato-Bold", size: 20))
111-
.foregroundColor((Asset.Colors.huskyPurple.swiftUIColor))
112-
.multilineTextAlignment(.center)
113-
Spacer()
108+
if viewModel.errorMessage == nil {
109+
VStack {
110+
Text("Loading workspaces available for you... Please make sure you have location service enabled.")
111+
.font(.custom("Lato-Bold", size: 20))
112+
.foregroundColor((Asset.Colors.huskyPurple.swiftUIColor))
113+
.multilineTextAlignment(.center)
114+
Spacer()
115+
}
116+
} else {
117+
VStack {
118+
Text(viewModel.errorMessage ?? "Something went wrong. Please try again later.")
119+
.font(.custom("Lato-Bold", size: 20))
120+
.foregroundColor((Asset.Colors.huskyPurple.swiftUIColor))
121+
.multilineTextAlignment(.center)
122+
.padding(.top, 20)
123+
Spacer()
124+
Button {
125+
viewModel.fetchWorkspacesList()
126+
} label: {
127+
Group {
128+
VStack {
129+
Image(systemName: "arrow.clockwise")
130+
.resizable()
131+
.frame(width: 50, height: 50)
132+
Text("Try again")
133+
.font(.custom("Lato-Bold", size: 20))
134+
}
135+
}
136+
.foregroundStyle(Asset.Colors.accentPink.swiftUIColor)
137+
138+
139+
}
140+
Spacer()
141+
}
114142
}
115143
} else if viewModel.workspaces?.count == 0 {
116144
VStack {

GoInfoGame/GoInfoGame/UI/InitialView/InitialViewModel.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class InitialViewModel: ObservableObject {
1717
@Published var workspaces: [Workspace]? = nil
1818
@Published var longQuests: [LongFormElement] = []
1919
@Published var isLoading: Bool = false
20+
@Published var errorMessage: String? = nil
2021

2122
@Published var showBiometricIDError: Bool = false
2223
@Published var biometricIDErrorMessage: String?
@@ -44,8 +45,10 @@ class InitialViewModel: ObservableObject {
4445
switch result {
4546
case .success(let workspacesResponse):
4647
self?.workspaces = workspacesResponse
48+
self?.errorMessage = nil
4749
case .failure(let error):
48-
self?.workspaces = []
50+
self?.workspaces = nil
51+
self?.errorMessage = error.localizedDescription
4952
print("Error fetching workspaces: \(error)")
5053
}
5154
}

0 commit comments

Comments
 (0)