Impressia/Vernissage/Widgets/UserProfileStatuses.swift

94 lines
3.3 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
@State private var statuses: [Status] = []
var body: some View {
VStack(alignment: .center) {
2023-01-09 10:06:21 +01:00
if firstLoadFinished == true {
ForEach(self.statuses, id: \.id) { 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-09 14:48:41 +01:00
ImageRowAsync(status: 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()
.onAppear {
Task {
do {
try await self.loadMoreStatuses()
} catch {
print("Error \(error.localizedDescription)")
}
}
}
.frame(idealWidth: .infinity, maxWidth: .infinity, alignment: .center)
}
}
} else {
LoadingIndicator()
}
}.onAppear {
Task {
do {
try await self.loadStatuses()
} catch {
print("Error \(error.localizedDescription)")
}
}
}
}
private func loadStatuses() async throws {
self.statuses = try await AccountService.shared.getStatuses(forAccountId: self.accountId, andContext: self.applicationState.accountData)
self.firstLoadFinished = true
if self.statuses.count < 40 {
self.allItemsLoaded = true
}
}
private func loadMoreStatuses() async throws {
if let lastStatusId = self.statuses.last?.id {
let previousStatuses = try await AccountService.shared.getStatuses(
forAccountId: self.accountId,
andContext: self.applicationState.accountData,
maxId: lastStatusId)
if previousStatuses.count < 40 {
self.allItemsLoaded = true
}
self.statuses.append(contentsOf: previousStatuses)
}
}
}
struct UserProfileStatuses_Previews: PreviewProvider {
static var previews: some View {
UserProfileStatuses(accountId: "")
}
}