Impressia/Vernissage/Views/TrendStatusesView.swift

120 lines
4.5 KiB
Swift
Raw Normal View History

2023-01-23 11:42:28 +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-23 11:42:28 +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-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
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
@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
}
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
ImageRowAsync(statusViewModel: item)
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
}
}
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] = []
2023-02-03 15:16:30 +01:00
for item in statuses.getStatusesWithImagesOnly() {
inPlaceStatuses.append(StatusModel(status: item))
}
2023-02-03 15:16:30 +01:00
self.statusViewModels = inPlaceStatuses
2023-01-23 11:42:28 +01:00
}
}
}