|
1 | 1 | import shared |
2 | 2 | import SwiftUI |
| 3 | +import Kingfisher |
3 | 4 |
|
4 | 5 | struct SpaceScreen: View { |
5 | 6 | let accountType: AccountType |
6 | 7 | @EnvironmentObject private var router: FlareRouter |
7 | | - @State private var podcastIdInput: String = "" |
| 8 | + @State private var presenter: PodcastListPresenter |
| 9 | + |
| 10 | + init(accountType: AccountType) { |
| 11 | + self.accountType = accountType |
| 12 | + _presenter = .init(initialValue: PodcastListPresenter(accountType: accountType)) |
| 13 | + } |
8 | 14 |
|
9 | 15 | var body: some View { |
10 | | - VStack(spacing: 20) { |
11 | | - Text("XSpaces list comming soon") |
| 16 | + ObservePresenter(presenter: presenter) { state in |
| 17 | + feedsListView(state) |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + @ViewBuilder |
| 22 | + private func feedsListView(_ state: PodcastListPresenterState) -> some View { |
| 23 | + switch onEnum(of: state.data) { |
| 24 | + case .loading: |
| 25 | + loadingView |
| 26 | + case let .success(successData): |
| 27 | + if successData.data.count == 0 { |
| 28 | + emptyView() |
| 29 | + } else { |
| 30 | + podcastsListView(podcastsList: successData.data) |
| 31 | + } |
| 32 | + case let .error(errorState): |
| 33 | + errorView(errorState: errorState) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private var loadingView: some View { |
| 38 | + VStack { |
| 39 | + ProgressView("Loading Spaces...") |
| 40 | + Spacer() |
| 41 | + } |
| 42 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 43 | + } |
| 44 | + |
| 45 | + @ViewBuilder |
| 46 | + private func podcastsListView(podcastsList: NSArray) -> some View { |
| 47 | + List { |
| 48 | + ForEach(0 ..< podcastsList.count, id: \.self) { index in |
| 49 | + if let podcast = podcastsList[index] as? UiPodcast { |
| 50 | + PodcastRowView(podcast: podcast, accountType: accountType) |
| 51 | + } else { |
| 52 | + Text("Invalid item at index \\(index)") |
| 53 | + .foregroundColor(.red) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + .listStyle(.plain) |
| 58 | + .navigationTitle("You follow the X Spaces") |
| 59 | + .navigationBarTitleDisplayMode(.inline) |
| 60 | + } |
| 61 | + |
| 62 | + private func emptyView() -> some View { |
| 63 | + VStack(spacing: 16) { |
| 64 | + Image(systemName: "mic.slash.fill") |
| 65 | + .font(.largeTitle) |
| 66 | + .foregroundColor(.secondary) |
| 67 | + Text("No Spaces Available") |
| 68 | + .font(.headline) |
| 69 | + Text("There are currently no live or recorded Spaces to show.") |
| 70 | + .font(.subheadline) |
| 71 | + .foregroundColor(.gray) |
| 72 | + } |
| 73 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 74 | + } |
| 75 | + |
| 76 | + private func errorView(errorState: UiStateError<NSArray>) -> some View { |
| 77 | + let errorMessage = errorState.throwable.message ?? "An unknown error occurred" |
| 78 | + return VStack(spacing: 16) { |
| 79 | + Image(systemName: "exclamationmark.triangle.fill") |
12 | 80 | .font(.largeTitle) |
13 | | - .padding(.top) |
| 81 | + .foregroundColor(.red) |
| 82 | + Text("Error Loading Spaces") |
| 83 | + .font(.headline) |
| 84 | + Text(errorMessage) |
| 85 | + .font(.subheadline) |
| 86 | + .foregroundColor(.gray) |
| 87 | + .multilineTextAlignment(.center) |
| 88 | + .padding(.horizontal) |
| 89 | + .buttonStyle(.borderedProminent) |
| 90 | + } |
| 91 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private struct PodcastRowView: View { |
| 96 | + let podcast: UiPodcast |
| 97 | + let accountType: AccountType |
| 98 | + @EnvironmentObject private var router: FlareRouter |
14 | 99 |
|
| 100 | + var body: some View { |
| 101 | + VStack(alignment: .leading, spacing: 8) { |
15 | 102 | HStack { |
16 | | - TextField("Enter Podcast/Space ID", text: $podcastIdInput) |
17 | | - .textFieldStyle(.roundedBorder) |
18 | | - .autocapitalization(.none) |
19 | | - .disableAutocorrection(true) |
20 | | - |
21 | | - Button("Confirm") { |
22 | | - guard !podcastIdInput.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return } |
23 | | - router.navigate(to: .podcastSheet(accountType: accountType, podcastId: podcastIdInput)) |
| 103 | + if !podcast.ended { |
| 104 | + LiveIndicatorView() |
24 | 105 | } |
25 | | - .buttonStyle(.borderedProminent) |
26 | | - .disabled(podcastIdInput.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 106 | + Spacer() |
| 107 | +// Image(systemName: "ellipsis") |
| 108 | +// .foregroundColor(.secondary) |
27 | 109 | } |
28 | | - .padding(.horizontal) |
29 | 110 |
|
30 | | - Button("Set Test Replay ID") { |
31 | | - podcastIdInput = "1jMJgkygVkXJL" |
| 111 | + Text(podcast.title) |
| 112 | + .font(.title3.weight(.semibold)) |
| 113 | + .lineLimit(3) |
| 114 | + |
| 115 | +// HStack(spacing: 4) { |
| 116 | +// Image(systemName: "headphones") |
| 117 | +// Text("\(podcast.listeners.count) Listening") |
| 118 | +// } |
| 119 | +// .font(.subheadline) |
| 120 | +// .foregroundColor(.secondary) |
| 121 | +// .padding(.bottom, 4) |
| 122 | + |
| 123 | + HStack(spacing: 8) { |
| 124 | + KFImage(URL(string: podcast.creator.avatar)) |
| 125 | + .resizable() |
| 126 | + .placeholder { Image(systemName: "person.circle.fill").resizable().foregroundColor(.gray) } |
| 127 | + .aspectRatio(contentMode: .fill) |
| 128 | + .frame(width: 24, height: 24) |
| 129 | + .clipShape(Circle()) |
| 130 | + |
| 131 | + Text(podcast.creator.name.raw) |
| 132 | + .font(.subheadline.weight(.medium)) |
| 133 | + .lineLimit(1) |
32 | 134 | } |
33 | | - .buttonStyle(.bordered) |
| 135 | + } |
| 136 | + .padding() |
| 137 | + .background(Color.teal.opacity(0.1)) |
| 138 | + .cornerRadius(12) |
| 139 | + .contentShape(Rectangle()) |
| 140 | + .onTapGesture { |
| 141 | + router.navigate(to: .podcastSheet(accountType: accountType, podcastId: podcast.id)) |
| 142 | + } |
| 143 | + .listRowSeparator(.hidden) |
| 144 | + .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)) |
| 145 | + } |
| 146 | +} |
34 | 147 |
|
35 | | - Spacer() |
| 148 | +private struct LiveIndicatorView: View { |
| 149 | + var body: some View { |
| 150 | + HStack(spacing: 4) { |
| 151 | + Image(systemName: "antenna.radiowaves.left.and.right") |
| 152 | + Text("Live") |
36 | 153 | } |
37 | | - .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) |
38 | | - .navigationTitle("XSpace Test") |
39 | | - .navigationBarTitleDisplayMode(.inline) |
| 154 | + .font(.caption.weight(.bold)) |
| 155 | + .foregroundColor(.red) |
| 156 | + .padding(.horizontal, 6) |
| 157 | + .padding(.vertical, 3) |
| 158 | + .background(Color.red.opacity(0.1)) |
| 159 | + .cornerRadius(6) |
40 | 160 | } |
41 | 161 | } |
0 commit comments