-
Notifications
You must be signed in to change notification settings - Fork 121
Bookings: Update tab handling for booking list #16188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f13d07
Move tab handling to a separate view
itsmeichigo 795d37e
Fix test build failures
itsmeichigo 5d175b3
Remove unused variable
itsmeichigo 61d1414
Use TabView for tab contents
itsmeichigo a95afcc
Remove spacing in BookingListContainerView
itsmeichigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
WooCommerce/Classes/Bookings/BookingList/BookingListContainerView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import SwiftUI | ||
| import struct Yosemite.Booking | ||
|
|
||
| struct BookingListContainerView: View { | ||
| @ObservedObject private var viewModel: BookingListContainerViewModel | ||
|
|
||
| init(viewModel: BookingListContainerViewModel) { | ||
| self.viewModel = viewModel | ||
| } | ||
|
|
||
| var body: some View { | ||
| NavigationStack { | ||
| ZStack { | ||
| ForEach(BookingListTab.allCases, id: \.rawValue) { tab in | ||
| BookingListView(viewModel: viewModel.listViewModel(for: tab)) { | ||
| headerView | ||
| } | ||
| .opacity(viewModel.selectedTab == tab ? 1 : 0) | ||
| } | ||
| } | ||
| .navigationTitle(Localization.viewTitle) | ||
| .toolbar { | ||
| ToolbarItem(placement: .confirmationAction) { | ||
| Button { | ||
| // TODO | ||
| } label: { | ||
| Image(systemName: "magnifyingglass") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private extension BookingListContainerView { | ||
| var headerView: some View { | ||
| VStack(spacing: 0) { | ||
| topTabView | ||
| Divider() | ||
| HStack { | ||
| Button { | ||
| // TODO | ||
| } label: { | ||
| Text(Localization.sortBy) | ||
| .font(.body) | ||
| .foregroundStyle(Color.accentColor) | ||
| } | ||
| Spacer() | ||
| Button { | ||
| // TODO | ||
| } label: { | ||
| Text(Localization.filter) | ||
| .font(.body) | ||
| .foregroundStyle(Color.accentColor) | ||
| } | ||
| } | ||
| .padding() | ||
| .background(Color(.listForeground(modal: false))) | ||
| Divider() | ||
| } | ||
| } | ||
|
|
||
| var topTabView: some View { | ||
| GeometryReader { geometry in | ||
| HStack { | ||
| ForEach(BookingListTab.allCases, id: \.rawValue) { tab in | ||
| Button { | ||
| withAnimation(.easeInOut(duration: 0.3)) { | ||
| viewModel.selectedTab = tab | ||
| } | ||
| } label: { | ||
| Text(tab.title) | ||
| .font(.subheadline) | ||
| .foregroundStyle(viewModel.selectedTab == tab ? Color.accentColor : Color.primary) | ||
| } | ||
| .frame(maxWidth: .infinity) | ||
| .padding(.vertical, 12) | ||
| } | ||
| } | ||
| .overlay(alignment: .bottom) { | ||
| Color.accentColor | ||
| .frame(width: geometry.size.width / CGFloat(BookingListTab.allCases.count), | ||
| height: Layout.selectedTabIndicatorHeight) | ||
| .offset(x: tabIndicatorOffset(containerWidth: geometry.size.width, | ||
| tabCount: BookingListTab.allCases.count, | ||
| selectedIndex: viewModel.selectedTab.rawValue)) | ||
| .animation(.easeInOut(duration: 0.3), value: viewModel.selectedTab.rawValue) | ||
| } | ||
| } | ||
| .frame(height: Layout.topTabBarHeight) | ||
| .background(Color(.listForeground(modal: false))) | ||
| } | ||
|
|
||
| /// SwiftUI's coordinate system places (0,0) at the center of the container, so we need to: | ||
| /// 1. Calculate how far the selected tab is from the left edge | ||
| /// 2. Adjust for the center-based coordinate system | ||
| /// 3. Center the indicator within the selected tab | ||
| /// | ||
| func tabIndicatorOffset(containerWidth: CGFloat, tabCount: Int, selectedIndex: Int) -> CGFloat { | ||
| let tabWidth = containerWidth / CGFloat(tabCount) | ||
| let distanceFromLeftEdge = tabWidth * CGFloat(selectedIndex) | ||
| let adjustmentForCenterOrigin = containerWidth / 2 | ||
| let centerWithinTab = tabWidth / 2 | ||
|
|
||
| return distanceFromLeftEdge - adjustmentForCenterOrigin + centerWithinTab | ||
| } | ||
| } | ||
| private extension BookingListContainerView { | ||
| enum Layout { | ||
| static let topTabBarHeight: CGFloat = 44 | ||
| static let selectedTabIndicatorHeight: CGFloat = 3.0 | ||
| } | ||
|
|
||
| enum Localization { | ||
| static let viewTitle = NSLocalizedString( | ||
| "bookingListView.view.title", | ||
| value: "Bookings", | ||
| comment: "Title of the booking list view" | ||
| ) | ||
| static let sortBy = NSLocalizedString( | ||
| "bookingListView.sortBy", | ||
| value: "Sort by", | ||
| comment: "Button to select the order of the booking list" | ||
| ) | ||
| static let filter = NSLocalizedString( | ||
| "bookingListView.filter", | ||
| value: "Filter", | ||
| comment: "Button to filter the booking list" | ||
| ) | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
WooCommerce/Classes/Bookings/BookingList/BookingListContainerViewModel.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import Foundation | ||
|
|
||
| /// View model for `BookingListContainerView` | ||
| final class BookingListContainerViewModel: ObservableObject { | ||
| private let todayListViewModel: BookingListViewModel | ||
| private let upcomingListViewModel: BookingListViewModel | ||
| private let allListViewModel: BookingListViewModel | ||
|
|
||
| @Published var selectedTab: BookingListTab = .today | ||
|
|
||
| init(siteID: Int64) { | ||
| self.todayListViewModel = BookingListViewModel(siteID: siteID, type: .today) | ||
| self.upcomingListViewModel = BookingListViewModel(siteID: siteID, type: .upcoming) | ||
| self.allListViewModel = BookingListViewModel(siteID: siteID, type: .all) | ||
| } | ||
|
|
||
| func listViewModel(for tab: BookingListTab) -> BookingListViewModel { | ||
| switch tab { | ||
| case .today: | ||
| todayListViewModel | ||
| case .upcoming: | ||
| upcomingListViewModel | ||
| case .all: | ||
| allListViewModel | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum BookingListTab: Int, CaseIterable { | ||
| case today | ||
| case upcoming | ||
| case all | ||
|
|
||
| var title: String { | ||
| switch self { | ||
| case .today: Localization.today | ||
| case .upcoming: Localization.upcoming | ||
| case .all: Localization.all | ||
| } | ||
| } | ||
|
|
||
| private enum Localization { | ||
| static let today = NSLocalizedString( | ||
| "bookingListView.today", | ||
| value: "Today", | ||
| comment: "Tab title for today's bookings" | ||
| ) | ||
| static let upcoming = NSLocalizedString( | ||
| "bookingListView.upcoming", | ||
| value: "Upcoming", | ||
| comment: "Tab title for upcoming bookings" | ||
| ) | ||
| static let all = NSLocalizedString( | ||
| "bookingListView.all", | ||
| value: "All", | ||
| comment: "Tab title for all bookings" | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please describe the reasoning behind this approach of
How about keeping the header above switchable list views and using a TabView:
We have other places in the app where the TabView is used.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - this was a workaround for the refresh control in the booking list. When the list is added to a navigation view with large title, pulling down the list will also pull down the big title while the header (tab bar and filter bar) stays fixed at the top, which looks bad. Injecting the header to a section inside the scroll view and ask the LazyVStack to pin the section header helped pulling the header down as well.
I wanted to keep the states of the lists instead of re-rendering and loading bookings again upon switching tabs.
Thanks for the great idea! I didn't think about using TabView just for the content, and TIL about hiding the index view. Using the TabView fixes both the problems above for me: managing the tab views and keeping the refresh control under the header view, instead of above the large navigation title:
I updated the BookingListView to remove the now redundant workarounds for the header and refresh control. I also added a check to avoid loading the booking list again if it's not empty.
Please take another look when you can 🙏