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-03 14:09:22 +01:00
|
|
|
NavigationLink(destination:
|
|
|
|
DetailsView(statusData: item)
|
|
|
|
.environmentObject(applicationState)) {
|
|
|
|
ImageRow(attachments: item.attachments())
|
2022-12-30 18:20:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 08:11:38 +01:00
|
|
|
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(CircularProgressViewStyle())
|
|
|
|
.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 {
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(CircularProgressViewStyle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.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()
|
|
|
|
}
|
|
|
|
}
|