Impressia/Vernissage/Views/StatusesView.swift

266 lines
9.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-02-19 10:32:38 +01:00
import PixelfedKit
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-02-03 15:16:30 +01:00
@EnvironmentObject private var client: Client
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
2023-01-25 15:39:04 +01:00
@State private var tag: Tag?
@State private var statusViewModels: [StatusModel] = []
2023-02-01 18:40:28 +01:00
@State private var state: ViewState = .loading
2023-01-21 18:01:17 +01:00
private let defaultLimit = 20
var body: some View {
2023-02-01 18:40:28 +01:00
self.mainBody()
.navigationBarTitle(self.getTitle())
.toolbar {
// TODO: It seems like pixelfed is not supporting the endpoints.
// self.getTrailingToolbar()
}
}
@ViewBuilder
private func mainBody() -> some View {
switch state {
case .loading:
LoadingIndicator()
.task {
await self.loadData()
}
case .loaded:
if self.statusViewModels.isEmpty {
NoDataView(imageSystemName: "photo.on.rectangle.angled", text: "Unfortunately, there are no photos here.")
} else {
ScrollView {
2023-02-01 20:01:18 +01:00
LazyVStack(alignment: .center) {
ForEach(self.statusViewModels, id: \.id) { item in
NavigationLink(value: RouteurDestinations.status(
id: item.id,
blurhash: item.mediaAttachments.first?.blurhash,
highestImageUrl: item.mediaAttachments.getHighestImage()?.url,
metaImageWidth: item.getImageWidth(),
metaImageHeight: item.getImageHeight())
) {
ImageRowAsync(statusViewModel: item)
2023-02-01 18:40:28 +01:00
}
2023-02-01 20:01:18 +01:00
.buttonStyle(EmptyButtonStyle())
}
if allItemsLoaded == false {
HStack {
Spacer()
LoadingIndicator()
.task {
do {
try await self.loadMoreStatuses()
} catch {
ErrorService.shared.handle(error, message: "Loading more statuses failed.", showToastr: !Task.isCancelled)
2023-02-01 18:40:28 +01:00
}
2023-02-01 20:01:18 +01:00
}
Spacer()
2023-02-01 18:40:28 +01:00
}
2023-01-21 18:01:17 +01:00
}
}
}
2023-02-01 18:40:28 +01:00
.refreshable {
do {
try await self.loadTopStatuses()
} catch {
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
}
2023-01-21 18:01:17 +01:00
}
}
2023-02-01 18:40:28 +01:00
case .error(let error):
ErrorView(error: error) {
self.state = .loading
await self.loadData()
}
.padding()
2023-01-21 18:01:17 +01:00
}
2023-02-01 18:40:28 +01:00
}
private func loadData() async {
do {
try await self.loadStatuses()
2023-01-25 15:39:04 +01:00
2023-02-01 18:40:28 +01:00
if case .hashtag(let hashtag) = self.listType {
await self.loadTag(hashtag: hashtag)
2023-01-21 18:01:17 +01:00
}
2023-02-01 18:40:28 +01:00
self.state = .loaded
} catch {
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
self.state = .error(error)
2023-01-21 18:01:17 +01:00
}
}
2023-02-01 18:40:28 +01:00
2023-01-21 18:01:17 +01:00
private func loadStatuses() async throws {
2023-02-01 20:01:18 +01:00
let statuses = try await self.loadFromApi()
if statuses.isEmpty {
self.allItemsLoaded = true
2023-01-21 18:01:17 +01:00
return
}
2023-02-01 20:01:18 +01:00
var inPlaceStatuses: [StatusModel] = []
2023-01-23 11:42:28 +01:00
for item in statuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
2023-01-21 18:01:17 +01:00
}
self.statusViewModels.append(contentsOf: inPlaceStatuses)
}
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
2023-02-01 18:40:28 +01:00
if previousStatuses.isEmpty {
2023-01-21 18:01:17 +01:00
self.allItemsLoaded = true
2023-02-01 20:01:18 +01:00
return
2023-01-21 18:01:17 +01:00
}
var inPlaceStatuses: [StatusModel] = []
2023-01-23 11:42:28 +01:00
for item in previousStatuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
2023-01-21 18:01:17 +01:00
}
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: [StatusModel] = []
2023-01-23 11:42:28 +01:00
for item in newestStatuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
2023-01-21 18:01:17 +01:00
}
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:
2023-02-03 15:16:30 +01:00
return try await self.client.publicTimeline?.getStatuses(
2023-01-23 08:43:04 +01:00
local: true,
remote: false,
maxId: maxId,
sinceId: sinceId,
minId: minId,
2023-02-03 15:16:30 +01:00
limit: self.defaultLimit) ?? []
2023-01-23 08:43:04 +01:00
case .federated:
2023-02-03 15:16:30 +01:00
return try await self.client.publicTimeline?.getStatuses(
2023-01-23 08:43:04 +01:00
local: false,
remote: true,
maxId: maxId,
sinceId: sinceId,
minId: minId,
2023-02-03 15:16:30 +01:00
limit: self.defaultLimit) ?? []
2023-01-23 08:43:04 +01:00
case .favourites:
2023-02-03 15:16:30 +01:00
return try await self.client.accounts?.favourites(
2023-01-23 08:43:04 +01:00
maxId: maxId,
sinceId: sinceId,
minId: minId,
2023-02-03 15:16:30 +01:00
limit: self.defaultLimit) ?? []
2023-01-23 08:43:04 +01:00
case .bookmarks:
2023-02-03 15:16:30 +01:00
return try await self.client.accounts?.bookmarks(
2023-01-23 08:43:04 +01:00
maxId: maxId,
sinceId: sinceId,
minId: minId,
2023-02-03 15:16:30 +01:00
limit: self.defaultLimit) ?? []
2023-01-23 18:01:27 +01:00
case .hashtag(let tag):
2023-02-03 15:16:30 +01:00
return try await self.client.publicTimeline?.getTagStatuses(
2023-01-23 18:01:27 +01:00
tag: tag,
local: false,
remote: true,
maxId: maxId,
sinceId: sinceId,
minId: minId,
2023-02-03 15:16:30 +01:00
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-25 15:39:04 +01:00
@ToolbarContentBuilder
private func getTrailingToolbar() -> some ToolbarContent {
if case .hashtag(let hashtag) = self.listType {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
Task {
if self.tag?.following == true {
await self.follow(hashtag: hashtag)
} else {
await self.unfollow(hashtag: hashtag)
}
}
} label: {
Image(systemName: self.tag?.following == true ? "number.square.fill" : "number.square")
.tint(.mainTextColor)
}
}
}
}
private func loadTag(hashtag: String) async {
do {
2023-02-03 15:16:30 +01:00
self.tag = try await self.client.tags?.get(tag: hashtag)
2023-01-25 15:39:04 +01:00
} catch {
ErrorService.shared.handle(error, message: "Error during loading tag from server.", showToastr: false)
}
}
private func follow(hashtag: String) async {
do {
2023-02-03 15:16:30 +01:00
self.tag = try await self.client.tags?.follow(tag: hashtag)
2023-01-25 15:39:04 +01:00
ToastrService.shared.showSuccess("You are following the tag.", imageSystemName: "number.square.fill")
} catch {
ErrorService.shared.handle(error, message: "Error during following tag.", showToastr: true)
}
}
private func unfollow(hashtag: String) async {
do {
2023-02-03 15:16:30 +01:00
self.tag = try await self.client.tags?.unfollow(tag: hashtag)
2023-01-25 15:39:04 +01:00
ToastrService.shared.showSuccess("Tag has been unfollowed.", imageSystemName: "number.square")
} catch {
ErrorService.shared.handle(error, message: "Error during unfollowing tag.", showToastr: true)
}
}
2023-01-21 18:01:17 +01:00
}