make the reply swipe reliable and add a telegram-style plate
iOS CI / Build and Test SwiftUI App (push) Canceled after 0s
Build Unsigned iOS and macOS Apps / Build Unsigned iOS IPA (push) Canceled after 0s
Build Unsigned iOS and macOS Apps / Build macOS ZIP (push) Canceled after 0s

- Lock the gesture into a reply swipe once it starts clearly
  horizontal (1.6 dominance, 12pt) and relax the ratio to 1.05
  afterwards, so a slightly diagonal finish no longer cancels the
  swipe while vertical scrolls stay untouched.
- Restyle the composer reply banner as a rounded telegram-like card
  with the reply header, close button and two-line quote preview.
- Cover the lock/follow behaviour in MessageReplySwipeTests.
This commit is contained in:
wt committed 2026-08-29 22:33:12 +07:00
1 parent 0ef77f7f6a
commit 2130a76735
4 files changed
+437 -144

No files matched your search

@@ -2,16 +2,30 @@ import CoreGraphics
struct MessageReplySwipePolicy { struct MessageReplySwipePolicy {
static let triggerDistance: CGFloat = 64 static let triggerDistance: CGFloat = 64
/// Horizontal travel required before the reply indicator appears. Keeps /// Horizontal travel required before the indicator starts following the
/// the per-cell state untouched during ordinary vertical scrolling. /// finger; keeps per-cell state untouched during ordinary scrolling.
static let activationDistance: CGFloat = 24 static let activationDistance: CGFloat = 24
static let maximumOffset: CGFloat = 76 static let maximumOffset: CGFloat = 76
/// The drag must be clearly horizontal: a mostly vertical timeline scroll /// Ratio required to lock the gesture into a reply swipe. Mostly vertical
/// (even a diagonal one) must never arm the reply swipe or move bubbles. /// timeline scrolls (even diagonal ones) never reach it.
static let horizontalDominance: CGFloat = 2.0 static let lockDominance: CGFloat = 1.6
/// Relaxed ratio once locked, so the finger can drift while finishing
/// the swipe without it being cancelled by the scroll view.
static let followDominance: CGFloat = 1.05
static func offset(for translation: CGSize) -> CGFloat { /// Whether this translation can lock the gesture as a horizontal swipe.
guard isHorizontal(translation) else { return 0 } static func canLock(_ translation: CGSize) -> Bool {
isHorizontal(translation, dominance: lockDominance)
&& abs(translation.width) >= activationDistance * 0.5
}
static func isHorizontal(_ translation: CGSize) -> Bool {
isHorizontal(translation, dominance: lockDominance)
}
static func offset(locked: Bool, translation: CGSize) -> CGFloat {
let dominance = locked ? followDominance : lockDominance
guard isHorizontal(translation, dominance: dominance) else { return 0 }
let width = abs(translation.width) let width = abs(translation.width)
guard width >= activationDistance else { return 0 } guard width >= activationDistance else { return 0 }
let distance = min((width - activationDistance) * 0.72 + 12, maximumOffset) let distance = min((width - activationDistance) * 0.72 + 12, maximumOffset)
@@ -19,20 +33,24 @@ struct MessageReplySwipePolicy {
} }
static func shouldReply( static func shouldReply(
locked: Bool,
translation: CGSize, translation: CGSize,
predictedEndTranslation: CGSize predictedEndTranslation: CGSize
) -> Bool { ) -> Bool {
let candidate = abs(predictedEndTranslation.width) > abs(translation.width) let candidate = abs(predictedEndTranslation.width) > abs(translation.width)
? predictedEndTranslation ? predictedEndTranslation
: translation : translation
return isHorizontal(candidate) && abs(candidate.width) >= triggerDistance let dominance = locked ? followDominance : lockDominance
return isHorizontal(candidate, dominance: dominance)
&& abs(candidate.width) >= triggerDistance
} }
static func progress(for offset: CGFloat) -> CGFloat { static func progress(for offset: CGFloat) -> CGFloat {
min(1, abs(offset) / triggerDistance) min(1, abs(offset) / triggerDistance)
} }
private static func isHorizontal(_ translation: CGSize) -> Bool { private static func isHorizontal(_ translation: CGSize, dominance: CGFloat) -> Bool {
abs(translation.width) > abs(translation.height) * horizontalDominance abs(translation.width) > abs(translation.height) * dominance
} }
} }
+349 -116
View File
@@ -1,11 +1,11 @@
import CoreTransferable import CoreTransferable
import Foundation import Foundation
import ImageIO import ImageIO
import os
import PhotosUI import PhotosUI
import SwiftData import SwiftData
import SwiftUI import SwiftUI
import UniformTypeIdentifiers import UniformTypeIdentifiers
import os
#if os(iOS) #if os(iOS)
import UIKit import UIKit
@@ -104,17 +104,23 @@ struct ChatView: View {
_showingGroupInfo = State(initialValue: false) _showingGroupInfo = State(initialValue: false)
_showingAttachmentPreview = State(initialValue: false) _showingAttachmentPreview = State(initialValue: false)
_attachmentPreviewPresentationPending = State(initialValue: false) _attachmentPreviewPresentationPending = State(initialValue: false)
_attachmentPreviewPresentationTask = State<Task<Void, Never>?>(initialValue: nil) _attachmentPreviewPresentationTask = State<Task<Void, Never>?>(
initialValue: nil
)
_pickedMediaItems = State<[PhotosPickerItem]>(initialValue: []) _pickedMediaItems = State<[PhotosPickerItem]>(initialValue: [])
_attachmentDrafts = State<[AttachmentDraft]>(initialValue: []) _attachmentDrafts = State<[AttachmentDraft]>(initialValue: [])
_isPreparingAttachments = State(initialValue: false) _isPreparingAttachments = State(initialValue: false)
_editingMessageID = State<String?>(initialValue: nil) _editingMessageID = State<String?>(initialValue: nil)
_replyingToMessageID = State<String?>(initialValue: nil) _replyingToMessageID = State<String?>(initialValue: nil)
_forwardingSelection = State<MessageForwardSelection?>(initialValue: nil) _forwardingSelection = State<MessageForwardSelection?>(
initialValue: nil
)
_selectedMessageIDs = State<Set<String>>(initialValue: []) _selectedMessageIDs = State<Set<String>>(initialValue: [])
_destructiveAction = State<MessageDestructiveAction?>(initialValue: nil) _destructiveAction = State<MessageDestructiveAction?>(initialValue: nil)
_replyThreadSelection = State<ReplyThreadSelection?>(initialValue: nil) _replyThreadSelection = State<ReplyThreadSelection?>(initialValue: nil)
_emojiPickerPresentation = State<EmojiPickerPresentation?>(initialValue: nil) _emojiPickerPresentation = State<EmojiPickerPresentation?>(
initialValue: nil
)
_hasCompletedInitialScroll = State(initialValue: false) _hasCompletedInitialScroll = State(initialValue: false)
_isNearTimelineBottom = State(initialValue: true) _isNearTimelineBottom = State(initialValue: true)
_historyLoadAnchorID = State<String?>(initialValue: nil) _historyLoadAnchorID = State<String?>(initialValue: nil)
@@ -175,7 +181,9 @@ struct ChatView: View {
Button { Button {
Task { Task {
await model.startCall( await model.startCall(
to: conversation.jid, withVideo: false) to: conversation.jid,
withVideo: false
)
} }
} label: { } label: {
Label("Аудиозвонок", systemImage: "phone") Label("Аудиозвонок", systemImage: "phone")
@@ -185,7 +193,10 @@ struct ChatView: View {
Button { Button {
Task { Task {
await model.startCall(to: conversation.jid, withVideo: true) await model.startCall(
to: conversation.jid,
withVideo: true
)
} }
} label: { } label: {
Label("Видеозвонок", systemImage: "video") Label("Видеозвонок", systemImage: "video")
@@ -211,7 +222,10 @@ struct ChatView: View {
} }
} }
#if os(iOS) #if os(iOS)
.toolbar(replyThreadSelection == nil ? .visible : .hidden, for: .navigationBar) .toolbar(
replyThreadSelection == nil ? .visible : .hidden,
for: .navigationBar
)
#endif #endif
.fileImporter( .fileImporter(
isPresented: $showingFileImporter, isPresented: $showingFileImporter,
@@ -220,7 +234,9 @@ struct ChatView: View {
) { result in ) { result in
guard case .success(let urls) = result else { return } guard case .success(let urls) = result else { return }
let preferredKind = fileImportMode.preferredKind let preferredKind = fileImportMode.preferredKind
Task { await stageImportedFiles(urls, preferredKind: preferredKind) } Task {
await stageImportedFiles(urls, preferredKind: preferredKind)
}
} }
.photosPicker( .photosPicker(
isPresented: $showingMediaPicker, isPresented: $showingMediaPicker,
@@ -378,7 +394,10 @@ struct ChatView: View {
@ViewBuilder @ViewBuilder
private var replyThreadOverlay: some View { private var replyThreadOverlay: some View {
if let selection = replyThreadSelection, if let selection = replyThreadSelection,
let root = model.message(withID: selection.rootID, in: conversation.jid) let root = model.message(
withID: selection.rootID,
in: conversation.jid
)
{ {
ReplyThreadOverlay( ReplyThreadOverlay(
model: model, model: model,
@@ -396,12 +415,21 @@ struct ChatView: View {
private var encryptionMenu: some View { private var encryptionMenu: some View {
Menu { Menu {
Section(conversation.isGroup ? "Шифрование этой группы" : "Шифрование этого чата") { Section(
ForEach(EncryptionPreference.allCases, id: \.rawValue) { preference in conversation.isGroup
? "Шифрование этой группы" : "Шифрование этого чата"
) {
ForEach(EncryptionPreference.allCases, id: \.rawValue) {
preference in
Button { Button {
model.setEncryptionPreference(preference, for: conversation.jid) model.setEncryptionPreference(
preference,
for: conversation.jid
)
} label: { } label: {
if model.encryptionPreference(for: conversation.jid) == preference { if model.encryptionPreference(for: conversation.jid)
== preference
{
Label(preference.title, systemImage: "checkmark") Label(preference.title, systemImage: "checkmark")
} else { } else {
Text(preference.title) Text(preference.title)
@@ -414,13 +442,19 @@ struct ChatView: View {
Button { Button {
showingEncryption = true showingEncryption = true
} label: { } label: {
Label("Устройства и отпечатки", systemImage: "checkmark.shield") Label(
"Устройства и отпечатки",
systemImage: "checkmark.shield"
)
} }
} }
} label: { } label: {
Label("Шифрование", systemImage: encryptionIcon) Label("Шифрование", systemImage: encryptionIcon)
} }
.help(encryptionEnabled ? "OMEMO включено" : "Сообщения отправляются без OMEMO") .help(
encryptionEnabled
? "OMEMO включено" : "Сообщения отправляются без OMEMO"
)
} }
private var canStartCall: Bool { private var canStartCall: Bool {
@@ -428,10 +462,13 @@ struct ChatView: View {
} }
private var selectedTimelineMessages: [ChatMessage] { private var selectedTimelineMessages: [ChatMessage] {
model.selectedMessages.filter { selectedMessageIDs.contains($0.clientID) } model.selectedMessages.filter {
selectedMessageIDs.contains($0.clientID)
}
} }
private func rebuildTimelineEntries(from newMessages: [ChatMessage]? = nil) { private func rebuildTimelineEntries(from newMessages: [ChatMessage]? = nil)
{
timelineEntries = ChatTimelineEntry.make(from: newMessages ?? messages) timelineEntries = ChatTimelineEntry.make(from: newMessages ?? messages)
} }
@@ -457,8 +494,10 @@ struct ChatView: View {
!model.isLoadingOlderHistory !model.isLoadingOlderHistory
else { return } else { return }
historyTopTriggerArmed = false historyTopTriggerArmed = false
historyLoadAnchorID = timelineEntries.first?.id historyLoadAnchorID =
model.loadOlderHistoryForSelectedConversation() timelineEntries.first?.id
model
.loadOlderHistoryForSelectedConversation()
} }
.onDisappear { .onDisappear {
historyTopTriggerArmed = true historyTopTriggerArmed = true
@@ -474,7 +513,8 @@ struct ChatView: View {
emptyChatTitle, emptyChatTitle,
systemImage: conversation.isGroup systemImage: conversation.isGroup
? "person.3.fill" ? "person.3.fill"
: (encryptionEnabled ? "lock.shield" : "lock.open"), : (encryptionEnabled
? "lock.shield" : "lock.open"),
description: Text(emptyChatDescription) description: Text(emptyChatDescription)
) )
.padding(.top, 80) .padding(.top, 80)
@@ -484,7 +524,8 @@ struct ChatView: View {
guard model.hasMoreOlderHistory, guard model.hasMoreOlderHistory,
!model.isLoadingOlderHistory !model.isLoadingOlderHistory
else { return } else { return }
model.loadOlderHistoryForSelectedConversation() model
.loadOlderHistoryForSelectedConversation()
} }
} else { } else {
ForEach(timelineEntries) { entry in ForEach(timelineEntries) { entry in
@@ -496,37 +537,65 @@ struct ChatView: View {
model: model, model: model,
message: message, message: message,
isSelectionMode: isSelectingMessages, isSelectionMode: isSelectingMessages,
isSelected: selectedMessageIDs.contains(message.clientID), isSelected: selectedMessageIDs.contains(
message.clientID
),
onAttachmentTap: { onAttachmentTap: {
Task { await model.previewAttachment(message) } Task {
await model.previewAttachment(
message
)
}
}, },
onEdit: { startEditing(message) }, onEdit: { startEditing(message) },
onReply: { startReplying(to: message) }, onReply: { startReplying(to: message) },
onForward: { presentForwarding([message]) }, onForward: {
presentForwarding([message])
},
onRetry: { onRetry: {
Task { await model.retryMediaMessage(message) } Task {
await model.retryMediaMessage(
message
)
}
},
onToggleSelection: {
toggleMessageSelection(message)
},
onBeginSelection: {
beginMessageSelection(with: message)
}, },
onToggleSelection: { toggleMessageSelection(message) },
onBeginSelection: { beginMessageSelection(with: message) },
onRetract: { onRetract: {
destructiveAction = .init( destructiveAction = .init(
kind: .retract, messages: [message]) kind: .retract,
messages: [message]
)
}, },
onDelete: { onDelete: {
destructiveAction = .init( destructiveAction = .init(
kind: .localDelete, messages: [message]) kind: .localDelete,
messages: [message]
)
}, },
onReplyTap: { targetID in onReplyTap: { targetID in
presentReplyThread( presentReplyThread(
rootID: targetID, rootID: targetID,
selectedReplyID: message.clientID selectedReplyID: message
.clientID
) )
}, },
onReact: { emoji in onReact: { emoji in
Task { await model.toggleReaction(emoji, on: message) } Task {
await model.toggleReaction(
emoji,
on: message
)
}
}, },
onReactPicker: { onReactPicker: {
emojiPickerPresentation = .reaction(message) emojiPickerPresentation = .reaction(
message
)
} }
) )
.id(message.clientID) .id(message.clientID)
@@ -537,8 +606,11 @@ struct ChatView: View {
timelineBottomSentinel timelineBottomSentinel
#if os(iOS) #if os(iOS)
ChatScrollMetricsObserver(identity: conversation.jid) { isNearBottom in ChatScrollMetricsObserver(
guard isNearTimelineBottom != isNearBottom else { return } identity: conversation.jid
) { isNearBottom in
guard isNearTimelineBottom != isNearBottom
else { return }
isNearTimelineBottom = isNearBottom isNearTimelineBottom = isNearBottom
} }
.frame(height: 0) .frame(height: 0)
@@ -557,7 +629,8 @@ struct ChatView: View {
rebuildTimelineEntries(from: newMessages) rebuildTimelineEntries(from: newMessages)
} }
#if os(macOS) #if os(macOS)
.onPreferenceChange(TimelineBottomYPreferenceKey.self) { bottomY in .onPreferenceChange(TimelineBottomYPreferenceKey.self) {
bottomY in
updateTimelineBottomProximity( updateTimelineBottomProximity(
bottomY: bottomY, bottomY: bottomY,
viewportHeight: viewport.size.height viewportHeight: viewport.size.height
@@ -572,13 +645,16 @@ struct ChatView: View {
let newID, let newID,
newID != oldID, newID != oldID,
isNearTimelineBottom isNearTimelineBottom
|| timelineEntries.last?.message.direction == .outgoing || timelineEntries.last?.message.direction
== .outgoing
else { else {
return return
} }
scrollToBottom(proxy, animated: true) scrollToBottom(proxy, animated: true)
} }
.onChange(of: model.isLoadingOlderHistory) { wasLoading, isLoading in .onChange(of: model.isLoadingOlderHistory) {
wasLoading,
isLoading in
guard wasLoading, !isLoading, guard wasLoading, !isLoading,
let anchor = historyLoadAnchorID let anchor = historyLoadAnchorID
else { return } else { return }
@@ -601,7 +677,9 @@ struct ChatView: View {
jumpToLatestButton(using: proxy) jumpToLatestButton(using: proxy)
.padding(.trailing, 14) .padding(.trailing, 14)
.padding(.bottom, 12) .padding(.bottom, 12)
.transition(.scale(scale: 0.82).combined(with: .opacity)) .transition(
.scale(scale: 0.82).combined(with: .opacity)
)
.zIndex(10) .zIndex(10)
} }
} }
@@ -698,12 +776,6 @@ struct ChatView: View {
private var composer: some View { private var composer: some View {
VStack(spacing: 8) { VStack(spacing: 8) {
if let editingMessage {
editingBanner(editingMessage)
} else if let replyingToMessage {
replyBanner(replyingToMessage)
}
HStack(alignment: .bottom, spacing: 8) { HStack(alignment: .bottom, spacing: 8) {
Group { Group {
if isCapturePresentationVisible { if isCapturePresentationVisible {
@@ -725,13 +797,23 @@ struct ChatView: View {
} }
.padding(.top, 6) .padding(.top, 6)
.background(Color.clear) .background(Color.clear)
.animation(.easeInOut(duration: 0.18), value: isCapturePresentationVisible) .animation(
.easeInOut(duration: 0.18),
value: isCapturePresentationVisible
)
} }
private var messageInput: some View { private var messageInput: some View {
VStack {
if let editingMessage {
editingBanner(editingMessage)
} else if let replyingToMessage {
replyBanner(replyingToMessage)
}
HStack(alignment: .bottom, spacing: 8) { HStack(alignment: .bottom, spacing: 8) {
TextField( TextField(
editingMessageID == nil ? "Сообщение" : "Изменить сообщение", editingMessageID == nil
? "Сообщение" : "Изменить сообщение",
text: $draft, text: $draft,
axis: .vertical axis: .vertical
) )
@@ -752,16 +834,23 @@ struct ChatView: View {
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("Эмодзи") .accessibilityLabel("Эмодзи")
} }
}
.padding(.leading, 14) .padding(.leading, 14)
.padding(.trailing, 12) .padding(.trailing, 12)
.padding(.vertical, 9) .padding(.vertical, 9)
.frame(minHeight: 42) .frame(minHeight: 42)
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 21, style: .continuous)) .background(
.ultraThinMaterial,
in: RoundedRectangle(cornerRadius: 21, style: .continuous)
)
.overlay( .overlay(
RoundedRectangle(cornerRadius: 21, style: .continuous) RoundedRectangle(cornerRadius: 21, style: .continuous)
.stroke( .stroke(
LinearGradient( LinearGradient(
colors: [Color.white.opacity(0.3), Color.secondary.opacity(0.18)], colors: [
Color.white.opacity(0.3),
Color.secondary.opacity(0.18),
],
startPoint: .top, startPoint: .top,
endPoint: .bottom endPoint: .bottom
), ),
@@ -778,18 +867,25 @@ struct ChatView: View {
Button(action: sendDraft) { Button(action: sendDraft) {
Image(systemName: "arrow.up") Image(systemName: "arrow.up")
.font(.system(size: 18, weight: .bold)) .font(.system(size: 18, weight: .bold))
.foregroundStyle(canUseComposer ? Color.accentColor : Color.secondary) .foregroundStyle(
canUseComposer ? Color.accentColor : Color.secondary
)
.frame(width: 42, height: 42) .frame(width: 42, height: 42)
.background(.ultraThinMaterial, in: Circle()) .background(.ultraThinMaterial, in: Circle())
.overlay( .overlay(
Circle() Circle()
.stroke(Color.secondary.opacity(0.2), lineWidth: 0.75) .stroke(
Color.secondary.opacity(0.2),
lineWidth: 0.75
)
) )
.shadow(color: .black.opacity(0.075), radius: 8, y: 2) .shadow(color: .black.opacity(0.075), radius: 8, y: 2)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.disabled(!canUseComposer) .disabled(!canUseComposer)
.accessibilityLabel(editingMessageID == nil ? "Отправить" : "Сохранить исправление") .accessibilityLabel(
editingMessageID == nil ? "Отправить" : "Сохранить исправление"
)
} else { } else {
TelegramRecordButton( TelegramRecordButton(
mode: .voice, mode: .voice,
@@ -808,7 +904,16 @@ struct ChatView: View {
.overlay(alignment: .top) { .overlay(alignment: .top) {
if isCapturePresentationVisible, !captureIsLocked { if isCapturePresentationVisible, !captureIsLocked {
recordingLockIndicator recordingLockIndicator
.offset(y: -74 + min(12, max(-14, captureDragTranslation.height * 0.12))) .offset(
y: -74
+ min(
12,
max(
-14,
captureDragTranslation.height * 0.12
)
)
)
.transition(.scale.combined(with: .opacity)) .transition(.scale.combined(with: .opacity))
} }
} }
@@ -829,7 +934,10 @@ struct ChatView: View {
dismissKeyboard() dismissKeyboard()
showingMediaPicker = true showingMediaPicker = true
} label: { } label: {
Label("Фото или видео (несколько)", systemImage: "photo.on.rectangle") Label(
"Фото или видео (несколько)",
systemImage: "photo.on.rectangle"
)
} }
Button { Button {
presentFileImporter(.audio) presentFileImporter(.audio)
@@ -860,7 +968,10 @@ struct ChatView: View {
.fill(.ultraThinMaterial) .fill(.ultraThinMaterial)
.overlay( .overlay(
Circle() Circle()
.stroke(Color.secondary.opacity(0.18), lineWidth: 0.75) .stroke(
Color.secondary.opacity(0.18),
lineWidth: 0.75
)
) )
if model.isSendingAttachment || isPreparingAttachments { if model.isSendingAttachment || isPreparingAttachments {
ProgressView() ProgressView()
@@ -877,7 +988,9 @@ struct ChatView: View {
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("Добавить вложение") .accessibilityLabel("Добавить вложение")
.help("Открыть камеру или добавить фото, видео, файлы, музыку и геопозицию") .help(
"Открыть камеру или добавить фото, видео, файлы, музыку и геопозицию"
)
} }
private var editingMessage: ChatMessage? { private var editingMessage: ChatMessage? {
@@ -893,7 +1006,8 @@ struct ChatView: View {
} }
private var requiresOMEMO: Bool { private var requiresOMEMO: Bool {
editingMessage?.security == .omemo || (editingMessageID == nil && encryptionEnabled) editingMessage?.security == .omemo
|| (editingMessageID == nil && encryptionEnabled)
} }
private var encryptionEnabled: Bool { private var encryptionEnabled: Bool {
@@ -902,11 +1016,13 @@ struct ChatView: View {
private var encryptionIcon: String { private var encryptionIcon: String {
if !encryptionEnabled { return "lock.open" } if !encryptionEnabled { return "lock.open" }
return model.isOMEMOReady ? "lock.fill" : "lock.trianglebadge.exclamationmark" return model.isOMEMOReady
? "lock.fill" : "lock.trianglebadge.exclamationmark"
} }
private var liveConversation: Conversation { private var liveConversation: Conversation {
model.conversations.first(where: { $0.jid == conversation.jid }) ?? conversation model.conversations.first(where: { $0.jid == conversation.jid })
?? conversation
} }
private var chatNavigationTitle: some View { private var chatNavigationTitle: some View {
@@ -922,7 +1038,10 @@ struct ChatView: View {
.transition(.opacity) .transition(.opacity)
} }
} }
.animation(.easeInOut(duration: 0.16), value: model.typingText(for: liveConversation)) .animation(
.easeInOut(duration: 0.16),
value: model.typingText(for: liveConversation)
)
.accessibilityElement(children: .combine) .accessibilityElement(children: .combine)
.padding() .padding()
} }
@@ -933,7 +1052,8 @@ struct ChatView: View {
private var emptyChatTitle: String { private var emptyChatTitle: String {
if conversation.isGroup { return "Групповая комната" } if conversation.isGroup { return "Групповая комната" }
return encryptionEnabled ? "Новый защищённый чат" : "Новый чат без шифрования" return encryptionEnabled
? "Новый защищённый чат" : "Новый чат без шифрования"
} }
private var emptyChatDescription: String { private var emptyChatDescription: String {
@@ -949,7 +1069,10 @@ struct ChatView: View {
private var groupJoinBanner: some View { private var groupJoinBanner: some View {
HStack(spacing: 10) { HStack(spacing: 10) {
Image(systemName: liveConversation.invitedBy == nil ? "person.3" : "envelope.badge") Image(
systemName: liveConversation.invitedBy == nil
? "person.3" : "envelope.badge"
)
.foregroundStyle(.tint) .foregroundStyle(.tint)
VStack(alignment: .leading, spacing: 2) { VStack(alignment: .leading, spacing: 2) {
Text( Text(
@@ -1003,7 +1126,9 @@ struct ChatView: View {
} }
private var recordingCancelHintOpacity: Double { private var recordingCancelHintOpacity: Double {
ComposerRecordingGesturePolicy.cancelHintOpacity(for: captureDragTranslation) ComposerRecordingGesturePolicy.cancelHintOpacity(
for: captureDragTranslation
)
} }
private var recordingStatusStrip: some View { private var recordingStatusStrip: some View {
@@ -1037,7 +1162,10 @@ struct ChatView: View {
} }
.padding(.horizontal, 13) .padding(.horizontal, 13)
.frame(minHeight: 42) .frame(minHeight: 42)
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 21, style: .continuous)) .background(
.ultraThinMaterial,
in: RoundedRectangle(cornerRadius: 21, style: .continuous)
)
.overlay( .overlay(
RoundedRectangle(cornerRadius: 21, style: .continuous) RoundedRectangle(cornerRadius: 21, style: .continuous)
.stroke(Color.secondary.opacity(0.2), lineWidth: 0.75) .stroke(Color.secondary.opacity(0.2), lineWidth: 0.75)
@@ -1054,7 +1182,9 @@ struct ChatView: View {
.foregroundStyle(.primary) .foregroundStyle(.primary)
.frame(width: 38, height: 55) .frame(width: 38, height: 55)
.background(.ultraThinMaterial, in: Capsule()) .background(.ultraThinMaterial, in: Capsule())
.overlay(Capsule().stroke(Color.secondary.opacity(0.22), lineWidth: 0.75)) .overlay(
Capsule().stroke(Color.secondary.opacity(0.22), lineWidth: 0.75)
)
.accessibilityLabel("Проведите вверх, чтобы зафиксировать запись") .accessibilityLabel("Проведите вверх, чтобы зафиксировать запись")
} }
@@ -1224,7 +1354,9 @@ struct ChatView: View {
draft = "" draft = ""
if let editingMessageID { if let editingMessageID {
self.editingMessageID = nil self.editingMessageID = nil
Task { await model.editMessage(id: editingMessageID, newBody: outgoing) } Task {
await model.editMessage(id: editingMessageID, newBody: outgoing)
}
} else { } else {
let replyTarget = replyingToMessage let replyTarget = replyingToMessage
replyingToMessageID = nil replyingToMessageID = nil
@@ -1266,7 +1398,10 @@ struct ChatView: View {
} }
let logger = Logger(subsystem: "Luma", category: "video-preview") let logger = Logger(subsystem: "Luma", category: "video-preview")
await waitForCameraFileSettling(at: media.url, logger: logger) await waitForCameraFileSettling(at: media.url, logger: logger)
let staged = await stageImportedFiles([media.url], preferredKind: media.kind) let staged = await stageImportedFiles(
[media.url],
preferredKind: media.kind
)
if staged { if staged {
if let draft = attachmentDrafts.last { if let draft = attachmentDrafts.last {
logger.info( logger.info(
@@ -1276,7 +1411,8 @@ struct ChatView: View {
} else { } else {
logger.error("camera staging produced no drafts") logger.error("camera staging produced no drafts")
if model.errorMessage == nil { if model.errorMessage == nil {
model.errorMessage = "Камера вернула файл, который не удалось подготовить к отправке." model.errorMessage =
"Камера вернула файл, который не удалось подготовить к отправке."
} }
} }
} }
@@ -1285,13 +1421,19 @@ struct ChatView: View {
/// flushed. Wait until the file size stops changing so staging never /// flushed. Wait until the file size stops changing so staging never
/// copies a truncated file. /// copies a truncated file.
private func waitForCameraFileSettling(at url: URL, logger: Logger) async { private func waitForCameraFileSettling(at url: URL, logger: Logger) async {
guard let initial = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize else { guard
let initial = try? url.resourceValues(forKeys: [.fileSizeKey])
.fileSize
else {
return return
} }
var previous = initial var previous = initial
for _ in 0..<10 { for _ in 0..<10 {
try? await Task.sleep(nanoseconds: 250_000_000) try? await Task.sleep(nanoseconds: 250_000_000)
guard let current = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize else { guard
let current = try? url.resourceValues(forKeys: [.fileSizeKey])
.fileSize
else {
return return
} }
if current == previous { if current == previous {
@@ -1339,7 +1481,8 @@ struct ChatView: View {
} }
} }
private func sendRecordedVideoNote(_ recording: VideoNoteRecorder.Recording) { private func sendRecordedVideoNote(_ recording: VideoNoteRecorder.Recording)
{
videoNoteIsSending = true videoNoteIsSending = true
Task { @MainActor in Task { @MainActor in
defer { defer {
@@ -1372,7 +1515,8 @@ struct ChatView: View {
private func presentReplyThread(rootID: String, selectedReplyID: String) { private func presentReplyThread(rootID: String, selectedReplyID: String) {
guard model.message(withID: rootID, in: conversation.jid) != nil else { guard model.message(withID: rootID, in: conversation.jid) != nil else {
model.errorMessage = "Исходное сообщение ещё не загружено в локальную историю." model.errorMessage =
"Исходное сообщение ещё не загружено в локальную историю."
return return
} }
withAnimation(.easeOut(duration: 0.2)) { withAnimation(.easeOut(duration: 0.2)) {
@@ -1404,12 +1548,17 @@ struct ChatView: View {
return sourceIdentifiers.contains(replyToID) return sourceIdentifiers.contains(replyToID)
} }
if !replies.contains(where: { $0.clientID == selectedReplyID }), if !replies.contains(where: { $0.clientID == selectedReplyID }),
let selected = model.message(withID: selectedReplyID, in: conversation.jid) let selected = model.message(
withID: selectedReplyID,
in: conversation.jid
)
{ {
replies.append(selected) replies.append(selected)
} }
return replies.sorted { lhs, rhs in return replies.sorted { lhs, rhs in
if lhs.timestamp == rhs.timestamp { return lhs.clientID < rhs.clientID } if lhs.timestamp == rhs.timestamp {
return lhs.clientID < rhs.clientID
}
return lhs.timestamp < rhs.timestamp return lhs.timestamp < rhs.timestamp
} }
} }
@@ -1441,7 +1590,8 @@ struct ChatView: View {
} }
guard !sourceURLs.isEmpty else { guard !sourceURLs.isEmpty else {
model.errorMessage = MediaPickerError.cannotReadSelection.errorDescription model.errorMessage =
MediaPickerError.cannotReadSelection.errorDescription
return return
} }
failedItemCount += max(0, sourceURLs.count - prepared.count) failedItemCount += max(0, sourceURLs.count - prepared.count)
@@ -1501,7 +1651,9 @@ struct ChatView: View {
} }
} }
private func loadPickedMediaFile(_ item: PhotosPickerItem) async throws -> URL { private func loadPickedMediaFile(_ item: PhotosPickerItem) async throws
-> URL
{
let supportsImage = item.supportedContentTypes.contains { let supportsImage = item.supportedContentTypes.contains {
$0.conforms(to: .image) $0.conforms(to: .image)
} }
@@ -1532,12 +1684,16 @@ struct ChatView: View {
} }
private func loadPickedPhoto(_ item: PhotosPickerItem) async throws -> URL { private func loadPickedPhoto(_ item: PhotosPickerItem) async throws -> URL {
let fileRepresentation = try? await item.loadTransferable(type: PickedPhoto.self) let fileRepresentation = try? await item.loadTransferable(
type: PickedPhoto.self
)
if let photo = fileRepresentation { if let photo = fileRepresentation {
return photo.url return photo.url
} }
let dataRepresentation = try await item.loadTransferable(type: Data.self) let dataRepresentation = try await item.loadTransferable(
type: Data.self
)
guard let data = dataRepresentation, !data.isEmpty else { guard let data = dataRepresentation, !data.isEmpty else {
throw MediaPickerError.cannotReadSelection throw MediaPickerError.cannotReadSelection
} }
@@ -1547,7 +1703,9 @@ struct ChatView: View {
} }
private func loadPickedVideo(_ item: PhotosPickerItem) async throws -> URL { private func loadPickedVideo(_ item: PhotosPickerItem) async throws -> URL {
guard let video = try await item.loadTransferable(type: PickedVideo.self) else { guard
let video = try await item.loadTransferable(type: PickedVideo.self)
else {
throw MediaPickerError.cannotReadSelection throw MediaPickerError.cannotReadSelection
} }
return video.url return video.url
@@ -1637,8 +1795,8 @@ struct ChatView: View {
Capsule() Capsule()
.fill(Color.accentColor) .fill(Color.accentColor)
.frame(width: 3, height: 34) .frame(width: 3, height: 34)
Image(systemName: "pencil") // Image(systemName: "pencil")
.foregroundStyle(.tint) // .foregroundStyle(.tint)
VStack(alignment: .leading, spacing: 1) { VStack(alignment: .leading, spacing: 1) {
Text("Редактирование") Text("Редактирование")
.font(.caption.weight(.semibold)) .font(.caption.weight(.semibold))
@@ -1650,43 +1808,60 @@ struct ChatView: View {
} }
Spacer() Spacer()
Button(action: cancelEditing) { Button(action: cancelEditing) {
Image(systemName: "xmark.circle.fill") Image(systemName: "xmark")
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("Отменить редактирование") .accessibilityLabel("Отменить редактирование")
} }
.padding(.horizontal, 14) // .padding(.horizontal, 14)
.padding(.top, 8) // .padding(.top, 8)
} }
/// Telegram-style reply plate: a rounded card above the composer with
/// the reply header, a close button and the quoted message preview.
private func replyBanner(_ message: ChatMessage) -> some View { private func replyBanner(_ message: ChatMessage) -> some View {
HStack(spacing: 10) { HStack(alignment: .center) {
Capsule() Capsule()
.fill(Color.accentColor) .fill(Color.accentColor)
.frame(width: 3, height: 36) .frame(width: 3, height: 34)
VStack(alignment: .leading, spacing: 1) { VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 7) {
// Image(systemName: "arrowshape.turn.up.left.fill")
// .font(.caption.weight(.semibold))
// .foregroundStyle(.tint)
Text( Text(
"Ответ: \(message.senderDisplayName ?? model.displayName(for: message.senderJID))" "Ответ: \(message.senderDisplayName ?? model.displayName(for: message.senderJID))"
) )
.font(.caption.weight(.semibold)) .font(.caption.weight(.semibold))
.foregroundStyle(.tint) .foregroundStyle(.tint)
.lineLimit(1)
}
Text(message.quotePreview) Text(message.quotePreview)
.font(.caption) .font(.caption)
.foregroundStyle(.secondary) .foregroundStyle(.primary.opacity(0.8))
.lineLimit(1) .lineLimit(1)
.frame(maxWidth: .infinity, alignment: .leading)
} }
Spacer() Spacer()
Button(action: cancelReplying) { Button(action: cancelReplying) {
Image(systemName: "xmark.circle.fill") Image(systemName: "xmark")
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("Отменить ответ") .accessibilityLabel("Отменить ответ")
} }
.padding(.horizontal, 14) // .padding(.horizontal, 12)
.padding(.top, 8) // .padding(.vertical, 8)
.padding(.bottom, 3) // .background(
// .thinMaterial,
// in: RoundedRectangle(cornerRadius: 14, style: .continuous)
// )
// .overlay {
// RoundedRectangle(cornerRadius: 14, style: .continuous)
// .stroke(Color.secondary.opacity(0.16), lineWidth: 0.75)
// }
// .padding(.horizontal, 10)
} }
private func perform(_ action: MessageDestructiveAction) { private func perform(_ action: MessageDestructiveAction) {
@@ -1751,7 +1926,9 @@ struct ChatView: View {
} }
} }
private func performInitialScrollAfterLayout(using proxy: ScrollViewProxy) async { private func performInitialScrollAfterLayout(using proxy: ScrollViewProxy)
async
{
guard !hasCompletedInitialScroll else { return } guard !hasCompletedInitialScroll else { return }
guard !model.selectedMessages.isEmpty else { guard !model.selectedMessages.isEmpty else {
// Empty conversation: there is nothing to scroll to, so the // Empty conversation: there is nothing to scroll to, so the
@@ -1903,14 +2080,19 @@ private struct TimelineBottomYPreferenceKey: PreferenceKey {
detach() detach()
observedScrollView = scrollView observedScrollView = scrollView
observations = [ observations = [
scrollView.observe(\.contentOffset, options: [.initial, .new]) { scrollView.observe(
\.contentOffset,
options: [.initial, .new]
) {
[weak self] _, _ in [weak self] _, _ in
self?.scheduleMetricsReport() self?.scheduleMetricsReport()
}, },
scrollView.observe(\.contentSize, options: [.new]) { [weak self] _, _ in scrollView.observe(\.contentSize, options: [.new]) {
[weak self] _, _ in
self?.scheduleMetricsReport() self?.scheduleMetricsReport()
}, },
scrollView.observe(\.bounds, options: [.new]) { [weak self] _, _ in scrollView.observe(\.bounds, options: [.new]) {
[weak self] _, _ in
self?.scheduleMetricsReport() self?.scheduleMetricsReport()
}, },
] ]
@@ -1960,7 +2142,9 @@ struct ComposerRecordingGesturePolicy {
static let longPressDelayNanoseconds: UInt64 = 240_000_000 static let longPressDelayNanoseconds: UInt64 = 240_000_000
static let minimumLongPressDuration: TimeInterval = 0.22 static let minimumLongPressDuration: TimeInterval = 0.22
static func resolution(for translation: CGSize) -> ComposerRecordingGestureResolution { static func resolution(for translation: CGSize)
-> ComposerRecordingGestureResolution
{
let cancelProgress = max(0, -translation.width) / cancelDistance let cancelProgress = max(0, -translation.width) / cancelDistance
let lockProgress = max(0, -translation.height) / lockDistance let lockProgress = max(0, -translation.height) / lockDistance
if cancelProgress >= 1, cancelProgress >= lockProgress { if cancelProgress >= 1, cancelProgress >= lockProgress {
@@ -1974,7 +2158,10 @@ struct ComposerRecordingGesturePolicy {
static func cancelHintOpacity(for translation: CGSize) -> Double { static func cancelHintOpacity(for translation: CGSize) -> Double {
let leftwardDistance = max(CGFloat.zero, -translation.width) let leftwardDistance = max(CGFloat.zero, -translation.width)
let fadeAmount = min(maximumCancelHintFade, leftwardDistance / cancelHintFadeDistance) let fadeAmount = min(
maximumCancelHintFade,
leftwardDistance / cancelHintFadeDistance
)
return Double(CGFloat(1) - fadeAmount) return Double(CGFloat(1) - fadeAmount)
} }
@@ -2023,7 +2210,9 @@ private struct TelegramRecordButton: View {
.fill(.ultraThinMaterial) .fill(.ultraThinMaterial)
.overlay { .overlay {
Circle() Circle()
.fill(isRecording ? Color.red.opacity(0.13) : Color.clear) .fill(
isRecording ? Color.red.opacity(0.13) : Color.clear
)
} }
.overlay( .overlay(
Circle() Circle()
@@ -2049,7 +2238,8 @@ private struct TelegramRecordButton: View {
.frame(width: 42, height: 42) .frame(width: 42, height: 42)
.scaleEffect(isRecording && !isLocked ? 1.34 : (isPressing ? 1.08 : 1)) .scaleEffect(isRecording && !isLocked ? 1.34 : (isPressing ? 1.08 : 1))
.shadow( .shadow(
color: isRecording ? Color.black.opacity(0.2) : Color.black.opacity(0.075), color: isRecording
? Color.black.opacity(0.2) : Color.black.opacity(0.075),
radius: 8, radius: 8,
y: isRecording ? 4 : 2 y: isRecording ? 4 : 2
) )
@@ -2072,10 +2262,15 @@ private struct TelegramRecordButton: View {
#endif #endif
.opacity(isEnabled ? 1 : 0.45) .opacity(isEnabled ? 1 : 0.45)
.allowsHitTesting(isEnabled && !isFinalizing) .allowsHitTesting(isEnabled && !isFinalizing)
.animation(.spring(response: 0.24, dampingFraction: 0.78), value: isRecording) .animation(
.spring(response: 0.24, dampingFraction: 0.78),
value: isRecording
)
.animation(.easeOut(duration: 0.12), value: isPressing) .animation(.easeOut(duration: 0.12), value: isPressing)
.accessibilityLabel(accessibilityLabel) .accessibilityLabel(accessibilityLabel)
.accessibilityHint("Во время записи проведите влево для отмены или вверх для фиксации") .accessibilityHint(
"Во время записи проведите влево для отмены или вверх для фиксации"
)
.onDisappear { .onDisappear {
longPressTask?.cancel() longPressTask?.cancel()
longPressTask = nil longPressTask = nil
@@ -2089,7 +2284,8 @@ private struct TelegramRecordButton: View {
private var accessibilityLabel: String { private var accessibilityLabel: String {
if isLocked { return "Отправить записанное сообщение" } if isLocked { return "Отправить записанное сообщение" }
return isRecording ? mode.recordingAccessibilityLabel : mode.idleAccessibilityLabel return isRecording
? mode.recordingAccessibilityLabel : mode.idleAccessibilityLabel
} }
private var recordGesture: some Gesture { private var recordGesture: some Gesture {
@@ -2170,7 +2366,8 @@ private struct TelegramRecordButton: View {
longPressTask = Task { @MainActor in longPressTask = Task { @MainActor in
do { do {
try await Task.sleep( try await Task.sleep(
nanoseconds: ComposerRecordingGesturePolicy.longPressDelayNanoseconds nanoseconds: ComposerRecordingGesturePolicy
.longPressDelayNanoseconds
) )
} catch { } catch {
return return
@@ -2233,19 +2430,30 @@ private struct TelegramRecordButton: View {
nil nil
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(
guard activeTouch == nil, let touch = touches.first else { return } _ touches: Set<UITouch>,
with event: UIEvent?
) {
guard activeTouch == nil, let touch = touches.first else {
return
}
activeTouch = touch activeTouch = touch
origin = touch.location(in: window) origin = touch.location(in: window)
onBegan?(eventDate(for: touch)) onBegan?(eventDate(for: touch))
} }
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesMoved(
_ touches: Set<UITouch>,
with event: UIEvent?
) {
guard let touch = trackedTouch(in: touches) else { return } guard let touch = trackedTouch(in: touches) else { return }
onMoved?(translation(for: touch)) onMoved?(translation(for: touch))
} }
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(
_ touches: Set<UITouch>,
with event: UIEvent?
) {
guard let touch = trackedTouch(in: touches) else { return } guard let touch = trackedTouch(in: touches) else { return }
let translation = translation(for: touch) let translation = translation(for: touch)
let date = eventDate(for: touch) let date = eventDate(for: touch)
@@ -2253,7 +2461,10 @@ private struct TelegramRecordButton: View {
onEnded?(translation, date) onEnded?(translation, date)
} }
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesCancelled(
_ touches: Set<UITouch>,
with event: UIEvent?
) {
guard trackedTouch(in: touches) != nil else { return } guard trackedTouch(in: touches) != nil else { return }
activeTouch = nil activeTouch = nil
onCancelled?() onCancelled?()
@@ -2328,7 +2539,9 @@ private struct MessageDestructiveAction: Identifiable {
let messages: [ChatMessage] let messages: [ChatMessage]
var id: String { var id: String {
let messageIDs = messages.map(\.clientID).sorted().joined(separator: "-") let messageIDs = messages.map(\.clientID).sorted().joined(
separator: "-"
)
return "\(kind == .localDelete ? "local" : "retract")-\(messageIDs)" return "\(kind == .localDelete ? "local" : "retract")-\(messageIDs)"
} }
@@ -2336,9 +2549,13 @@ private struct MessageDestructiveAction: Identifiable {
let plural = messages.count > 1 let plural = messages.count > 1
switch kind { switch kind {
case .localDelete: case .localDelete:
return plural ? "Удалить выбранные сообщения у себя?" : "Удалить сообщение у себя?" return plural
? "Удалить выбранные сообщения у себя?"
: "Удалить сообщение у себя?"
case .retract: case .retract:
return plural ? "Удалить выбранные сообщения у всех?" : "Удалить сообщение у всех?" return plural
? "Удалить выбранные сообщения у всех?"
: "Удалить сообщение у всех?"
} }
} }
@@ -2383,7 +2600,12 @@ private struct PickedPhoto: Transferable {
FileRepresentation(contentType: .image) { photo in FileRepresentation(contentType: .image) { photo in
SentTransferredFile(photo.url) SentTransferredFile(photo.url)
} importing: { received in } importing: { received in
PickedPhoto(url: try copyPickedMediaFile(received.file, fallbackExtension: "jpg")) PickedPhoto(
url: try copyPickedMediaFile(
received.file,
fallbackExtension: "jpg"
)
)
} }
} }
} }
@@ -2395,12 +2617,19 @@ private struct PickedVideo: Transferable {
FileRepresentation(contentType: .movie) { video in FileRepresentation(contentType: .movie) { video in
SentTransferredFile(video.url) SentTransferredFile(video.url)
} importing: { received in } importing: { received in
PickedVideo(url: try copyPickedMediaFile(received.file, fallbackExtension: "mov")) PickedVideo(
url: try copyPickedMediaFile(
received.file,
fallbackExtension: "mov"
)
)
} }
} }
} }
private func copyPickedMediaFile(_ source: URL, fallbackExtension: String) throws -> URL { private func copyPickedMediaFile(_ source: URL, fallbackExtension: String)
throws -> URL
{
let fileManager = FileManager.default let fileManager = FileManager.default
let rootDirectory = fileManager.temporaryDirectory let rootDirectory = fileManager.temporaryDirectory
.appendingPathComponent("LumaPhotoPicker", isDirectory: true) .appendingPathComponent("LumaPhotoPicker", isDirectory: true)
@@ -2412,7 +2641,9 @@ private func copyPickedMediaFile(_ source: URL, fallbackExtension: String) throw
withIntermediateDirectories: true withIntermediateDirectories: true
) )
let values = try? source.resourceValues(forKeys: [.nameKey, .contentTypeKey]) let values = try? source.resourceValues(forKeys: [
.nameKey, .contentTypeKey,
])
let fileExtension = let fileExtension =
source.pathExtension.isEmpty source.pathExtension.isEmpty
? (values?.contentType?.preferredFilenameExtension ?? fallbackExtension) ? (values?.contentType?.preferredFilenameExtension ?? fallbackExtension)
@@ -2425,7 +2656,9 @@ private func copyPickedMediaFile(_ source: URL, fallbackExtension: String) throw
} }
let forbidden = CharacterSet(charactersIn: "/\\:") let forbidden = CharacterSet(charactersIn: "/\\:")
.union(.controlCharacters) .union(.controlCharacters)
filename = filename.components(separatedBy: forbidden).joined(separator: "_") filename = filename.components(separatedBy: forbidden).joined(
separator: "_"
)
let destination = selectionDirectory.appendingPathComponent(filename) let destination = selectionDirectory.appendingPathComponent(filename)
let accessed = source.startAccessingSecurityScopedResource() let accessed = source.startAccessingSecurityScopedResource()
defer { defer {
@@ -27,6 +27,7 @@ struct MessageBubble: View {
@State private var replySwipeOffset: CGFloat = 0 @State private var replySwipeOffset: CGFloat = 0
@State private var replySwipeArmed = false @State private var replySwipeArmed = false
@State private var replySwipeLocked = false
var body: some View { var body: some View {
ZStack { ZStack {
@@ -53,6 +54,7 @@ struct MessageBubble: View {
guard selecting else { return } guard selecting else { return }
replySwipeOffset = 0 replySwipeOffset = 0
replySwipeArmed = false replySwipeArmed = false
replySwipeLocked = false
} }
.contextMenu { .contextMenu {
if !isSelectionMode { if !isSelectionMode {
@@ -176,7 +178,16 @@ struct MessageBubble: View {
private var replySwipeGesture: some Gesture { private var replySwipeGesture: some Gesture {
DragGesture(minimumDistance: 20) DragGesture(minimumDistance: 20)
.onChanged { value in .onChanged { value in
let offset = MessageReplySwipePolicy.offset(for: value.translation) // Once the gesture is clearly horizontal it locks in, so a
// slightly diagonal finish can neither break the reply swipe
// nor let the scroll view steal it.
if !replySwipeLocked, MessageReplySwipePolicy.canLock(value.translation) {
replySwipeLocked = true
}
let offset = MessageReplySwipePolicy.offset(
locked: replySwipeLocked,
translation: value.translation
)
guard offset != 0 else { guard offset != 0 else {
// Vertical (or not clearly horizontal) movement: snap the // Vertical (or not clearly horizontal) movement: snap the
// indicator back and never publish per-cell state changes // indicator back and never publish per-cell state changes
@@ -191,6 +202,7 @@ struct MessageBubble: View {
} }
replySwipeOffset = offset replySwipeOffset = offset
let armed = MessageReplySwipePolicy.shouldReply( let armed = MessageReplySwipePolicy.shouldReply(
locked: replySwipeLocked,
translation: value.translation, translation: value.translation,
predictedEndTranslation: value.translation predictedEndTranslation: value.translation
) )
@@ -201,9 +213,11 @@ struct MessageBubble: View {
} }
.onEnded { value in .onEnded { value in
let shouldReply = MessageReplySwipePolicy.shouldReply( let shouldReply = MessageReplySwipePolicy.shouldReply(
locked: replySwipeLocked,
translation: value.translation, translation: value.translation,
predictedEndTranslation: value.predictedEndTranslation predictedEndTranslation: value.predictedEndTranslation
) )
replySwipeLocked = false
if replySwipeOffset != 0 || replySwipeArmed { if replySwipeOffset != 0 || replySwipeArmed {
withAnimation(.spring(response: 0.24, dampingFraction: 0.82)) { withAnimation(.spring(response: 0.24, dampingFraction: 0.82)) {
replySwipeOffset = 0 replySwipeOffset = 0
+45 -17
View File
@@ -5,10 +5,12 @@ import XCTest
final class MessageReplySwipeTests: XCTestCase { final class MessageReplySwipeTests: XCTestCase {
func testHorizontalSwipeInEitherDirectionReplies() { func testHorizontalSwipeInEitherDirectionReplies() {
XCTAssertTrue(MessageReplySwipePolicy.shouldReply( XCTAssertTrue(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: 70, height: 8), translation: CGSize(width: 70, height: 8),
predictedEndTranslation: CGSize(width: 72, height: 9) predictedEndTranslation: CGSize(width: 72, height: 9)
)) ))
XCTAssertTrue(MessageReplySwipePolicy.shouldReply( XCTAssertTrue(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: -70, height: 8), translation: CGSize(width: -70, height: 8),
predictedEndTranslation: CGSize(width: -72, height: 9) predictedEndTranslation: CGSize(width: -72, height: 9)
)) ))
@@ -16,6 +18,7 @@ final class MessageReplySwipeTests: XCTestCase {
func testVerticalTimelineScrollDoesNotReply() { func testVerticalTimelineScrollDoesNotReply() {
XCTAssertFalse(MessageReplySwipePolicy.shouldReply( XCTAssertFalse(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: 42, height: 80), translation: CGSize(width: 42, height: 80),
predictedEndTranslation: CGSize(width: 50, height: 105) predictedEndTranslation: CGSize(width: 50, height: 105)
)) ))
@@ -23,17 +26,33 @@ final class MessageReplySwipeTests: XCTestCase {
func testDiagonalTimelineScrollDoesNotReply() { func testDiagonalTimelineScrollDoesNotReply() {
XCTAssertFalse(MessageReplySwipePolicy.shouldReply( XCTAssertFalse(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: 54, height: 44), translation: CGSize(width: 54, height: 44),
predictedEndTranslation: CGSize(width: 76, height: 58) predictedEndTranslation: CGSize(width: 76, height: 58)
)) ))
XCTAssertEqual( XCTAssertEqual(
MessageReplySwipePolicy.offset(for: CGSize(width: 54, height: 44)), MessageReplySwipePolicy.offset(locked: false, translation: CGSize(width: 54, height: 44)),
0
)
}
func testDiagonalScrollAtOldThresholdDoesNotReply() {
// width/height ≈ 1.5 passed the old 1.4 dominance and moved bubbles
// during scrolling; the stricter policy must stay silent.
XCTAssertFalse(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: 60, height: 40),
predictedEndTranslation: CGSize(width: 60, height: 40)
))
XCTAssertEqual(
MessageReplySwipePolicy.offset(locked: false, translation: CGSize(width: 60, height: 40)),
0 0
) )
} }
func testShortSwipeDoesNotReply() { func testShortSwipeDoesNotReply() {
XCTAssertFalse(MessageReplySwipePolicy.shouldReply( XCTAssertFalse(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: 36, height: 4), translation: CGSize(width: 36, height: 4),
predictedEndTranslation: CGSize(width: 48, height: 5) predictedEndTranslation: CGSize(width: 48, height: 5)
)) ))
@@ -41,39 +60,48 @@ final class MessageReplySwipeTests: XCTestCase {
func testFastHorizontalFlickUsesPredictedEnd() { func testFastHorizontalFlickUsesPredictedEnd() {
XCTAssertTrue(MessageReplySwipePolicy.shouldReply( XCTAssertTrue(MessageReplySwipePolicy.shouldReply(
locked: false,
translation: CGSize(width: -35, height: 3), translation: CGSize(width: -35, height: 3),
predictedEndTranslation: CGSize(width: -90, height: 7) predictedEndTranslation: CGSize(width: -90, height: 7)
)) ))
} }
func testDiagonalScrollAtOldThresholdDoesNotReply() {
// width/height ≈ 1.5 passed the old 1.4 dominance and moved bubbles
// during scrolling; the stricter policy must stay silent.
XCTAssertFalse(MessageReplySwipePolicy.shouldReply(
translation: CGSize(width: 60, height: 40),
predictedEndTranslation: CGSize(width: 60, height: 40)
))
XCTAssertEqual(
MessageReplySwipePolicy.offset(for: CGSize(width: 60, height: 40)),
0
)
}
func testIndicatorStaysHiddenUntilActivationDistance() { func testIndicatorStaysHiddenUntilActivationDistance() {
XCTAssertEqual( XCTAssertEqual(
MessageReplySwipePolicy.offset(for: CGSize(width: 20, height: 0)), MessageReplySwipePolicy.offset(locked: false, translation: CGSize(width: 20, height: 0)),
0 0
) )
XCTAssertGreaterThan( XCTAssertGreaterThan(
MessageReplySwipePolicy.offset(for: CGSize(width: 24, height: 0)), MessageReplySwipePolicy.offset(locked: false, translation: CGSize(width: 24, height: 0)),
0 0
) )
} }
func testVisualOffsetIsCapped() { func testVisualOffsetIsCapped() {
XCTAssertEqual( XCTAssertEqual(
MessageReplySwipePolicy.offset(for: CGSize(width: 500, height: 0)), MessageReplySwipePolicy.offset(locked: true, translation: CGSize(width: 500, height: 0)),
MessageReplySwipePolicy.maximumOffset MessageReplySwipePolicy.maximumOffset
) )
} }
func testCanLockRequiresClearHorizontalStart() {
XCTAssertTrue(MessageReplySwipePolicy.canLock(CGSize(width: 24, height: 10)))
XCTAssertFalse(MessageReplySwipePolicy.canLock(CGSize(width: 30, height: 20)))
XCTAssertFalse(MessageReplySwipePolicy.canLock(CGSize(width: 8, height: 5)))
} }
func testLockedSwipeToleratesVerticalDrift() {
// Once locked the finger may drift: a 1.17 ratio would be rejected
// by the 1.6 lock dominance but must still count as a reply swipe.
XCTAssertTrue(MessageReplySwipePolicy.shouldReply(
locked: true,
translation: CGSize(width: 70, height: 60),
predictedEndTranslation: CGSize(width: 70, height: 60)
))
XCTAssertGreaterThan(
MessageReplySwipePolicy.offset(locked: true, translation: CGSize(width: 70, height: 60)),
0
)
}
}