mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
1182 lines
65 KiB
Swift
1182 lines
65 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import Postbox
|
|
import SwiftSignalKit
|
|
import Display
|
|
import AsyncDisplayKit
|
|
import TelegramCore
|
|
|
|
public class ChatController: TelegramController {
|
|
private var containerLayout = ContainerViewLayout()
|
|
|
|
private let account: Account
|
|
private let peerId: PeerId
|
|
private let messageId: MessageId?
|
|
|
|
private let peerDisposable = MetaDisposable()
|
|
private let navigationActionDisposable = MetaDisposable()
|
|
|
|
private let messageIndexDisposable = MetaDisposable()
|
|
|
|
private let _peerReady = Promise<Bool>()
|
|
private var didSetPeerReady = false
|
|
private let peerView = Promise<PeerView>()
|
|
|
|
private var presentationInterfaceState = ChatPresentationInterfaceState()
|
|
|
|
private var chatTitleView: ChatTitleView?
|
|
private var leftNavigationButton: ChatNavigationButton?
|
|
private var rightNavigationButton: ChatNavigationButton?
|
|
private var chatInfoNavigationButton: ChatNavigationButton?
|
|
|
|
private let galleryHiddenMesageAndMediaDisposable = MetaDisposable()
|
|
|
|
private var controllerInteraction: ChatControllerInteraction?
|
|
private var interfaceInteraction: ChatPanelInterfaceInteraction?
|
|
|
|
private let controllerNavigationDisposable = MetaDisposable()
|
|
private let sentMessageEventsDisposable = MetaDisposable()
|
|
private let messageActionCallbackDisposable = MetaDisposable()
|
|
private let editMessageDisposable = MetaDisposable()
|
|
private let enqueueMediaMessageDisposable = MetaDisposable()
|
|
private var resolvePeerByNameDisposable: MetaDisposable?
|
|
|
|
private let editingMessage = ValuePromise<Bool>(false, ignoreRepeated: true)
|
|
|
|
private let botCallbackAlertMessage = Promise<String?>(nil)
|
|
private var botCallbackAlertMessageDisposable: Disposable?
|
|
|
|
private var contextQueryState: (ChatPresentationInputQuery?, Disposable)?
|
|
|
|
private var audioRecorderValue: ManagedAudioRecorder?
|
|
private var audioRecorderFeedback: HapticFeedback?
|
|
private var audioRecorder = Promise<ManagedAudioRecorder?>()
|
|
private var audioRecorderDisposable: Disposable?
|
|
|
|
public init(account: Account, peerId: PeerId, messageId: MessageId? = nil) {
|
|
self.account = account
|
|
self.peerId = peerId
|
|
self.messageId = messageId
|
|
|
|
performanceSpinnerAcquire()
|
|
|
|
super.init(account: account)
|
|
|
|
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
|
|
|
|
self.ready.set(.never())
|
|
|
|
self.scrollToTop = { [weak self] in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
strongSelf.chatDisplayNode.historyNode.scrollToStartOfHistory()
|
|
}
|
|
}
|
|
|
|
let controllerInteraction = ChatControllerInteraction(openMessage: { [weak self] id in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
var galleryMedia: Media?
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(id) {
|
|
for media in message.media {
|
|
if let file = media as? TelegramMediaFile {
|
|
galleryMedia = file
|
|
} else if let image = media as? TelegramMediaImage {
|
|
galleryMedia = image
|
|
} else if let webpage = media as? TelegramMediaWebpage, case let .Loaded(content) = webpage.content {
|
|
if let file = content.file {
|
|
galleryMedia = file
|
|
} else if let image = content.image {
|
|
galleryMedia = image
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let galleryMedia = galleryMedia {
|
|
if let file = galleryMedia as? TelegramMediaFile, file.isMusic || file.isVoice {
|
|
if let applicationContext = strongSelf.account.applicationContext as? TelegramApplicationContext {
|
|
let player = ManagedAudioPlaylistPlayer(postbox: strongSelf.account.postbox, playlist: peerMessageHistoryAudioPlaylist(account: strongSelf.account, messageId: id))
|
|
applicationContext.mediaManager.setPlaylistPlayer(player)
|
|
player.control(.navigation(.next))
|
|
}
|
|
} else {
|
|
let gallery = GalleryController(account: strongSelf.account, messageId: id)
|
|
|
|
strongSelf.galleryHiddenMesageAndMediaDisposable.set(gallery.hiddenMedia.start(next: { [weak strongSelf] messageIdAndMedia in
|
|
if let strongSelf = strongSelf {
|
|
if let messageIdAndMedia = messageIdAndMedia {
|
|
strongSelf.controllerInteraction?.hiddenMedia = [messageIdAndMedia.0: [messageIdAndMedia.1]]
|
|
} else {
|
|
strongSelf.controllerInteraction?.hiddenMedia = [:]
|
|
}
|
|
strongSelf.chatDisplayNode.historyNode.forEachItemNode { itemNode in
|
|
if let itemNode = itemNode as? ChatMessageItemView {
|
|
itemNode.updateHiddenMedia()
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
|
|
strongSelf.present(gallery, in: .window, with: GalleryControllerPresentationArguments(transitionArguments: { [weak self] messageId, media in
|
|
if let strongSelf = self {
|
|
var transitionNode: ASDisplayNode?
|
|
strongSelf.chatDisplayNode.historyNode.forEachItemNode { itemNode in
|
|
if let itemNode = itemNode as? ChatMessageItemView {
|
|
if let result = itemNode.transitionNode(id: messageId, media: media) {
|
|
transitionNode = result
|
|
}
|
|
}
|
|
}
|
|
if let transitionNode = transitionNode {
|
|
return GalleryTransitionArguments(transitionNode: transitionNode, transitionContainerNode: strongSelf.chatDisplayNode, transitionBackgroundNode: strongSelf.chatDisplayNode.historyNode)
|
|
}
|
|
}
|
|
return nil
|
|
}))
|
|
}
|
|
}
|
|
}
|
|
}, openPeer: { [weak self] id, navigation in
|
|
if let strongSelf = self {
|
|
if id == strongSelf.peerId {
|
|
switch navigation {
|
|
case .info:
|
|
strongSelf.navigationButtonAction(.openChatInfo)
|
|
case let .chat(textInputState):
|
|
if let textInputState = textInputState {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return ($0.updatedInterfaceState {
|
|
return $0.withUpdatedComposeInputState(textInputState)
|
|
}).updatedInputMode({ _ in
|
|
return .text
|
|
})
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
if let id = id {
|
|
switch navigation {
|
|
case .info:
|
|
break
|
|
case let .chat(textInputState):
|
|
if let textInputState = textInputState {
|
|
(strongSelf.account.postbox.modify({ modifier -> Void in
|
|
modifier.updatePeerChatInterfaceState(id, update: { currentState in
|
|
if let currentState = currentState as? ChatInterfaceState {
|
|
return currentState.withUpdatedComposeInputState(textInputState)
|
|
} else {
|
|
return ChatInterfaceState().withUpdatedComposeInputState(textInputState)
|
|
}
|
|
return currentState
|
|
})
|
|
})).start(completed: {
|
|
if let strongSelf = self {
|
|
(strongSelf.navigationController as? NavigationController)?.pushViewController(ChatController(account: strongSelf.account, peerId: id, messageId: nil))
|
|
}
|
|
})
|
|
} else {
|
|
(strongSelf.navigationController as? NavigationController)?.pushViewController(ChatController(account: strongSelf.account, peerId: id, messageId: nil))
|
|
}
|
|
}
|
|
} else {
|
|
switch navigation {
|
|
case .info:
|
|
break
|
|
case let .chat(textInputState):
|
|
if let textInputState = textInputState {
|
|
let controller = PeerSelectionController(account: strongSelf.account)
|
|
controller.peerSelected = { [weak controller] peerId in
|
|
if let strongSelf = self, let strongController = controller {
|
|
if peerId == strongSelf.peerId {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return ($0.updatedInterfaceState {
|
|
return $0.withUpdatedComposeInputState(textInputState)
|
|
}).updatedInputMode({ _ in
|
|
return .text
|
|
})
|
|
})
|
|
strongController.dismiss()
|
|
} else {
|
|
(strongSelf.account.postbox.modify({ modifier -> Void in
|
|
modifier.updatePeerChatInterfaceState(peerId, update: { currentState in
|
|
if let currentState = currentState as? ChatInterfaceState {
|
|
return currentState.withUpdatedComposeInputState(textInputState)
|
|
} else {
|
|
return ChatInterfaceState().withUpdatedComposeInputState(textInputState)
|
|
}
|
|
return currentState
|
|
})
|
|
}) |> deliverOnMainQueue).start(completed: {
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: true, { $0.updatedInterfaceState({ $0.withoutSelectionState() }) })
|
|
|
|
let ready = ValuePromise<Bool>()
|
|
|
|
strongSelf.controllerNavigationDisposable.set((ready.get() |> take(1) |> deliverOnMainQueue).start(next: { _ in
|
|
if let strongController = controller {
|
|
strongController.dismiss()
|
|
}
|
|
}))
|
|
|
|
(strongSelf.navigationController as? NavigationController)?.replaceTopController(ChatController(account: strongSelf.account, peerId: peerId), animated: false, ready: ready)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
strongSelf.present(controller, in: .window)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, openPeerMention: { [weak self] name in
|
|
if let strongSelf = self {
|
|
let disposable: MetaDisposable
|
|
if let resolvePeerByNameDisposable = strongSelf.resolvePeerByNameDisposable {
|
|
disposable = resolvePeerByNameDisposable
|
|
} else {
|
|
disposable = MetaDisposable()
|
|
strongSelf.resolvePeerByNameDisposable = disposable
|
|
}
|
|
disposable.set((resolvePeerByName(account: strongSelf.account, name: name, ageLimit: 10) |> take(1) |> deliverOnMainQueue).start(next: { peerId in
|
|
if let strongSelf = self {
|
|
if let peerId = peerId {
|
|
(strongSelf.navigationController as? NavigationController)?.pushViewController(ChatController(account: strongSelf.account, peerId: peerId, messageId: nil))
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
}, openMessageContextMenu: { [weak self] id, node, frame in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(id) {
|
|
if let contextMenuController = contextMenuForChatPresentationIntefaceState(strongSelf.presentationInterfaceState, account: strongSelf.account, message: message, interfaceInteraction: strongSelf.interfaceInteraction) {
|
|
strongSelf.present(contextMenuController, in: .window, with: ContextMenuControllerPresentationArguments(sourceNodeAndRect: { [weak strongSelf, weak node] in
|
|
if let node = node {
|
|
return (node, frame)
|
|
} else {
|
|
return nil
|
|
}
|
|
}))
|
|
}
|
|
}
|
|
}
|
|
}, navigateToMessage: { [weak self] fromId, id in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if id.peerId == strongSelf.peerId {
|
|
var fromIndex: MessageIndex?
|
|
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(fromId) {
|
|
fromIndex = MessageIndex(message)
|
|
}
|
|
|
|
if let fromIndex = fromIndex {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(id) {
|
|
strongSelf.chatDisplayNode.historyNode.scrollToMessage(from: fromIndex, to: MessageIndex(message))
|
|
} else {
|
|
strongSelf.messageIndexDisposable.set((strongSelf.account.postbox.messageIndexAtId(id) |> deliverOnMainQueue).start(next: { [weak strongSelf] index in
|
|
if let strongSelf = strongSelf, let index = index {
|
|
strongSelf.chatDisplayNode.historyNode.scrollToMessage(from: fromIndex, to: index)
|
|
}
|
|
}))
|
|
}
|
|
}
|
|
} else {
|
|
(strongSelf.navigationController as? NavigationController)?.pushViewController(ChatController(account: strongSelf.account, peerId: id.peerId, messageId: id))
|
|
}
|
|
}
|
|
}, clickThroughMessage: { [weak self] in
|
|
self?.chatDisplayNode.dismissInput()
|
|
}, toggleMessageSelection: { [weak self] id in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(id) {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: true, { $0.updatedInterfaceState { $0.withToggledSelectedMessage(id) } })
|
|
}
|
|
}
|
|
}, sendMessage: { [weak self] text in
|
|
if let strongSelf = self {
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({})
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: text, attributes: [], media: nil, replyToMessageId: nil)]).start()
|
|
}
|
|
}, sendSticker: { [weak self] file in
|
|
if let strongSelf = self {
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({})
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: "", attributes: [], media: file, replyToMessageId: nil)]).start()
|
|
}
|
|
}, requestMessageActionCallback: { [weak self] messageId, data in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return $0.updatedTitlePanelContext {
|
|
if !$0.contains(where: {
|
|
switch $0 {
|
|
case .requestInProgress:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
var updatedContexts = $0
|
|
updatedContexts.append(.requestInProgress)
|
|
return updatedContexts
|
|
}
|
|
return $0
|
|
}
|
|
})
|
|
|
|
strongSelf.messageActionCallbackDisposable.set((requestMessageActionCallback(account: strongSelf.account, messageId: messageId, data: data) |> afterDisposed {
|
|
Queue.mainQueue().async {
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return $0.updatedTitlePanelContext {
|
|
if let index = $0.index(where: {
|
|
switch $0 {
|
|
case .requestInProgress:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
var updatedContexts = $0
|
|
updatedContexts.remove(at: index)
|
|
return updatedContexts
|
|
}
|
|
return $0
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}).start(next: { result in
|
|
if let strongSelf = self {
|
|
switch result {
|
|
case .none:
|
|
break
|
|
case let .alert(text):
|
|
let message: Signal<String?, NoError> = .single(text)
|
|
let noMessage: Signal<String?, NoError> = .single(nil)
|
|
let delayedNoMessage: Signal<String?, NoError> = noMessage |> delay(1.0, queue: Queue.mainQueue())
|
|
strongSelf.botCallbackAlertMessage.set(message |> then(delayedNoMessage))
|
|
case let .url(url):
|
|
if let applicationContext = strongSelf.account.applicationContext as? TelegramApplicationContext {
|
|
applicationContext.openUrl(url)
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
}, openUrl: { [weak self] url in
|
|
if let strongSelf = self {
|
|
if let applicationContext = strongSelf.account.applicationContext as? TelegramApplicationContext {
|
|
//openUrl(url, in: (strongSelf.view.window as! Window))
|
|
applicationContext.openUrl(url)
|
|
}
|
|
}
|
|
}, shareCurrentLocation: { [weak self] in
|
|
if let strongSelf = self {
|
|
|
|
}
|
|
}, shareAccountContact: { [weak self] in
|
|
if let strongSelf = self {
|
|
}
|
|
}, sendBotCommand: { [weak self] messageId, command in
|
|
if let strongSelf = self {
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({})
|
|
var postAsReply = false
|
|
if !command.contains("@") && (strongSelf.peerId.namespace == Namespaces.Peer.CloudChannel || strongSelf.peerId.namespace == Namespaces.Peer.CloudGroup) {
|
|
postAsReply = true
|
|
}
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: command, attributes: [], media: nil, replyToMessageId: postAsReply ? messageId : nil)]).start()
|
|
}
|
|
}, updateInputState: { [weak self] f in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return $0.updatedInterfaceState {
|
|
return $0.withUpdatedEffectiveInputState(f($0.effectiveInputState))
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
self.controllerInteraction = controllerInteraction
|
|
|
|
self.chatTitleView = ChatTitleView(frame: CGRect())
|
|
self.navigationItem.titleView = self.chatTitleView
|
|
self.chatTitleView?.pressed = { [weak self] in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
return $0.updatedTitlePanelContext {
|
|
if let index = $0.index(where: {
|
|
switch $0 {
|
|
case .chatInfo:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
var updatedContexts = $0
|
|
updatedContexts.remove(at: index)
|
|
return updatedContexts
|
|
} else {
|
|
var updatedContexts = $0
|
|
updatedContexts.insert(.chatInfo, at: 0)
|
|
return updatedContexts
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
let chatInfoButtonItem = UIBarButtonItem(customDisplayNode: ChatAvatarNavigationNode())!
|
|
chatInfoButtonItem.target = self
|
|
chatInfoButtonItem.action = #selector(self.rightNavigationButtonAction)
|
|
self.chatInfoNavigationButton = ChatNavigationButton(action: .openChatInfo, buttonItem: chatInfoButtonItem)
|
|
|
|
self.updateChatPresentationInterfaceState(animated: false, interactive: false, { return $0 })
|
|
|
|
self.peerView.set(account.viewTracker.peerView(peerId))
|
|
|
|
peerDisposable.set((self.peerView.get()
|
|
|> deliverOnMainQueue).start(next: { [weak self] peerView in
|
|
if let strongSelf = self {
|
|
if let peer = peerView.peers[peerId] {
|
|
strongSelf.chatTitleView?.peerView = peerView
|
|
(strongSelf.chatInfoNavigationButton?.buttonItem.customDisplayNode as? ChatAvatarNavigationNode)?.avatarNode.setPeer(account: strongSelf.account, peer: peer)
|
|
}
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: false, { return $0.updatedPeer { _ in return peerView.peers[peerId] } })
|
|
if !strongSelf.didSetPeerReady {
|
|
strongSelf.didSetPeerReady = true
|
|
strongSelf._peerReady.set(.single(true))
|
|
}
|
|
}
|
|
}))
|
|
|
|
botCallbackAlertMessageDisposable = (self.botCallbackAlertMessage.get()
|
|
|> deliverOnMainQueue).start(next: { [weak self] message in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: false, {
|
|
return $0.updatedTitlePanelContext {
|
|
if let message = message {
|
|
if let index = $0.index(where: {
|
|
switch $0 {
|
|
case .toastAlert:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
if $0[index] != ChatTitlePanelContext.toastAlert(message) {
|
|
var updatedContexts = $0
|
|
updatedContexts[index] = .toastAlert(message)
|
|
return updatedContexts
|
|
} else {
|
|
return $0
|
|
}
|
|
} else {
|
|
var updatedContexts = $0
|
|
updatedContexts.append(.toastAlert(message))
|
|
return updatedContexts
|
|
}
|
|
} else {
|
|
if let index = $0.index(where: {
|
|
switch $0 {
|
|
case .toastAlert:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
var updatedContexts = $0
|
|
updatedContexts.remove(at: index)
|
|
return updatedContexts
|
|
} else {
|
|
return $0
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
self.audioRecorderDisposable = (self.audioRecorder.get() |> deliverOnMainQueue).start(next: { [weak self] audioRecorder in
|
|
if let strongSelf = self {
|
|
if strongSelf.audioRecorderValue !== audioRecorder {
|
|
strongSelf.audioRecorderValue = audioRecorder
|
|
|
|
if let audioRecorder = audioRecorder {
|
|
/*(audioRecorder.recordingState
|
|
|> filter { state in
|
|
switch state {
|
|
case .recording:
|
|
return true
|
|
case .paused:
|
|
return false
|
|
}
|
|
} |> take(1) |> deliverOnMainQueue).start(completed: {
|
|
self?.audioRecorderFeedback?.tap()
|
|
})*/
|
|
} else {
|
|
strongSelf.audioRecorderFeedback = nil
|
|
}
|
|
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, {
|
|
$0.updatedInputTextPanelState { panelState in
|
|
if let audioRecorder = audioRecorder {
|
|
if panelState.audioRecordingState == nil {
|
|
return panelState.withUpdatedAudioRecordingState(ChatTextInputPanelAudioRecordingState(recorder: audioRecorder))
|
|
}
|
|
} else {
|
|
return panelState.withUpdatedAudioRecordingState(nil)
|
|
}
|
|
return panelState
|
|
}
|
|
})
|
|
|
|
if let audioRecorder = audioRecorder {
|
|
audioRecorder.start()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
required public init(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
self.messageIndexDisposable.dispose()
|
|
self.navigationActionDisposable.dispose()
|
|
self.galleryHiddenMesageAndMediaDisposable.dispose()
|
|
self.peerDisposable.dispose()
|
|
self.controllerNavigationDisposable.dispose()
|
|
self.sentMessageEventsDisposable.dispose()
|
|
self.messageActionCallbackDisposable.dispose()
|
|
self.editMessageDisposable.dispose()
|
|
self.enqueueMediaMessageDisposable.dispose()
|
|
self.resolvePeerByNameDisposable?.dispose()
|
|
self.botCallbackAlertMessageDisposable?.dispose()
|
|
self.contextQueryState?.1.dispose()
|
|
self.audioRecorderDisposable?.dispose()
|
|
}
|
|
|
|
var chatDisplayNode: ChatControllerNode {
|
|
get {
|
|
return super.displayNode as! ChatControllerNode
|
|
}
|
|
}
|
|
|
|
override public func loadDisplayNode() {
|
|
self.displayNode = ChatControllerNode(account: self.account, peerId: self.peerId, messageId: self.messageId, controllerInteraction: self.controllerInteraction!)
|
|
|
|
let initialData = self.chatDisplayNode.historyNode.initialData
|
|
|> take(1)
|
|
|> beforeNext { [weak self] initialData in
|
|
if let strongSelf = self, let initialData = initialData {
|
|
if let interfaceState = initialData.chatInterfaceState as? ChatInterfaceState {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: false, { $0.updatedInterfaceState({ _ in return interfaceState }) })
|
|
}
|
|
}
|
|
}
|
|
|
|
self.ready.set(combineLatest(self.chatDisplayNode.historyNode.historyReady.get(), self._peerReady.get(), initialData) |> map { historyReady, peerReady, _ in
|
|
return historyReady && peerReady
|
|
})
|
|
|
|
self.chatDisplayNode.historyNode.visibleContentOffsetChanged = { [weak self] offset in
|
|
if let strongSelf = self {
|
|
let offsetAlpha: CGFloat
|
|
switch offset {
|
|
case let .known(offset):
|
|
if offset < 40.0 {
|
|
offsetAlpha = 0.0
|
|
} else {
|
|
offsetAlpha = 1.0
|
|
}
|
|
case .unknown:
|
|
offsetAlpha = 1.0
|
|
case .none:
|
|
offsetAlpha = 0.0
|
|
}
|
|
|
|
if !strongSelf.chatDisplayNode.navigateToLatestButton.alpha.isEqual(to: offsetAlpha) {
|
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.beginFromCurrentState], animations: {
|
|
strongSelf.chatDisplayNode.navigateToLatestButton.alpha = offsetAlpha
|
|
}, completion: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
self.chatDisplayNode.requestLayout = { [weak self] transition in
|
|
self?.requestLayout(transition: transition)
|
|
}
|
|
|
|
self.chatDisplayNode.setupSendActionOnViewUpdate = { [weak self] f in
|
|
self?.chatDisplayNode.historyNode.layoutActionOnViewTransition = { [weak self] transition in
|
|
f()
|
|
if let strongSelf = self {
|
|
var mappedTransition: (ChatHistoryListViewTransition, ListViewUpdateSizeAndInsets?)?
|
|
|
|
strongSelf.chatDisplayNode.containerLayoutUpdated(strongSelf.containerLayout, navigationBarHeight: strongSelf.navigationBar.frame.maxY, transition: .animated(duration: 0.4, curve: .spring), listViewTransaction: { updateSizeAndInsets in
|
|
var options = transition.options
|
|
let _ = options.insert(.Synchronous)
|
|
let _ = options.insert(.LowLatency)
|
|
options.remove(.AnimateInsertion)
|
|
options.insert(.RequestItemInsertionAnimations)
|
|
|
|
let deleteItems = transition.deleteItems.map({ item in
|
|
return ListViewDeleteItem(index: item.index, directionHint: nil)
|
|
})
|
|
|
|
var maxInsertedItem: Int?
|
|
var insertItems: [ListViewInsertItem] = []
|
|
for i in 0 ..< transition.insertItems.count {
|
|
let item = transition.insertItems[i]
|
|
if item.directionHint == .Down && (maxInsertedItem == nil || maxInsertedItem! < item.index) {
|
|
maxInsertedItem = item.index
|
|
}
|
|
insertItems.append(ListViewInsertItem(index: item.index, previousIndex: item.previousIndex, item: item.item, directionHint: item.directionHint == .Down ? .Up : nil))
|
|
}
|
|
|
|
let scrollToItem = ListViewScrollToItem(index: 0, position: .Top, animated: true, curve: .Spring(duration: 0.4), directionHint: .Up)
|
|
|
|
var stationaryItemRange: (Int, Int)?
|
|
if let maxInsertedItem = maxInsertedItem {
|
|
stationaryItemRange = (maxInsertedItem + 1, Int.max)
|
|
}
|
|
|
|
mappedTransition = (ChatHistoryListViewTransition(historyView: transition.historyView, deleteItems: deleteItems, insertItems: insertItems, updateItems: transition.updateItems, options: options, scrollToItem: scrollToItem, stationaryItemRange: stationaryItemRange, initialData: transition.initialData), updateSizeAndInsets)
|
|
})
|
|
|
|
if let mappedTransition = mappedTransition {
|
|
return mappedTransition
|
|
}
|
|
}
|
|
return (transition, nil)
|
|
}
|
|
}
|
|
|
|
self.chatDisplayNode.requestUpdateChatInterfaceState = { [weak self] animated, f in
|
|
self?.updateChatPresentationInterfaceState(animated: animated, interactive: true, { $0.updatedInterfaceState(f) })
|
|
}
|
|
|
|
self.chatDisplayNode.displayAttachmentMenu = { [weak self] in
|
|
if let strongSelf = self {
|
|
if true {
|
|
strongSelf.chatDisplayNode.dismissInput()
|
|
|
|
let emptyController = LegacyEmptyController()
|
|
let navigationController = makeLegacyNavigationController(rootController: emptyController)
|
|
navigationController.setNavigationBarHidden(true, animated: false)
|
|
|
|
let legacyController = LegacyController(legacyController: navigationController, presentation: .custom)
|
|
|
|
var presentOverlayController: ((UIViewController) -> (() -> Void))?
|
|
let controller = legacyAttachmentMenu(parentController: legacyController, presentOverlayController: { controller in
|
|
if let presentOverlayController = presentOverlayController {
|
|
return presentOverlayController(controller)
|
|
} else {
|
|
return {
|
|
}
|
|
}
|
|
}, openGallery: {
|
|
self?.presentMediaPicker(fileMode: false)
|
|
}, openCamera: { cameraView, menuController in
|
|
if let strongSelf = self {
|
|
presentedLegacyCamera(cameraView: cameraView, menuController: menuController, parentController: strongSelf, sendMessagesWithSignals: { signals in
|
|
self?.enqueueMediaMessages(signals: signals)
|
|
})
|
|
}
|
|
}, openFileGallery: {
|
|
self?.presentMediaPicker(fileMode: true)
|
|
}, sendMessagesWithSignals: { [weak self] signals in
|
|
self?.enqueueMediaMessages(signals: signals)
|
|
})
|
|
controller.applicationInterface = legacyController.applicationInterface
|
|
controller.didDismiss = { [weak legacyController] _ in
|
|
legacyController?.dismiss()
|
|
}
|
|
|
|
strongSelf.present(legacyController, in: .window)
|
|
controller.present(in: emptyController, sourceView: nil, animated: true)
|
|
|
|
presentOverlayController = { [weak legacyController] controller in
|
|
if let strongSelf = self, let legacyController = legacyController {
|
|
let childController = LegacyController(legacyController: controller, presentation: .custom)
|
|
legacyController.present(childController, in: .window)
|
|
return { [weak childController] in
|
|
childController?.dismiss()
|
|
}
|
|
} else {
|
|
return {
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
let controller = ChatMediaActionSheetController()
|
|
controller.photo = { [weak strongSelf] asset in
|
|
if let strongSelf = strongSelf {
|
|
var randomId: Int64 = 0
|
|
arc4random_buf(&randomId, 8)
|
|
let size = CGSize(width: CGFloat(asset.pixelWidth), height: CGFloat(asset.pixelHeight))
|
|
let scaledSize = size.aspectFitted(CGSize(width: 1280.0, height: 1280.0))
|
|
let resource = PhotoLibraryMediaResource(localIdentifier: asset.localIdentifier)
|
|
|
|
if false {
|
|
let media = TelegramMediaImage(imageId: MediaId(namespace: Namespaces.Media.LocalImage, id: randomId), representations: [TelegramMediaImageRepresentation(dimensions: scaledSize, resource: resource)])
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({})
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: "", attributes: [], media: media, replyToMessageId: nil)]).start()
|
|
} else {
|
|
let media = TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.LocalFile, id: randomId), resource: resource, previewRepresentations: [], mimeType: "image/jpeg", size: 0, attributes: [.FileName(fileName: "image.jpeg"), .ImageSize(size: scaledSize)])
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: "", attributes: [], media: media, replyToMessageId: nil)]).start()
|
|
}
|
|
}
|
|
}
|
|
controller.files = { [weak strongSelf] in
|
|
if let strongSelf = strongSelf {
|
|
|
|
}
|
|
}
|
|
controller.location = { [weak strongSelf] in
|
|
if let strongSelf = strongSelf {
|
|
let mapInputController = MapInputController()
|
|
strongSelf.present(mapInputController, in: .window)
|
|
}
|
|
}
|
|
controller.contacts = { [weak strongSelf] in
|
|
if let strongSelf = strongSelf {
|
|
}
|
|
}
|
|
strongSelf.present(controller, in: .window)
|
|
}
|
|
}
|
|
|
|
self.chatDisplayNode.navigateToLatestButton.tapped = { [weak self] in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
strongSelf.chatDisplayNode.historyNode.scrollToEndOfHistory()
|
|
}
|
|
}
|
|
|
|
let interfaceInteraction = ChatPanelInterfaceInteraction(setupReplyMessage: { [weak self] messageId in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(messageId) {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState { $0.withUpdatedReplyMessageId(message.id) } })
|
|
strongSelf.chatDisplayNode.ensureInputViewFocused()
|
|
}
|
|
}
|
|
}, setupEditMessage: { [weak self] messageId in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(messageId) {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState { $0.withUpdatedEditMessage(ChatEditMessageState(messageId: messageId, inputState: ChatTextInputState(inputText: message.text))) } })
|
|
strongSelf.chatDisplayNode.ensureInputViewFocused()
|
|
}
|
|
}
|
|
}, beginMessageSelection: { [weak self] messageId in
|
|
if let strongSelf = self, strongSelf.isNodeLoaded {
|
|
if let message = strongSelf.chatDisplayNode.historyNode.messageInCurrentHistoryView(messageId) {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true,{ $0.updatedInterfaceState { $0.withUpdatedSelectedMessage(message.id) } })
|
|
}
|
|
}
|
|
}, deleteSelectedMessages: { [weak self] in
|
|
if let strongSelf = self {
|
|
if let messageIds = strongSelf.presentationInterfaceState.interfaceState.selectionState?.selectedIds, !messageIds.isEmpty {
|
|
strongSelf.account.postbox.modify({ modifier in
|
|
modifier.deleteMessages(Array(messageIds))
|
|
}).start()
|
|
}
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState { $0.withoutSelectionState() } })
|
|
}
|
|
}, forwardSelectedMessages: { [weak self] in
|
|
if let strongSelf = self {
|
|
//let controller = ShareRecipientsActionSheetController()
|
|
//strongSelf.present(controller, in: .window)
|
|
|
|
if let forwardMessageIdsSet = strongSelf.presentationInterfaceState.interfaceState.selectionState?.selectedIds {
|
|
let forwardMessageIds = Array(forwardMessageIdsSet).sorted()
|
|
|
|
let controller = PeerSelectionController(account: strongSelf.account)
|
|
controller.peerSelected = { [weak controller] peerId in
|
|
if let strongSelf = self, let strongController = controller {
|
|
if peerId == strongSelf.peerId {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: true, { $0.updatedInterfaceState({ $0.withUpdatedForwardMessageIds(forwardMessageIds).withoutSelectionState() }) })
|
|
strongController.dismiss()
|
|
} else {
|
|
(strongSelf.account.postbox.modify({ modifier -> Void in
|
|
modifier.updatePeerChatInterfaceState(peerId, update: { currentState in
|
|
if let currentState = currentState as? ChatInterfaceState {
|
|
return currentState.withUpdatedForwardMessageIds(forwardMessageIds)
|
|
} else {
|
|
return ChatInterfaceState().withUpdatedForwardMessageIds(forwardMessageIds)
|
|
}
|
|
return currentState
|
|
})
|
|
}) |> deliverOnMainQueue).start(completed: {
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: false, interactive: true, { $0.updatedInterfaceState({ $0.withoutSelectionState() }) })
|
|
|
|
let ready = ValuePromise<Bool>()
|
|
|
|
strongSelf.controllerNavigationDisposable.set((ready.get() |> take(1) |> deliverOnMainQueue).start(next: { _ in
|
|
if let strongController = controller {
|
|
strongController.dismiss()
|
|
}
|
|
}))
|
|
|
|
(strongSelf.navigationController as? NavigationController)?.replaceTopController(ChatController(account: strongSelf.account, peerId: peerId), animated: false, ready: ready)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
strongSelf.present(controller, in: .window)
|
|
}
|
|
}
|
|
}, updateTextInputState: { [weak self] f in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState { $0.withUpdatedEffectiveInputState(f($0.effectiveInputState)) } })
|
|
}
|
|
}, updateInputMode: { [weak self] f in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInputMode(f) })
|
|
}
|
|
}, editMessage: { [weak self] messageId, text in
|
|
if let strongSelf = self {
|
|
let editingMessage = strongSelf.editingMessage
|
|
editingMessage.set(true)
|
|
strongSelf.editMessageDisposable.set((requestEditMessage(account: strongSelf.account, messageId: messageId, text: text) |> deliverOnMainQueue |> afterDisposed({
|
|
editingMessage.set(false)
|
|
})).start(completed: {
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState({ $0.withUpdatedEditMessage(nil) }) })
|
|
}
|
|
}))
|
|
}
|
|
}, beginMessageSearch: { [weak self] in
|
|
if let strongSelf = self {
|
|
|
|
}
|
|
}, openPeerInfo: { [weak self] in
|
|
self?.navigationButtonAction(.openChatInfo)
|
|
}, togglePeerNotifications: {
|
|
|
|
}, sendContextResult: { [weak self] results, result in
|
|
self?.enqueueChatContextResult(results, result)
|
|
}, sendBotCommand: { [weak self] botPeer, command in
|
|
if let strongSelf = self {
|
|
if let peer = strongSelf.presentationInterfaceState.peer, let addressName = botPeer.addressName {
|
|
let messageText: String
|
|
if peer is TelegramUser {
|
|
messageText = command
|
|
} else {
|
|
messageText = command + "@" + addressName
|
|
}
|
|
let replyMessageId = strongSelf.presentationInterfaceState.interfaceState.replyMessageId
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: false, {
|
|
$0.updatedInterfaceState { $0.withUpdatedReplyMessageId(nil).withUpdatedComposeInputState(ChatTextInputState(inputText: "")) }
|
|
})
|
|
}
|
|
})
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: messageText, attributes: [], media: nil, replyToMessageId: replyMessageId)]).start()
|
|
}
|
|
}
|
|
}, beginAudioRecording: { [weak self] in
|
|
self?.requestAudioRecorder()
|
|
}, finishAudioRecording: { [weak self] sendAudio in
|
|
self?.dismissAudioRecorder(sendAudio: sendAudio)
|
|
}, statuses: ChatPanelInterfaceInteractionStatuses(editingMessage: self.editingMessage.get()))
|
|
|
|
self.interfaceInteraction = interfaceInteraction
|
|
self.chatDisplayNode.interfaceInteraction = interfaceInteraction
|
|
|
|
self.displayNodeDidLoad()
|
|
|
|
self.sentMessageEventsDisposable.set(self.account.pendingMessageManager.deliveredMessageEvents(peerId: self.peerId).start(next: { _ in
|
|
serviceSoundManager.playMessageDeliveredSound()
|
|
}))
|
|
}
|
|
|
|
override public func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
}
|
|
|
|
override public func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
|
|
self.chatDisplayNode.historyNode.preloadPages = true
|
|
self.chatDisplayNode.historyNode.canReadHistory.set(true)
|
|
|
|
self.chatDisplayNode.loadInputPanels()
|
|
}
|
|
|
|
override public func viewWillDisappear(_ animated: Bool) {
|
|
super.viewWillDisappear(animated)
|
|
|
|
self.chatDisplayNode.historyNode.canReadHistory.set(false)
|
|
let peerId = self.peerId
|
|
let timestamp = Int32(Date().timeIntervalSince1970)
|
|
let interfaceState = self.presentationInterfaceState.interfaceState.withUpdatedTimestamp(timestamp)
|
|
self.account.postbox.modify({ modifier -> Void in
|
|
modifier.updatePeerChatInterfaceState(peerId, update: { _ in
|
|
return interfaceState
|
|
})
|
|
}).start()
|
|
}
|
|
|
|
override public func viewDidDisappear(_ animated: Bool) {
|
|
super.viewDidDisappear(animated)
|
|
|
|
self.updateChatPresentationInterfaceState(animated: false, interactive: false, {
|
|
$0.updatedTitlePanelContext {
|
|
if let index = $0.index(where: {
|
|
switch $0 {
|
|
case .chatInfo:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}) {
|
|
var updatedContexts = $0
|
|
updatedContexts.remove(at: index)
|
|
return updatedContexts
|
|
} else {
|
|
return $0
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
|
|
super.containerLayoutUpdated(layout, transition: transition)
|
|
|
|
self.containerLayout = layout
|
|
|
|
self.chatDisplayNode.containerLayoutUpdated(layout, navigationBarHeight: self.navigationHeight, transition: transition, listViewTransaction: { updateSizeAndInsets in
|
|
self.chatDisplayNode.historyNode.updateLayout(transition: transition, updateSizeAndInsets: updateSizeAndInsets)
|
|
})
|
|
}
|
|
|
|
func updateChatPresentationInterfaceState(animated: Bool = true, interactive: Bool, _ f: (ChatPresentationInterfaceState) -> ChatPresentationInterfaceState) {
|
|
let temporaryChatPresentationInterfaceState = f(self.presentationInterfaceState)
|
|
let inputTextPanelState = inputTextPanelStateForChatPresentationInterfaceState(temporaryChatPresentationInterfaceState, account: self.account)
|
|
var updatedChatPresentationInterfaceState = temporaryChatPresentationInterfaceState.updatedInputTextPanelState({ _ in return inputTextPanelState })
|
|
|
|
if let (updatedContextQueryState, updatedContextQuerySignal) = contextQueryResultStateForChatInterfacePresentationState(updatedChatPresentationInterfaceState, account: self.account, currentQuery: self.contextQueryState?.0) {
|
|
self.contextQueryState?.1.dispose()
|
|
var inScope = true
|
|
var inScopeResult: ((ChatPresentationInputQueryResult?) -> ChatPresentationInputQueryResult?)?
|
|
self.contextQueryState = (updatedContextQueryState, (updatedContextQuerySignal |> deliverOnMainQueue).start(next: { [weak self] result in
|
|
if let strongSelf = self {
|
|
if Thread.isMainThread && inScope {
|
|
inScope = false
|
|
inScopeResult = result
|
|
} else {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: false, {
|
|
$0.updatedInputQueryResult { previousResult in
|
|
return result(previousResult)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}))
|
|
inScope = false
|
|
if let inScopeResult = inScopeResult {
|
|
updatedChatPresentationInterfaceState = updatedChatPresentationInterfaceState.updatedInputQueryResult { previousResult in
|
|
return inScopeResult(previousResult)
|
|
}
|
|
}
|
|
}
|
|
|
|
self.presentationInterfaceState = updatedChatPresentationInterfaceState
|
|
if self.isNodeLoaded {
|
|
self.chatDisplayNode.updateChatPresentationInterfaceState(updatedChatPresentationInterfaceState, animated: animated, interactive: interactive)
|
|
}
|
|
|
|
if let button = leftNavigationButtonForChatInterfaceState(updatedChatPresentationInterfaceState.interfaceState, currentButton: self.leftNavigationButton, target: self, selector: #selector(self.leftNavigationButtonAction)) {
|
|
self.navigationItem.setLeftBarButton(button.buttonItem, animated: true)
|
|
self.leftNavigationButton = button
|
|
} else if let _ = self.leftNavigationButton {
|
|
self.navigationItem.setLeftBarButton(nil, animated: true)
|
|
self.leftNavigationButton = nil
|
|
}
|
|
|
|
if let button = rightNavigationButtonForChatInterfaceState(updatedChatPresentationInterfaceState.interfaceState, currentButton: self.rightNavigationButton, target: self, selector: #selector(self.rightNavigationButtonAction), chatInfoNavigationButton: self.chatInfoNavigationButton) {
|
|
self.navigationItem.setRightBarButton(button.buttonItem, animated: true)
|
|
self.rightNavigationButton = button
|
|
} else if let _ = self.rightNavigationButton {
|
|
self.navigationItem.setRightBarButton(nil, animated: true)
|
|
self.rightNavigationButton = nil
|
|
}
|
|
|
|
if let controllerInteraction = self.controllerInteraction {
|
|
if updatedChatPresentationInterfaceState.interfaceState.selectionState != controllerInteraction.selectionState {
|
|
let animated = controllerInteraction.selectionState == nil || updatedChatPresentationInterfaceState.interfaceState.selectionState == nil
|
|
controllerInteraction.selectionState = updatedChatPresentationInterfaceState.interfaceState.selectionState
|
|
self.chatDisplayNode.historyNode.forEachItemNode { itemNode in
|
|
if let itemNode = itemNode as? ChatMessageItemView {
|
|
itemNode.updateSelectionState(animated: animated)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func leftNavigationButtonAction() {
|
|
if let button = self.leftNavigationButton {
|
|
self.navigationButtonAction(button.action)
|
|
}
|
|
}
|
|
|
|
@objc func rightNavigationButtonAction() {
|
|
if let button = self.rightNavigationButton {
|
|
self.navigationButtonAction(button.action)
|
|
}
|
|
}
|
|
|
|
private func navigationButtonAction(_ action: ChatNavigationButtonAction) {
|
|
switch action {
|
|
case .cancelMessageSelection:
|
|
self.updateChatPresentationInterfaceState(animated: true, interactive: true, { $0.updatedInterfaceState { $0.withoutSelectionState() } })
|
|
case .clearHistory:
|
|
let actionSheet = ActionSheetController()
|
|
actionSheet.setItemGroups([ActionSheetItemGroup(items: [
|
|
ActionSheetButtonItem(title: "Delete All Messages", color: .destructive, action: { [weak actionSheet] in
|
|
actionSheet?.dismissAnimated()
|
|
})
|
|
]), ActionSheetItemGroup(items: [
|
|
ActionSheetButtonItem(title: "Cancel", color: .accent, action: { [weak actionSheet] in
|
|
actionSheet?.dismissAnimated()
|
|
})
|
|
])])
|
|
self.present(actionSheet, in: .window)
|
|
case .openChatInfo:
|
|
self.navigationActionDisposable.set((self.peerView.get()
|
|
|> take(1)
|
|
|> deliverOnMainQueue).start(next: { [weak self] peerView in
|
|
if let strongSelf = self, let _ = peerView.peers[peerView.peerId] {
|
|
let chatInfoController = PeerInfoController(account: strongSelf.account, peerId: peerView.peerId)
|
|
(strongSelf.navigationController as? NavigationController)?.pushViewController(chatInfoController)
|
|
}
|
|
}))
|
|
break
|
|
}
|
|
}
|
|
|
|
private func presentMediaPicker(fileMode: Bool) {
|
|
legacyAssetPicker(fileMode: fileMode).start(next: { [weak self] generator in
|
|
if let strongSelf = self {
|
|
var presentOverlayController: ((UIViewController) -> (() -> Void))?
|
|
let controller = generator({ controller in
|
|
return presentOverlayController!(controller)
|
|
})
|
|
let legacyController = LegacyController(legacyController: controller, presentation: .modal)
|
|
|
|
presentOverlayController = { [weak legacyController] controller in
|
|
if let strongSelf = self, let legacyController = legacyController {
|
|
let childController = LegacyController(legacyController: controller, presentation: .custom)
|
|
legacyController.present(childController, in: .window)
|
|
return { [weak childController] in
|
|
childController?.dismiss()
|
|
}
|
|
} else {
|
|
return {
|
|
}
|
|
}
|
|
}
|
|
|
|
configureLegacyAssetPicker(controller)
|
|
controller.descriptionGenerator = legacyAssetPickerItemGenerator()
|
|
controller.completionBlock = { [weak self, weak legacyController] signals in
|
|
if let strongSelf = self, let legacyController = legacyController {
|
|
legacyController.dismiss()
|
|
strongSelf.enqueueMediaMessages(signals: signals)
|
|
}
|
|
}
|
|
controller.dismissalBlock = { [weak legacyController] in
|
|
if let legacyController = legacyController {
|
|
legacyController.dismiss()
|
|
}
|
|
}
|
|
strongSelf.present(legacyController, in: .window)
|
|
}
|
|
})
|
|
}
|
|
|
|
private func enqueueMediaMessages(signals: [Any]?) {
|
|
self.enqueueMediaMessageDisposable.set((legacyAssetPickerEnqueueMessages(account: self.account, peerId: self.peerId, signals: signals!) |> deliverOnMainQueue).start(next: { [weak self] messages in
|
|
if let strongSelf = self {
|
|
let replyMessageId = strongSelf.presentationInterfaceState.interfaceState.replyMessageId
|
|
strongSelf.chatDisplayNode.setupSendActionOnViewUpdate({
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: false, {
|
|
$0.updatedInterfaceState { $0.withUpdatedReplyMessageId(nil) }
|
|
})
|
|
}
|
|
})
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: messages.map { $0.withUpdatedReplyToMessageId(replyMessageId) }).start()
|
|
}
|
|
}))
|
|
}
|
|
|
|
private func enqueueChatContextResult(_ results: ChatContextResultCollection, _ result: ChatContextResult) {
|
|
if let message = outgoingMessageWithChatContextResult(results, result) {
|
|
let replyMessageId = self.presentationInterfaceState.interfaceState.replyMessageId
|
|
self.chatDisplayNode.setupSendActionOnViewUpdate({ [weak self] in
|
|
if let strongSelf = self {
|
|
strongSelf.updateChatPresentationInterfaceState(animated: true, interactive: false, {
|
|
$0.updatedInterfaceState { $0.withUpdatedReplyMessageId(nil).withUpdatedComposeInputState(ChatTextInputState(inputText: "")) }
|
|
})
|
|
}
|
|
})
|
|
enqueueMessages(account: self.account, peerId: self.peerId, messages: [message.withUpdatedReplyToMessageId(replyMessageId)]).start()
|
|
}
|
|
}
|
|
|
|
private func requestAudioRecorder() {
|
|
if self.audioRecorderValue == nil {
|
|
if let applicationContext = self.account.applicationContext as? TelegramApplicationContext {
|
|
if self.audioRecorderFeedback == nil {
|
|
//self.audioRecorderFeedback = HapticFeedback()
|
|
self.audioRecorderFeedback?.prepareTap()
|
|
}
|
|
self.audioRecorder.set(applicationContext.mediaManager.audioRecorder())
|
|
}
|
|
}
|
|
}
|
|
|
|
private func dismissAudioRecorder(sendAudio: Bool) {
|
|
if let audioRecorderValue = self.audioRecorderValue {
|
|
audioRecorderValue.stop()
|
|
if sendAudio {
|
|
(audioRecorderValue.takenRecordedData() |> deliverOnMainQueue).start(next: { [weak self] data in
|
|
if let strongSelf = self, let data = data {
|
|
if data.duration < 0.5 {
|
|
strongSelf.audioRecorderFeedback?.error()
|
|
strongSelf.audioRecorderFeedback = nil
|
|
} else {
|
|
var randomId: Int64 = 0
|
|
arc4random_buf(&randomId, 8)
|
|
|
|
let resource = LocalFileMediaResource(fileId: randomId)
|
|
|
|
strongSelf.account.postbox.mediaBox.storeResourceData(resource.id, data: data.compressedData)
|
|
|
|
var waveformBuffer: MemoryBuffer?
|
|
if let waveform = data.waveform {
|
|
waveformBuffer = MemoryBuffer(data: waveform)
|
|
}
|
|
|
|
enqueueMessages(account: strongSelf.account, peerId: strongSelf.peerId, messages: [.message(text: "", attributes: [], media: TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.LocalFile, id: randomId), resource: resource, previewRepresentations: [], mimeType: "audio/ogg", size: data.compressedData.count, attributes: [.Audio(isVoice: true, duration: Int(data.duration), title: nil, performer: nil, waveform: waveformBuffer)]), replyToMessageId: nil)]).start()
|
|
|
|
strongSelf.audioRecorderFeedback?.success()
|
|
strongSelf.audioRecorderFeedback = nil
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
self.audioRecorder.set(.single(nil))
|
|
}
|
|
}
|