Impressia/Vernissage/Views/HomeFeedView.swift

106 lines
4.1 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
2023-01-23 18:01:27 +01:00
@EnvironmentObject var routerPath: RouterPath
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
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.status(
2023-01-26 15:10:47 +01:00
id: item.rebloggedStatusId ?? item.id,
2023-01-23 18:01:27 +01:00
blurhash: item.attachments().first?.blurhash,
2023-01-27 15:46:09 +01:00
highestImageUrl: item.attachments().getHighestImage()?.url,
2023-01-23 18:01:27 +01:00
metaImageWidth: item.attachments().first?.metaImageWidth,
metaImageHeight: item.attachments().first?.metaImageHeight)
) {
ImageRow(statusData: 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 {
2023-01-21 18:01:17 +01:00
let newStatusesCount = try await HomeTimelineService.shared.onBottomOfList(for: accountData)
2023-01-11 13:16:43 +01:00
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 {
2023-01-20 13:47:38 +01:00
ErrorService.shared.handle(error, message: "Error during download statuses from server.", showToastr: !Task.isCancelled)
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-21 18:01:17 +01:00
_ = try await HomeTimelineService.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 {
2023-01-21 18:01:17 +01:00
_ = try await HomeTimelineService.shared.onTopOfList(for: accountData)
2023-01-01 18:13:36 +01:00
}
2022-12-30 18:20:54 +01:00
} catch {
2023-01-20 13:47:38 +01:00
ErrorService.shared.handle(error, message: "Error during download statuses from server.", showToastr: !Task.isCancelled)
2022-12-30 18:20:54 +01:00
}
}
}
}