Vernissage/Vernissage/Views/PaginableStatusesView.swift

194 lines
6.2 KiB
Swift
Raw Permalink Normal View History

2023-02-01 20:01:18 +01:00
//
// https://mczachurski.dev
2023-04-09 20:51:33 +02:00
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-02-01 20:01:18 +01:00
//
import SwiftUI
import Nuke
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-02-01 20:01:18 +01:00
2023-10-19 13:24:02 +02:00
@MainActor
2023-02-01 20:01:18 +01:00
struct PaginableStatusesView: View {
public enum ListType: Hashable {
case favourites
case bookmarks
2023-04-20 15:03:43 +02:00
public var title: LocalizedStringKey {
switch self {
case .favourites:
return "statuses.navigationBar.favourites"
case .bookmarks:
return "statuses.navigationBar.bookmarks"
}
}
2023-02-01 20:01:18 +01:00
}
2023-10-19 13:24:02 +02:00
@Environment(ApplicationState.self) var applicationState
@Environment(Client.self) var client
@Environment(RouterPath.self) var routerPath
2023-02-01 20:01:18 +01:00
@State public var listType: ListType
@State private var allItemsLoaded = false
@State private var statusViewModels: [StatusModel] = []
@State private var state: ViewState = .loading
@State private var page = 1
2023-05-26 16:06:38 +02:00
// Gallery parameters.
@State private var imageColumns = 3
@State private var containerWidth: Double = UIScreen.main.bounds.width
@State private var containerHeight: Double = UIScreen.main.bounds.height
2023-02-01 20:01:18 +01:00
private let defaultLimit = 10
private let imagePrefetcher = ImagePrefetcher(destination: .diskCache)
2023-02-01 20:01:18 +01:00
var body: some View {
self.mainBody()
2023-04-20 15:03:43 +02:00
.navigationTitle(self.listType.title)
2023-02-01 20:01:18 +01:00
}
2023-02-01 20:01:18 +01:00
@ViewBuilder
private func mainBody() -> some View {
switch state {
case .loading:
LoadingIndicator()
.task {
await self.loadData()
}
case .loaded:
if self.statusViewModels.isEmpty {
2023-03-13 13:53:36 +01:00
NoDataView(imageSystemName: "photo.on.rectangle.angled", text: "statuses.title.noPhotos")
2023-02-01 20:01:18 +01:00
} else {
self.list()
2023-02-01 20:01:18 +01:00
}
case .error(let error):
ErrorView(error: error) {
self.state = .loading
self.page = 1
self.allItemsLoaded = false
await self.loadData()
}
.padding()
}
}
@ViewBuilder
private func list() -> some View {
ScrollView {
2023-05-26 16:06:38 +02:00
if self.imageColumns > 1 {
2023-10-08 11:35:45 +02:00
WaterfallGrid($statusViewModels, refreshId: Binding.constant(""), columns: $imageColumns, hideLoadMore: $allItemsLoaded) { item in
2023-05-26 16:06:38 +02:00
ImageRowAsync(statusViewModel: item, containerWidth: $containerWidth)
} onLoadMore: {
do {
try await self.loadMoreStatuses()
} catch {
ErrorService.shared.handle(error, message: "statuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
}
}
2023-05-26 16:06:38 +02:00
} else {
LazyVStack(alignment: .center) {
ForEach(self.statusViewModels, id: \.id) { item in
ImageRowAsync(statusViewModel: item, containerWidth: $containerWidth)
}
2023-05-26 16:06:38 +02:00
if allItemsLoaded == false {
HStack {
Spacer()
LoadingIndicator()
.task {
do {
try await self.loadMoreStatuses()
} catch {
ErrorService.shared.handle(error, message: "statuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
}
}
2023-05-26 16:06:38 +02:00
Spacer()
}
}
}
}
}
2023-05-26 16:06:38 +02:00
.gallery { galleryProperties in
self.imageColumns = galleryProperties.imageColumns
self.containerWidth = galleryProperties.containerWidth
self.containerHeight = galleryProperties.containerHeight
}
}
2023-02-01 20:01:18 +01:00
private func loadData() async {
do {
try await self.loadStatuses()
withAnimation {
self.state = .loaded
}
2023-02-01 20:01:18 +01:00
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "statuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
2023-02-01 20:01:18 +01:00
self.state = .error(error)
}
}
2023-02-01 20:01:18 +01:00
private func loadStatuses() async throws {
let statuses = try await self.loadFromApi()
2023-02-01 20:01:18 +01:00
if statuses.isEmpty {
self.allItemsLoaded = true
return
}
2023-02-01 20:01:18 +01:00
// TODO: It seems that paging is not supported and we cannot download additiona data.
self.allItemsLoaded = true
2023-02-01 20:01:18 +01:00
var inPlaceStatuses: [StatusModel] = []
for item in statuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
}
// Prefetch images.
self.prefetch(statusModels: inPlaceStatuses)
2023-02-01 20:01:18 +01:00
self.statusViewModels.append(contentsOf: inPlaceStatuses)
}
2023-02-01 20:01:18 +01:00
private func loadMoreStatuses() async throws {
self.page = self.page + 1
2023-02-01 20:01:18 +01:00
let previousStatuses = try await self.loadFromApi()
if previousStatuses.isEmpty {
self.allItemsLoaded = true
return
}
2023-02-01 20:01:18 +01:00
var inPlaceStatuses: [StatusModel] = []
for item in previousStatuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
}
// Prefetch images.
self.prefetch(statusModels: inPlaceStatuses)
2023-02-01 20:01:18 +01:00
self.statusViewModels.append(contentsOf: inPlaceStatuses)
}
2023-02-01 20:01:18 +01:00
private func loadFromApi() async throws -> [Status] {
switch self.listType {
case .favourites:
2023-02-03 15:16:30 +01:00
return try await self.client.accounts?.favourites(limit: self.defaultLimit, page: self.page) ?? []
2023-02-01 20:01:18 +01:00
case .bookmarks:
2023-02-03 15:16:30 +01:00
return try await self.client.accounts?.bookmarks(limit: self.defaultLimit, page: self.page) ?? []
2023-02-01 20:01:18 +01:00
}
}
private func prefetch(statusModels: [StatusModel]) {
imagePrefetcher.startPrefetching(with: statusModels.getAllImagesUrls())
}
2023-02-01 20:01:18 +01:00
}