Impressia/Vernissage/Views/HomeFeedView.swift

81 lines
2.7 KiB
Swift
Raw Normal View History

2022-12-30 18:20:54 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
struct HomeFeedView: View {
2023-01-01 18:13:36 +01:00
@Environment(\.managedObjectContext) private var viewContext
2022-12-31 16:31:05 +01:00
@EnvironmentObject var applicationState: ApplicationState
2022-12-30 18:20:54 +01:00
@State private var showLoading = false
private static let initialColumns = 1
@State private var gridColumns = Array(repeating: GridItem(.flexible()), count: initialColumns)
2023-01-01 18:13:36 +01:00
@FetchRequest(sortDescriptors: [SortDescriptor(\.id, order: .reverse)]) var dbStatuses: FetchedResults<StatusData>
2022-12-30 18:20:54 +01:00
var body: some View {
ZStack {
ScrollView {
LazyVGrid(columns: gridColumns) {
2023-01-02 19:12:25 +01:00
ForEach(dbStatuses, id: \.self) { item in
2023-01-06 13:05:21 +01:00
NavigationLink(destination: StatusView(statusId: item.id)
2023-01-05 11:55:20 +01:00
.environmentObject(applicationState)) {
2023-01-03 14:09:22 +01:00
ImageRow(attachments: item.attachments())
2022-12-30 18:20:54 +01:00
}
}
2023-01-02 08:11:38 +01:00
2023-01-06 13:05:21 +01:00
LoadingIndicator()
2023-01-02 08:11:38 +01:00
.onAppear {
Task {
do {
2023-01-03 14:09:22 +01:00
if let accountData = self.applicationState.accountData {
try await TimelineService.shared.onBottomOfList(for: accountData)
}
2023-01-02 08:11:38 +01:00
} catch {
print("Error", error)
}
}
}
2022-12-30 18:20:54 +01:00
}
}
if showLoading {
2023-01-06 13:05:21 +01:00
LoadingIndicator()
2022-12-30 18:20:54 +01:00
}
}
.refreshable {
do {
2023-01-03 14:09:22 +01:00
if let accountData = self.applicationState.accountData {
try await TimelineService.shared.onTopOfList(for: accountData)
}
2022-12-30 18:20:54 +01:00
} catch {
print("Error", error)
}
}
.task {
do {
2023-01-01 18:13:36 +01:00
if self.dbStatuses.isEmpty {
self.showLoading = true
2023-01-03 14:09:22 +01:00
if let accountData = self.applicationState.accountData {
try await TimelineService.shared.onTopOfList(for: accountData)
}
2023-01-01 18:13:36 +01:00
self.showLoading = false
}
2022-12-30 18:20:54 +01:00
} catch {
2022-12-31 16:31:05 +01:00
self.showLoading = false
2022-12-30 18:20:54 +01:00
print("Error", error)
}
}
}
}
struct HomeFeedView_Previews: PreviewProvider {
static var previews: some View {
HomeFeedView()
}
}