Skip to content

Commit

Permalink
Merge pull request #16490 from wordpress-mobile/fix/16241-my-site-ref…
Browse files Browse the repository at this point in the history
…resh
  • Loading branch information
frosty authored May 17, 2021
2 parents 69d5b02 + dd5c124 commit 73c84b2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 15 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* [**] A new author can be chosen for Posts and Pages on multi-author sites. [#16281]
* [*] Fixed the Follow Sites Quick Start Tour so that Reader Search is highlighted. [#16391]
* [*] Enabled approving login authentication requests via push notification while the app is in the foreground. [#16075]

* [**] Added pull-to-refresh to the My Site screen when a user has no sites. [#16241]

17.3
-----
Expand Down
110 changes: 96 additions & 14 deletions WordPress/Classes/ViewRelated/Blog/My Site/MySiteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class MySiteViewController: UIViewController, NoResultsViewHost {
///
private var blogDetailsViewController: BlogDetailsViewController?

/// When we display a no results view, we'll do so in a scrollview so that
/// we can allow pull to refresh to sync the user's list of sites.
///
private var noResultsScrollView: UIScrollView?
private var noResultsRefreshControl: UIRefreshControl?

// MARK: - View Lifecycle

override func viewDidLoad() {
Expand Down Expand Up @@ -147,10 +153,30 @@ class MySiteViewController: UIViewController, NoResultsViewHost {
showBlogDetails(for: mainBlog)
}

@objc
private func syncBlogs() {
guard let account = defaultAccount() else {
return
}

let finishSync = { [weak self] in
self?.noResultsRefreshControl?.endRefreshing()
}

let blogService = BlogService(managedObjectContext: ContextManager.shared.mainContext)
blogService.syncBlogs(for: account) {
finishSync()
} failure: { (error) in
finishSync()
}
}

// MARK: - No Sites UI logic

private func hideNoSites() {
hideNoResults()

cleanupNoResultsView()
}

private func showNoSites() {
Expand All @@ -161,25 +187,81 @@ class MySiteViewController: UIViewController, NoResultsViewHost {

hideBlogDetails()

guard noResultsViewController.view.superview == nil else {
return
}

addMeButtonToNavigationBar(email: defaultAccount()?.email, meScenePresenter: meScenePresenter)

configureAndDisplayNoResults(
on: view,
title: NSLocalizedString(
"Create a new site for your business, magazine, or personal blog; or connect an existing WordPress installation.",
comment: "Text shown when the account has no sites."),
buttonTitle: NSLocalizedString(
"Add new site",
comment: "Title of button to add a new site."),
image: "mysites-nosites") { noResultsViewController in

noResultsViewController.actionButtonHandler = { [weak self] in
self?.presentInterfaceForAddingNewSite()
}
makeNoResultsScrollView()
configureNoResultsView()
addNoResultsViewAndConfigureConstraints()
}

private func makeNoResultsScrollView() {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = .basicBackground

view.addSubview(scrollView)
view.pinSubviewToAllEdges(scrollView)

let refreshControl = UIRefreshControl()
scrollView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(syncBlogs), for: .valueChanged)
noResultsRefreshControl = refreshControl

noResultsScrollView = scrollView
}

private func configureNoResultsView() {
noResultsViewController.configure(title: NSLocalizedString(
"Create a new site for your business, magazine, or personal blog; or connect an existing WordPress installation.",
comment: "Text shown when the account has no sites."),
buttonTitle: NSLocalizedString(
"Add new site",
comment: "Title of button to add a new site."),
image: "mysites-nosites")
noResultsViewController.actionButtonHandler = { [weak self] in
self?.presentInterfaceForAddingNewSite()
}
}

// MARK: - Add Site Alert
private func addNoResultsViewAndConfigureConstraints() {
guard let scrollView = noResultsScrollView else {
return
}

addChild(noResultsViewController)
scrollView.addSubview(noResultsViewController.view)
noResultsViewController.view.frame = scrollView.frame

guard let nrv = noResultsViewController.view else {
return
}

nrv.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
nrv.widthAnchor.constraint(equalTo: view.widthAnchor),
nrv.centerXAnchor.constraint(equalTo: view.centerXAnchor),
nrv.topAnchor.constraint(equalTo: scrollView.topAnchor),
nrv.bottomAnchor.constraint(equalTo: view.safeBottomAnchor)
])

noResultsViewController.didMove(toParent: self)
}

private func cleanupNoResultsView() {
noResultsRefreshControl?.removeFromSuperview()
noResultsRefreshControl = nil

noResultsScrollView?.refreshControl = nil
noResultsScrollView?.removeFromSuperview()
noResultsScrollView = nil
}

// MARK: - Add Site Alert

@objc
func presentInterfaceForAddingNewSite() {
Expand Down

0 comments on commit 73c84b2

Please sign in to comment.