Swiftgram/TelegramUI/BotCheckoutPriceItem.swift
2017-07-14 15:34:30 +03:00

147 lines
5.8 KiB
Swift

import Foundation
import Display
import AsyncDisplayKit
import SwiftSignalKit
class BotCheckoutPriceItem: ListViewItem, ItemListItem {
let theme: PresentationTheme
let title: String
let label: String
let isFinal: Bool
let sectionId: ItemListSectionId
let requestsNoInset: Bool = true
init(theme: PresentationTheme, title: String, label: String, isFinal: Bool, sectionId: ItemListSectionId) {
self.theme = theme
self.title = title
self.label = label
self.isFinal = isFinal
self.sectionId = sectionId
}
func nodeConfiguredForWidth(async: @escaping (@escaping () -> Void) -> Void, width: CGFloat, previousItem: ListViewItem?, nextItem: ListViewItem?, completion: @escaping (ListViewItemNode, @escaping () -> (Signal<Void, NoError>?, () -> Void)) -> Void) {
async {
let node = BotCheckoutPriceItemNode()
let (layout, apply) = node.asyncLayout()(self, width, itemListNeighbors(item: self, topItem: previousItem as? ItemListItem, bottomItem: nextItem as? ItemListItem))
node.contentSize = layout.contentSize
node.insets = layout.insets
completion(node, {
return (nil, { apply() })
})
}
}
func updateNode(async: @escaping (@escaping () -> Void) -> Void, node: ListViewItemNode, width: CGFloat, previousItem: ListViewItem?, nextItem: ListViewItem?, animation: ListViewItemUpdateAnimation, completion: @escaping (ListViewItemNodeLayout, @escaping () -> Void) -> Void) {
if let node = node as? BotCheckoutPriceItemNode {
Queue.mainQueue().async {
let makeLayout = node.asyncLayout()
async {
let (layout, apply) = makeLayout(self, width, itemListNeighbors(item: self, topItem: previousItem as? ItemListItem, bottomItem: nextItem as? ItemListItem))
Queue.mainQueue().async {
completion(layout, {
apply()
})
}
}
}
}
}
let selectable: Bool = false
}
private let titleFont = Font.regular(17.0)
private let finalFont = Font.semibold(17.0)
private func priceItemInsets(_ neighbors: ItemListNeighbors) -> UIEdgeInsets {
var insets = UIEdgeInsets()
switch neighbors.top {
case .otherSection:
insets.top += 8.0
case .none, .sameSection:
break
}
switch neighbors.bottom {
case .none, .otherSection:
insets.bottom += 8.0
case .sameSection:
break
}
return insets
}
class BotCheckoutPriceItemNode: ListViewItemNode {
let titleNode: TextNode
let labelNode: TextNode
private var item: BotCheckoutPriceItem?
init() {
self.titleNode = TextNode()
self.titleNode.isLayerBacked = true
self.labelNode = TextNode()
self.labelNode.isLayerBacked = true
super.init(layerBacked: false, dynamicBounce: false)
self.addSubnode(self.titleNode)
self.addSubnode(self.labelNode)
}
func asyncLayout() -> (_ item: BotCheckoutPriceItem, _ width: CGFloat, _ insets: ItemListNeighbors) -> (ListViewItemNodeLayout, () -> Void) {
let makeTitleLayout = TextNode.asyncLayout(self.titleNode)
let makeLabelLayout = TextNode.asyncLayout(self.labelNode)
return { item, width, neighbors in
let rightInset: CGFloat = 16.0
let contentSize = CGSize(width: width, height: 34.0)
let insets = priceItemInsets(neighbors)
let textFont: UIFont
let textColor: UIColor
if item.isFinal {
textFont = finalFont
textColor = item.theme.list.itemPrimaryTextColor
} else {
textFont = titleFont
textColor = item.theme.list.itemSecondaryTextColor
}
let (titleLayout, titleApply) = makeTitleLayout(NSAttributedString(string: item.title, font: textFont, textColor: textColor), nil, 1, .end, CGSize(width: width - 20, height: CGFloat.greatestFiniteMagnitude), .natural, nil, UIEdgeInsets())
let (labelLayout, labelApply) = makeLabelLayout(NSAttributedString(string: item.label, font: textFont, textColor: textColor), nil, 1, .end, CGSize(width: width - 20, height: CGFloat.greatestFiniteMagnitude), .natural, nil, UIEdgeInsets())
return (ListViewItemNodeLayout(contentSize: contentSize, insets: insets), { [weak self] in
if let strongSelf = self {
strongSelf.item = item
let _ = titleApply()
let _ = labelApply()
let leftInset: CGFloat = 16.0
strongSelf.titleNode.frame = CGRect(origin: CGPoint(x: leftInset, y: floor((contentSize.height - titleLayout.size.height) / 2.0)), size: titleLayout.size)
strongSelf.labelNode.frame = CGRect(origin: CGPoint(x: width - rightInset - labelLayout.size.width, y: floor((contentSize.height - labelLayout.size.height) / 2.0)), size: labelLayout.size)
}
})
}
}
override func animateInsertion(_ currentTimestamp: Double, duration: Double, short: Bool) {
self.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.4)
}
override func animateAdded(_ currentTimestamp: Double, duration: Double) {
self.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2)
}
override func animateRemoved(_ currentTimestamp: Double, duration: Double) {
self.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.15, removeOnCompletion: false)
}
}