mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
229 lines
8.8 KiB
Swift
229 lines
8.8 KiB
Swift
import Foundation
|
|
import Display
|
|
import AsyncDisplayKit
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
import TelegramCore
|
|
|
|
public class ContactSelectionController: ViewController {
|
|
private let account: Account
|
|
|
|
private var contactsNode: ContactSelectionControllerNode {
|
|
return self.displayNode as! ContactSelectionControllerNode
|
|
}
|
|
|
|
private let index: PeerNameIndex = .lastNameFirst
|
|
private let titleProducer: (PresentationStrings) -> String
|
|
private let options: [ContactListAdditionalOption]
|
|
|
|
private var _ready = Promise<Bool>()
|
|
override public var ready: Promise<Bool> {
|
|
return self._ready
|
|
}
|
|
|
|
private let _result = Promise<PeerId?>()
|
|
var result: Signal<PeerId?, NoError> {
|
|
return self._result.get()
|
|
}
|
|
|
|
private let confirmation: (PeerId) -> Signal<Bool, NoError>
|
|
|
|
private let createActionDisposable = MetaDisposable()
|
|
private let confirmationDisposable = MetaDisposable()
|
|
|
|
private var presentationData: PresentationData
|
|
private var presentationDataDisposable: Disposable?
|
|
|
|
public var displayNavigationActivity: Bool = false {
|
|
didSet {
|
|
if self.displayNavigationActivity != oldValue {
|
|
if self.displayNavigationActivity {
|
|
self.navigationItem.setRightBarButton(UIBarButtonItem(customDisplayNode: ProgressNavigationButtonNode(color: self.presentationData.theme.rootController.navigationBar.accentTextColor)), animated: false)
|
|
} else {
|
|
self.navigationItem.setRightBarButton(nil, animated: false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public init(account: Account, title: @escaping (PresentationStrings) -> String, options: [ContactListAdditionalOption] = [], confirmation: @escaping (PeerId) -> Signal<Bool, NoError> = { _ in .single(true) }) {
|
|
self.account = account
|
|
self.titleProducer = title
|
|
self.options = options
|
|
self.confirmation = confirmation
|
|
|
|
self.presentationData = account.telegramApplicationContext.currentPresentationData.with { $0 }
|
|
|
|
super.init(navigationBarPresentationData: NavigationBarPresentationData(presentationData: self.presentationData))
|
|
|
|
self.statusBar.statusBarStyle = self.presentationData.theme.rootController.statusBar.style.style
|
|
|
|
self.title = self.titleProducer(self.presentationData.strings)
|
|
|
|
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: self.presentationData.strings.Common_Back, style: .plain, target: nil, action: nil)
|
|
|
|
self.scrollToTop = { [weak self] in
|
|
if let strongSelf = self {
|
|
strongSelf.contactsNode.contactListNode.scrollToTop()
|
|
}
|
|
}
|
|
|
|
self.presentationDataDisposable = (account.telegramApplicationContext.presentationData
|
|
|> deliverOnMainQueue).start(next: { [weak self] presentationData in
|
|
if let strongSelf = self {
|
|
let previousTheme = strongSelf.presentationData.theme
|
|
let previousStrings = strongSelf.presentationData.strings
|
|
|
|
strongSelf.presentationData = presentationData
|
|
|
|
if previousTheme !== presentationData.theme || previousStrings !== presentationData.strings {
|
|
strongSelf.updateThemeAndStrings()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
required public init(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
self.createActionDisposable.dispose()
|
|
self.presentationDataDisposable?.dispose()
|
|
}
|
|
|
|
private func updateThemeAndStrings() {
|
|
self.statusBar.statusBarStyle = self.presentationData.theme.rootController.statusBar.style.style
|
|
self.navigationBar?.updatePresentationData(NavigationBarPresentationData(presentationData: self.presentationData))
|
|
self.title = self.titleProducer(self.presentationData.strings)
|
|
self.tabBarItem.title = self.presentationData.strings.Contacts_Title
|
|
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: self.presentationData.strings.Common_Back, style: .plain, target: nil, action: nil)
|
|
}
|
|
|
|
@objc func cancelPressed() {
|
|
self._result.set(.single(nil))
|
|
|
|
if let presentationArguments = self.presentationArguments as? ViewControllerPresentationArguments {
|
|
switch presentationArguments.presentationAnimation {
|
|
case .modalSheet:
|
|
self.contactsNode.animateOut()
|
|
case .none:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
override public func loadDisplayNode() {
|
|
self.displayNode = ContactSelectionControllerNode(account: self.account, options: self.options)
|
|
self._ready.set(self.contactsNode.contactListNode.ready)
|
|
|
|
self.contactsNode.navigationBar = self.navigationBar
|
|
|
|
self.contactsNode.requestDeactivateSearch = { [weak self] in
|
|
self?.deactivateSearch()
|
|
}
|
|
|
|
self.contactsNode.requestOpenPeerFromSearch = { [weak self] peerId in
|
|
self?.openPeer(peerId: peerId)
|
|
}
|
|
|
|
self.contactsNode.contactListNode.activateSearch = { [weak self] in
|
|
self?.activateSearch()
|
|
}
|
|
|
|
self.contactsNode.contactListNode.openPeer = { [weak self] peer in
|
|
self?.openPeer(peerId: peer.id)
|
|
}
|
|
|
|
self.contactsNode.dismiss = { [weak self] in
|
|
self?.presentingViewController?.dismiss(animated: true, completion: nil)
|
|
}
|
|
|
|
self.displayNodeDidLoad()
|
|
}
|
|
|
|
override public func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
if let presentationArguments = self.presentationArguments as? ViewControllerPresentationArguments {
|
|
switch presentationArguments.presentationAnimation {
|
|
case .modalSheet:
|
|
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: self.presentationData.strings.Common_Cancel, style: .plain, target: self, action: #selector(cancelPressed))
|
|
case .none:
|
|
break
|
|
}
|
|
}
|
|
|
|
self.contactsNode.contactListNode.enableUpdates = true
|
|
}
|
|
|
|
override public func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
|
|
if let presentationArguments = self.presentationArguments as? ViewControllerPresentationArguments {
|
|
switch presentationArguments.presentationAnimation {
|
|
case .modalSheet:
|
|
self.contactsNode.animateIn()
|
|
case .none:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
override public func viewDidDisappear(_ animated: Bool) {
|
|
super.viewDidDisappear(animated)
|
|
|
|
self.contactsNode.contactListNode.enableUpdates = false
|
|
}
|
|
|
|
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
|
super.containerLayoutUpdated(layout, transition: transition)
|
|
|
|
self.contactsNode.containerLayoutUpdated(layout, navigationBarHeight: self.navigationHeight, transition: transition)
|
|
}
|
|
|
|
private func activateSearch() {
|
|
if self.displayNavigationBar {
|
|
if let scrollToTop = self.scrollToTop {
|
|
scrollToTop()
|
|
}
|
|
self.contactsNode.activateSearch()
|
|
self.setDisplayNavigationBar(false, transition: .animated(duration: 0.5, curve: .spring))
|
|
}
|
|
}
|
|
|
|
private func deactivateSearch() {
|
|
if !self.displayNavigationBar {
|
|
self.setDisplayNavigationBar(true, transition: .animated(duration: 0.5, curve: .spring))
|
|
self.contactsNode.deactivateSearch()
|
|
}
|
|
}
|
|
|
|
private func openPeer(peerId: PeerId) {
|
|
self.contactsNode.contactListNode.listNode.clearHighlightAnimated(true)
|
|
self.confirmationDisposable.set((self.confirmation(peerId) |> deliverOnMainQueue).start(next: { [weak self] value in
|
|
if let strongSelf = self {
|
|
if value {
|
|
strongSelf._result.set(.single(peerId))
|
|
strongSelf.dismiss()
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
|
|
override open func dismiss(completion: (() -> Void)? = nil) {
|
|
if let presentationArguments = self.presentationArguments as? ViewControllerPresentationArguments {
|
|
switch presentationArguments.presentationAnimation {
|
|
case .modalSheet:
|
|
self.contactsNode.animateOut(completion: completion)
|
|
case .none:
|
|
completion?()
|
|
}
|
|
}
|
|
}
|
|
|
|
public func dismissSearch() {
|
|
self.deactivateSearch()
|
|
}
|
|
}
|