mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
226 lines
9.6 KiB
Swift
226 lines
9.6 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import Display
|
|
import AsyncDisplayKit
|
|
import ComponentFlow
|
|
import SwiftSignalKit
|
|
import AccountContext
|
|
import TelegramCore
|
|
import MultilineTextComponent
|
|
import AvatarNode
|
|
import TelegramPresentationData
|
|
import CheckNode
|
|
import BundleIconComponent
|
|
|
|
final class CountryListItemComponent: Component {
|
|
enum SelectionState: Equatable {
|
|
case none
|
|
case editing(isSelected: Bool, isTinted: Bool)
|
|
}
|
|
|
|
let context: AccountContext
|
|
let theme: PresentationTheme
|
|
let title: String
|
|
let selectionState: SelectionState
|
|
let hasNext: Bool
|
|
let action: () -> Void
|
|
|
|
init(
|
|
context: AccountContext,
|
|
theme: PresentationTheme,
|
|
title: String,
|
|
selectionState: SelectionState,
|
|
hasNext: Bool,
|
|
action: @escaping () -> Void
|
|
) {
|
|
self.context = context
|
|
self.theme = theme
|
|
self.title = title
|
|
self.selectionState = selectionState
|
|
self.hasNext = hasNext
|
|
self.action = action
|
|
}
|
|
|
|
static func ==(lhs: CountryListItemComponent, rhs: CountryListItemComponent) -> Bool {
|
|
if lhs.context !== rhs.context {
|
|
return false
|
|
}
|
|
if lhs.theme !== rhs.theme {
|
|
return false
|
|
}
|
|
if lhs.title != rhs.title {
|
|
return false
|
|
}
|
|
if lhs.selectionState != rhs.selectionState {
|
|
return false
|
|
}
|
|
if lhs.hasNext != rhs.hasNext {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
final class View: UIView {
|
|
private let containerButton: HighlightTrackingButton
|
|
|
|
private let title = ComponentView<Empty>()
|
|
private let separatorLayer: SimpleLayer
|
|
|
|
private var checkLayer: CheckLayer?
|
|
|
|
private var component: CountryListItemComponent?
|
|
private weak var state: EmptyComponentState?
|
|
|
|
override init(frame: CGRect) {
|
|
self.separatorLayer = SimpleLayer()
|
|
|
|
self.containerButton = HighlightTrackingButton()
|
|
|
|
super.init(frame: frame)
|
|
|
|
self.layer.addSublayer(self.separatorLayer)
|
|
self.addSubview(self.containerButton)
|
|
|
|
self.containerButton.addTarget(self, action: #selector(self.pressed), for: .touchUpInside)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
@objc private func pressed() {
|
|
guard let component = self.component else {
|
|
return
|
|
}
|
|
component.action()
|
|
}
|
|
|
|
func update(component: CountryListItemComponent, availableSize: CGSize, state: EmptyComponentState, environment: Environment<Empty>, transition: Transition) -> CGSize {
|
|
let themeUpdated = self.component?.theme !== component.theme
|
|
|
|
var hasSelectionUpdated = false
|
|
if let previousComponent = self.component {
|
|
switch previousComponent.selectionState {
|
|
case .none:
|
|
if case .none = component.selectionState {
|
|
} else {
|
|
hasSelectionUpdated = true
|
|
}
|
|
case .editing:
|
|
if case .editing = component.selectionState {
|
|
} else {
|
|
hasSelectionUpdated = true
|
|
}
|
|
}
|
|
}
|
|
|
|
self.component = component
|
|
self.state = state
|
|
|
|
let contextInset: CGFloat = 0.0
|
|
|
|
let height: CGFloat = 44.0
|
|
let verticalInset: CGFloat = 0.0
|
|
var leftInset: CGFloat = 16.0
|
|
let rightInset: CGFloat = contextInset * 2.0 + 8.0
|
|
|
|
if case let .editing(isSelected, isTinted) = component.selectionState {
|
|
leftInset += 44.0
|
|
let checkSize: CGFloat = 22.0
|
|
|
|
let checkLayer: CheckLayer
|
|
if let current = self.checkLayer {
|
|
checkLayer = current
|
|
if themeUpdated {
|
|
var theme = CheckNodeTheme(theme: component.theme, style: .plain)
|
|
if isTinted {
|
|
theme.backgroundColor = theme.backgroundColor.mixedWith(component.theme.list.itemBlocksBackgroundColor, alpha: 0.5)
|
|
}
|
|
checkLayer.theme = theme
|
|
}
|
|
checkLayer.setSelected(isSelected, animated: !transition.animation.isImmediate)
|
|
} else {
|
|
var theme = CheckNodeTheme(theme: component.theme, style: .plain)
|
|
if isTinted {
|
|
theme.backgroundColor = theme.backgroundColor.mixedWith(component.theme.list.itemBlocksBackgroundColor, alpha: 0.5)
|
|
}
|
|
checkLayer = CheckLayer(theme: theme)
|
|
self.checkLayer = checkLayer
|
|
self.containerButton.layer.addSublayer(checkLayer)
|
|
checkLayer.frame = CGRect(origin: CGPoint(x: -checkSize, y: floor((height - verticalInset * 2.0 - checkSize) / 2.0)), size: CGSize(width: checkSize, height: checkSize))
|
|
checkLayer.setSelected(isSelected, animated: false)
|
|
checkLayer.setNeedsDisplay()
|
|
}
|
|
transition.setFrame(layer: checkLayer, frame: CGRect(origin: CGPoint(x: floor((54.0 - checkSize) * 0.5), y: floor((height - verticalInset * 2.0 - checkSize) / 2.0)), size: CGSize(width: checkSize, height: checkSize)))
|
|
} else {
|
|
if let checkLayer = self.checkLayer {
|
|
self.checkLayer = nil
|
|
transition.setPosition(layer: checkLayer, position: CGPoint(x: -checkLayer.bounds.width * 0.5, y: checkLayer.position.y), completion: { [weak checkLayer] _ in
|
|
checkLayer?.removeFromSuperlayer()
|
|
})
|
|
}
|
|
}
|
|
|
|
let previousTitleFrame = self.title.view?.frame
|
|
var previousTitleContents: UIView?
|
|
if hasSelectionUpdated && !"".isEmpty {
|
|
previousTitleContents = self.title.view?.snapshotView(afterScreenUpdates: false)
|
|
}
|
|
|
|
let titleSize = self.title.update(
|
|
transition: .immediate,
|
|
component: AnyComponent(MultilineTextComponent(
|
|
text: .plain(NSAttributedString(string: component.title, font: Font.regular(17.0), textColor: component.theme.list.itemPrimaryTextColor))
|
|
)),
|
|
environment: {},
|
|
containerSize: CGSize(width: availableSize.width - leftInset - rightInset, height: 100.0)
|
|
)
|
|
|
|
let centralContentHeight: CGFloat = titleSize.height
|
|
|
|
let titleFrame = CGRect(origin: CGPoint(x: leftInset, y: floor((height - verticalInset * 2.0 - centralContentHeight) / 2.0)), size: titleSize)
|
|
if let titleView = self.title.view {
|
|
if titleView.superview == nil {
|
|
titleView.isUserInteractionEnabled = false
|
|
self.containerButton.addSubview(titleView)
|
|
}
|
|
titleView.frame = titleFrame
|
|
if let previousTitleFrame, previousTitleFrame.origin.x != titleFrame.origin.x {
|
|
transition.animatePosition(view: titleView, from: CGPoint(x: previousTitleFrame.origin.x - titleFrame.origin.x, y: 0.0), to: CGPoint(), additive: true)
|
|
}
|
|
|
|
if let previousTitleFrame, let previousTitleContents, previousTitleFrame.size != titleSize {
|
|
previousTitleContents.frame = CGRect(origin: previousTitleFrame.origin, size: previousTitleFrame.size)
|
|
self.addSubview(previousTitleContents)
|
|
|
|
transition.setFrame(view: previousTitleContents, frame: CGRect(origin: titleFrame.origin, size: previousTitleFrame.size))
|
|
transition.setAlpha(view: previousTitleContents, alpha: 0.0, completion: { [weak previousTitleContents] _ in
|
|
previousTitleContents?.removeFromSuperview()
|
|
})
|
|
transition.animateAlpha(view: titleView, from: 0.0, to: 1.0)
|
|
}
|
|
}
|
|
|
|
if themeUpdated {
|
|
self.separatorLayer.backgroundColor = component.theme.list.itemPlainSeparatorColor.cgColor
|
|
}
|
|
let separatorLeftInset = leftInset + 44.0
|
|
transition.setFrame(layer: self.separatorLayer, frame: CGRect(origin: CGPoint(x: separatorLeftInset, y: height), size: CGSize(width: availableSize.width - separatorLeftInset, height: UIScreenPixel)))
|
|
self.separatorLayer.isHidden = !component.hasNext
|
|
|
|
let containerFrame = CGRect(origin: CGPoint(x: contextInset, y: verticalInset), size: CGSize(width: availableSize.width - contextInset * 2.0, height: height - verticalInset * 2.0))
|
|
transition.setFrame(view: self.containerButton, frame: containerFrame)
|
|
|
|
return CGSize(width: availableSize.width, height: height)
|
|
}
|
|
}
|
|
|
|
func makeView() -> View {
|
|
return View(frame: CGRect())
|
|
}
|
|
|
|
func update(view: View, availableSize: CGSize, state: EmptyComponentState, environment: Environment<Empty>, transition: Transition) -> CGSize {
|
|
return view.update(component: self, availableSize: availableSize, state: state, environment: environment, transition: transition)
|
|
}
|
|
}
|