Skip to content
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

[Woo POS] Adjust pagination height threshold. Fix GhostItemCardView rendering when no more items. #14234

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
8 changes: 5 additions & 3 deletions WooCommerce/Classes/POS/Presentation/ItemListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private extension ItemListView {
})
}
GhostItemCardView()
.renderedIf(viewModel.state == .loading)
.renderedIf(viewModel.state == .loading && viewModel.hasMoreItems)
}
.padding(.bottom, floatingControlAreaSize.height)
.padding(.horizontal, Constants.itemListPadding)
Expand All @@ -141,8 +141,8 @@ private extension ItemListView {
if viewModel.state == .loading {
return
}
let viewHeight = UIScreen.main.bounds.height
if maxY < viewHeight {
let threshold = Constants.viewHeight * Constants.scrollThresholdMultiplier
if maxY < threshold {
Task {
await viewModel.loadNextItems()
}
Expand Down Expand Up @@ -207,6 +207,8 @@ private extension ItemListView {
static let iconPadding: CGFloat = 26
static let itemListPadding: CGFloat = 16
static let bannerCardPadding: CGFloat = 16
static let viewHeight: CGFloat = UIScreen.main.bounds.height
static let scrollThresholdMultiplier: CGFloat = 1.7
staskus marked this conversation as resolved.
Show resolved Hide resolved
}

enum Localization {
Expand Down
3 changes: 3 additions & 0 deletions WooCommerce/Classes/POS/ViewModels/ItemListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class ItemListViewModel: ItemListViewModelProtocol {
@Published var showSimpleProductsModal: Bool = false

@Published private var currentPage: Int = Constants.initialPage
@Published private(set) var hasMoreItems: Bool = true

var shouldShowHeaderBanner: Bool {
// The banner it's shown as long as it hasn't already been dismissed once:
Expand Down Expand Up @@ -44,6 +45,7 @@ final class ItemListViewModel: ItemListViewModelProtocol {
state = .loading
items = try await itemProvider.providePointOfSaleItems(pageNumber: currentPage)
if items.count == 0 {
hasMoreItems = false
state = .empty
} else {
state = .loaded(items)
Expand Down Expand Up @@ -71,6 +73,7 @@ final class ItemListViewModel: ItemListViewModelProtocol {
}
// If there are no new items, we just return what was already in memory
if uniqueNewItems.count == 0 {
hasMoreItems = false
state = .loaded(items)
} else {
items.append(contentsOf: uniqueNewItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,41 @@ final class ItemListViewModelTests: XCTestCase {

XCTAssertTrue(sut.showSimpleProductsModal)
}

func test_viewModel_when_has_items_then_hasMoreItems_is_true() async {
// Given/When
await sut.loadInitialItems()

// Then
XCTAssertTrue(sut.hasMoreItems)
}

func test_viewModel_when_has_no_items_then_hasMoreItems_is_false() async {
// Given
let itemProvider = MockPOSItemProvider()
itemProvider.shouldReturnZeroItems = true
let sut = ItemListViewModel(itemProvider: itemProvider)

// When
await sut.loadInitialItems()

// Then
XCTAssertFalse(sut.hasMoreItems)
}

func test_viewModel_when_loadNextItems_has_no_new_items_then_hasMoreItems_is_false() async {
// Given/When
await sut.loadInitialItems()

XCTAssertTrue(sut.hasMoreItems)
XCTAssertEqual(sut.items.count, 2, "Initial state")

await sut.loadNextItems()

// Then
XCTAssertEqual(sut.items.count, 2, "Check there are no new items")
XCTAssertFalse(sut.hasMoreItems)
}
}

private extension ItemListViewModelTests {
Expand Down
Loading