Skip to content

Commit

Permalink
Merge pull request #522 from pennlabs/anli/tweak-home-error
Browse files Browse the repository at this point in the history
Tweak home page error screen
  • Loading branch information
anli5005 authored Mar 28, 2024
2 parents 02e0712 + 25a582d commit 53995cc
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions PennMobile/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

import SwiftUI

extension Optional {
mutating func makeNilIfError<Success, Failure: Error>() where Wrapped == Result<Success, Failure> {
if case .some(.failure) = self {
self = nil
}
}
}

struct HomeViewData {
var firstName: String?

Expand All @@ -32,11 +40,22 @@ struct HomeViewData {
switch result {
case .some(.success(let item)):
content(item)
case .some(.failure):
case .some(.failure(let error)):
HomeCardView {
Text("Couldn't load \(description)")
.padding()
VStack {
Image(systemName: "pc")
.font(.system(size: 60))
.symbolRenderingMode(.multicolor)
.padding(.bottom, 4)
Text("Couldn't load \(description) :(")
.fontWeight(.bold)
Text(error.localizedDescription)
.font(.caption)
}
.padding(.horizontal)
.padding(.vertical, 24)
}
.multilineTextAlignment(.center)
case nil:
SwiftUI.EmptyView()
}
Expand Down Expand Up @@ -67,7 +86,7 @@ struct HomeViewData {
}
}

sectionContent(newsArticles, description: "article") { articles in
sectionContent(newsArticles, description: "news article") { articles in
ForEach(articles) { article in
NewsCardView(article: article)
}
Expand Down Expand Up @@ -145,6 +164,9 @@ struct HomeViewData {
print("Fetching HomeViewModel (force = \(force), isFetching = \(isFetching))")

data.firstName = account?.firstName
data.polls.makeNilIfError()
data.posts.makeNilIfError()
data.newsArticles.makeNilIfError()

async let pollsTask = Task {
let polls = await PollsNetworkManager.instance.getActivePolls().mapError { $0 as Error }
Expand Down

0 comments on commit 53995cc

Please sign in to comment.