Impressia/Vernissage/Views/StatusesView.swift

209 lines
7.3 KiB
Swift
Raw Normal View History

2023-01-21 18:01:17 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
2023-01-23 08:43:04 +01:00
import MastodonKit
2023-01-21 18:01:17 +01:00
2023-01-23 08:43:04 +01:00
struct StatusesView: View {
2023-01-23 18:01:27 +01:00
public enum ListType: Hashable {
2023-01-23 08:43:04 +01:00
case local
case federated
case favourites
case bookmarks
2023-01-23 18:01:27 +01:00
case hashtag(tag: String)
2023-01-23 08:43:04 +01:00
}
2023-01-21 18:01:17 +01:00
@EnvironmentObject private var applicationState: ApplicationState
2023-01-23 18:01:27 +01:00
@EnvironmentObject private var routerPath: RouterPath
2023-01-23 08:43:04 +01:00
@State public var listType: ListType
2023-01-21 18:01:17 +01:00
@State private var allItemsLoaded = false
@State private var firstLoadFinished = false
@State private var statusViewModels: [StatusViewModel] = []
private let defaultLimit = 20
var body: some View {
ScrollView {
VStack(alignment: .center) {
if firstLoadFinished == true {
ForEach(self.statusViewModels, id: \.uniqueId) { item in
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.status(
id: item.id,
blurhash: item.mediaAttachments.first?.blurhash,
metaImageWidth: item.getImageWidth(),
metaImageHeight: item.getImageHeight())
) {
ImageRowAsync(statusViewModel: item)
}
.buttonStyle(EmptyButtonStyle())
2023-01-21 18:01:17 +01:00
}
LazyVStack {
if allItemsLoaded == false && firstLoadFinished == true {
HStack {
Spacer()
LoadingIndicator()
.task {
do {
try await self.loadMoreStatuses()
} catch {
ErrorService.shared.handle(error, message: "Loading more statuses failed.", showToastr: true)
}
}
Spacer()
}
}
}
}
}
}
2023-01-23 08:43:04 +01:00
.navigationBarTitle(self.getTitle())
2023-01-21 18:01:17 +01:00
.overlay(alignment: .center) {
if firstLoadFinished == false {
LoadingIndicator()
} else {
if self.statusViewModels.isEmpty {
VStack {
Image(systemName: "photo.on.rectangle.angled")
.font(.largeTitle)
.padding(.bottom, 4)
Text("Unfortunately, there are no photos here.")
.font(.title3)
}.foregroundColor(.lightGrayColor)
}
}
}
.task {
do {
try await self.loadStatuses()
} catch {
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
}
}.refreshable {
do {
try await self.loadTopStatuses()
} catch {
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
}
}
}
private func loadStatuses() async throws {
guard firstLoadFinished == false else {
return
}
2023-01-23 08:43:04 +01:00
let statuses = try await self.loadFromApi()
2023-01-21 18:01:17 +01:00
var inPlaceStatuses: [StatusViewModel] = []
2023-01-23 11:42:28 +01:00
for item in statuses.getStatusesWithImagesOnly() {
2023-01-21 18:01:17 +01:00
inPlaceStatuses.append(StatusViewModel(status: item))
}
self.firstLoadFinished = true
self.statusViewModels.append(contentsOf: inPlaceStatuses)
if statuses.count < self.defaultLimit {
self.allItemsLoaded = true
}
}
private func loadMoreStatuses() async throws {
if let lastStatusId = self.statusViewModels.last?.id {
2023-01-23 08:43:04 +01:00
let previousStatuses = try await self.loadFromApi(maxId: lastStatusId)
2023-01-21 18:01:17 +01:00
if previousStatuses.count < self.defaultLimit {
self.allItemsLoaded = true
}
var inPlaceStatuses: [StatusViewModel] = []
2023-01-23 11:42:28 +01:00
for item in previousStatuses.getStatusesWithImagesOnly() {
2023-01-21 18:01:17 +01:00
inPlaceStatuses.append(StatusViewModel(status: item))
}
self.statusViewModels.append(contentsOf: inPlaceStatuses)
}
}
private func loadTopStatuses() async throws {
if let firstStatusId = self.statusViewModels.first?.id {
2023-01-23 08:43:04 +01:00
let newestStatuses = try await self.loadFromApi(sinceId: firstStatusId)
2023-01-21 18:01:17 +01:00
var inPlaceStatuses: [StatusViewModel] = []
2023-01-23 11:42:28 +01:00
for item in newestStatuses.getStatusesWithImagesOnly() {
2023-01-21 18:01:17 +01:00
inPlaceStatuses.append(StatusViewModel(status: item))
}
self.statusViewModels.insert(contentsOf: inPlaceStatuses, at: 0)
}
}
2023-01-23 08:43:04 +01:00
private func loadFromApi(maxId: String? = nil, sinceId: String? = nil, minId: String? = nil) async throws -> [Status] {
switch self.listType {
case .local:
return try await PublicTimelineService.shared.getStatuses(
accountData: self.applicationState.accountData,
local: true,
remote: false,
maxId: maxId,
sinceId: sinceId,
minId: minId,
limit: self.defaultLimit)
case .federated:
return try await PublicTimelineService.shared.getStatuses(
accountData: self.applicationState.accountData,
local: false,
remote: true,
maxId: maxId,
sinceId: sinceId,
minId: minId,
limit: self.defaultLimit)
case .favourites:
return try await AccountService.shared.favourites(
accountData: self.applicationState.accountData,
maxId: maxId,
sinceId: sinceId,
minId: minId,
limit: self.defaultLimit)
case .bookmarks:
return try await AccountService.shared.bookmarks(
accountData: self.applicationState.accountData,
maxId: maxId,
sinceId: sinceId,
minId: minId,
limit: self.defaultLimit)
2023-01-23 18:01:27 +01:00
case .hashtag(let tag):
return try await PublicTimelineService.shared.getTagStatuses(
accountData: self.applicationState.accountData,
tag: tag,
local: false,
remote: true,
maxId: maxId,
sinceId: sinceId,
minId: minId,
limit: self.defaultLimit)
2023-01-23 08:43:04 +01:00
}
}
private func getTitle() -> String {
switch self.listType {
case .local:
return "Local"
case .federated:
return "Federeted"
case .favourites:
return "Favourites"
case .bookmarks:
return "Bookmarks"
2023-01-23 18:01:27 +01:00
case .hashtag(let tag):
return "#\(tag)"
2023-01-23 08:43:04 +01:00
}
}
2023-01-21 18:01:17 +01:00
}