|
| 1 | +import SwiftUI |
| 2 | +import WordPressUI |
| 3 | +import WordPressKit |
| 4 | +import WordPressData |
| 5 | + |
| 6 | +struct TagsView: View { |
| 7 | + @ObservedObject var viewModel: TagsViewModel |
| 8 | + |
| 9 | + var body: some View { |
| 10 | + Group { |
| 11 | + if !viewModel.searchText.isEmpty { |
| 12 | + TagsSearchView(viewModel: viewModel) |
| 13 | + } else { |
| 14 | + TagsListView(viewModel: viewModel) |
| 15 | + } |
| 16 | + } |
| 17 | + .navigationTitle(Strings.title) |
| 18 | + .searchable(text: $viewModel.searchText) |
| 19 | + .textInputAutocapitalization(.never) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +private struct TagsListView: View { |
| 24 | + @ObservedObject var viewModel: TagsViewModel |
| 25 | + |
| 26 | + var body: some View { |
| 27 | + List { |
| 28 | + if let response = viewModel.response { |
| 29 | + DataViewPaginatedForEach(response: response) { tag in |
| 30 | + TagRowView(tag: tag) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + .listStyle(.plain) |
| 35 | + .overlay { |
| 36 | + if let response = viewModel.response { |
| 37 | + if response.isEmpty { |
| 38 | + EmptyStateView( |
| 39 | + Strings.empty, |
| 40 | + systemImage: "tag", |
| 41 | + description: Strings.emptyDescription |
| 42 | + ) |
| 43 | + } |
| 44 | + } else if viewModel.isLoading { |
| 45 | + ProgressView() |
| 46 | + } else if let error = viewModel.error { |
| 47 | + EmptyStateView.failure(error: error) { |
| 48 | + Task { await viewModel.refresh() } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + .onAppear { |
| 53 | + viewModel.onAppear() |
| 54 | + } |
| 55 | + .refreshable { |
| 56 | + await viewModel.refresh() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +private struct TagsSearchView: View { |
| 62 | + @ObservedObject var viewModel: TagsViewModel |
| 63 | + |
| 64 | + var body: some View { |
| 65 | + DataViewSearchView( |
| 66 | + searchText: viewModel.searchText, |
| 67 | + search: viewModel.search |
| 68 | + ) { response in |
| 69 | + DataViewPaginatedForEach(response: response) { tag in |
| 70 | + TagRowView(tag: tag) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +private struct TagsPaginatedForEach: View { |
| 77 | + @ObservedObject var response: TagsPaginatedResponse |
| 78 | + |
| 79 | + var body: some View { |
| 80 | + DataViewPaginatedForEach(response: response) { tag in |
| 81 | + TagRowView(tag: tag) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +private struct TagRowView: View { |
| 87 | + let tag: RemotePostTag |
| 88 | + |
| 89 | + var body: some View { |
| 90 | + Text(tag.name ?? "") |
| 91 | + .font(.body) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private enum Strings { |
| 96 | + static let title = NSLocalizedString( |
| 97 | + "tags.title", |
| 98 | + value: "Tags", |
| 99 | + comment: "Title for the tags screen" |
| 100 | + ) |
| 101 | + |
| 102 | + static let empty = NSLocalizedString( |
| 103 | + "tags.empty.title", |
| 104 | + value: "No Tags", |
| 105 | + comment: "Title for empty state when there are no tags" |
| 106 | + ) |
| 107 | + |
| 108 | + static let emptyDescription = NSLocalizedString( |
| 109 | + "tags.empty.description", |
| 110 | + value: "Tags help organize your content and make it easier for readers to find related posts.", |
| 111 | + comment: "Description for empty state when there are no tags" |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +class TagsViewController: UIHostingController<TagsView> { |
| 116 | + let viewModel: TagsViewModel |
| 117 | + |
| 118 | + init(blog: Blog) { |
| 119 | + viewModel = TagsViewModel(blog: blog) |
| 120 | + super.init(rootView: .init(viewModel: viewModel)) |
| 121 | + } |
| 122 | + |
| 123 | + @MainActor @preconcurrency required dynamic init?(coder aDecoder: NSCoder) { |
| 124 | + fatalError("init(coder:) has not been implemented") |
| 125 | + } |
| 126 | +} |
0 commit comments