Skip to content

Commit

Permalink
Fix brave#916: Add pull to refresh (brave#3769)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson authored and KacperWybranski committed Sep 4, 2021
1 parent 4766c15 commit f64fcad
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions BraveShared/BraveStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ extension Strings {
public static let defaultBrowserCalloutCloseAccesabilityLabel =
NSLocalizedString("defaultBrowserCalloutCloseAccesabilityLabel", tableName: "BraveShared",
bundle: .braveShared, value: "Close default browser callout", comment: "")
public static let enablePullToRefresh =
NSLocalizedString("enablePullToRefresh", tableName: "BraveShared",
bundle: .braveShared, value: "Enable Pull-to-refresh", comment: "Describes whether or not the feature that allows the user to pull down from the top of a web page a certain amount before it triggers a page refresh")
}

extension Strings {
Expand Down
3 changes: 3 additions & 0 deletions Client/Application/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ extension Preferences {

/// Whether or not the app (in regular browsing mode) will follow universal links
static let followUniversalLinks = Option<Bool>(key: "general.follow-universal-links", default: true)

/// Whether or not the pull-to-refresh control is added to web views
static let enablePullToRefresh = Option<Bool>(key: "general.enable-pull-to-refresh", default: true)
}

final class DefaultBrowserIntro {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ class BrowserViewController: UIViewController {
Preferences.Privacy.privateBrowsingOnly.observe(from: self)
Preferences.General.tabBarVisibility.observe(from: self)
Preferences.General.alwaysRequestDesktopSite.observe(from: self)
Preferences.General.enablePullToRefresh.observe(from: self)
Preferences.Shields.allShields.forEach { $0.observe(from: self) }
Preferences.Privacy.blockAllCookies.observe(from: self)
Preferences.Rewards.hideRewardsIcon.observe(from: self)
Expand Down Expand Up @@ -2162,7 +2163,8 @@ extension BrowserViewController: TabManagerDelegate {

updateFindInPageVisibility(visible: false, tab: previous)
updateTabsBarVisibility()

selected?.updatePullToRefreshVisibility()

topToolbar.locationView.loading = selected?.loading ?? false
navigationToolbar.updateBackStatus(selected?.canGoBack ?? false)
navigationToolbar.updateForwardStatus(selected?.canGoForward ?? false)
Expand Down Expand Up @@ -2758,6 +2760,8 @@ extension BrowserViewController: PreferencesObserver {
case Preferences.General.alwaysRequestDesktopSite.key:
tabManager.reset()
self.tabManager.reloadSelectedTab()
case Preferences.General.enablePullToRefresh.key:
tabManager.selectedTab?.updatePullToRefreshVisibility()
case Preferences.Shields.blockAdsAndTracking.key,
Preferences.Shields.httpsEverywhere.key,
Preferences.Shields.blockScripts.key,
Expand Down
20 changes: 19 additions & 1 deletion Client/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class Tab: NSObject {

/// A helper property that handles native to Brave Search communication.
var braveSearchManager: BraveSearchManager?

private lazy var refreshControl = UIRefreshControl().then {
$0.addTarget(self, action: #selector(reload), for: .valueChanged)
}

func createWebview() {
if webView == nil {
Expand Down Expand Up @@ -417,13 +421,20 @@ class Tab: NSObject {
webView?.stopLoading()
}

func reload() {
@objc func reload() {
// Clear the user agent before further navigation.
// Proper User Agent setting happens in BVC's WKNavigationDelegate.
// This prevents a bug with back-forward list, going back or forward and reloading the tab
// loaded wrong user agent.
webView?.customUserAgent = nil

defer {
if let refreshControl = webView?.scrollView.refreshControl,
refreshControl.isRefreshing {
refreshControl.endRefreshing()
}
}

// Refreshing error, safe browsing warning pages.
if let originalUrlFromErrorUrl = webView?.url?.originalURLFromErrorURL {
webView?.load(URLRequest(url: originalUrlFromErrorUrl))
Expand Down Expand Up @@ -548,8 +559,15 @@ class Tab: NSObject {
return
}

updatePullToRefreshVisibility()

self.urlDidChangeDelegate?.tab(self, urlDidChangeTo: url)
}

func updatePullToRefreshVisibility() {
guard let url = webView?.url, let webView = webView else { return }
webView.scrollView.refreshControl = url.isLocalUtility || !Preferences.General.enablePullToRefresh.value ? nil : refreshControl
}

func isDescendentOf(_ ancestor: Tab) -> Bool {
return sequence(first: parent) { $0?.parent }.contains { $0 == ancestor }
Expand Down
6 changes: 6 additions & 0 deletions Client/Frontend/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class SettingsViewController: TableViewController {
}, cellClass: MultilineButtonCell.self))
}

general.rows.append(
.boolRow(title: Strings.enablePullToRefresh,
option: Preferences.General.enablePullToRefresh,
image: #imageLiteral(resourceName: "settings-pull-to-refresh").template)
)

return general
}()

Expand Down

0 comments on commit f64fcad

Please sign in to comment.