Fix animated emoji haptics

This commit is contained in:
Ilya Laktyushin
2020-11-17 13:55:34 +04:00
parent 5d2724a09e
commit 3b7ee797b3

View File

@@ -1303,6 +1303,7 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView {
} }
let beatingHearts: [UInt32] = [0x2764, 0x1F90E, 0x1F9E1, 0x1F499, 0x1F49A, 0x1F49C, 0x1F49B, 0x1F5A4, 0x1F90D] let beatingHearts: [UInt32] = [0x2764, 0x1F90E, 0x1F9E1, 0x1F499, 0x1F49A, 0x1F49C, 0x1F49B, 0x1F5A4, 0x1F90D]
let heart = 0x2764
let peach = 0x1F351 let peach = 0x1F351
let coffin = 0x26B0 let coffin = 0x26B0
@@ -1312,7 +1313,12 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView {
return view.values[PreferencesKeys.appConfiguration] as? AppConfiguration ?? .defaultValue return view.values[PreferencesKeys.appConfiguration] as? AppConfiguration ?? .defaultValue
} }
if let text = self.item?.message.text, let firstScalar = text.unicodeScalars.first { if let text = self.item?.message.text, var firstScalar = text.unicodeScalars.first {
var textEmoji = text.strippedEmoji
if beatingHearts.contains(firstScalar.value) {
textEmoji = "❤️"
firstScalar = UnicodeScalar(heart)!
}
return .optionalAction({ return .optionalAction({
if shouldPlay { if shouldPlay {
let _ = (appConfiguration let _ = (appConfiguration
@@ -1322,7 +1328,7 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView {
} }
let emojiSounds = AnimatedEmojiSoundsConfiguration.with(appConfiguration: appConfiguration, account: item.context.account) let emojiSounds = AnimatedEmojiSoundsConfiguration.with(appConfiguration: appConfiguration, account: item.context.account)
for (emoji, file) in emojiSounds.sounds { for (emoji, file) in emojiSounds.sounds {
if emoji.strippedEmoji == text.strippedEmoji { if emoji.strippedEmoji == textEmoji.strippedEmoji {
let mediaManager = item.context.sharedContext.mediaManager let mediaManager = item.context.sharedContext.mediaManager
let mediaPlayer = MediaPlayer(audioSessionManager: mediaManager.audioSession, postbox: item.context.account.postbox, resourceReference: .standalone(resource: file.resource), streamable: .none, video: false, preferSoftwareDecoding: false, enableSound: true, fetchAutomatically: true, ambient: true) let mediaPlayer = MediaPlayer(audioSessionManager: mediaManager.audioSession, postbox: item.context.account.postbox, resourceReference: .standalone(resource: file.resource), streamable: .none, video: false, preferSoftwareDecoding: false, enableSound: true, fetchAutomatically: true, ambient: true)
mediaPlayer.togglePlayPause() mediaPlayer.togglePlayPause()
@@ -1334,25 +1340,25 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView {
strongSelf.mediaStatusDisposable.set((mediaPlayer.status strongSelf.mediaStatusDisposable.set((mediaPlayer.status
|> deliverOnMainQueue).start(next: { [weak self, weak animationNode] status in |> deliverOnMainQueue).start(next: { [weak self, weak animationNode] status in
if let strongSelf = self { if let strongSelf = self {
if firstScalar.value == coffin || firstScalar.value == peach {
var haptic: EmojiHaptic var haptic: EmojiHaptic?
if let current = strongSelf.haptic { if let current = strongSelf.haptic {
haptic = current haptic = current
} else { } else {
if beatingHearts.contains(firstScalar.value) { if firstScalar.value == heart {
haptic = HeartbeatHaptic() haptic = HeartbeatHaptic()
} else if firstScalar.value == coffin { } else if firstScalar.value == coffin {
haptic = CoffinHaptic() haptic = CoffinHaptic()
} else { } else if firstScalar.value == peach {
haptic = PeachHaptic() haptic = PeachHaptic()
} }
haptic.enabled = true haptic?.enabled = true
strongSelf.haptic = haptic strongSelf.haptic = haptic
} }
if !haptic.active {
if let haptic = haptic, !haptic.active {
haptic.start(time: 0.0) haptic.start(time: 0.0)
} }
}
switch status.status { switch status.status {
case .playing: case .playing: