mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
More progress with the Swift example
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.facebook.AsyncDisplayKit.$(PRODUCT_NAME:rfc1034identifier)</string>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
|
||||
53
examples/Swift/Sample/TailLoadingCellNode.swift
Normal file
53
examples/Swift/Sample/TailLoadingCellNode.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// TailLoadingCellNode.swift
|
||||
// Sample
|
||||
//
|
||||
// Created by Adlai Holler on 2/1/16.
|
||||
// Copyright © 2016 Facebook. All rights reserved.
|
||||
//
|
||||
|
||||
import AsyncDisplayKit
|
||||
import UIKit
|
||||
|
||||
final class TailLoadingCellNode: ASCellNode {
|
||||
let spinner = SpinnerNode()
|
||||
let text = ASTextNode()
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
addSubnode(text)
|
||||
text.attributedString = NSAttributedString(
|
||||
string: "Loading…",
|
||||
attributes: [
|
||||
NSFontAttributeName: UIFont.systemFontOfSize(12),
|
||||
NSForegroundColorAttributeName: UIColor.lightGrayColor(),
|
||||
NSKernAttributeName: -0.3
|
||||
])
|
||||
addSubnode(spinner)
|
||||
}
|
||||
|
||||
override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {
|
||||
return ASStackLayoutSpec(
|
||||
direction: .Horizontal,
|
||||
spacing: 16,
|
||||
justifyContent: .Center,
|
||||
alignItems: .Center,
|
||||
children: [ text, spinner ])
|
||||
}
|
||||
}
|
||||
|
||||
final class SpinnerNode: ASDisplayNode {
|
||||
var activityIndicatorView: UIActivityIndicatorView {
|
||||
return view as! UIActivityIndicatorView
|
||||
}
|
||||
|
||||
override init() {
|
||||
super.init(viewBlock: { UIActivityIndicatorView(activityIndicatorStyle: .Gray) }, didLoadBlock: nil)
|
||||
preferredFrameSize.height = 32
|
||||
}
|
||||
|
||||
override func didLoad() {
|
||||
super.didLoad()
|
||||
activityIndicatorView.startAnimating()
|
||||
}
|
||||
}
|
||||
@@ -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