Impressia/Vernissage/Views/HomeFeedView.swift

109 lines
4.0 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
2023-01-11 13:16:43 +01:00
@State private var firstLoadFinished = false
@State private var allItemsBottomLoaded = false
2022-12-30 18:20:54 +01:00
private static let initialColumns = 1
@State private var gridColumns = Array(repeating: GridItem(.flexible()), count: initialColumns)
2023-01-11 13:16:43 +01:00
@FetchRequest var dbStatuses: FetchedResults<StatusData>
2022-12-30 18:20:54 +01:00
2023-01-11 13:16:43 +01:00
init(accountId: String) {
_dbStatuses = FetchRequest<StatusData>(
sortDescriptors: [SortDescriptor(\.id, order: .reverse)],
predicate: NSPredicate(format: "pixelfedAccount.id = %@", accountId))
}
2023-01-01 18:13:36 +01:00
2022-12-30 18:20:54 +01:00
var body: some View {
2023-01-11 13:16:43 +01:00
ScrollView {
LazyVGrid(columns: gridColumns) {
ForEach(dbStatuses, id: \.self) { item in
NavigationLink(destination: StatusView(statusId: item.id,
imageBlurhash: item.attachments().first?.blurhash,
imageWidth: item.attachments().first?.metaImageWidth,
imageHeight: item.attachments().first?.metaImageHeight)
.environmentObject(applicationState)) {
ImageRow(status: item)
2022-12-30 18:20:54 +01:00
}
2023-01-11 13:16:43 +01:00
.buttonStyle(EmptyButtonStyle())
}
if allItemsBottomLoaded == false && firstLoadFinished == true {
2023-01-06 13:05:21 +01:00
LoadingIndicator()
2023-01-11 13:16:43 +01:00
.task {
do {
if let accountData = self.applicationState.accountData {
let newStatusesCount = try await TimelineService.shared.onBottomOfList(for: accountData)
if newStatusesCount == 0 {
allItemsBottomLoaded = true
2023-01-03 14:09:22 +01:00
}
2023-01-02 08:11:38 +01:00
}
2023-01-11 13:16:43 +01:00
} catch {
print("Error", error)
2023-01-02 08:11:38 +01:00
}
}
2022-12-30 18:20:54 +01:00
}
}
2023-01-11 13:16:43 +01:00
}
.overlay(alignment: .center) {
if firstLoadFinished == false {
2023-01-06 13:05:21 +01:00
LoadingIndicator()
2023-01-11 13:16:43 +01:00
} else {
if self.dbStatuses.isEmpty {
VStack {
Image(systemName: "photo.on.rectangle.angled")
.font(.largeTitle)
.padding(.bottom, 4)
Text("Unfortunately, there are no photos here.")
.font(.title3)
}.foregroundColor(.lightGrayColor)
}
2022-12-30 18:20:54 +01:00
}
}
.refreshable {
do {
2023-01-03 14:09:22 +01:00
if let accountData = self.applicationState.accountData {
2023-01-11 13:16:43 +01:00
_ = try await TimelineService.shared.onTopOfList(for: accountData)
2023-01-03 14:09:22 +01:00
}
2022-12-30 18:20:54 +01:00
} catch {
print("Error", error)
}
}
.task {
do {
2023-01-11 13:16:43 +01:00
defer {
Task { @MainActor in
self.firstLoadFinished = true
2023-01-03 14:09:22 +01:00
}
2023-01-11 13:16:43 +01:00
}
if self.dbStatuses.isEmpty == false {
return
}
if let accountData = self.applicationState.accountData {
_ = try await TimelineService.shared.onTopOfList(for: accountData)
2023-01-01 18:13:36 +01:00
}
2022-12-30 18:20:54 +01:00
} catch {
print("Error", error)
}
}
}
}
struct HomeFeedView_Previews: PreviewProvider {
static var previews: some View {
2023-01-11 13:16:43 +01:00
HomeFeedView(accountId: "")
2022-12-30 18:20:54 +01:00
}
}