IceCubesApp/Packages/Explore/Sources/Explore/ExploreView.swift

238 lines
8.1 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import Account
import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
import Models
2023-01-17 10:36:01 +00:00
import Network
import Shimmer
2023-01-17 10:36:01 +00:00
import Status
import SwiftUI
2023-09-18 19:03:52 +00:00
@MainActor
public struct ExploreView: View {
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
@Environment(Client.self) private var client
@Environment(RouterPath.self) private var routerPath
2023-01-17 10:36:01 +00:00
@State private var viewModel = ExploreViewModel()
2023-01-17 10:36:01 +00:00
public init() {}
public var body: some View {
List {
if !viewModel.isLoaded {
quickAccessView
loadingView
} else if !viewModel.searchQuery.isEmpty {
if let results = viewModel.results[viewModel.searchQuery] {
2023-01-28 07:29:49 +00:00
if results.isEmpty, !viewModel.isSearching {
EmptyView(iconName: "magnifyingglass",
title: "explore.search.empty.title",
message: "explore.search.empty.message")
.listRowBackground(theme.secondaryBackgroundColor)
.listRowSeparator(.hidden)
} else {
makeSearchResultsView(results: results)
}
} else {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowBackground(theme.secondaryBackgroundColor)
.listRowSeparator(.hidden)
.id(UUID())
}
} else if viewModel.allSectionsEmpty {
2023-01-12 05:30:43 +00:00
EmptyView(iconName: "magnifyingglass",
title: "explore.search.title",
message: "explore.search.message-\(client.server)")
2023-01-17 10:36:01 +00:00
.listRowBackground(theme.secondaryBackgroundColor)
.listRowSeparator(.hidden)
} else {
quickAccessView
2023-01-12 05:30:43 +00:00
if !viewModel.trendingTags.isEmpty {
trendingTagsSection
}
if !viewModel.suggestedAccounts.isEmpty {
suggestedAccountsSection
}
if !viewModel.trendingStatuses.isEmpty {
trendingPostsSection
}
if !viewModel.trendingLinks.isEmpty {
trendingLinksSection
}
}
}
.task {
viewModel.client = client
await viewModel.fetchTrending()
}
.refreshable {
Task {
2023-02-28 17:55:08 +00:00
SoundEffectManager.shared.playSound(of: .pull)
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
await viewModel.fetchTrending()
2023-02-28 17:55:08 +00:00
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
SoundEffectManager.shared.playSound(of: .refresh)
}
}
.listStyle(.grouped)
2022-12-29 09:39:34 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.navigationTitle("explore.navigation-title")
2022-12-27 06:51:44 +00:00
.searchable(text: $viewModel.searchQuery,
2023-07-18 06:24:02 +00:00
placement: .navigationBarDrawer(displayMode: .always),
prompt: Text("explore.search.prompt"))
.searchScopes($viewModel.searchScope) {
ForEach(ExploreViewModel.SearchScope.allCases, id: \.self) { scope in
Text(scope.localizedString)
}
}
.task(id: viewModel.searchQuery) {
do {
try await Task.sleep(for: .milliseconds(150))
await viewModel.search()
} catch {}
}
}
2023-07-19 05:46:25 +00:00
private var quickAccessView: some View {
ScrollView(.horizontal) {
HStack {
Button("explore.section.trending.tags") {
routerPath.navigate(to: RouterDestination.tagsList(tags: viewModel.trendingTags))
}
.buttonStyle(.bordered)
Button("explore.section.suggested-users") {
routerPath.navigate(to: RouterDestination.accountsList(accounts: viewModel.suggestedAccounts))
}
.buttonStyle(.bordered)
Button("explore.section.trending.posts") {
routerPath.navigate(to: RouterDestination.trendingTimeline)
}
.buttonStyle(.bordered)
}
.padding(.horizontal, 16)
}
.scrollIndicators(.never)
.listRowInsets(EdgeInsets())
.listRowBackground(theme.secondaryBackgroundColor)
.listRowSeparator(.hidden)
}
2023-01-17 10:36:01 +00:00
2022-12-27 09:04:39 +00:00
private var loadingView: some View {
ForEach(Status.placeholders()) { status in
StatusRowView(viewModel: .init(status: status, client: client, routerPath: routerPath))
2022-12-27 09:04:39 +00:00
.padding(.vertical, 8)
.redacted(reason: .placeholder)
2023-09-18 16:55:11 +00:00
.allowsHitTesting(false)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-27 09:04:39 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-27 09:04:39 +00:00
@ViewBuilder
private func makeSearchResultsView(results: SearchResults) -> some View {
2023-09-16 12:15:03 +00:00
if !results.accounts.isEmpty, viewModel.searchScope == .all || viewModel.searchScope == .people {
Section("explore.section.users") {
2022-12-27 09:04:39 +00:00
ForEach(results.accounts) { account in
if let relationship = results.relationships.first(where: { $0.id == account.id }) {
AccountsListRow(viewModel: .init(account: account, relationShip: relationship))
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-27 09:04:39 +00:00
}
}
}
}
2023-09-16 12:15:03 +00:00
if !results.hashtags.isEmpty, viewModel.searchScope == .all || viewModel.searchScope == .hashtags {
Section("explore.section.tags") {
2022-12-27 09:04:39 +00:00
ForEach(results.hashtags) { tag in
TagRowView(tag: tag)
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-27 09:04:39 +00:00
.padding(.vertical, 4)
}
}
}
2023-09-16 12:15:03 +00:00
if !results.statuses.isEmpty, viewModel.searchScope == .all || viewModel.searchScope == .posts {
Section("explore.section.posts") {
2022-12-27 09:04:39 +00:00
ForEach(results.statuses) { status in
StatusRowView(viewModel: .init(status: status, client: client, routerPath: routerPath))
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2022-12-27 09:04:39 +00:00
.padding(.vertical, 8)
}
}
}
}
2023-01-17 10:36:01 +00:00
private var suggestedAccountsSection: some View {
Section("explore.section.suggested-users") {
ForEach(viewModel.suggestedAccounts
2023-03-13 12:38:28 +00:00
.prefix(upTo: viewModel.suggestedAccounts.count > 3 ? 3 : viewModel.suggestedAccounts.count))
{ account in
if let relationship = viewModel.suggestedAccountsRelationShips.first(where: { $0.id == account.id }) {
AccountsListRow(viewModel: .init(account: account, relationShip: relationship))
.listRowBackground(theme.primaryBackgroundColor)
}
2023-03-13 12:38:28 +00:00
}
2023-03-02 19:15:07 +00:00
NavigationLink(value: RouterDestination.accountsList(accounts: viewModel.suggestedAccounts)) {
Text("see-more")
2022-12-24 14:09:17 +00:00
.foregroundColor(theme.tintColor)
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
}
2023-01-17 10:36:01 +00:00
private var trendingTagsSection: some View {
Section("explore.section.trending.tags") {
ForEach(viewModel.trendingTags
2023-03-13 12:38:28 +00:00
.prefix(upTo: viewModel.trendingTags.count > 5 ? 5 : viewModel.trendingTags.count))
{ tag in
TagRowView(tag: tag)
.listRowBackground(theme.primaryBackgroundColor)
.padding(.vertical, 4)
}
2023-03-02 19:15:07 +00:00
NavigationLink(value: RouterDestination.tagsList(tags: viewModel.trendingTags)) {
Text("see-more")
2022-12-24 14:09:17 +00:00
.foregroundColor(theme.tintColor)
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
}
2023-01-17 10:36:01 +00:00
private var trendingPostsSection: some View {
Section("explore.section.trending.posts") {
ForEach(viewModel.trendingStatuses
2023-03-13 12:38:28 +00:00
.prefix(upTo: viewModel.trendingStatuses.count > 3 ? 3 : viewModel.trendingStatuses.count))
{ status in
StatusRowView(viewModel: .init(status: status, client: client, routerPath: routerPath))
2023-03-13 12:38:28 +00:00
.listRowBackground(theme.primaryBackgroundColor)
.padding(.vertical, 8)
}
2023-01-17 10:36:01 +00:00
2023-03-02 19:15:07 +00:00
NavigationLink(value: RouterDestination.trendingTimeline) {
Text("see-more")
2022-12-24 14:09:17 +00:00
.foregroundColor(theme.tintColor)
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
}
2023-01-17 10:36:01 +00:00
private var trendingLinksSection: some View {
Section("explore.section.trending.links") {
ForEach(viewModel.trendingLinks
2023-03-13 12:38:28 +00:00
.prefix(upTo: viewModel.trendingLinks.count > 3 ? 3 : viewModel.trendingLinks.count))
{ card in
StatusRowCardView(card: card)
.listRowBackground(theme.primaryBackgroundColor)
.padding(.vertical, 8)
}
NavigationLink(value: RouterDestination.trendingLinks(cards: viewModel.trendingLinks)) {
Text("see-more")
2022-12-24 14:09:17 +00:00
.foregroundColor(theme.tintColor)
}
2022-12-29 09:39:34 +00:00
.listRowBackground(theme.primaryBackgroundColor)
}
}
}