Skip to content

Commit

Permalink
Merge pull request #513 from pennlabs/anli/03-16-rearchitect-homeview…
Browse files Browse the repository at this point in the history
…data

Rearchitect HomeViewData
  • Loading branch information
anli5005 authored Mar 22, 2024
2 parents 7bf0414 + f2a096e commit 740b1da
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 139 deletions.
125 changes: 45 additions & 80 deletions PennMobile/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,98 +24,63 @@ struct HomeView<Model: HomeViewModel>: View {

var body: some View {
Group {
switch viewModel.data {
case .none:
ProgressView()
.controlSize(.large)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
case .some(.success(let data)):
NavigationStack {
ScrollView {
TimelineView(.periodic(from: Date.midnightYesterday, by: 24 * 60 * 60)) { context in
VStack(spacing: 0) {
VStack {
Text("\(context.date, format: dateFormatStyle)")
.font(.largeTitle)
.fontWeight(.bold)
.background(GeometryReader { geometry in
let minY = geometry.frame(in: .global).minY
Color.clear.onChange(of: minY) { minY in
showTitle = minY <= 16
}
})

if let splashText {
HStack(alignment: .top) {
Text(splashText)
.fontWeight(.medium)
.opacity(0.7)
NavigationStack {
ScrollView {
TimelineView(.periodic(from: Date.midnightYesterday, by: 24 * 60 * 60)) { context in
VStack(spacing: 0) {
VStack {
Text("\(context.date, format: dateFormatStyle)")
.font(.largeTitle)
.fontWeight(.bold)
.background(GeometryReader { geometry in
let minY = geometry.frame(in: .global).minY
Color.clear.onChange(of: minY) { minY in
showTitle = minY <= 16
}
})

if let splashText {
HStack(alignment: .top) {
Text(splashText)
.fontWeight(.medium)
.opacity(0.7)
}
}
.offset(y: -16)
.padding(.bottom)
.multilineTextAlignment(.center)

data.content(for: context.date)
.frame(maxWidth: 480)
.frame(maxWidth: .infinity)
}
.offset(y: -16)
.padding(.bottom)
// Hack for forcing the navbar to always render
.navigationTitle(Text(showTitle ? "\(context.date, format: dateFormatStyle)" : "\u{200C}"))
.navigationBarTitleDisplayMode(.inline)
#if DEBUG
.toolbar {
if (!(viewModel is MockHomeViewModel)) {
ToolbarItem(placement: .primaryAction) {
NavigationLink("Debug") {
HomeView<MockHomeViewModel>()
}
.multilineTextAlignment(.center)

viewModel.data.content(for: context.date)
.frame(maxWidth: 480)
.frame(maxWidth: .infinity)
}
.padding(.bottom)
// Hack for forcing the navbar to always render
.navigationTitle(Text(showTitle ? "\(context.date, format: dateFormatStyle)" : "\u{200C}"))
.navigationBarTitleDisplayMode(.inline)
#if DEBUG
.toolbar {
if (!(viewModel is MockHomeViewModel)) {
ToolbarItem(placement: .primaryAction) {
NavigationLink("Debug") {
HomeView<MockHomeViewModel>()
}
}
}
#endif
.onAppear {
chooseSplashText(data: data, for: context.date)
}
.onChange(of: context.date) { date in
chooseSplashText(data: data, for: date)
}
}
}
.refreshable {
try? await viewModel.fetchData(force: true)
}
}
case .some(.failure(let error)):
VStack(spacing: 16) {
Image(systemName: "exclamationmark.triangle.fill")
.resizable()
.scaledToFit()
.frame(height: 50)
.foregroundStyle(.red)

Text("Something went wrong.")
.font(.headline)
.fontWeight(.medium)

Text(error.localizedDescription)
.font(.caption)
.frame(maxWidth: 400)

Button("Retry") {
Task {
viewModel.clearData()
try? await viewModel.fetchData(force: true)
#endif
.onAppear {
chooseSplashText(data: viewModel.data, for: context.date)
}
.onChange(of: context.date) { date in
chooseSplashText(data: viewModel.data, for: date)
}
}
.buttonStyle(BorderedProminentButtonStyle())
}
.multilineTextAlignment(.center)
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.refreshable {
try? await viewModel.fetchData(force: true)
}
}
}.onAppear {
Task {
Expand Down
Loading

0 comments on commit 740b1da

Please sign in to comment.