|
| 1 | +import Combine |
| 2 | +import SwiftUI |
| 3 | +import Yosemite |
| 4 | + |
| 5 | +/// Shows a list of inbox notes as shown in WooCommerce Admin in core. |
| 6 | +struct Inbox: View { |
| 7 | + /// View model that drives the view. |
| 8 | + @ObservedObject private(set) var viewModel: InboxViewModel |
| 9 | + |
| 10 | + init(viewModel: InboxViewModel) { |
| 11 | + self.viewModel = viewModel |
| 12 | + } |
| 13 | + |
| 14 | + var body: some View { |
| 15 | + Group { |
| 16 | + switch viewModel.syncState { |
| 17 | + case .results: |
| 18 | + InfiniteScrollList(isLoading: viewModel.shouldShowBottomActivityIndicator, |
| 19 | + loadAction: viewModel.onLoadNextPageAction) { |
| 20 | + ForEach(viewModel.noteRowViewModels) { rowViewModel in |
| 21 | + if #available(iOS 15.0, *) { |
| 22 | + // In order to show full-width separator, the default list separator is hidden and a `Divider` is shown inside the row. |
| 23 | + InboxNoteRow(viewModel: rowViewModel) |
| 24 | + .listRowSeparator(.hidden) |
| 25 | + } else { |
| 26 | + InboxNoteRow(viewModel: rowViewModel) |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + case .empty: |
| 31 | + // TODO: 5954 - update empty state |
| 32 | + EmptyState(title: Localization.emptyStateTitle, |
| 33 | + description: Localization.emptyStateMessage, |
| 34 | + image: .emptyProductsTabImage) |
| 35 | + .frame(maxHeight: .infinity) |
| 36 | + case .syncingFirstPage: |
| 37 | + List { |
| 38 | + ForEach(viewModel.placeholderRowViewModels) { rowViewModel in |
| 39 | + InboxNoteRow(viewModel: rowViewModel) |
| 40 | + .redacted(reason: .placeholder) |
| 41 | + .shimmering() |
| 42 | + } |
| 43 | + } |
| 44 | + .listStyle(PlainListStyle()) |
| 45 | + } |
| 46 | + } |
| 47 | + .background(Color(.listBackground).ignoresSafeArea()) |
| 48 | + .navigationTitle(Localization.title) |
| 49 | + .onAppear { |
| 50 | + viewModel.onLoadTrigger.send() |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +private extension Inbox { |
| 56 | + enum Localization { |
| 57 | + static let title = NSLocalizedString("Inbox", comment: "Title for the screen that shows inbox notes.") |
| 58 | + static let emptyStateTitle = NSLocalizedString("Congrats, you’ve read everything!", |
| 59 | + comment: "Title displayed if there are no inbox notes in the inbox screen.") |
| 60 | + static let emptyStateMessage = NSLocalizedString("Come back soon for more tips and insights on growing your store", |
| 61 | + comment: "Message displayed if there are no inbox notes to display in the inbox screen.") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#if DEBUG |
| 66 | + |
| 67 | +/// Allows mocking for previewing `Inbox` view. |
| 68 | +private final class PreviewInboxNotesStoresManager: DefaultStoresManager { |
| 69 | + private let inboxNotes: [InboxNote] |
| 70 | + |
| 71 | + init(inboxNotes: [InboxNote], sessionManager: SessionManager = SessionManager.standard) { |
| 72 | + self.inboxNotes = inboxNotes |
| 73 | + super.init(sessionManager: sessionManager) |
| 74 | + } |
| 75 | + |
| 76 | + // MARK: - Overridden Methods |
| 77 | + |
| 78 | + override func dispatch(_ action: Action) { |
| 79 | + if let action = action as? InboxNotesAction { |
| 80 | + onInboxNotesAction(action: action) |
| 81 | + } else { |
| 82 | + super.dispatch(action) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private func onInboxNotesAction(action: InboxNotesAction) { |
| 87 | + switch action { |
| 88 | + case .loadAllInboxNotes(_, _, _, _, _, _, let completion): |
| 89 | + completion(.success(inboxNotes)) |
| 90 | + return |
| 91 | + default: |
| 92 | + return |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +extension InboxNote { |
| 98 | + static func placeholder() -> InboxNote { |
| 99 | + .init(siteID: 255, |
| 100 | + id: 0, |
| 101 | + name: "", |
| 102 | + type: "", |
| 103 | + status: "", |
| 104 | + actions: [.init(id: 0, name: "", label: "Accept Apple Pay", status: "", url: "https://wordpress.com")], |
| 105 | + title: "Boost sales this holiday season with Apple Pay!", |
| 106 | + content: "", |
| 107 | + isRemoved: false, |
| 108 | + isRead: false, |
| 109 | + dateCreated: .init()) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +struct Inbox_Previews: PreviewProvider { |
| 114 | + static var previews: some View { |
| 115 | + Group { |
| 116 | + // Placeholder state. |
| 117 | + Inbox(viewModel: .init(siteID: 122)) |
| 118 | + .preferredColorScheme(.light) |
| 119 | + Inbox(viewModel: .init(siteID: 122)) |
| 120 | + .preferredColorScheme(.dark) |
| 121 | + // Empty state. |
| 122 | + Inbox(viewModel: .init(siteID: 322, |
| 123 | + stores: PreviewInboxNotesStoresManager(inboxNotes: []))) |
| 124 | + .preferredColorScheme(.light) |
| 125 | + Inbox(viewModel: .init(siteID: 322, |
| 126 | + stores: PreviewInboxNotesStoresManager(inboxNotes: []))) |
| 127 | + .preferredColorScheme(.dark) |
| 128 | + // Results state. |
| 129 | + Inbox(viewModel: .init(siteID: 322, |
| 130 | + stores: PreviewInboxNotesStoresManager(inboxNotes: [.placeholder(), .placeholder()]))) |
| 131 | + .preferredColorScheme(.light) |
| 132 | + Inbox(viewModel: .init(siteID: 322, |
| 133 | + stores: PreviewInboxNotesStoresManager(inboxNotes: [.placeholder(), .placeholder()]))) |
| 134 | + .preferredColorScheme(.dark) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#endif |
0 commit comments