|
1 | 1 | import SwiftUI |
| 2 | +import struct Yosemite.Booking |
2 | 3 |
|
3 | 4 | struct BookingListView: View { |
4 | | - // periphery:ignore |
5 | 5 | @ObservedObject private var viewModel: BookingListViewModel |
| 6 | + @State private var selectedTabIndex = 0 |
| 7 | + |
| 8 | + @Namespace var topID |
| 9 | + |
| 10 | + private let tabs = [Localization.today, Localization.upcoming, Localization.all] |
6 | 11 |
|
7 | 12 | init(viewModel: BookingListViewModel) { |
8 | 13 | self.viewModel = viewModel |
9 | 14 | } |
10 | 15 |
|
11 | 16 | var body: some View { |
12 | | - Text("Hello, World!") |
| 17 | + NavigationStack { |
| 18 | + VStack { |
| 19 | + switch viewModel.syncState { |
| 20 | + case .empty: |
| 21 | + headerView |
| 22 | + Spacer() |
| 23 | + Text("No bookings found") // TODO: update this in WOOMOB-1394 |
| 24 | + Spacer() |
| 25 | + case .syncingFirstPage: |
| 26 | + headerView |
| 27 | + Spacer() |
| 28 | + ProgressView().progressViewStyle(.circular) |
| 29 | + Spacer() |
| 30 | + case .results: |
| 31 | + bookingList |
| 32 | + } |
| 33 | + } |
| 34 | + .navigationTitle(Localization.viewTitle) |
| 35 | + .toolbar { |
| 36 | + ToolbarItem(placement: .confirmationAction) { |
| 37 | + Button { |
| 38 | + // TODO |
| 39 | + } label: { |
| 40 | + Image(systemName: "magnifyingglass") |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + .task { |
| 45 | + viewModel.loadBookings() |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +private extension BookingListView { |
| 52 | + var headerView: some View { |
| 53 | + VStack(spacing: 0) { |
| 54 | + topTabView |
| 55 | + Divider() |
| 56 | + HStack { |
| 57 | + Button { |
| 58 | + // TODO |
| 59 | + } label: { |
| 60 | + Text(Localization.sortBy) |
| 61 | + .font(.body) |
| 62 | + .foregroundStyle(Color.accentColor) |
| 63 | + } |
| 64 | + Spacer() |
| 65 | + Button { |
| 66 | + // TODO |
| 67 | + } label: { |
| 68 | + Text(Localization.filter) |
| 69 | + .font(.body) |
| 70 | + .foregroundStyle(Color.accentColor) |
| 71 | + } |
| 72 | + } |
| 73 | + .padding() |
| 74 | + .background(Color(.listForeground(modal: false))) |
| 75 | + Divider() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + var topTabView: some View { |
| 80 | + GeometryReader { geometry in |
| 81 | + HStack { |
| 82 | + ForEach(Array(tabs.enumerated()), id: \.element) { (index, title) in |
| 83 | + Button { |
| 84 | + withAnimation(.easeInOut(duration: 0.3)) { |
| 85 | + selectedTabIndex = index |
| 86 | + } |
| 87 | + } label: { |
| 88 | + Text(title) |
| 89 | + .font(.subheadline) |
| 90 | + .foregroundStyle(selectedTabIndex == index ? Color.accentColor : Color.primary) |
| 91 | + } |
| 92 | + .frame(maxWidth: .infinity) |
| 93 | + .padding(.vertical, 12) |
| 94 | + } |
| 95 | + } |
| 96 | + .overlay(alignment: .bottom) { |
| 97 | + Color.accentColor |
| 98 | + .frame(width: geometry.size.width / CGFloat(tabs.count), |
| 99 | + height: Layout.selectedTabIndicatorHeight) |
| 100 | + .offset(x: tabIndicatorOffset(containerWidth: geometry.size.width, |
| 101 | + tabCount: tabs.count, |
| 102 | + selectedIndex: selectedTabIndex)) |
| 103 | + .animation(.easeInOut(duration: 0.3), value: selectedTabIndex) |
| 104 | + } |
| 105 | + } |
| 106 | + .frame(height: Layout.topTabBarHeight) |
| 107 | + .background(Color(.listForeground(modal: false))) |
| 108 | + } |
| 109 | + |
| 110 | + var bookingList: some View { |
| 111 | + ScrollViewReader { proxy in |
| 112 | + ScrollView { |
| 113 | + LazyVStack(spacing: 0, pinnedViews: .sectionHeaders) { |
| 114 | + Section { |
| 115 | + ForEach(viewModel.bookings) { item in |
| 116 | + bookingItem(item) |
| 117 | + } |
| 118 | + } header: { |
| 119 | + headerView |
| 120 | + } |
| 121 | + .id(topID) |
| 122 | + |
| 123 | + InfiniteScrollIndicator(showContent: viewModel.shouldShowBottomActivityIndicator) |
| 124 | + .padding(.top, Layout.viewPadding) |
| 125 | + .onAppear { |
| 126 | + viewModel.onLoadNextPageAction() |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + .refreshable { |
| 131 | + await viewModel.onRefreshAction() |
| 132 | + // workaround as navigation bar is not snapped back after refreshing |
| 133 | + proxy.scrollTo(topID, anchor: .top) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + func bookingItem(_ booking: Booking) -> some View { |
| 139 | + VStack(spacing: 0) { |
| 140 | + VStack(alignment: .leading) { |
| 141 | + Text(booking.startDate.formatted(date: .numeric, time: .shortened)) |
| 142 | + .font(.body) |
| 143 | + .fontWeight(.medium) |
| 144 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 145 | + |
| 146 | + // TODO: fetch bookable products & customer to get names or wait for API update |
| 147 | + Text(String(format: "%@ • %@", "Women's Hair cut", "Marianne")) |
| 148 | + .font(.footnote) |
| 149 | + .fontWeight(.medium) |
| 150 | + .foregroundStyle(Color.secondary) |
| 151 | + |
| 152 | + HStack { |
| 153 | + // TODO: update this when attendance status is available |
| 154 | + // Update badge colors if design changes as statuses are not clarified now. |
| 155 | + statusBadge(text: "Booked", color: Layout.defaultBadgeColor) |
| 156 | + statusBadge(text: booking.bookingStatus.localizedTitle, color: Layout.defaultBadgeColor) |
| 157 | + Spacer() |
| 158 | + } |
| 159 | + } |
| 160 | + .padding(Layout.viewPadding) |
| 161 | + |
| 162 | + Divider() |
| 163 | + .padding(.leading, Layout.viewPadding) |
| 164 | + } |
| 165 | + .background(Color(.listForeground(modal: false))) // TODO: update selected background color as part of selection handling |
| 166 | + } |
| 167 | + |
| 168 | + func statusBadge(text: String, color: Color) -> some View { |
| 169 | + Text(text) |
| 170 | + .font(.caption2) |
| 171 | + .padding(.horizontal, 8) |
| 172 | + .padding(.vertical, 4) |
| 173 | + .background(color.clipShape(RoundedRectangle(cornerRadius: 4))) |
| 174 | + } |
| 175 | + |
| 176 | + /// SwiftUI's coordinate system places (0,0) at the center of the container, so we need to: |
| 177 | + /// 1. Calculate how far the selected tab is from the left edge |
| 178 | + /// 2. Adjust for the center-based coordinate system |
| 179 | + /// 3. Center the indicator within the selected tab |
| 180 | + /// |
| 181 | + func tabIndicatorOffset(containerWidth: CGFloat, tabCount: Int, selectedIndex: Int) -> CGFloat { |
| 182 | + let tabWidth = containerWidth / CGFloat(tabCount) |
| 183 | + let distanceFromLeftEdge = tabWidth * CGFloat(selectedIndex) |
| 184 | + let adjustmentForCenterOrigin = containerWidth / 2 |
| 185 | + let centerWithinTab = tabWidth / 2 |
| 186 | + |
| 187 | + return distanceFromLeftEdge - adjustmentForCenterOrigin + centerWithinTab |
13 | 188 | } |
14 | 189 | } |
| 190 | +private extension BookingListView { |
| 191 | + enum Layout { |
| 192 | + static let viewPadding: CGFloat = 16 |
| 193 | + static let topTabBarHeight: CGFloat = 44 |
| 194 | + static let selectedTabIndicatorHeight: CGFloat = 3.0 |
| 195 | + static let defaultBadgeColor = Color(uiColor: .init(light: .systemGray6, dark: .systemGray5)) |
| 196 | + } |
| 197 | + |
| 198 | + enum Localization { |
| 199 | + static let viewTitle = NSLocalizedString( |
| 200 | + "bookingListView.view.title", |
| 201 | + value: "Bookings", |
| 202 | + comment: "Title of the booking list view" |
| 203 | + ) |
| 204 | + static let sortBy = NSLocalizedString( |
| 205 | + "bookingListView.sortBy", |
| 206 | + value: "Sort by", |
| 207 | + comment: "Button to select the order of the booking list" |
| 208 | + ) |
| 209 | + static let filter = NSLocalizedString( |
| 210 | + "bookingListView.filter", |
| 211 | + value: "Filter", |
| 212 | + comment: "Button to filter the booking list" |
| 213 | + ) |
| 214 | + static let today = NSLocalizedString( |
| 215 | + "bookingListView.today", |
| 216 | + value: "Today", |
| 217 | + comment: "Tab title for today's bookings" |
| 218 | + ) |
| 219 | + static let upcoming = NSLocalizedString( |
| 220 | + "bookingListView.upcoming", |
| 221 | + value: "Upcoming", |
| 222 | + comment: "Tab title for upcoming bookings" |
| 223 | + ) |
| 224 | + static let all = NSLocalizedString( |
| 225 | + "bookingListView.all", |
| 226 | + value: "All", |
| 227 | + comment: "Tab title for all bookings" |
| 228 | + ) |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +#Preview { |
| 233 | + BookingListView(viewModel: BookingListViewModel(siteID: 123)) |
| 234 | +} |
0 commit comments