IGListKit/Examples/Examples-iOS/IGListKitExamples/ViewControllers/LoadMoreViewController.swift
Tim Oliver f92b9339ee Standarize the copyright notice in all source files
Summary:
The standardized Meta copyright notice is "Copyright (c) Meta Platforms, Inc. and affiliates." and not "Copyright (c) Meta Platforms, Inc. and its affiliates." (Dropping the "its")

This diff updates the copyright notice in each source file to the correct this.

Reviewed By: willbailey

Differential Revision: D44737667

fbshipit-source-id: 643bf36df76723e70d9d826c53cf8f29b8a0c8cc
2023-04-06 02:44:16 -07:00

80 lines
2.6 KiB
Swift

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import IGListKit
import UIKit
final class LoadMoreViewController: UIViewController, ListAdapterDataSource, UIScrollViewDelegate {
lazy var adapter: ListAdapter = {
return ListAdapter(updater: ListAdapterUpdater(), viewController: self)
}()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
lazy var items = Array(0...20)
var loading = false
let spinToken = "spinner"
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: ListAdapterDataSource
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
var objects = items as [ListDiffable]
if loading {
objects.append(spinToken as ListDiffable)
}
return objects
}
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
if let obj = object as? String, obj == spinToken {
return spinnerSectionController()
} else {
return LabelSectionController()
}
}
func emptyView(for listAdapter: ListAdapter) -> 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 {
// 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)
}
}
}
}
}