diff options
author | Nicholas Tay <nick@windblume.net> | 2022-03-16 13:10:34 +1100 |
---|---|---|
committer | Nicholas Tay <nick@windblume.net> | 2022-03-16 13:10:34 +1100 |
commit | a296cb07500b1300385dae413149494eeb6d5905 (patch) | |
tree | 3ff70c05d346019a26f6fd6c8f17837e58942e20 | |
parent | 0955c54d7b021a6293b267fb8a31ff75c0daa154 (diff) | |
download | forayios-a296cb07500b1300385dae413149494eeb6d5905.tar.gz forayios-a296cb07500b1300385dae413149494eeb6d5905.tar.bz2 forayios-a296cb07500b1300385dae413149494eeb6d5905.zip |
Add refresh control + fix weird caching
It might be my web server causing the weird caching with headers (it's
just a json file on nginx). Changing the policy in the request seems to
have fixed it though.
Refresh control references:
- https://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift
-rw-r--r-- | foray/Base.lproj/Main.storyboard | 3 | ||||
-rw-r--r-- | foray/ForayTableViewController.swift | 18 |
2 files changed, 19 insertions, 2 deletions
diff --git a/foray/Base.lproj/Main.storyboard b/foray/Base.lproj/Main.storyboard index 17cfb05..b6b3f86 100644 --- a/foray/Base.lproj/Main.storyboard +++ b/foray/Base.lproj/Main.storyboard @@ -122,6 +122,9 @@ </connections> </tableView> <navigationItem key="navigationItem" title="Foray" id="UHk-DR-0EX"/> + <refreshControl key="refreshControl" opaque="NO" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" id="SfP-IV-1Ks"> + <autoresizingMask key="autoresizingMask"/> + </refreshControl> </tableViewController> <placeholder placeholderIdentifier="IBFirstResponder" id="pPD-Wc-Rc9" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/> </objects> diff --git a/foray/ForayTableViewController.swift b/foray/ForayTableViewController.swift index 67f0bc5..0643051 100644 --- a/foray/ForayTableViewController.swift +++ b/foray/ForayTableViewController.swift @@ -64,6 +64,20 @@ class ForayTableViewController: UITableViewController { alert.view.addSubview(loadingIndicator) present(alert, animated: true, completion: nil) + reloadApiData() + + dismiss(animated: false, completion: nil) + + // Not 100% sure what this does (the for: bit) + self.refreshControl?.addTarget(self, action: #selector(doRefresh), for: UIControl.Event.valueChanged) + } + + // Not sure why need @objc. Is it due to class private/public? + @objc func doRefresh(sender: AnyObject) { + reloadApiData() + } + + func reloadApiData() { loadApiData(onComplete: { (apiItems) in self.items = apiItems @@ -77,9 +91,8 @@ class ForayTableViewController: UITableViewController { self.sections.sort { (lhs, rhs) in lhs.year < rhs.year } self.tableView.reloadData() + self.refreshControl?.endRefreshing() }) - - dismiss(animated: false, completion: nil) } func loadApiData(onComplete: @escaping ([MyItem]) -> ()) { @@ -97,6 +110,7 @@ class ForayTableViewController: UITableViewController { var request = URLRequest(url: URL(string: "https://.../dummy.json")!) let authData = ("..:..").data(using: .utf8)!.base64EncodedString() request.addValue("Basic \(authData)", forHTTPHeaderField: "Authorization") + request.cachePolicy = .reloadRevalidatingCacheData // Needed otherwise default caching policy seems not to check properly URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in print("finished getting data") |