Vernissage/Vernissage/Views/TrendStatusesView.swift

137 lines
5.4 KiB
Swift
Raw Permalink Normal View History

2023-01-23 11:42:28 +01:00
//
// https://mczachurski.dev
2023-04-09 20:51:33 +02:00
// Copyright © 2023 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
2023-10-19 13:24:02 +02:00
@MainActor
2023-01-23 11:42:28 +01:00
struct TrendStatusesView: View {
2023-10-19 13:24:02 +02:00
@Environment(ApplicationState.self) var applicationState
@Environment(Client.self) var client
2023-02-03 15:16:30 +01:00
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
2023-05-26 16:06:38 +02:00
// Gallery parameters.
@State private var imageColumns = 3
@State private var containerWidth: Double = UIScreen.main.bounds.width
@State private var containerHeight: Double = UIScreen.main.bounds.height
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())
2023-10-19 09:29:49 +02:00
.onChange(of: tabSelectedValue) {
2023-01-23 11:42:28 +01:00
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-05-26 16:06:38 +02:00
.gallery { galleryProperties in
self.imageColumns = galleryProperties.imageColumns
self.containerWidth = galleryProperties.containerWidth
self.containerHeight = galleryProperties.containerHeight
}
.refreshable {
do {
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
try await self.loadStatuses()
HapticService.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
} catch {
ErrorService.shared.handle(error, message: "trendingStatuses.error.loadingStatusesFailed", showToastr: !Task.isCancelled)
}
}
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 {
2023-05-26 16:06:38 +02:00
if self.imageColumns > 1 {
2023-10-08 11:35:45 +02:00
WaterfallGrid($statusViewModels, refreshId: Binding.constant(""), columns: $imageColumns, hideLoadMore: Binding.constant(true)) { item in
2023-05-26 16:06:38 +02:00
ImageRowAsync(statusViewModel: item, containerWidth: $containerWidth)
} onLoadMore: { }
} else {
LazyVStack(alignment: .center) {
ForEach(self.statusViewModels, id: \.id) { item in
ImageRowAsync(statusViewModel: item, containerWidth: Binding.constant(UIScreen.main.bounds.width))
}
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
}
}
}