mirror of
https://github.com/Instagram/IGListKit
synced 2026-05-04 14:09:28 +00:00
Summary: - Fixes #105 - ~~Added a 'SpinnerSectionController' allowing us to trigger the activity spinner each time~~ - Changed the words array to be numbers instead (I found it personally difficult to realise if more cells were actually added as the text didn't seem to change, by using numbers you can quite clearly see it increment by 5 each time you load more) - No changes/new tests as it is a fix to an example - [x] All tests pass. Demo project builds and runs. - [x] I added tests, an experiment, or detailed why my change isn't tested. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/CONTRIBUTING.md) Closes https://github.com/Instagram/IGListKit/pull/109 Differential Revision: D4066700 Pulled By: rnystrom fbshipit-source-id: 7779a8ba39361cfa2e782db9460e693e90f1a478
84 lines
3 KiB
Swift
84 lines
3 KiB
Swift
/**
|
|
Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
|
|
|
|
The examples provided by Facebook are for non-commercial testing and evaluation
|
|
purposes only. Facebook reserves all rights not expressly granted.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
import UIKit
|
|
import IGListKit
|
|
|
|
class LoadMoreViewController: UIViewController, IGListAdapterDataSource, UIScrollViewDelegate {
|
|
|
|
lazy var adapter: IGListAdapter = {
|
|
return IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0)
|
|
}()
|
|
let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
|
|
|
|
lazy var items = Array(0...20)
|
|
var loading = false
|
|
let spinToken = NSObject()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
view.addSubview(collectionView)
|
|
adapter.collectionView = collectionView
|
|
adapter.dataSource = self
|
|
adapter.scrollViewDelegate = self
|
|
}
|
|
|
|
override func viewDidLayoutSubviews() {
|
|
super.viewDidLayoutSubviews()
|
|
collectionView.frame = view.bounds
|
|
}
|
|
|
|
//MARK: IGListAdapterDataSource
|
|
|
|
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
|
|
var objects = items as [IGListDiffable]
|
|
|
|
if loading {
|
|
objects.append(spinToken)
|
|
}
|
|
|
|
return objects
|
|
}
|
|
|
|
func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
|
|
if let obj = object as? NSObject, obj === spinToken {
|
|
return spinnerSectionController()
|
|
} else {
|
|
return LabelSectionController()
|
|
}
|
|
}
|
|
|
|
func emptyView(for listAdapter: IGListAdapter) -> UIView? { return nil }
|
|
|
|
//MARK: UIScrollViewDelegate
|
|
|
|
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
|
|
let distance = scrollView.contentSize.height - (targetContentOffset.pointee.y + scrollView.bounds.height)
|
|
if !loading && distance < 200 {
|
|
loading = true
|
|
adapter.performUpdates(animated: true, completion: nil)
|
|
DispatchQueue.global(qos: .default).async(execute: {
|
|
// fake background loading task
|
|
sleep(2)
|
|
DispatchQueue.main.async {
|
|
self.loading = false
|
|
let itemCount = self.items.count
|
|
self.items.append(contentsOf: Array(itemCount..<itemCount + 5))
|
|
self.adapter.performUpdates(animated: true, completion: nil)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
}
|