Add pull to refresh

This commit is contained in:
Shadowfacts 2021-10-02 11:18:38 -04:00
parent 86719b4528
commit e470d7b58e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 26 additions and 2 deletions

View File

@ -74,6 +74,7 @@ class BrowserWebViewController: UIViewController {
// this doesn't default to .default :S // this doesn't default to .default :S
webView.scrollView.indicatorStyle = .default webView.scrollView.indicatorStyle = .default
webView.scrollView.keyboardDismissMode = .interactive webView.scrollView.keyboardDismissMode = .interactive
configureWebViewRefreshControl()
webView.translatesAutoresizingMaskIntoConstraints = false webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView) view.addSubview(webView)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
@ -154,14 +155,28 @@ class BrowserWebViewController: UIViewController {
} }
} }
private func configureWebViewRefreshControl(pullToRefreshEnabled: Bool = Preferences.shared.pullToRefreshEnabled) {
if pullToRefreshEnabled {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(navigator, action: #selector(NavigationManager.reload), for: .valueChanged)
webView.scrollView.refreshControl = refreshControl
} else {
webView.scrollView.refreshControl = nil
}
}
func reload() { func reload() {
loaded = false loaded = false
loadedFallback = false loadedFallback = false
document = nil document = nil
loadDocument() loadDocument() {
DispatchQueue.main.async {
self.webView.scrollView.refreshControl?.endRefreshing()
}
}
} }
private func loadDocument() { private func loadDocument(completion: (() -> Void)? = nil) {
guard !loaded else { return } guard !loaded else { return }
webView.isHidden = true webView.isHidden = true
@ -207,6 +222,8 @@ class BrowserWebViewController: UIViewController {
} }
} }
} }
completion?()
} }
if #available(iOS 15.0, *) { if #available(iOS 15.0, *) {
task!.attribution = .user task!.attribution = .user

View File

@ -49,6 +49,9 @@ class Preferences: Codable, ObservableObject {
if let stored = try container.decodeIfPresent(Bool.self, forKey: .hideToolbarsWhenScrolling) { if let stored = try container.decodeIfPresent(Bool.self, forKey: .hideToolbarsWhenScrolling) {
hideToolbarsWhenScrolling = stored hideToolbarsWhenScrolling = stored
} }
if let stored = try container.decodeIfPresent(Bool.self, forKey: .pullToRefreshEnabled) {
pullToRefreshEnabled = stored
}
} }
func encode(to encoder: Encoder) throws { func encode(to encoder: Encoder) throws {
@ -63,6 +66,7 @@ class Preferences: Codable, ObservableObject {
try container.encode(useReaderMode, forKey: .useReaderMode) try container.encode(useReaderMode, forKey: .useReaderMode)
try container.encode(hideToolbarsWhenScrolling, forKey: .hideToolbarsWhenScrolling) try container.encode(hideToolbarsWhenScrolling, forKey: .hideToolbarsWhenScrolling)
try container.encode(pullToRefreshEnabled, forKey: .pullToRefreshEnabled)
} }
@Published var homepage = AppDelegate.defaultHomepage @Published var homepage = AppDelegate.defaultHomepage
@ -74,6 +78,7 @@ class Preferences: Codable, ObservableObject {
@Published var useReaderMode = false @Published var useReaderMode = false
@Published var hideToolbarsWhenScrolling = true @Published var hideToolbarsWhenScrolling = true
@Published var pullToRefreshEnabled = true
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case homepage case homepage
@ -85,6 +90,7 @@ class Preferences: Codable, ObservableObject {
case useReaderMode case useReaderMode
case hideToolbarsWhenScrolling case hideToolbarsWhenScrolling
case pullToRefreshEnabled
} }
} }

View File

@ -74,6 +74,7 @@ struct PreferencesView: View {
private var behaviorSection: some View { private var behaviorSection: some View {
Section(header: Text("Behavior")) { Section(header: Text("Behavior")) {
Toggle("Pull to Refresh", isOn: $preferences.pullToRefreshEnabled)
Toggle("Hide Toolbars When Scrolling", isOn: $preferences.hideToolbarsWhenScrolling) Toggle("Hide Toolbars When Scrolling", isOn: $preferences.hideToolbarsWhenScrolling)
} }
} }