Skip to content

Commit 5a992df

Browse files
committed
Adding tests
1 parent 4b7e364 commit 5a992df

File tree

10 files changed

+233
-123
lines changed

10 files changed

+233
-123
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Yosemite
2+
import UIKit
3+
4+
class NativeProductDetailCoordinator: ProductDetailCoordinator {
5+
func viewController(
6+
product: Product,
7+
presentationStyle: ProductDetailPresenter.PresentationStyle,
8+
forceReadOnly: Bool,
9+
onDeleteCompletion: (() -> Void)? = nil) -> UIViewController {
10+
return ProductDetailsFactory.productDetails(product: product,
11+
presentationStyle: presentationStyle.asProductFormPresentationStyle,
12+
forceReadOnly: false,
13+
onDeleteCompletion: onDeleteCompletion ?? {})
14+
}
15+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import UIKit
2+
import Yosemite
3+
4+
final class ProductDetailPresenter {
5+
enum PresentationStyle {
6+
case undefined
7+
case contained(containerViewController: () -> UIViewController?)
8+
9+
var asProductFormPresentationStyle: ProductFormPresentationStyle {
10+
switch self {
11+
case .contained(let containerViewController):
12+
.contained(containerViewController: containerViewController)
13+
case .undefined:
14+
.navigationStack
15+
}
16+
}
17+
}
18+
19+
static var shared = ProductDetailPresenter()
20+
21+
private let ciabChecker: CIABEligibilityCheckerProtocol
22+
private let coordinatorFactory: ProductDetailCoordinatorFactoryProtocol
23+
24+
init(ciabChecker: CIABEligibilityCheckerProtocol = CIABEligibilityChecker(),
25+
coordinatorFactory: ProductDetailCoordinatorFactoryProtocol = ProductDetailCoordinatorFactory.default) {
26+
self.ciabChecker = ciabChecker
27+
self.coordinatorFactory = coordinatorFactory
28+
}
29+
30+
func viewController(product: Product,
31+
presentationStyle: PresentationStyle = .undefined,
32+
forceReadOnly: Bool,
33+
onDeleteCompletion: (() -> Void)? = nil) -> UIViewController {
34+
35+
let coordinator: ProductDetailCoordinator
36+
if shouldOpenInWeb(product: product) {
37+
coordinator = coordinatorFactory.webCoordinator()
38+
} else {
39+
coordinator = coordinatorFactory.nativeCoordinator()
40+
}
41+
42+
let viewController = coordinator.viewController(product: product,
43+
presentationStyle: presentationStyle,
44+
forceReadOnly: forceReadOnly,
45+
onDeleteCompletion: onDeleteCompletion)
46+
47+
return viewController
48+
}
49+
50+
private func shouldOpenInWeb(product: Product) -> Bool {
51+
return ciabChecker.isCurrentSiteCIAB && product.productType == .booking
52+
}
53+
}
54+
55+
protocol ProductDetailCoordinator {
56+
func viewController(
57+
product: Product,
58+
presentationStyle: ProductDetailPresenter.PresentationStyle,
59+
forceReadOnly: Bool,
60+
onDeleteCompletion: (() -> Void)?) -> UIViewController
61+
}
62+
63+
protocol ProductDetailCoordinatorFactoryProtocol {
64+
func webCoordinator() -> ProductDetailCoordinator
65+
func nativeCoordinator() -> ProductDetailCoordinator
66+
}
67+
68+
class ProductDetailCoordinatorFactory: ProductDetailCoordinatorFactoryProtocol {
69+
static let `default` = ProductDetailCoordinatorFactory()
70+
71+
private let stores: StoresManager
72+
73+
init(stores: StoresManager = ServiceLocator.stores) {
74+
self.stores = stores
75+
}
76+
77+
func webCoordinator() -> ProductDetailCoordinator {
78+
return WebViewProductDetailCoordinator(site: stores.sessionManager.defaultSite!)
79+
}
80+
81+
func nativeCoordinator() -> ProductDetailCoordinator {
82+
return NativeProductDetailCoordinator()
83+
}
84+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Yosemite
2+
import Foundation
3+
4+
enum ProductURLProvider {
5+
static func editAdminURL(for product: Product, site: Site) -> URL? {
6+
guard let base = site.adminURLWithFallback() else { return nil }
7+
8+
var components = URLComponents(url: base, resolvingAgainstBaseURL: false)!
9+
components.queryItems = [
10+
URLQueryItem(name: "page", value: "next-admin"),
11+
URLQueryItem(name: "p", value: "/woocommerce/products/edit/\(product.productID)")
12+
]
13+
14+
return components.url
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import UIKit
2+
import Yosemite
3+
4+
final class WebViewProductDetailCoordinator: NSObject, ProductDetailCoordinator {
5+
private var onDismiss: (() -> Void)?
6+
7+
private let site: Site
8+
9+
init(site: Site) {
10+
self.site = site
11+
}
12+
13+
func viewController(product: Product,
14+
presentationStyle: ProductDetailPresenter.PresentationStyle,
15+
forceReadOnly: Bool,
16+
onDeleteCompletion: (() -> Void)? = nil) -> UIViewController {
17+
guard let url = ProductURLProvider.editAdminURL(for: product, site: site) else {
18+
return UIViewController()
19+
}
20+
21+
let viewModel = WPAdminWebViewModel(title: product.name, initialURL: url)
22+
let webViewController = AuthenticatedWebViewController(viewModel: viewModel)
23+
24+
return webViewController
25+
}
26+
27+
@objc
28+
private func dismissWebView() {
29+
let completion = onDismiss
30+
onDismiss = nil
31+
completion?()
32+
}
33+
}

WooCommerce/Classes/Routing/ProductDetailRouter.swift

Lines changed: 0 additions & 119 deletions
This file was deleted.

WooCommerce/Classes/ViewRelated/Products/Product Loader/ProductLoaderViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private extension ProductLoaderViewController {
227227
/// Presents the ProductFormViewController, as a childViewController, for a given Product.
228228
///
229229
func presentProductDetails(for product: Product) {
230-
let viewController = ProductDetailRouter.shared.viewController(product: product,
230+
let viewController = ProductDetailPresenter.shared.viewController(product: product,
231231
presentationStyle: .contained(containerViewController: { [weak self] in self }),
232232
forceReadOnly: forceReadOnly)
233233
attachProductDetailsChildViewController(viewController)

WooCommerce/Classes/ViewRelated/Products/ProductsSplitViewCoordinator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private extension ProductsSplitViewCoordinator {
122122
}
123123

124124
func showProductForm(product: Product) {
125-
let viewController = ProductDetailRouter.shared.viewController(product: product,
125+
let viewController = ProductDetailPresenter.shared.viewController(product: product,
126126
forceReadOnly: false,
127127
onDeleteCompletion: { [weak self] in
128128
self?.onSecondaryProductFormDeletion()

WooCommerce/Classes/ViewRelated/Products/ProductsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ extension ProductsViewController: UITableViewDelegate {
12141214
private extension ProductsViewController {
12151215
func didSelectProduct(product: Product) {
12161216
guard isSplitViewEnabled else {
1217-
let viewController = ProductDetailRouter.shared.viewController(product: product,
1217+
let viewController = ProductDetailPresenter.shared.viewController(product: product,
12181218
forceReadOnly: false)
12191219
navigationController?.pushViewController(viewController, animated: true)
12201220
return

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,6 @@
13401340
640DA3482E97DE4F00317FB2 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640DA3472E97DE4F00317FB2 /* SceneDelegate.swift */; };
13411341
64D355A52E99048E005F53F7 /* TestingSceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64D355A42E99048E005F53F7 /* TestingSceneDelegate.swift */; };
13421342
68051E1E2E9DFE5500228196 /* POSNotificationSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68051E1D2E9DFE5100228196 /* POSNotificationSchedulerTests.swift */; };
1343-
680BA59A2A4C377900F5559D /* UpgradeViewState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 680BA5992A4C377900F5559D /* UpgradeViewState.swift */; };
13441343
680E36B52BD8B9B900E8BCEA /* OrderSubscriptionTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 680E36B42BD8B9B900E8BCEA /* OrderSubscriptionTableViewCell.xib */; };
13451344
680E36B72BD8C49F00E8BCEA /* OrderSubscriptionTableViewCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 680E36B62BD8C49F00E8BCEA /* OrderSubscriptionTableViewCellViewModel.swift */; };
13461345
682140AF2E125437005E86AB /* UILabel+SalesChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 682140AE2E125430005E86AB /* UILabel+SalesChannel.swift */; };
@@ -5779,6 +5778,7 @@
57795778
3F09040E2D26A40800D8ACCE /* WordPressAuthenticatorTests */ = {isa = PBXFileSystemSynchronizedRootGroup; exceptions = (3FD28D5E2D271391002EBB3D /* PBXFileSystemSynchronizedBuildFileExceptionSet */, ); explicitFileTypes = {}; explicitFolders = (); path = WordPressAuthenticatorTests; sourceTree = "<group>"; };
57805779
3FD9BFBE2E0A2533004A8DC8 /* WooCommerceScreenshots */ = {isa = PBXFileSystemSynchronizedRootGroup; exceptions = (3FD9BFC42E0A2534004A8DC8 /* PBXFileSystemSynchronizedBuildFileExceptionSet */, ); explicitFileTypes = {}; explicitFolders = (); path = WooCommerceScreenshots; sourceTree = "<group>"; };
57815780
646A2C682E9FCD7E003A32A1 /* Routing */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Routing; sourceTree = "<group>"; };
5781+
6489D8522EA667AC00D96802 /* Routing */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Routing; sourceTree = "<group>"; };
57825782
DEDB5D342E7A68950022E5A1 /* Bookings */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Bookings; sourceTree = "<group>"; };
57835783
/* End PBXFileSystemSynchronizedRootGroup section */
57845784

@@ -9649,6 +9649,7 @@
96499649
B56DB3E02049BFAA00D4AA8E /* WooCommerceTests */ = {
96509650
isa = PBXGroup;
96519651
children = (
9652+
6489D8522EA667AC00D96802 /* Routing */,
96529653
DEB387962C2E80060025256E /* GoogleAds */,
96539654
DABF35242C11B40C006AF826 /* POS */,
96549655
B958A7CF28B527FB00823EEF /* Universal Links */,
@@ -13276,6 +13277,9 @@
1327613277
dependencies = (
1327713278
B56DB3DF2049BFAA00D4AA8E /* PBXTargetDependency */,
1327813279
);
13280+
fileSystemSynchronizedGroups = (
13281+
6489D8522EA667AC00D96802 /* Routing */,
13282+
);
1327913283
name = WooCommerceTests;
1328013284
packageProductDependencies = (
1328113285
3F2B4AEB2DDC319800E5E49C /* XcodeTarget_WooCommerceTests */,

0 commit comments

Comments
 (0)