2023-01-21 18:01:17 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2022 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2023-01-21 18:01:17 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2023-02-19 10:32:38 +01:00
|
|
|
import PixelfedKit
|
2023-04-07 14:20:12 +02:00
|
|
|
import ClientKit
|
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-04-01 12:10:59 +02: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-03-07 16:45:44 +01:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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?
|
2023-01-31 12:20:49 +01:00
|
|
|
@State private var statusViewModels: [StatusModel] = []
|
2023-02-01 18:40:28 +01:00
|
|
|
@State private var state: ViewState = .loading
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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()
|
2023-02-21 08:36:14 +01:00
|
|
|
.navigationTitle(self.getTitle())
|
2023-02-01 18:40:28 +01:00
|
|
|
.toolbar {
|
|
|
|
// TODO: It seems like pixelfed is not supporting the endpoints.
|
|
|
|
// self.getTrailingToolbar()
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-01 18:40:28 +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 18:40:28 +01:00
|
|
|
} else {
|
2023-03-29 09:35:01 +02:00
|
|
|
self.list()
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
@ViewBuilder
|
|
|
|
private func list() -> some View {
|
|
|
|
ScrollView {
|
|
|
|
LazyVStack(alignment: .center) {
|
|
|
|
ForEach(self.statusViewModels, id: \.id) { item in
|
|
|
|
ImageRowAsync(statusViewModel: item)
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.refreshable {
|
|
|
|
do {
|
|
|
|
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
|
|
|
|
try await self.loadTopStatuses()
|
|
|
|
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "statuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02: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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .loaded
|
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "statuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .error(error)
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02: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()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-01 20:01:18 +01:00
|
|
|
if statuses.isEmpty {
|
|
|
|
self.allItemsLoaded = true
|
2023-01-21 18:01:17 +01:00
|
|
|
return
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
var inPlaceStatuses: [StatusModel] = []
|
2023-01-23 11:42:28 +01:00
|
|
|
for item in statuses.getStatusesWithImagesOnly() {
|
2023-01-31 12:20:49 +01:00
|
|
|
inPlaceStatuses.append(StatusModel(status: item))
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-21 18:01:17 +01:00
|
|
|
self.statusViewModels.append(contentsOf: inPlaceStatuses)
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-21 18:01:17 +01:00
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
var inPlaceStatuses: [StatusModel] = []
|
2023-01-23 11:42:28 +01:00
|
|
|
for item in previousStatuses.getStatusesWithImagesOnly() {
|
2023-01-31 12:20:49 +01:00
|
|
|
inPlaceStatuses.append(StatusModel(status: item))
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-21 18:01:17 +01:00
|
|
|
self.statusViewModels.append(contentsOf: inPlaceStatuses)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-21 18:01:17 +01:00
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
var inPlaceStatuses: [StatusModel] = []
|
2023-01-23 11:42:28 +01:00
|
|
|
for item in newestStatuses.getStatusesWithImagesOnly() {
|
2023-01-31 12:20:49 +01:00
|
|
|
inPlaceStatuses.append(StatusModel(status: item))
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-21 18:01:17 +01:00
|
|
|
self.statusViewModels.insert(contentsOf: inPlaceStatuses, at: 0)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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,
|
|
|
|
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
|
|
|
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-03-07 16:45:44 +01:00
|
|
|
let hashtagsFromApi = try await self.client.search?.search(query: tag, resultsType: .hashtags)
|
|
|
|
guard let hashtagsFromApi = hashtagsFromApi, hashtagsFromApi.hashtags.isEmpty == false else {
|
2023-03-13 13:53:36 +01:00
|
|
|
ToastrService.shared.showError(title: "global.error.hashtagNotExists", imageSystemName: "exclamationmark.octagon")
|
2023-03-07 16:45:44 +01:00
|
|
|
dismiss()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-07 16:45:44 +01:00
|
|
|
return []
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-13 13:53:36 +01:00
|
|
|
private func getTitle() -> LocalizedStringKey {
|
2023-01-23 08:43:04 +01:00
|
|
|
switch self.listType {
|
|
|
|
case .local:
|
2023-03-13 13:53:36 +01:00
|
|
|
return "statuses.navigationBar.localTimeline"
|
2023-01-23 08:43:04 +01:00
|
|
|
case .federated:
|
2023-03-13 13:53:36 +01:00
|
|
|
return "statuses.navigationBar.federatedTimeline"
|
2023-01-23 08:43:04 +01:00
|
|
|
case .favourites:
|
2023-03-13 13:53:36 +01:00
|
|
|
return "statuses.navigationBar.favourites"
|
2023-01-23 08:43:04 +01:00
|
|
|
case .bookmarks:
|
2023-03-13 13:53:36 +01:00
|
|
|
return "statuses.navigationBar.bookmarks"
|
2023-01-23 18:01:27 +01:00
|
|
|
case .hashtag(let tag):
|
|
|
|
return "#\(tag)"
|
2023-01-23 08:43:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-25 15:39:04 +01:00
|
|
|
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 {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "global.error.errorDuringDownloadHashtag", showToastr: false)
|
2023-01-25 15:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-25 15:39:04 +01:00
|
|
|
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-03-13 13:53:36 +01:00
|
|
|
ToastrService.shared.showSuccess("statuses.title.tagFollowed", imageSystemName: "number.square.fill")
|
2023-01-25 15:39:04 +01:00
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "statuses.error.tagFollowFailed", showToastr: true)
|
2023-01-25 15:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-25 15:39:04 +01:00
|
|
|
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-03-13 13:53:36 +01:00
|
|
|
ToastrService.shared.showSuccess("statuses.title.tagUnfollowed", imageSystemName: "number.square")
|
2023-01-25 15:39:04 +01:00
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "statuses.error.tagUnfollowFailed", showToastr: true)
|
2023-01-25 15:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|