Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ final class CollapsibleShipmentCardViewModel: ObservableObject, Identifiable {

var onSelectionChange: (() -> Void)?

var hasSelectedAnItem: Bool {
var selectedShipmentIds: [String] {
if mainShipmentRow.selected {
return true
if childShipmentRows.isNotEmpty {
return childShipmentRows.map { $0.shipmentId }
} else {
return [mainShipmentRow.shipmentId]
}
}

return childShipmentRows
.filter { $0.selected }
.isNotEmpty
.map(\.shipmentId)
}

init(parentShipmentId: String,
Expand Down Expand Up @@ -50,6 +54,7 @@ final class CollapsibleShipmentCardViewModel: ObservableObject, Identifiable {
func selectAll() {
mainShipmentRow.setSelected(true)
childShipmentRows.forEach({ $0.setSelected(true) })
onSelectionChange?()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import SwiftUI

struct MoveToShipmentNoticeViewModel {
enum MoveTo {
case existingShipment(index: Int)
case newShipment
}

let selectedItemsCount: Int
let existingShipmentsCount: Int
let currentShipmentIndex: Int?
let actionHandler: ((MoveTo) -> Void)
}

struct MoveToShipmentNotice: View {
let viewModel: MoveToShipmentNoticeViewModel

var body: some View {
HStack {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an adaptive stack instead to ensure the contents are readable when the font size is really large on a narrow device.

Text(String.localizedStringWithFormat(Localization.message, viewModel.selectedItemsCount))
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(Color(uiColor: .text))

Spacer()

if viewModel.existingShipmentsCount == 0 {
moveToNewShipment
} else {
menuWithExistingShipments
}
}
.padding(.horizontal, Layout.horizontalPadding)
.padding(.vertical, Layout.verticalPadding)
.background(.thickMaterial)
.cornerRadius(Layout.cornerRadius)
.shadow(color: .black.opacity(Layout.shadowColorOpacity),
radius: Layout.cornerRadius,
y: Layout.shadowYOffset)
}
}

private extension MoveToShipmentNotice {
var moveToNewShipment: some View {
Button {
viewModel.actionHandler(.newShipment)
} label: {
HStack(spacing: Layout.horizontalSpacing) {
Text(Localization.moveToNewShipment)
.font(.subheadline)
.fontWeight(.semibold)
}
.foregroundColor(Color(.accent))
}
}

var menuWithExistingShipments: some View {
Menu {
ForEach(0..<viewModel.existingShipmentsCount, id: \.self) { index in
if viewModel.currentShipmentIndex != index {
Button(String.localizedStringWithFormat(Localization.shipment, index + 1), action: {
viewModel.actionHandler(.existingShipment(index: index))
})
}
}

Button(Localization.newShipment, action: {
viewModel.actionHandler(.newShipment)
})
} label: {
HStack(spacing: Layout.horizontalSpacing) {
Text(Localization.moveTo)
.font(.subheadline)
.fontWeight(.semibold)

Image(systemName: "chevron.up.chevron.down")
}
.foregroundColor(Color(.accent))
}
.environment(\.menuOrder, .fixed)
}
}

private extension MoveToShipmentNotice {
enum Layout {
static let horizontalPadding: CGFloat = 16
static let verticalPadding: CGFloat = 22
static let horizontalSpacing: CGFloat = 8
static let cornerRadius: CGFloat = 8
static let shadowYOffset: CGFloat = 2
static let shadowColorOpacity: CGFloat = 0.16
}

enum Localization {
static let message = NSLocalizedString(
"wooShippingSplitShipments.MoveToShipmentNotice.title",
value: "%1$d selected",
comment: "The number of selected items in split shipments flow. %1$d is the number of selected items. Reads like: 2 selected"
)
static let shipment = NSLocalizedString(
"wooShippingSplitShipments.MoveToShipmentNotice.shipment",
value: "Shipment %1$d",
comment: "Label used in the button to select the shipment in split shipments flow. %1$d is the shipment number. Reads like: Shipment 1"
)
static let moveTo = NSLocalizedString(
"wooShippingSplitShipments.MoveToShipmentNotice.moveTo",
value: "Move to",
comment: "Button to move selected items to a shipment in split shipments flow"
)
static let moveToNewShipment = NSLocalizedString(
"wooShippingSplitShipments.MoveToShipmentNotice.moveToNewShipment",
value: "Move to new shipment",
comment: "Button to move selected items to a new shipment in split shipments flow"
)
static let newShipment = NSLocalizedString(
"wooShippingSplitShipments.MoveToShipmentNotice.newShipment",
value: "New shipment",
comment: "Title of the button to move selected items to a new shipment in split shipments flow"
)
}
}

#Preview {
MoveToShipmentNotice(viewModel: MoveToShipmentNoticeViewModel(selectedItemsCount: 4,
existingShipmentsCount: 3,
currentShipmentIndex: 1,
actionHandler: { _ in }))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ struct WooShippingSplitShipmentsDetailView: View {

var body: some View {
NavigationView {
ScrollView {
VStack(alignment: .leading, spacing: Layout.contentPadding) {
AdaptiveStack(horizontalAlignment: .leading) {
Text(viewModel.itemsCountLabel)
.headlineStyle()
Spacer()
Text(viewModel.itemsDetailLabel)
.foregroundStyle(Color(.textSubtle))
}
ZStack(alignment: .bottom) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a VStack instead, because otherwise the notice would cover the scroll view when it's long enough:

ScrollView {
VStack(alignment: .leading, spacing: Layout.contentPadding) {
AdaptiveStack(horizontalAlignment: .leading) {
Text(viewModel.itemsCountLabel)
.headlineStyle()
Spacer()
Text(viewModel.itemsDetailLabel)
.foregroundStyle(Color(.textSubtle))
}

VStack(spacing: Layout.verticalSpacing) {
ForEach(viewModel.shipmentCardViewModels) { item in
CollapsibleShipmentCard(viewModel: item)
VStack(spacing: Layout.verticalSpacing) {
ForEach(viewModel.shipmentCardViewModels) { item in
CollapsibleShipmentCard(viewModel: item)
}
}
}
.padding(Layout.contentPadding)
}
.padding(Layout.contentPadding)

noticeStack
.padding(Layout.contentPadding)
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(Localization.title)
Expand All @@ -41,20 +46,73 @@ struct WooShippingSplitShipmentsDetailView: View {
}
}
}
.notice($viewModel.instructionsNotice, autoDismiss: false)
.onAppear {
viewModel.onAppear()
}
}
}

private extension WooShippingSplitShipmentsDetailView {
var noticeStack: some View {
VStack(spacing: Layout.contentPadding) {
if let message = viewModel.instructions {
InstructionsSnackbar(message: message) {
viewModel.dismissInstructions()
}
}

if let moveTo = viewModel.moveToNoticeViewModel {
MoveToShipmentNotice(viewModel: moveTo)
}
}
}
}

private struct InstructionsSnackbar: View {
let message: String
let actionHandler: () -> Void

var body: some View {
HStack(alignment: .top, spacing: Layout.hSpacing) {
BoldableTextView(message)
.foregroundStyle(Color(.textInverted))

Spacer()

Button {
actionHandler()
} label: {
Image(systemName: "xmark")
.foregroundStyle(Color(.withColorStudio(.gray)))
}
}
.padding(WooShippingSplitShipmentsDetailView.Layout.contentPadding)
.background {
RoundedRectangle(cornerRadius: WooShippingSplitShipmentsDetailView.Layout.cornerRadius)
.fill(Color(.text))
.shadow(color: Color(.text).opacity(Layout.shadowColorOpacity),
radius: WooShippingSplitShipmentsDetailView.Layout.shadowRadius,
y: WooShippingSplitShipmentsDetailView.Layout.shadowYOffset)
}
}

private enum Layout {
static let hSpacing: CGFloat = 8
static let shadowColorOpacity: CGFloat = 0.16
}
}

fileprivate extension WooShippingSplitShipmentsDetailView {
enum Layout {
static let contentPadding: CGFloat = 16
static let borderCornerRadius: CGFloat = 8
static let shadowRadius: CGFloat = 8
static let shadowYOffset: CGFloat = 2
static let borderWidth: CGFloat = 0.5
static let verticalSpacing: CGFloat = 8
static let cornerRadius: CGFloat = 8
}

enum Localization {
static let title = NSLocalizedString(
"wooShippingSplitShipmentsDetailView.title",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject {

let shipmentCardViewModels: [CollapsibleShipmentCardViewModel]

@Published var instructionsNotice: Notice?
@Published private(set) var moveToNoticeViewModel: MoveToShipmentNoticeViewModel?

@Published private(set) var instructions: String?
private var dismissedInstructions: Bool = false


init(order: Order,
config: WooShippingConfig,
Expand Down Expand Up @@ -75,42 +79,62 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject {

func onAppear() {
showInstructionsNotice()
showMoveToNotice()
}

func selectAll() {
shipmentCardViewModels.forEach {
$0.selectAll()
}
}

func dismissInstructions() {
instructions = nil
dismissedInstructions = true
}
}

private extension WooShippingSplitShipmentsViewModel {
func configureSelectionCallback() {
shipmentCardViewModels.forEach { viewModel in
viewModel.onSelectionChange = { [weak self] in
self?.checkSelectionAndHideInstructions()
self?.showMoveToNotice()
}
}
}

func showInstructionsNotice() {
if hasSelectedAnItem() == false {
instructionsNotice = Notice(message: Localization.SelectionInstructionsNotice.message,
feedbackType: .success,
actionTitle: Localization.SelectionInstructionsNotice.dismiss) { [weak self] in
self?.instructionsNotice = nil
}
if !dismissedInstructions {
instructions = Localization.SelectionInstructionsNotice.message
}
}

func checkSelectionAndHideInstructions() {
if hasSelectedAnItem() {
instructionsNotice = nil
func showMoveToNotice() {
let selectedItemsCount = {
shipmentCardViewModels.map { $0.selectedShipmentIds.count }.reduce(0, +)
}()

guard selectedItemsCount > 0 else {
return self.moveToNoticeViewModel = nil
}
}

func hasSelectedAnItem() -> Bool {
shipmentCardViewModels.contains(where: { $0.hasSelectedAnItem })
// TODO: Use count and index values from shipment tabs
moveToNoticeViewModel = MoveToShipmentNoticeViewModel(selectedItemsCount: selectedItemsCount,
existingShipmentsCount: 3,
currentShipmentIndex: 2,
actionHandler: { [weak self] moveTo in
guard let self else { return }

self.moveToNoticeViewModel = nil
self.instructions = nil

switch moveTo {
case .existingShipment:
break
case .newShipment:
break
}
})
}

/// Configures the labels in the section header.
Expand Down
4 changes: 4 additions & 0 deletions WooCommerce/WooCommerce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,7 @@
EEBB9B3B2D8E5071008D6CE5 /* SelectableShipmentRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBB9B3A2D8E5058008D6CE5 /* SelectableShipmentRow.swift */; };
EEBB9B3D2D8E5099008D6CE5 /* SelectableShipmentRowViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBB9B3C2D8E508E008D6CE5 /* SelectableShipmentRowViewModel.swift */; };
EEBB9B402D8FE5B6008D6CE5 /* WooShippingSplitShipmentsViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBB9B3F2D8FE5B4008D6CE5 /* WooShippingSplitShipmentsViewModelTests.swift */; };
EEBBC3BC2D92A1E0008D6CE5 /* MoveToShipmentNotice.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBBC3BB2D92A1C6008D6CE5 /* MoveToShipmentNotice.swift */; };
EEBDF7DA2A2EF69B00EFEF47 /* ShareProductCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBDF7D92A2EF69B00EFEF47 /* ShareProductCoordinator.swift */; };
EEBDF7DF2A2F674100EFEF47 /* ShareProductAIEligibilityChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBDF7DE2A2F674100EFEF47 /* ShareProductAIEligibilityChecker.swift */; };
EEBDF7E22A2F685C00EFEF47 /* DefaultShareProductAIEligibilityCheckerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBDF7E12A2F685C00EFEF47 /* DefaultShareProductAIEligibilityCheckerTests.swift */; };
Expand Down Expand Up @@ -6255,6 +6256,7 @@
EEBB9B3A2D8E5058008D6CE5 /* SelectableShipmentRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectableShipmentRow.swift; sourceTree = "<group>"; };
EEBB9B3C2D8E508E008D6CE5 /* SelectableShipmentRowViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectableShipmentRowViewModel.swift; sourceTree = "<group>"; };
EEBB9B3F2D8FE5B4008D6CE5 /* WooShippingSplitShipmentsViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WooShippingSplitShipmentsViewModelTests.swift; sourceTree = "<group>"; };
EEBBC3BB2D92A1C6008D6CE5 /* MoveToShipmentNotice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveToShipmentNotice.swift; sourceTree = "<group>"; };
EEBDF7D92A2EF69B00EFEF47 /* ShareProductCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareProductCoordinator.swift; sourceTree = "<group>"; };
EEBDF7DE2A2F674100EFEF47 /* ShareProductAIEligibilityChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareProductAIEligibilityChecker.swift; sourceTree = "<group>"; };
EEBDF7E12A2F685C00EFEF47 /* DefaultShareProductAIEligibilityCheckerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultShareProductAIEligibilityCheckerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -14013,6 +14015,7 @@
EE7E75A62D83EAD200E6FF5B /* WooShipping Split Shipments */ = {
isa = PBXGroup;
children = (
EEBBC3BB2D92A1C6008D6CE5 /* MoveToShipmentNotice.swift */,
EEBB9B3C2D8E508E008D6CE5 /* SelectableShipmentRowViewModel.swift */,
EEBB9B3A2D8E5058008D6CE5 /* SelectableShipmentRow.swift */,
EEBB81702D8C0834008D6CE5 /* CollapsibleShipmentCard.swift */,
Expand Down Expand Up @@ -16033,6 +16036,7 @@
CEE113952CFA2F7700F53E30 /* WooShippingSelectedPackageView.swift in Sources */,
454B28BE23BF63C600CD2091 /* DateIntervalFormatter+Helpers.swift in Sources */,
AE6DBE3B2732CAAD00957E7A /* AdaptiveStack.swift in Sources */,
EEBBC3BC2D92A1E0008D6CE5 /* MoveToShipmentNotice.swift in Sources */,
03A6C18628B8CC7F00AADF23 /* InPersonPaymentsOnboardingErrorButtonViewModel.swift in Sources */,
024DF3052372ADCD006658FE /* KeyboardScrollable.swift in Sources */,
BAE4F8432734325C00871344 /* SettingsViewModel.swift in Sources */,
Expand Down