mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-15 21:45:19 +00:00
437 lines
23 KiB
Swift
437 lines
23 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import AsyncDisplayKit
|
|
import Display
|
|
import SwiftSignalKit
|
|
import TelegramCore
|
|
import TelegramPresentationData
|
|
import ItemListUI
|
|
import LocationResources
|
|
import AppBundle
|
|
import LiveLocationTimerNode
|
|
import TelegramStringFormatting
|
|
|
|
public enum LocationActionListItemIcon: Equatable {
|
|
case location
|
|
case liveLocation
|
|
case stopLiveLocation
|
|
case extendLiveLocation
|
|
case venue(TelegramMediaMap)
|
|
|
|
public static func ==(lhs: LocationActionListItemIcon, rhs: LocationActionListItemIcon) -> Bool {
|
|
switch lhs {
|
|
case .location:
|
|
if case .location = rhs {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case .liveLocation:
|
|
if case .liveLocation = rhs {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case .stopLiveLocation:
|
|
if case .stopLiveLocation = rhs {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case .extendLiveLocation:
|
|
if case .extendLiveLocation = rhs {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
case let .venue(lhsVenue):
|
|
if case let .venue(rhsVenue) = rhs, lhsVenue.venue?.id == rhsVenue.venue?.id {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func generateLocationIcon(theme: PresentationTheme) -> UIImage {
|
|
return generateImage(CGSize(width: 40.0, height: 40.0), rotatedContext: { size, context in
|
|
context.clear(CGRect(origin: CGPoint(), size: size))
|
|
context.setFillColor(theme.chat.inputPanel.actionControlFillColor.cgColor)
|
|
context.fillEllipse(in: CGRect(origin: CGPoint(), size: size))
|
|
|
|
context.translateBy(x: size.width / 2.0, y: size.height / 2.0)
|
|
context.scaleBy(x: 1.0, y: -1.0)
|
|
context.translateBy(x: -size.width / 2.0, y: -size.height / 2.0)
|
|
|
|
if let image = generateTintedImage(image: UIImage(bundleImageName: "Location/SendLocationIcon"), color: theme.chat.inputPanel.actionControlForegroundColor) {
|
|
context.draw(image.cgImage!, in: CGRect(origin: CGPoint(x: floor((size.width - image.size.width) / 2.0), y: floor((size.height - image.size.height) / 2.0)), size: image.size))
|
|
}
|
|
})!
|
|
}
|
|
|
|
private enum LiveLocationIconType {
|
|
case start
|
|
case stop
|
|
case extend
|
|
}
|
|
private func generateLiveLocationIcon(theme: PresentationTheme, type: LiveLocationIconType) -> UIImage {
|
|
return generateImage(CGSize(width: 40.0, height: 40.0), rotatedContext: { size, context in
|
|
let imageName: String
|
|
let color: UIColor
|
|
switch type {
|
|
case .start:
|
|
imageName = "Location/SendLiveLocationIcon"
|
|
color = UIColor(rgb: 0x6cc139)
|
|
case .stop:
|
|
imageName = "Location/SendLocationIcon"
|
|
color = UIColor(rgb: 0xff6464)
|
|
case .extend:
|
|
imageName = "Location/SendLocationIcon"
|
|
color = UIColor(rgb: 0x6cc139)
|
|
}
|
|
|
|
context.clear(CGRect(origin: CGPoint(), size: size))
|
|
context.setFillColor(color.cgColor)
|
|
context.fillEllipse(in: CGRect(origin: CGPoint(), size: size))
|
|
|
|
context.translateBy(x: size.width / 2.0, y: size.height / 2.0)
|
|
context.scaleBy(x: 1.0, y: -1.0)
|
|
context.translateBy(x: -size.width / 2.0, y: -size.height / 2.0)
|
|
|
|
if let image = generateTintedImage(image: UIImage(bundleImageName: imageName), color: theme.chat.inputPanel.actionControlForegroundColor) {
|
|
context.draw(image.cgImage!, in: CGRect(origin: CGPoint(x: floor((size.width - image.size.width) / 2.0), y: floor((size.height - image.size.height) / 2.0)), size: image.size))
|
|
|
|
if case .extend = type {
|
|
context.setStrokeColor(theme.chat.inputPanel.actionControlForegroundColor.cgColor)
|
|
context.setLineWidth(2.0 - UIScreenPixel)
|
|
context.setLineCap(.round)
|
|
|
|
let length: CGFloat = 6.0 + UIScreenPixel
|
|
context.move(to: CGPoint(x: 8.0 + 0.0, y: image.size.height / 2.0))
|
|
context.addLine(to: CGPoint(x: 8.0 + length, y: image.size.height / 2.0))
|
|
context.strokePath()
|
|
|
|
context.move(to: CGPoint(x: 8.0 + length / 2.0, y: image.size.height / 2.0 - length / 2.0))
|
|
context.addLine(to: CGPoint(x: 8.0 + length / 2.0, y: image.size.height / 2.0 + length / 2.0))
|
|
context.strokePath()
|
|
} else if case .stop = type {
|
|
context.setStrokeColor(color.cgColor)
|
|
context.setLineWidth(5.0)
|
|
|
|
context.move(to: CGPoint(x: 10.0, y: size.height - 10.0))
|
|
context.addLine(to: CGPoint(x: size.width - 10.0, y: 10.0))
|
|
context.strokePath()
|
|
|
|
context.setStrokeColor(theme.chat.inputPanel.actionControlForegroundColor.cgColor)
|
|
context.setLineWidth(2.0 - UIScreenPixel)
|
|
context.setLineCap(.round)
|
|
|
|
context.move(to: CGPoint(x: 12.0, y: size.height - 12.0))
|
|
context.addLine(to: CGPoint(x: size.width - 12.0, y: 12.0))
|
|
context.strokePath()
|
|
}
|
|
}
|
|
})!
|
|
}
|
|
|
|
final class LocationActionListItem: ListViewItem {
|
|
let presentationData: ItemListPresentationData
|
|
let engine: TelegramEngine
|
|
let title: String
|
|
let subtitle: String
|
|
let icon: LocationActionListItemIcon
|
|
let beginTimeAndTimeout: (Double, Double)?
|
|
let action: () -> Void
|
|
let highlighted: (Bool) -> Void
|
|
|
|
public init(presentationData: ItemListPresentationData, engine: TelegramEngine, title: String, subtitle: String, icon: LocationActionListItemIcon, beginTimeAndTimeout: (Double, Double)?, action: @escaping () -> Void, highlighted: @escaping (Bool) -> Void = { _ in }) {
|
|
self.presentationData = presentationData
|
|
self.engine = engine
|
|
self.title = title
|
|
self.subtitle = subtitle
|
|
self.icon = icon
|
|
self.beginTimeAndTimeout = beginTimeAndTimeout
|
|
self.action = action
|
|
self.highlighted = highlighted
|
|
}
|
|
|
|
public func nodeConfiguredForParams(async: @escaping (@escaping () -> Void) -> Void, params: ListViewItemLayoutParams, synchronousLoads: Bool, previousItem: ListViewItem?, nextItem: ListViewItem?, completion: @escaping (ListViewItemNode, @escaping () -> (Signal<Void, NoError>?, (ListViewItemApply) -> Void)) -> Void) {
|
|
async {
|
|
let node = LocationActionListItemNode()
|
|
let makeLayout = node.asyncLayout()
|
|
let (nodeLayout, nodeApply) = makeLayout(self, params, nextItem is LocationActionListItem || nextItem is LocationLiveListItem)
|
|
node.contentSize = nodeLayout.contentSize
|
|
node.insets = nodeLayout.insets
|
|
|
|
completion(node, nodeApply)
|
|
}
|
|
}
|
|
|
|
public func updateNode(async: @escaping (@escaping () -> Void) -> Void, node: @escaping () -> ListViewItemNode, params: ListViewItemLayoutParams, previousItem: ListViewItem?, nextItem: ListViewItem?, animation: ListViewItemUpdateAnimation, completion: @escaping (ListViewItemNodeLayout, @escaping (ListViewItemApply) -> Void) -> Void) {
|
|
Queue.mainQueue().async {
|
|
if let nodeValue = node() as? LocationActionListItemNode {
|
|
let layout = nodeValue.asyncLayout()
|
|
async {
|
|
let (nodeLayout, apply) = layout(self, params, nextItem is LocationActionListItem || nextItem is LocationLiveListItem)
|
|
Queue.mainQueue().async {
|
|
completion(nodeLayout, { info in
|
|
apply().1(info)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public var selectable: Bool {
|
|
return true
|
|
}
|
|
|
|
public func selected(listView: ListView) {
|
|
listView.clearHighlightAnimated(false)
|
|
self.action()
|
|
}
|
|
}
|
|
|
|
final class LocationActionListItemNode: ListViewItemNode {
|
|
private let backgroundNode: ASDisplayNode
|
|
private let separatorNode: ASDisplayNode
|
|
private let highlightedBackgroundNode: ASDisplayNode
|
|
private var titleNode: TextNode?
|
|
private var subtitleNode: TextNode?
|
|
private let iconNode: ASImageNode
|
|
private let venueIconNode: TransformImageNode
|
|
private var timerNode: ChatMessageLiveLocationTimerNode?
|
|
private var wavesNode: LiveLocationWavesNode?
|
|
|
|
private var item: LocationActionListItem?
|
|
private var layoutParams: ListViewItemLayoutParams?
|
|
|
|
required init() {
|
|
self.backgroundNode = ASDisplayNode()
|
|
self.backgroundNode.isLayerBacked = true
|
|
|
|
self.separatorNode = ASDisplayNode()
|
|
self.separatorNode.isLayerBacked = true
|
|
|
|
self.highlightedBackgroundNode = ASDisplayNode()
|
|
self.highlightedBackgroundNode.isLayerBacked = true
|
|
|
|
self.iconNode = ASImageNode()
|
|
self.iconNode.displaysAsynchronously = false
|
|
self.iconNode.displayWithoutProcessing = true
|
|
|
|
self.venueIconNode = TransformImageNode()
|
|
|
|
super.init(layerBacked: false, dynamicBounce: false, rotated: false, seeThrough: false)
|
|
|
|
self.addSubnode(self.backgroundNode)
|
|
self.addSubnode(self.separatorNode)
|
|
self.addSubnode(self.iconNode)
|
|
self.addSubnode(self.venueIconNode)
|
|
}
|
|
|
|
override func layoutForParams(_ params: ListViewItemLayoutParams, item: ListViewItem, previousItem: ListViewItem?, nextItem: ListViewItem?) {
|
|
if let item = self.item {
|
|
let makeLayout = self.asyncLayout()
|
|
let (nodeLayout, nodeApply) = makeLayout(item, params, nextItem is LocationActionListItem)
|
|
self.contentSize = nodeLayout.contentSize
|
|
self.insets = nodeLayout.insets
|
|
let _ = nodeApply()
|
|
}
|
|
}
|
|
|
|
override func setHighlighted(_ highlighted: Bool, at point: CGPoint, animated: Bool) {
|
|
super.setHighlighted(highlighted, at: point, animated: animated)
|
|
|
|
self.item?.highlighted(highlighted)
|
|
|
|
if highlighted {
|
|
self.highlightedBackgroundNode.alpha = 1.0
|
|
if self.highlightedBackgroundNode.supernode == nil {
|
|
self.insertSubnode(self.highlightedBackgroundNode, aboveSubnode: self.separatorNode)
|
|
}
|
|
} else {
|
|
if self.highlightedBackgroundNode.supernode != nil {
|
|
if animated {
|
|
self.highlightedBackgroundNode.layer.animateAlpha(from: self.highlightedBackgroundNode.alpha, to: 0.0, duration: 0.4, completion: { [weak self] completed in
|
|
if let strongSelf = self {
|
|
if completed {
|
|
strongSelf.highlightedBackgroundNode.removeFromSupernode()
|
|
}
|
|
}
|
|
})
|
|
self.highlightedBackgroundNode.alpha = 0.0
|
|
} else {
|
|
self.highlightedBackgroundNode.removeFromSupernode()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func asyncLayout() -> (_ item: LocationActionListItem, _ params: ListViewItemLayoutParams, _ hasSeparator: Bool) -> (ListViewItemNodeLayout, () -> (Signal<Void, NoError>?, (ListViewItemApply) -> Void)) {
|
|
let currentItem = self.item
|
|
|
|
let makeTitleLayout = TextNode.asyncLayout(self.titleNode)
|
|
let makeSubtitleLayout = TextNode.asyncLayout(self.subtitleNode)
|
|
let iconLayout = self.venueIconNode.asyncLayout()
|
|
|
|
return { [weak self] item, params, hasSeparator in
|
|
let leftInset: CGFloat = 65.0 + params.leftInset
|
|
let rightInset: CGFloat = params.rightInset
|
|
let verticalInset: CGFloat = 8.0
|
|
let iconSize: CGFloat = 40.0
|
|
|
|
let titleFont = Font.medium(item.presentationData.fontSize.itemListBaseFontSize)
|
|
let subtitleFont = Font.regular(floor(item.presentationData.fontSize.itemListBaseFontSize * 14.0 / 17.0))
|
|
|
|
let titleAttributedString = NSAttributedString(string: item.title, font: titleFont, textColor: item.presentationData.theme.list.itemPrimaryTextColor)
|
|
let (titleLayout, titleApply) = makeTitleLayout(TextNodeLayoutArguments(attributedString: titleAttributedString, backgroundColor: nil, maximumNumberOfLines: 1, truncationType: .end, constrainedSize: CGSize(width: params.width - leftInset - rightInset - 15.0, height: CGFloat.greatestFiniteMagnitude), alignment: .natural, cutout: nil, insets: UIEdgeInsets()))
|
|
|
|
let subtitleAttributedString = NSAttributedString(string: item.subtitle, font: subtitleFont, textColor: item.presentationData.theme.list.itemSecondaryTextColor)
|
|
let (subtitleLayout, subtitleApply) = makeSubtitleLayout(TextNodeLayoutArguments(attributedString: subtitleAttributedString, backgroundColor: nil, maximumNumberOfLines: 1, truncationType: .end, constrainedSize: CGSize(width: params.width - leftInset - rightInset - 15.0, height: CGFloat.greatestFiniteMagnitude), alignment: .natural, cutout: nil, insets: UIEdgeInsets()))
|
|
|
|
let titleSpacing: CGFloat = 1.0
|
|
let bottomInset: CGFloat = hasSeparator ? 0.0 : 4.0
|
|
var contentSize = CGSize(width: params.width, height: verticalInset * 2.0 + titleLayout.size.height + titleSpacing + subtitleLayout.size.height + bottomInset)
|
|
if hasSeparator {
|
|
contentSize.height = max(52.0, contentSize.height)
|
|
}
|
|
let nodeLayout = ListViewItemNodeLayout(contentSize: contentSize, insets: UIEdgeInsets())
|
|
|
|
return (nodeLayout, { [weak self] in
|
|
var updatedTheme: PresentationTheme?
|
|
if currentItem?.presentationData.theme !== item.presentationData.theme {
|
|
updatedTheme = item.presentationData.theme
|
|
}
|
|
|
|
var updatedIcon: LocationActionListItemIcon?
|
|
if currentItem?.icon != item.icon || updatedTheme != nil {
|
|
updatedIcon = item.icon
|
|
}
|
|
|
|
return (nil, { _ in
|
|
if let strongSelf = self {
|
|
strongSelf.item = item
|
|
strongSelf.layoutParams = params
|
|
|
|
if let _ = updatedTheme {
|
|
strongSelf.separatorNode.backgroundColor = item.presentationData.theme.list.itemPlainSeparatorColor
|
|
strongSelf.backgroundNode.backgroundColor = item.presentationData.theme.list.plainBackgroundColor
|
|
strongSelf.highlightedBackgroundNode.backgroundColor = item.presentationData.theme.list.itemHighlightedBackgroundColor
|
|
}
|
|
|
|
var arguments: TransformImageCustomArguments?
|
|
if let updatedIcon = updatedIcon {
|
|
switch updatedIcon {
|
|
case .location:
|
|
strongSelf.iconNode.isHidden = false
|
|
strongSelf.venueIconNode.isHidden = true
|
|
strongSelf.iconNode.image = generateLocationIcon(theme: item.presentationData.theme)
|
|
case .liveLocation:
|
|
strongSelf.iconNode.isHidden = false
|
|
strongSelf.venueIconNode.isHidden = true
|
|
strongSelf.iconNode.image = generateLiveLocationIcon(theme: item.presentationData.theme, type: .start)
|
|
case .stopLiveLocation:
|
|
strongSelf.iconNode.isHidden = false
|
|
strongSelf.venueIconNode.isHidden = true
|
|
strongSelf.iconNode.image = generateLiveLocationIcon(theme: item.presentationData.theme, type: .stop)
|
|
case .extendLiveLocation:
|
|
strongSelf.iconNode.isHidden = false
|
|
strongSelf.venueIconNode.isHidden = true
|
|
strongSelf.iconNode.image = generateLiveLocationIcon(theme: item.presentationData.theme, type: .extend)
|
|
case let .venue(venue):
|
|
strongSelf.iconNode.isHidden = true
|
|
strongSelf.venueIconNode.isHidden = false
|
|
|
|
let type = venue.venue?.type
|
|
var flag: String?
|
|
if let venue = venue.venue, venue.provider == "city", let countryCode = venue.id {
|
|
flag = flagEmoji(countryCode: countryCode)
|
|
}
|
|
|
|
if venue.venue?.provider == "city" {
|
|
arguments = VenueIconArguments(defaultBackgroundColor: item.presentationData.theme.chat.inputPanel.actionControlFillColor, defaultForegroundColor: .white)
|
|
}
|
|
strongSelf.venueIconNode.setSignal(venueIcon(engine: item.engine, type: type ?? "", flag: flag, background: true))
|
|
}
|
|
|
|
switch updatedIcon {
|
|
case .stopLiveLocation, .extendLiveLocation:
|
|
let wavesNode = LiveLocationWavesNode(color: item.presentationData.theme.chat.inputPanel.actionControlForegroundColor, extend: updatedIcon == .extendLiveLocation)
|
|
strongSelf.addSubnode(wavesNode)
|
|
strongSelf.wavesNode = wavesNode
|
|
default:
|
|
strongSelf.wavesNode?.removeFromSupernode()
|
|
strongSelf.wavesNode = nil
|
|
}
|
|
strongSelf.wavesNode?.color = item.presentationData.theme.chat.inputPanel.actionControlForegroundColor
|
|
}
|
|
|
|
let iconApply = iconLayout(TransformImageArguments(corners: ImageCorners(), imageSize: CGSize(width: iconSize, height: iconSize), boundingSize: CGSize(width: iconSize, height: iconSize), intrinsicInsets: UIEdgeInsets(), custom: arguments))
|
|
iconApply()
|
|
|
|
let titleNode = titleApply()
|
|
if strongSelf.titleNode == nil {
|
|
strongSelf.titleNode = titleNode
|
|
strongSelf.addSubnode(titleNode)
|
|
}
|
|
|
|
let subtitleNode = subtitleApply()
|
|
if strongSelf.subtitleNode == nil {
|
|
strongSelf.subtitleNode = subtitleNode
|
|
strongSelf.addSubnode(subtitleNode)
|
|
}
|
|
|
|
let titleFrame = CGRect(origin: CGPoint(x: leftInset, y: verticalInset), size: titleLayout.size)
|
|
titleNode.frame = titleFrame
|
|
|
|
let subtitleFrame = CGRect(origin: CGPoint(x: leftInset, y: verticalInset + titleLayout.size.height + titleSpacing), size: subtitleLayout.size)
|
|
subtitleNode.frame = subtitleFrame
|
|
|
|
let separatorHeight = UIScreenPixel
|
|
let topHighlightInset: CGFloat = separatorHeight
|
|
|
|
let iconNodeFrame = CGRect(origin: CGPoint(x: params.leftInset + 15.0, y: floorToScreenPixels((contentSize.height - bottomInset - iconSize) / 2.0)), size: CGSize(width: iconSize, height: iconSize))
|
|
strongSelf.iconNode.frame = iconNodeFrame
|
|
strongSelf.venueIconNode.frame = iconNodeFrame
|
|
|
|
strongSelf.wavesNode?.frame = CGRect(origin: CGPoint(x: params.leftInset + 11.0, y: floorToScreenPixels((contentSize.height - bottomInset - iconSize) / 2.0) - 4.0), size: CGSize(width: 48.0, height: 48.0))
|
|
|
|
strongSelf.backgroundNode.frame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: contentSize.width, height: contentSize.height))
|
|
strongSelf.highlightedBackgroundNode.frame = CGRect(origin: CGPoint(x: 0.0, y: -nodeLayout.insets.top - topHighlightInset), size: CGSize(width: contentSize.width, height: contentSize.height + topHighlightInset))
|
|
strongSelf.separatorNode.frame = CGRect(origin: CGPoint(x: leftInset, y: nodeLayout.contentSize.height - separatorHeight), size: CGSize(width: nodeLayout.size.width, height: separatorHeight))
|
|
strongSelf.separatorNode.isHidden = !hasSeparator
|
|
|
|
if let (beginTimestamp, timeout) = item.beginTimeAndTimeout {
|
|
let timerNode: ChatMessageLiveLocationTimerNode
|
|
if let current = strongSelf.timerNode {
|
|
timerNode = current
|
|
} else {
|
|
timerNode = ChatMessageLiveLocationTimerNode()
|
|
strongSelf.addSubnode(timerNode)
|
|
strongSelf.timerNode = timerNode
|
|
}
|
|
let timerSize = CGSize(width: 28.0, height: 28.0)
|
|
timerNode.update(backgroundColor: item.presentationData.theme.list.itemAccentColor.withAlphaComponent(0.4), foregroundColor: item.presentationData.theme.list.itemAccentColor, textColor: item.presentationData.theme.list.itemAccentColor, beginTimestamp: beginTimestamp, timeout: Int32(timeout) == liveLocationIndefinitePeriod ? -1.0 : timeout, strings: item.presentationData.strings)
|
|
timerNode.frame = CGRect(origin: CGPoint(x: contentSize.width - 16.0 - timerSize.width, y: floorToScreenPixels((contentSize.height - timerSize.height) / 2.0) - 2.0), size: timerSize)
|
|
} else if let timerNode = strongSelf.timerNode {
|
|
strongSelf.timerNode = nil
|
|
timerNode.removeFromSupernode()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
override func animateInsertion(_ currentTimestamp: Double, duration: Double, options: ListViewItemAnimationOptions) {
|
|
self.layer.animateAlpha(from: 0.0, to: 1.0, duration: duration * 0.5)
|
|
}
|
|
|
|
override func animateRemoved(_ currentTimestamp: Double, duration: Double) {
|
|
self.layer.animateAlpha(from: 1.0, to: 0.0, duration: duration * 0.5, removeOnCompletion: false)
|
|
}
|
|
}
|