Skip to content

Commit 610ce7d

Browse files
authored
Merge pull request #9796 from woocommerce/feat/9785-share-store
Dashboard: Add button to share store
2 parents a214f22 + b9ddde8 commit 610ce7d

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

RELEASE-NOTES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
- [Internal] Orders: Composite components (within a composite product) are now indented, to show their relationship to the parent composite product. [https://github.com/woocommerce/woocommerce-ios/pull/9780]
77
- [*] Add Products: A new view is display to celebrate when the first product is created in a store. [https://github.com/woocommerce/woocommerce-ios/pull/9790]
88
- [*] Product form: a share action is shown in the navigation bar if the product can be shared and no more than one action is displayed, in addition to the more menu > Share. [https://github.com/woocommerce/woocommerce-ios/pull/9789]
9-
9+
- [*] My Store: A new button to share the current store is added on the top right of the screen. [https://github.com/woocommerce/woocommerce-ios/pull/9796]
10+
1011
13.7
1112
-----
1213
- [Internal] Adds guidance for new Customs rule when shipping to some EU countries. [https://github.com/woocommerce/woocommerce-ios/pull/9715]

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewController.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ final class DashboardViewController: UIViewController {
7979
return view
8080
}()
8181

82+
private lazy var shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareStore))
83+
8284
/// Stores an animator for showing/hiding the header view while there is an animation in progress
8385
/// so we can interrupt and reverse if needed
8486
private var headerAnimator: UIViewPropertyAnimator?
@@ -303,6 +305,7 @@ private extension DashboardViewController {
303305
configureTitle()
304306
configureContainerStackView()
305307
configureHeaderStackView()
308+
configureShareButton()
306309
}
307310

308311
func configureTabBarItem() {
@@ -315,6 +318,22 @@ private extension DashboardViewController {
315318
navigationItem.title = Localization.title
316319
}
317320

321+
func configureShareButton() {
322+
guard viewModel.siteURLToShare != nil else {
323+
return
324+
}
325+
navigationItem.rightBarButtonItem = shareButton
326+
}
327+
328+
@objc
329+
func shareStore() {
330+
guard let url = viewModel.siteURLToShare else {
331+
return
332+
}
333+
SharingHelper.shareURL(url: url, from: shareButton, in: self)
334+
ServiceLocator.analytics.track(.dashboardShareStoreButtonTapped)
335+
}
336+
318337
func configureContainerStackView() {
319338
containerStackView.axis = .vertical
320339
containerView.addSubview(containerStackView)

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewModel.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ final class DashboardViewModel {
3232
private let analytics: Analytics
3333
private let justInTimeMessagesManager: JustInTimeMessagesProvider
3434

35+
var siteURLToShare: URL? {
36+
if let site = stores.sessionManager.defaultSite,
37+
site.isPublic, // only show share button if the site is public.
38+
let url = URL(string: site.url) {
39+
return url
40+
}
41+
return nil
42+
}
43+
3544
init(siteID: Int64,
3645
stores: StoresManager = ServiceLocator.stores,
3746
featureFlags: FeatureFlagService = ServiceLocator.featureFlagService,

WooCommerce/WooCommerceTests/ViewRelated/Dashboard/DashboardViewModelTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import enum Yosemite.JustInTimeMessageAction
77
import struct Yosemite.JustInTimeMessage
88
import struct Yosemite.StoreOnboardingTask
99
import enum Yosemite.StoreOnboardingTasksAction
10+
import struct Yosemite.Site
1011
@testable import WooCommerce
1112

1213
final class DashboardViewModelTests: XCTestCase {
@@ -466,6 +467,35 @@ final class DashboardViewModelTests: XCTestCase {
466467
// Then
467468
XCTAssertFalse(sut.showOnboarding)
468469
}
470+
471+
func test_siteURLToShare_return_nil_if_site_is_not_public() {
472+
// Given
473+
let sessionManager = SessionManager.makeForTesting()
474+
sessionManager.defaultSite = Site.fake().copy(isPublic: false)
475+
let stores = MockStoresManager(sessionManager: sessionManager)
476+
let viewModel = DashboardViewModel(siteID: 123, stores: stores)
477+
478+
// When
479+
let siteURLToShare = viewModel.siteURLToShare
480+
481+
// Then
482+
XCTAssertNil(siteURLToShare)
483+
}
484+
485+
func test_siteURLToShare_return_url_if_site_is_public() {
486+
// Given
487+
let sessionManager = SessionManager.makeForTesting()
488+
let expectedURL = "https://example.com"
489+
sessionManager.defaultSite = Site.fake().copy(url: expectedURL, isPublic: true)
490+
let stores = MockStoresManager(sessionManager: sessionManager)
491+
let viewModel = DashboardViewModel(siteID: 123, stores: stores)
492+
493+
// When
494+
let siteURLToShare = viewModel.siteURLToShare
495+
496+
// Then
497+
assertEqual(expectedURL, siteURLToShare?.absoluteString)
498+
}
469499
}
470500

471501
private extension DashboardViewModelTests {

0 commit comments

Comments
 (0)