diff --git a/pkg/tui/cart.go b/pkg/tui/cart.go index 1e68dfb..a7c8fa1 100644 --- a/pkg/tui/cart.go +++ b/pkg/tui/cart.go @@ -28,7 +28,7 @@ func (m Model) updateCartViewport() Model { } func (m Model) generateCartContent() string { - if len(m.Cart) == 0 { + if m.IsCartEmpty() { emptyStyle := m.theme.TextDim(). Align(lipgloss.Center). Width(m.widthContent). @@ -179,7 +179,7 @@ func (m Model) CartUpdate(msg tea.Msg) (Model, tea.Cmd) { return m, m.syncCartItemCmd(coffeeID, newQty) } case "p", "enter": - if len(m.Cart) > 0 { + if !m.IsCartEmpty() { return m.ShippingSwitch() } } diff --git a/pkg/tui/confirmation.go b/pkg/tui/confirmation.go index 156c3a2..4563106 100644 --- a/pkg/tui/confirmation.go +++ b/pkg/tui/confirmation.go @@ -150,10 +150,8 @@ func (m Model) generateReviewContent() string { } view.WriteString("\n") - subtotal := 0 - for _, item := range m.GetCartItemsSlice() { - subtotal += item.Coffee.Price * item.Quantity - } + subtotal := m.CalculateSubtotal() + view.WriteString(fmt.Sprintf("subtotal: %s\n", formatUSD(subtotal))) view.WriteString(m.theme.TextAccent().Render(fmt.Sprintf("total: %s", formatUSD(subtotal))) + "\n") view.WriteString("\n") diff --git a/pkg/tui/footer.go b/pkg/tui/footer.go index b731b75..78f258f 100644 --- a/pkg/tui/footer.go +++ b/pkg/tui/footer.go @@ -135,7 +135,7 @@ func (m Model) BuildFooter() string { } } else if m.currentPage == cartPage { // In cart view, show proceed option - if len(m.Cart) > 0 { + if !m.IsCartEmpty() { footerText = fmt.Sprintf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", keybindStyle.Render("j/k"), descStyle.Render("items"), diff --git a/pkg/tui/header.go b/pkg/tui/header.go index dcc9278..9771726 100644 --- a/pkg/tui/header.go +++ b/pkg/tui/header.go @@ -8,12 +8,8 @@ import ( func (m Model) BuildHeader() string { // Calculate cart total and item count - total := 0 - itemCount := 0 - for _, item := range m.Cart { - total += item.Quantity * item.Coffee.Price - itemCount += item.Quantity - } + total := m.CalculateSubtotal() + itemCount := m.CartItemCount() // Define colors grayStyle := m.theme.TextBody() diff --git a/pkg/tui/menu.go b/pkg/tui/menu.go index 5a9206a..ba748f2 100644 --- a/pkg/tui/menu.go +++ b/pkg/tui/menu.go @@ -32,12 +32,8 @@ func (m Model) BuildMenuView() string { hint := base.Render("press esc to close") - itemCount := 0 - total := 0 - for _, item := range m.Cart { - total += item.Quantity * item.Coffee.Price - itemCount += item.Quantity - } + total := m.CalculateSubtotal() + itemCount := m.CartItemCount() cartHint := base.Render(fmt.Sprintf("cart $%.2f [%d]", float64(total)/100, itemCount)) diff --git a/pkg/tui/model.go b/pkg/tui/model.go index 2e6e5b7..e052331 100644 --- a/pkg/tui/model.go +++ b/pkg/tui/model.go @@ -755,6 +755,29 @@ func (m Model) GetCartItemsSlice() []*models.CartItem { return items } +// IsCartEmpty returns true when the cart has no items +func (m Model) IsCartEmpty() bool { + return len(m.Cart) == 0 +} + +// CartItemCount returns the total number of items (sum of quanities) in the cart +func (m Model) CartItemCount() int { + count := 0 + for _, item := range m.Cart { + count += item.Quantity + } + return count +} + +// CalculateSubtotal returns the cart subtotal in cents +func (m Model) CalculateSubtotal() int { + subtotal := 0 + for _, item := range m.Cart { + subtotal += item.Coffee.Price * item.Quantity + } + return subtotal +} + // NewModel creates a new model using the default renderer. func NewModel(username string) Model { return newModelWithRenderer(username, lipgloss.DefaultRenderer())