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
|
2023-02-19 10:32:38 +01:00
|
|
|
import PixelfedKit
|
2023-01-23 11:42:28 +01:00
|
|
|
|
|
|
|
struct TrendStatusesView: View {
|
|
|
|
@EnvironmentObject private var applicationState: ApplicationState
|
2023-02-03 15:16:30 +01:00
|
|
|
@EnvironmentObject private var client: Client
|
|
|
|
|
2023-01-23 11:42:28 +01:00
|
|
|
@State public var accountId: String
|
|
|
|
|
2023-02-19 10:43:37 +01:00
|
|
|
@State private var tabSelectedValue: Pixelfed.Trends.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("")) {
|
2023-03-13 13:53:36 +01:00
|
|
|
Text("trendingStatuses.title.daily", comment: "Daily").tag(Pixelfed.Trends.TrendRange.daily)
|
|
|
|
Text("trendingStatuses.title.monthly", comment: "Monthly").tag(Pixelfed.Trends.TrendRange.monthly)
|
|
|
|
Text("trendingStatuses.title.yearly", comment: "Yearly").tag(Pixelfed.Trends.TrendRange.yearly)
|
2023-01-23 11:42:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
.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 {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
self.mainBody()
|
|
|
|
}
|
2023-03-13 13:53:36 +01:00
|
|
|
.navigationTitle("trendingStatuses.navigationBar.title")
|
2023-02-01 18:40:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
LoadingIndicator()
|
|
|
|
.task {
|
|
|
|
do {
|
|
|
|
try await self.loadStatuses()
|
|
|
|
self.state = .loaded
|
|
|
|
} catch {
|
|
|
|
if !Task.isCancelled {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: true)
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: false)
|
2023-02-01 18:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .loaded:
|
|
|
|
if self.statusViewModels.isEmpty {
|
2023-03-13 13:53:36 +01:00
|
|
|
NoDataView(imageSystemName: "photo.on.rectangle.angled", text: "trendingStatuses.title.noPhotos")
|
2023-02-01 18:40:28 +01:00
|
|
|
} 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 {
|
2023-02-19 12:54:31 +01:00
|
|
|
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
|
2023-02-01 18:40:28 +01:00
|
|
|
try await self.loadStatuses()
|
2023-02-19 12:54:31 +01:00
|
|
|
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
|
2023-02-01 18:40:28 +01:00
|
|
|
} catch {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
|
2023-02-01 18:40:28 +01:00
|
|
|
}
|
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 {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: true)
|
2023-02-01 18:40:28 +01:00
|
|
|
self.state = .error(error)
|
|
|
|
} else {
|
2023-03-13 13:53:36 +01:00
|
|
|
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: false)
|
2023-02-01 18:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2023-02-03 15:16:30 +01:00
|
|
|
if let statuses = try await client.trends?.statuses(range: tabSelectedValue) {
|
|
|
|
var inPlaceStatuses: [StatusModel] = []
|
|
|
|
|
|
|
|
for item in statuses.getStatusesWithImagesOnly() {
|
|
|
|
inPlaceStatuses.append(StatusModel(status: item))
|
|
|
|
}
|
|
|
|
|
|
|
|
self.statusViewModels = inPlaceStatuses
|
2023-01-23 11:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|