Swiftgram/submodules/TelegramUI/TelegramUI/ThemeColorsGridControllerItem.swift
Peter b317aab568 Add 'submodules/TelegramUI/' from commit 'fa3ac0b61a27c8dd3296518a15891a6f9750cbf2'
git-subtree-dir: submodules/TelegramUI
git-subtree-mainline: 5c1613d1048026b9e00a6ce753775cef87eb53fa
git-subtree-split: fa3ac0b61a27c8dd3296518a15891a6f9750cbf2
2019-06-11 19:00:46 +01:00

87 lines
3.0 KiB
Swift

import Foundation
import UIKit
import Display
import TelegramCore
import SwiftSignalKit
import AsyncDisplayKit
import Postbox
final class ThemeColorsGridControllerItem: GridItem {
let context: AccountContext
let wallpaper: TelegramWallpaper
let selected: Bool
let interaction: ThemeColorsGridControllerInteraction
let section: GridSection? = nil
init(context: AccountContext, wallpaper: TelegramWallpaper, selected: Bool, interaction: ThemeColorsGridControllerInteraction) {
self.context = context
self.wallpaper = wallpaper
self.selected = selected
self.interaction = interaction
}
func node(layout: GridNodeLayout, synchronousLoad: Bool) -> GridItemNode {
let node = ThemeColorsGridControllerItemNode()
node.setup(context: self.context, wallpaper: self.wallpaper, selected: self.selected, interaction: self.interaction)
return node
}
func update(node: GridItemNode) {
guard let node = node as? ThemeColorsGridControllerItemNode else {
assertionFailure()
return
}
node.setup(context: self.context, wallpaper: self.wallpaper, selected: self.selected, interaction: self.interaction)
}
}
final class ThemeColorsGridControllerItemNode: GridItemNode {
private let wallpaperNode: SettingsThemeWallpaperNode
private var selectionNode: GridMessageSelectionNode?
private var currentState: (AccountContext, TelegramWallpaper, Bool)?
private var interaction: ThemeColorsGridControllerInteraction?
override init() {
self.wallpaperNode = SettingsThemeWallpaperNode()
super.init()
self.addSubnode(self.wallpaperNode)
}
override func didLoad() {
super.didLoad()
self.view.isExclusiveTouch = true
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapGesture(_:))))
}
func setup(context: AccountContext, wallpaper: TelegramWallpaper, selected: Bool, interaction: ThemeColorsGridControllerInteraction) {
self.interaction = interaction
if self.currentState == nil || self.currentState!.0 !== context || wallpaper != self.currentState!.1 || selected != self.currentState!.2 {
self.currentState = (context, wallpaper, selected)
self.setNeedsLayout()
}
}
@objc func tapGesture(_ recognizer: UITapGestureRecognizer) {
if case .ended = recognizer.state {
if let (_, wallpaper, _) = self.currentState {
self.interaction?.openWallpaper(wallpaper)
}
}
}
override func layout() {
super.layout()
let bounds = self.bounds
if let (context, wallpaper, selected) = self.currentState {
self.wallpaperNode.setWallpaper(context: context, wallpaper: wallpaper, selected: selected, size: bounds.size)
self.selectionNode?.frame = CGRect(origin: CGPoint(), size: bounds.size)
}
}
}