mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
More progress with the Swift example
This commit is contained in:
@@ -14,10 +14,23 @@ import AsyncDisplayKit
|
||||
|
||||
final class ViewController: ASViewController, ASTableDataSource, ASTableDelegate {
|
||||
|
||||
struct State {
|
||||
var rowCount: Int
|
||||
var showingSpinner: Bool
|
||||
static let empty = State(rowCount: 20, showingSpinner: false)
|
||||
}
|
||||
|
||||
enum Action {
|
||||
case BeginBatchFetch
|
||||
case EndBatchFetch(resultCount: Int)
|
||||
}
|
||||
|
||||
var tableNode: ASTableNode {
|
||||
return node as! ASTableNode
|
||||
}
|
||||
|
||||
private(set) var state: State = .empty
|
||||
|
||||
init() {
|
||||
super.init(node: ASTableNode())
|
||||
tableNode.delegate = self
|
||||
@@ -35,6 +48,11 @@ final class ViewController: ASViewController, ASTableDataSource, ASTableDelegate
|
||||
// MARK: ASTableView data source and delegate.
|
||||
|
||||
func tableView(tableView: ASTableView, nodeForRowAtIndexPath indexPath: NSIndexPath) -> ASCellNode {
|
||||
NSLog("Number of rows %d", tableView.numberOfRowsInSection(0))
|
||||
if state.showingSpinner && indexPath.row == tableView.numberOfRowsInSection(0) - 1 {
|
||||
return TailLoadingCellNode()
|
||||
}
|
||||
|
||||
let node = ASTextCellNode()
|
||||
node.text = String(format: "[%ld.%ld] says hello!", indexPath.section, indexPath.row)
|
||||
|
||||
@@ -46,7 +64,70 @@ final class ViewController: ASViewController, ASTableDataSource, ASTableDelegate
|
||||
}
|
||||
|
||||
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return 20
|
||||
var count = state.rowCount
|
||||
if state.showingSpinner {
|
||||
count += 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func tableView(tableView: ASTableView, willBeginBatchFetchWithContext context: ASBatchContext) {
|
||||
context.cancelBatchFetching()
|
||||
dispatch_async(dispatch_get_main_queue()) {
|
||||
let oldState = self.state
|
||||
self.state = ViewController.handleAction(.BeginBatchFetch, fromState: oldState)
|
||||
self.render(oldState)
|
||||
}
|
||||
return;
|
||||
|
||||
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(NSTimeInterval(NSEC_PER_SEC) * 3))
|
||||
dispatch_after(time, dispatch_get_main_queue()) {
|
||||
let action = Action.EndBatchFetch(resultCount: 20)
|
||||
let oldState = self.state
|
||||
self.state = ViewController.handleAction(action, fromState: oldState)
|
||||
self.render(oldState)
|
||||
context.completeBatchFetching(true)
|
||||
}
|
||||
}
|
||||
|
||||
func render(oldState: State) {
|
||||
let tableView = tableNode.view
|
||||
tableView.beginUpdates()
|
||||
|
||||
// Add or remove items
|
||||
let rowCountChange = state.rowCount - oldState.rowCount
|
||||
if rowCountChange > 0 {
|
||||
let indexPaths = (oldState.rowCount..<state.rowCount).map { index in
|
||||
NSIndexPath(forRow: index, inSection: 0)
|
||||
}
|
||||
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
|
||||
} else if rowCountChange < 0 {
|
||||
assertionFailure("Deleting rows is not implemented. YAGNI.")
|
||||
}
|
||||
|
||||
// Add or remove spinner.
|
||||
if state.showingSpinner && !oldState.showingSpinner {
|
||||
if state.showingSpinner {
|
||||
// Add spinner.
|
||||
let spinnerIndexPath = NSIndexPath(forRow: state.rowCount, inSection: 0)
|
||||
tableView.insertRowsAtIndexPaths([ spinnerIndexPath ], withRowAnimation: .None)
|
||||
} else {
|
||||
// Remove spinner.
|
||||
let spinnerIndexPath = NSIndexPath(forRow: oldState.rowCount, inSection: 0)
|
||||
tableView.deleteRowsAtIndexPaths([ spinnerIndexPath ], withRowAnimation: .None)
|
||||
}
|
||||
}
|
||||
tableView.endUpdatesAnimated(false, completion: nil)
|
||||
}
|
||||
|
||||
static func handleAction(action: Action, var fromState state: State) -> State {
|
||||
switch action {
|
||||
case .BeginBatchFetch:
|
||||
state.showingSpinner = true
|
||||
case let .EndBatchFetch(resultCount):
|
||||
state.rowCount += resultCount
|
||||
state.showingSpinner = false
|
||||
}
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user