Impressia/Vernissage/Widgets/UserProfileStatuses.swift

107 lines
3.8 KiB
Swift
Raw Normal View History

2023-01-09 10:06:21 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
2023-01-10 08:04:25 +01:00
import MastodonKit
2023-01-09 10:06:21 +01:00
struct UserProfileStatuses: View {
@EnvironmentObject private var applicationState: ApplicationState
@State public var accountId: String
@State private var allItemsLoaded = false
@State private var firstLoadFinished = false
2023-01-10 20:38:02 +01:00
@State private var statusViewModels: [StatusViewModel] = []
private let defaultLimit = 20
2023-01-09 10:06:21 +01:00
var body: some View {
VStack(alignment: .center) {
2023-01-09 10:06:21 +01:00
if firstLoadFinished == true {
2023-01-11 13:16:43 +01:00
ForEach(self.statusViewModels, id: \.uniqueId) { item in
NavigationLink(destination: StatusView(statusId: item.id,
imageBlurhash: item.mediaAttachments.first?.blurhash,
imageWidth: item.getImageWidth(),
imageHeight: item.getImageHeight())
2023-01-09 10:06:21 +01:00
.environmentObject(applicationState)) {
2023-01-10 20:38:02 +01:00
ImageRowAsync(statusViewModel: item)
2023-01-09 10:06:21 +01:00
}
2023-01-09 14:48:41 +01:00
.buttonStyle(EmptyButtonStyle())
2023-01-09 10:06:21 +01:00
}
LazyVStack {
if allItemsLoaded == false && firstLoadFinished == true {
LoadingIndicator()
2023-01-11 13:16:43 +01:00
.task {
do {
try await self.loadMoreStatuses()
} catch {
print("Error \(error.localizedDescription)")
2023-01-09 10:06:21 +01:00
}
}
.frame(idealWidth: .infinity, maxWidth: .infinity, alignment: .center)
}
}
} else {
LoadingIndicator()
}
2023-01-11 13:16:43 +01:00
}.task {
do {
try await self.loadStatuses()
} catch {
print("Error \(error.localizedDescription)")
2023-01-09 10:06:21 +01:00
}
}
}
private func loadStatuses() async throws {
2023-01-10 20:38:02 +01:00
let statuses = try await AccountService.shared.getStatuses(
forAccountId: self.accountId,
andContext: self.applicationState.accountData,
limit: self.defaultLimit)
var inPlaceStatuses: [StatusViewModel] = []
for item in statuses {
inPlaceStatuses.append(StatusViewModel(status: item))
}
2023-01-09 10:06:21 +01:00
self.firstLoadFinished = true
2023-01-10 20:38:02 +01:00
self.statusViewModels.append(contentsOf: inPlaceStatuses)
2023-01-09 10:06:21 +01:00
2023-01-10 20:38:02 +01:00
if statuses.count < self.defaultLimit {
2023-01-09 10:06:21 +01:00
self.allItemsLoaded = true
}
}
private func loadMoreStatuses() async throws {
2023-01-10 20:38:02 +01:00
if let lastStatusId = self.statusViewModels.last?.id {
2023-01-09 10:06:21 +01:00
let previousStatuses = try await AccountService.shared.getStatuses(
forAccountId: self.accountId,
andContext: self.applicationState.accountData,
2023-01-10 20:38:02 +01:00
maxId: lastStatusId,
limit: self.defaultLimit)
2023-01-09 10:06:21 +01:00
2023-01-10 20:38:02 +01:00
if previousStatuses.count < self.defaultLimit {
2023-01-09 10:06:21 +01:00
self.allItemsLoaded = true
}
2023-01-10 20:38:02 +01:00
var inPlaceStatuses: [StatusViewModel] = []
for item in previousStatuses {
inPlaceStatuses.append(StatusViewModel(status: item))
}
self.statusViewModels.append(contentsOf: inPlaceStatuses)
2023-01-09 10:06:21 +01:00
}
}
}
struct UserProfileStatuses_Previews: PreviewProvider {
static var previews: some View {
UserProfileStatuses(accountId: "")
}
}