import Foundation import AsyncDisplayKit import Display import TelegramCore import Postbox import SwiftSignalKit private enum SubscriberAction { case join case kicked case muteNotifications case unmuteNotifications } private func titleAndColorForAction(_ action: SubscriberAction) -> (String, UIColor) { switch action { case .join: return ("Join", UIColor(0x007ee5)) case .kicked: return ("Join", UIColor.gray) case .muteNotifications: return ("Mute", UIColor(0x007ee5)) case .unmuteNotifications: return ("Unmute", UIColor(0x007ee5)) } } private func actionForPeer(_ peer: Peer) -> SubscriberAction? { if let channel = peer as? TelegramChannel { switch channel.participationStatus { case .kicked: return .kicked case .left: return .join case .member: return .muteNotifications } return .join } else { return nil } } final class ChatChannelSubscriberInputPanelNode: ChatInputPanelNode { private let button: UIButton private let activityIndicator: UIActivityIndicatorView private var action: SubscriberAction? private let actionDisposable = MetaDisposable() private var presentationInterfaceState = ChatPresentationInterfaceState() override init() { self.button = UIButton() self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) self.activityIndicator.isHidden = true super.init() self.view.addSubview(self.button) self.view.addSubview(self.activityIndicator) button.addTarget(self, action: #selector(self.buttonPressed), for: [.touchUpInside]) } deinit { self.actionDisposable.dispose() } override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if self.bounds.contains(point) { return self.button } else { return nil } } @objc func buttonPressed() { guard let account = self.account, let action = self.action, let peer = self.presentationInterfaceState.peer else { return } switch action { case .join: self.activityIndicator.isHidden = false self.activityIndicator.startAnimating() self.actionDisposable.set((joinChannel(account: account, peerId: peer.id) |> afterDisposed { [weak self] in Queue.mainQueue().async { if let strongSelf = self { strongSelf.activityIndicator.isHidden = true strongSelf.activityIndicator.stopAnimating() } } }).start()) case .kicked: break case .muteNotifications: break case .unmuteNotifications: break } } override func updateLayout(width: CGFloat, transition: ContainedViewLayoutTransition, interfaceState: ChatPresentationInterfaceState) -> CGFloat { if self.presentationInterfaceState != interfaceState { let previousState = self.presentationInterfaceState self.presentationInterfaceState = interfaceState if let peer = interfaceState.peer, previousState.peer == nil || !peer.isEqual(previousState.peer!) { if let action = actionForPeer(peer) { self.action = action let (title, color) = titleAndColorForAction(action) self.button.setTitle(title, for: []) self.button.setTitleColor(color, for: [.normal]) self.button.setTitleColor(color.withAlphaComponent(0.5), for: [.highlighted]) self.button.sizeToFit() } else { self.action = nil } } } let panelHeight: CGFloat = 47.0 let buttonSize = self.button.bounds.size self.button.frame = CGRect(origin: CGPoint(x: floor((width - buttonSize.width) / 2.0), y: floor((panelHeight - buttonSize.height) / 2.0)), size: buttonSize) let indicatorSize = self.activityIndicator.bounds.size self.activityIndicator.frame = CGRect(origin: CGPoint(x: width - indicatorSize.width - 12.0, y: floor((panelHeight - indicatorSize.height) / 2.0)), size: indicatorSize) return 47.0 } }