2023-01-23 11:42:28 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2022 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import MastodonKit
|
|
|
|
|
|
|
|
struct TrendStatusesView: View {
|
|
|
|
|
|
|
|
@EnvironmentObject private var applicationState: ApplicationState
|
|
|
|
@State public var accountId: String
|
|
|
|
|
|
|
|
@State private var tabSelectedValue: Mastodon.PixelfedTrends.TrendRange = .daily
|
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-01-23 11:42:28 +01:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ScrollView {
|
|
|
|
Picker(selection: $tabSelectedValue, label: Text("")) {
|
|
|
|
Text("Daily").tag(Mastodon.PixelfedTrends.TrendRange.daily)
|
|
|
|
Text("Monthly").tag(Mastodon.PixelfedTrends.TrendRange.monthly)
|
|
|
|
Text("Yearly").tag(Mastodon.PixelfedTrends.TrendRange.yearly)
|
|
|
|
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
.pickerStyle(SegmentedPickerStyle())
|
|
|
|
.onChange(of: tabSelectedValue) { _ in
|
|
|
|
Task {
|
|
|
|
do {
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .loading
|
2023-01-23 11:42:28 +01:00
|
|
|
self.statusViewModels = []
|
|
|
|
try await self.loadStatuses()
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
self.mainBody()
|
|
|
|
}
|
|
|
|
.navigationBarTitle("Trends")
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
do {
|
|
|
|
try await self.loadStatuses()
|
|
|
|
self.state = .loaded
|
|
|
|
} catch {
|
|
|
|
if !Task.isCancelled {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: true)
|
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .loaded:
|
|
|
|
if self.statusViewModels.isEmpty {
|
|
|
|
NoDataView(imageSystemName: "photo.on.rectangle.angled", text: "Unfortunately, there are no photos here.")
|
|
|
|
} else {
|
|
|
|
LazyVStack(alignment: .center) {
|
2023-01-26 20:35:24 +01:00
|
|
|
ForEach(self.statusViewModels, id: \.id) { item in
|
2023-01-23 18:01:27 +01:00
|
|
|
NavigationLink(value: RouteurDestinations.status(
|
|
|
|
id: item.id,
|
|
|
|
blurhash: item.mediaAttachments.first?.blurhash,
|
2023-01-27 15:46:09 +01:00
|
|
|
highestImageUrl: item.mediaAttachments.getHighestImage()?.url,
|
2023-01-23 18:01:27 +01:00
|
|
|
metaImageWidth: item.getImageWidth(),
|
|
|
|
metaImageHeight: item.getImageHeight())
|
|
|
|
) {
|
|
|
|
ImageRowAsync(statusViewModel: item)
|
|
|
|
}
|
|
|
|
.buttonStyle(EmptyButtonStyle())
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.refreshable {
|
|
|
|
do {
|
|
|
|
try await self.loadStatuses()
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: !Task.isCancelled)
|
|
|
|
}
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
|
|
|
|
case .error(let error):
|
|
|
|
ErrorView(error: error) {
|
|
|
|
do {
|
|
|
|
self.state = .loading
|
|
|
|
try await self.loadStatuses()
|
|
|
|
self.state = .loaded
|
|
|
|
} catch {
|
|
|
|
if !Task.isCancelled {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: true)
|
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
|
|
|
ErrorService.shared.handle(error, message: "Loading statuses failed.", showToastr: false)
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.padding()
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loadStatuses() async throws {
|
|
|
|
let statuses = try await TrendsService.shared.statuses(
|
2023-01-31 12:20:49 +01:00
|
|
|
for: self.applicationState.account,
|
2023-01-23 11:42:28 +01:00
|
|
|
range: tabSelectedValue)
|
|
|
|
|
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-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.statusViewModels = inPlaceStatuses
|
|
|
|
}
|
|
|
|
}
|