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

[Shipping labels] Use backend data for saved packages #14517

Merged
merged 54 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
a9c0818
Update Model.swift
bozidarsevo Nov 25, 2024
aa4a6d3
Parse special dimensions data
bozidarsevo Nov 25, 2024
3a50a90
Update WooShippingSavedPredefinedPackage.swift.swift
bozidarsevo Nov 25, 2024
52468c5
Add WooShippingAddPackageViewModel
bozidarsevo Nov 25, 2024
88fa038
Start using WooShippingAddPackageViewModel
bozidarsevo Nov 25, 2024
1923fa1
Start using WooShippingAddPackageViewModel
bozidarsevo Nov 25, 2024
d63850f
Merge branch 'issue/14506-Use-backend-list-of-packages' of https://gi…
bozidarsevo Nov 25, 2024
07e8799
Using carriers data from backend
bozidarsevo Nov 26, 2024
509e96b
Remove WooShippingPackagesRepository
bozidarsevo Nov 26, 2024
6fd796f
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 26, 2024
1943cb0
Update WooSavedPackagesSelectionView.swift
bozidarsevo Nov 26, 2024
c9a14b0
Update WooShippingAddPackageView.swift
bozidarsevo Nov 26, 2024
849d724
Remove WooShippingPackagesRepository (#14519)
bozidarsevo Nov 26, 2024
75fd833
Update selection and staring
bozidarsevo Nov 26, 2024
b14146d
Merge branch 'issue/14506-Use-backend-list-of-packages-carriers' of h…
bozidarsevo Nov 26, 2024
f7ca4fb
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 27, 2024
037e509
Merge branch 'issue/14506-Use-backend-list-of-packages' of https://gi…
bozidarsevo Nov 27, 2024
563aa47
Add loading indicator to carriers view
bozidarsevo Nov 27, 2024
412b61e
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 27, 2024
f9ebfbe
Update project.pbxproj
bozidarsevo Nov 27, 2024
fab22b3
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 27, 2024
faef58d
Update project.pbxproj
bozidarsevo Nov 27, 2024
ab4d25c
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 28, 2024
26d6c0b
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 28, 2024
9e9da76
Update project.pbxproj
bozidarsevo Nov 28, 2024
efe1078
Update project.pbxproj
bozidarsevo Nov 28, 2024
28a84b2
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 28, 2024
84d13ee
Update project.pbxproj
bozidarsevo Nov 28, 2024
cf1ee49
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 28, 2024
9dd9050
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 28, 2024
36b7890
Update WooCarrierPackagesSelectionViewModelTests.swift
bozidarsevo Nov 28, 2024
f93f5cc
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 28, 2024
48a2316
Update project.pbxproj
bozidarsevo Nov 28, 2024
87a86a0
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 28, 2024
9dc0041
Add one more dimensions fallback
bozidarsevo Nov 28, 2024
a88aaab
Add one more dimensions fallback
bozidarsevo Nov 28, 2024
1d265b9
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 28, 2024
f51e2dd
Update WooSavedPackagesSelectionView.swift
bozidarsevo Nov 28, 2024
2cff553
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Nov 29, 2024
790844f
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Nov 29, 2024
33d11ac
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Dec 2, 2024
691dd6f
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Dec 2, 2024
6afa8fa
Update project.pbxproj
bozidarsevo Dec 2, 2024
ca675d3
Update Model.swift
bozidarsevo Dec 2, 2024
45402ec
Update project.pbxproj
bozidarsevo Dec 2, 2024
63c93ef
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Dec 2, 2024
debcbb7
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Dec 2, 2024
bab0bcb
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Dec 2, 2024
d824f50
Merge branch 'issue/14506-Use-backend-list-of-packages' into issue/14…
bozidarsevo Dec 2, 2024
662a872
[Shipping labels] Use backend data for carrier (predefined) packages …
bozidarsevo Dec 2, 2024
9e01c27
Merge branch 'issue/14484-account-settings-for-UI-custom-packages' in…
bozidarsevo Dec 2, 2024
08e1b10
Merge branch 'trunk' into issue/14506-Use-backend-list-of-packages
bozidarsevo Dec 2, 2024
4b6b20e
Merge branch 'trunk' into issue/14506-Use-backend-list-of-packages
bozidarsevo Dec 3, 2024
848163d
Merge branch 'trunk' into issue/14506-Use-backend-list-of-packages
bozidarsevo Dec 3, 2024
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 @@ -4122,7 +4122,7 @@ extension Networking.WooShippingPackagesResponse {
storeOptions: CopiableProp<ShippingLabelStoreOptions> = .copy,
customPackages: CopiableProp<[WooShippingCustomPackage]> = .copy,
savedPredefinedPackages: CopiableProp<[WooShippingSavedPredefinedPackage]> = .copy,
allPredefinedOptions: CopiableProp<[WooShippingPredefinedOption]> = .copy
allPredefinedOptions: CopiableProp<[WooShippingCarrierPredefinedOptions]> = .copy
) -> Networking.WooShippingPackagesResponse {
let storeOptions = storeOptions ?? self.storeOptions
let customPackages = customPackages ?? self.customPackages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public struct WooShippingPredefinedOption: Equatable, GeneratedFakeable {
}
}

/// Represents a predefined carrier option in Shipping Labels for the WooCommerce Shipping extension.
///
public struct WooShippingCarrierPredefinedOptions: Equatable, GeneratedFakeable {
public let carrierID: String
public let predefinedOptions: [WooShippingPredefinedOption]
}

// MARK: Decodable
extension WooShippingPredefinedOption: Decodable {
public init(from decoder: Decoder) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ extension WooShippingPredefinedPackage: Decodable {
let id = try container.decode(String.self, forKey: .id)
let name = try container.decode(String.self, forKey: .name)
let isLetter = try container.decodeIfPresent(Bool.self, forKey: .isLetter) ?? false
let dimensions = try container.decode(String.self, forKey: .dimensions)
var dimensions: String = ""
if let dimensionsString = try? container.decodeIfPresent(String.self, forKey: .dimensions) {
dimensions = dimensionsString
}
else if let dimensionsDict = try? container.decodeIfPresent([String: String].self, forKey: .dimensions) {
if let dimensionsOuter = dimensionsDict["outer"] {
dimensions = dimensionsOuter
}
else if let dimensionsInner = dimensionsDict["inner"] {
dimensions = dimensionsInner
}
}
else if let outerDimensionsString = try? container.decodeIfPresent(String.self, forKey: .outerDimensions) {
dimensions = outerDimensionsString
}
let groupId = try container.decode(String.self, forKey: .groupId)
var boxWeight: String = ""
// Looks like some endpoints have boxWeight as String and some as Double
Expand All @@ -86,7 +100,10 @@ extension WooShippingPredefinedPackage: Decodable {
case name
case isLetter = "is_letter"
case dimensions
case outer
case inner
case groupId = "group_id"
case boxWeight = "box_weight"
case outerDimensions = "outer_dimensions"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import WooFoundation
/// Container struct for a predefined Shipping Label Packages for the WooCommerce Shipping extension with group title and provider identifier.
///
public struct WooShippingSavedPredefinedPackage: Equatable, GeneratedFakeable, Identifiable {
let groupTitle: String
let providerID: String
let package: WooShippingPredefinedPackage
public let groupTitle: String
public let providerID: String
public let package: WooShippingPredefinedPackage

public var id: String {
return package.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public struct WooShippingPackagesResponse: Equatable, GeneratedFakeable, Generat
public let savedPredefinedPackages: [WooShippingSavedPredefinedPackage]

/// All predefined options
public let allPredefinedOptions: [WooShippingPredefinedOption]
public let allPredefinedOptions: [WooShippingCarrierPredefinedOptions]

public init(storeOptions: ShippingLabelStoreOptions,
customPackages: [WooShippingCustomPackage],
savedPredefinedPackages: [WooShippingSavedPredefinedPackage],
allPredefinedOptions: [WooShippingPredefinedOption]) {
allPredefinedOptions: [WooShippingCarrierPredefinedOptions]) {
self.storeOptions = storeOptions
self.customPackages = customPackages
self.savedPredefinedPackages = savedPredefinedPackages
Expand All @@ -44,21 +44,23 @@ extension WooShippingPackagesResponse: Decodable {
}

let allPredefinedPackagesData: [String: AnyCodable] = try packagesData.decode([String: AnyCodable].self, forKey: .predefined)
var allPredefinedOptions: [WooShippingPredefinedOption] = []
var allPredefinedOptions: [WooShippingCarrierPredefinedOptions] = []
var allSavedPredefinedPackages: [WooShippingSavedPredefinedPackage] = []

allPredefinedPackagesData.forEach { (key, value) in
// key is a carrier id, for example "usps"
if let provider: [String: Any]? = try? value.toDictionary() {
var providerOptions: [WooShippingPredefinedOption] = []
provider?.forEach({ (providerKey, providerValue) in
// providerKey is package group id, for example "pri_flat_boxes"
let providerValueDict = providerValue as? [String: Any]
let title: String = providerValueDict?["title"] as? String ?? ""
let packages = WooShippingPackagesResponse.getAllPredefinedPackages(packageDefinitions: providerValueDict)
let option = WooShippingPredefinedOption(title: title, providerID: key, predefinedPackages: packages)
allPredefinedOptions.append(option)
providerOptions.append(option)
allSavedPredefinedPackages.append(contentsOf: WooShippingPackagesResponse.savedPackages(savedOptions: savedPredefinedOptions, option: option))
})
allPredefinedOptions.append(WooShippingCarrierPredefinedOptions(carrierID: key, predefinedOptions: providerOptions))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,47 @@ struct WooPackageGroup {
}

struct WooCarrierPackagesView: View {
@ObservedObject private var viewModel: WooCarrierPackagesSelectionViewModel

init(viewModel: WooCarrierPackagesSelectionViewModel) {
self.viewModel = viewModel
}
let carrierTab: WooShippingCarrierPackages
@Binding var selectedPackageId: String? // Track the selected package index
@Binding var starredPackages: Set<String>
let tapAction: (String) -> Void
let starAction: (String) -> Void

var body: some View {
List {
if let packageGroups = viewModel.selectedCarrierTab?.packageGroups {
ForEach(packageGroups, id: \.id) { packageGroup in
Section {
ForEach(packageGroup.packages, id: \.id) { package in
PackageOptionView(
isSelected: viewModel.selectedPackageId == package.id,
package: package,
showTopDivider: false,
showSource: false,
tapAction: {
viewModel.selectedPackageId = viewModel.selectedPackageId == package.id ? nil : package.id
},
starAction: {
// Just temporary, will be replaced with proper logic
if viewModel.isPackageStarred(package) {
Task {
await viewModel.unstarPackage(package)
}
}
else {
Task {
await viewModel.starPackage(package)
}
}
},
starred: viewModel.isPackageStarred(package)
)
.alignmentGuide(.listRowSeparatorLeading) { _ in
return 16
}
}
} header: {
HStack {
Text(packageGroup.name.uppercased())
.foregroundColor(.secondary)
.fontWeight(.regular)
.multilineTextAlignment(.leading)
Spacer()
ForEach(carrierTab.packageGroups, id: \.id) { packageGroup in
Section {
ForEach(packageGroup.packages, id: \.id) { package in
PackageOptionView(
isSelected: selectedPackageId == package.id,
package: package,
showTopDivider: false,
showSource: false,
tapAction: {
tapAction(package.id)
},
starAction: {
starAction(package.id)
// Just temporary, will be replaced with proper logic
},
starred: starredPackages.contains(package.id)
)
.alignmentGuide(.listRowSeparatorLeading) { _ in
return 16
}
.padding(.horizontal)
.background(Color.clear)
}
.listRowInsets(.zero)
} header: {
HStack {
Text(packageGroup.name.uppercased())
.foregroundColor(.secondary)
.fontWeight(.regular)
.multilineTextAlignment(.leading)
Spacer()
}
.padding(.horizontal)
.background(Color.clear)
}
.listRowInsets(.zero)
}
}
.listStyle(.plain)
Expand All @@ -84,21 +73,27 @@ struct WooCarrierPackagesSelectionView: View {
static let tabPadding: CGFloat = 9.0
}

@ObservedObject private var viewModel: WooCarrierPackagesSelectionViewModel
@ObservedObject private var viewModel: WooShippingAddPackageViewModel
let addPackageAction: (WooShippingPackageDataRepresentable) -> Void

init(viewModel: WooCarrierPackagesSelectionViewModel,
init(viewModel: WooShippingAddPackageViewModel,
addPackageAction: @escaping (WooShippingPackageDataRepresentable) -> Void) {
self.viewModel = viewModel
self.addPackageAction = addPackageAction
}

var body: some View {
VStack(spacing: 0) {
if viewModel.selectedTabIndex != nil, viewModel.tabs.isNotEmpty {
TopTabView(tabs: viewModel.tabs,
if viewModel.isLoadingPackages {
// TODO: think of a better progress/loading indicator
ProgressView()
.progressViewStyle(.circular)
.padding()
}
if viewModel.selectedCarriersTabIndex != nil, viewModel.carrierTabs.isNotEmpty {
TopTabView(tabs: viewModel.carrierTabs,
showContent: .constant(false),
selectedTabIndex: $viewModel.selectedTabIndex,
selectedTabIndex: $viewModel.selectedCarriersTabIndex,
tabsContainerHorizontalPadding: nil,
selectedStateColor: Color.accentColor,
unselectedStateColor: .secondary,
Expand All @@ -108,23 +103,32 @@ struct WooCarrierPackagesSelectionView: View {
tabItemContentHorizontalPadding: Constants.tabItemContentHorizontalPadding,
tabItemContentVerticalPadding: Constants.tabItemContentVerticalPadding)
}
if viewModel.selectedCarrierTab != nil {
WooCarrierPackagesView(viewModel: viewModel)
if let selectedCarrierTab = viewModel.selectedCarrierTab {
WooCarrierPackagesView(carrierTab: selectedCarrierTab,
selectedPackageId: $viewModel.selectedCarriersPackageId,
starredPackages: $viewModel.starredCarriersPackages,
tapAction: { packageID in
viewModel.selectedCarriersPackageId = viewModel.selectedCarriersPackageId == packageID ? nil : packageID
}, starAction: { packageID in
Task {
await viewModel.starUnstarPackage(packageID)
}
})
}
Spacer()
Divider()
Button(WooShippingAddPackageView.Localization.addPackage) {
addPackageButtonTapped()
}
.disabled(viewModel.selectedPackageId == nil)
.disabled(viewModel.selectedCarriersPackageId == nil)
.buttonStyle(PrimaryButtonStyle())
.padding()
}
}

private func addPackageButtonTapped() {
// call addPackageAction with data from selected package
guard let selectedPackage = viewModel.selectedPackage else { return }
guard let selectedPackage = viewModel.selectedCarriersPackage else { return }

addPackageAction(selectedPackage)
}
Expand Down

This file was deleted.

Loading