added more anomations
This commit is contained in:
1 parent
0d6c562abd
commit
3e990e7af9
4 files changed
+435
-1
No files matched your search
@@ -75,6 +75,13 @@ struct ChatView: View {
|
||||
@State private var captureSuspendsArchiveSync = false
|
||||
@State private var pickerResetToken = UUID()
|
||||
@State private var timelineEntries: [ChatTimelineEntry] = []
|
||||
/// Messages-style entrance state: which rows currently animate, which IDs
|
||||
/// the timeline already showed, and whether the opening cascade of this
|
||||
/// conversation has played.
|
||||
@State private var entranceModes: [String: MessageBubbleEntrance.Mode] = [:]
|
||||
@State private var knownEntranceIDs: Set<String> = []
|
||||
@State private var hasPlayedOpeningEntrance = false
|
||||
@State private var entranceReleaseToken = UUID()
|
||||
|
||||
/// Messages of this conversation straight from the SwiftData store,
|
||||
/// ordered like `AppModel.selectedMessages`: timestamp, then clientID.
|
||||
@@ -143,6 +150,13 @@ struct ChatView: View {
|
||||
_captureSuspendsArchiveSync = State(initialValue: false)
|
||||
_pickerResetToken = State(initialValue: UUID())
|
||||
_timelineEntries = State<[ChatTimelineEntry]>(initialValue: [])
|
||||
_entranceModes = State<[String: MessageBubbleEntrance.Mode]>(
|
||||
initialValue: [:]
|
||||
)
|
||||
_knownEntranceIDs = State<Set<String>>(initialValue: [])
|
||||
_hasPlayedOpeningEntrance = State(initialValue: false)
|
||||
_entranceConversationID = State<String?>(initialValue: nil)
|
||||
_entranceReleaseToken = State(initialValue: UUID())
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@@ -376,6 +390,10 @@ struct ChatView: View {
|
||||
historyTopTriggerVisible = false
|
||||
historyAutoContinueCount = 0
|
||||
emptyHistoryRetryCount = 0
|
||||
entranceModes = [:]
|
||||
knownEntranceIDs = []
|
||||
hasPlayedOpeningEntrance = false
|
||||
entranceReleaseToken = UUID()
|
||||
}
|
||||
.onDisappear {
|
||||
model.endConversationViewing(jid: conversation.jid)
|
||||
@@ -478,7 +496,87 @@ struct ChatView: View {
|
||||
|
||||
private func rebuildTimelineEntries(from newMessages: [ChatMessage]? = nil)
|
||||
{
|
||||
timelineEntries = ChatTimelineEntry.make(from: newMessages ?? messages)
|
||||
let source = newMessages ?? messages
|
||||
timelineEntries = ChatTimelineEntry.make(from: source)
|
||||
updateEntranceModes(for: source)
|
||||
}
|
||||
|
||||
/// Decides which rows play a Messages-style entrance after a rebuild.
|
||||
/// The first page of an opened conversation cascades bottom-up; rows that
|
||||
/// appear later (a send, an incoming message) launch from the composer's
|
||||
/// edge. Modes are released once the animation window closes so a row
|
||||
/// recycled by the lazy stack during scrolling reappears statically.
|
||||
private func updateEntranceModes(for newMessages: [ChatMessage]) {
|
||||
let ids = newMessages.map(\.clientID)
|
||||
let known = knownEntranceIDs
|
||||
knownEntranceIDs = Set(ids)
|
||||
|
||||
guard hasPlayedOpeningEntrance else {
|
||||
guard !ids.isEmpty else { return }
|
||||
hasPlayedOpeningEntrance = true
|
||||
// The first message ever in an empty chat should launch out of
|
||||
// the composer like any other send, not cascade in.
|
||||
if ids.count == 1, known.isEmpty,
|
||||
newMessages.first?.direction == .outgoing
|
||||
{
|
||||
applyLaunch(for: newMessages, ids: ids)
|
||||
return
|
||||
}
|
||||
let delays = ChatMessageEntrancePolicy.entranceDelays(
|
||||
forIDs: ids
|
||||
)
|
||||
for (id, delay) in delays {
|
||||
entranceModes[id] = .cascade(delay: delay)
|
||||
}
|
||||
scheduleEntranceRelease(
|
||||
ids: Set(delays.keys),
|
||||
after: ChatMessageEntrancePolicy.entranceCascadeDuration
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
applyLaunch(
|
||||
for: newMessages,
|
||||
ids: ChatMessageEntrancePolicy.appendedLaunchIDs(
|
||||
in: ids,
|
||||
after: known
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private func applyLaunch(for messages: [ChatMessage], ids: [String]) {
|
||||
guard !ids.isEmpty else { return }
|
||||
let launched = Set(ids)
|
||||
var applied: Set<String> = []
|
||||
for message in messages where launched.contains(message.clientID) {
|
||||
entranceModes[message.clientID] = .launch(
|
||||
outgoing: message.direction == .outgoing
|
||||
)
|
||||
applied.insert(message.clientID)
|
||||
}
|
||||
scheduleEntranceRelease(
|
||||
ids: applied,
|
||||
after: ChatMessageEntrancePolicy.entranceAnimationDuration
|
||||
)
|
||||
}
|
||||
|
||||
private func scheduleEntranceRelease(
|
||||
ids: Set<String>,
|
||||
after delay: TimeInterval
|
||||
) {
|
||||
guard !ids.isEmpty else { return }
|
||||
let token = entranceReleaseToken
|
||||
Task { @MainActor in
|
||||
try? await Task.sleep(
|
||||
nanoseconds: UInt64(delay * 1_000_000_000)
|
||||
)
|
||||
guard !Task.isCancelled, token == entranceReleaseToken else {
|
||||
return
|
||||
}
|
||||
for id in ids {
|
||||
entranceModes[id] = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var isSelectingMessages: Bool {
|
||||
@@ -606,6 +704,13 @@ struct ChatView: View {
|
||||
)
|
||||
}
|
||||
)
|
||||
.modifier(
|
||||
MessageBubbleEntrance(
|
||||
mode: entranceModes[
|
||||
message.clientID
|
||||
] ?? .settled
|
||||
)
|
||||
)
|
||||
.id(message.clientID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Messages-style entrance for chat rows.
|
||||
///
|
||||
/// Opening a conversation reveals its first page as a bottom-up cascade
|
||||
/// (`.cascade`), and a row appended while the timeline is already rendered
|
||||
/// springs out of the composer's send control when outgoing (`.launch`).
|
||||
/// Everything else stays `.settled`, which keeps history scrolling, reactions
|
||||
/// and selection updates free of animation.
|
||||
struct MessageBubbleEntrance: ViewModifier {
|
||||
enum Mode: Equatable {
|
||||
case settled
|
||||
case cascade(delay: TimeInterval)
|
||||
case launch(outgoing: Bool)
|
||||
|
||||
var isSettled: Bool {
|
||||
if case .settled = self { return true }
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
let mode: Mode
|
||||
|
||||
// A settled row must never pass through an invisible frame, so only the
|
||||
// rows that actually animate start hidden.
|
||||
@State private var isRevealed: Bool
|
||||
@State private var hasStarted = false
|
||||
|
||||
init(mode: Mode) {
|
||||
self.mode = mode
|
||||
_isRevealed = State(initialValue: mode.isSettled)
|
||||
}
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
content
|
||||
.opacity(isRevealed ? 1 : 0)
|
||||
.scaleEffect(isRevealed ? 1 : displacedScale, anchor: scaleAnchor)
|
||||
.offset(y: isRevealed ? 0 : displacedRise)
|
||||
.onAppear(perform: reveal)
|
||||
}
|
||||
|
||||
/// Outgoing bubbles shrink toward the trailing bottom corner, where the
|
||||
/// composer's send control sits; incoming ones grow from their own edge.
|
||||
private var displacedScale: CGFloat {
|
||||
switch mode {
|
||||
case .settled, .cascade:
|
||||
return ChatMessageEntrancePolicy.entranceScale
|
||||
case .launch(let outgoing):
|
||||
return outgoing
|
||||
? ChatMessageEntrancePolicy.sendScale
|
||||
: ChatMessageEntrancePolicy.receiveScale
|
||||
}
|
||||
}
|
||||
|
||||
private var scaleAnchor: UnitPoint {
|
||||
if case .launch(let outgoing) = mode, outgoing { return .bottomTrailing }
|
||||
return .bottom
|
||||
}
|
||||
|
||||
private var displacedRise: CGFloat {
|
||||
switch mode {
|
||||
case .settled, .cascade:
|
||||
return ChatMessageEntrancePolicy.entranceRise
|
||||
case .launch(let outgoing):
|
||||
return outgoing
|
||||
? ChatMessageEntrancePolicy.sendRise
|
||||
: ChatMessageEntrancePolicy.receiveRise
|
||||
}
|
||||
}
|
||||
|
||||
private func reveal() {
|
||||
guard !hasStarted else { return }
|
||||
hasStarted = true
|
||||
guard !isRevealed else { return }
|
||||
let delay: TimeInterval
|
||||
if case .cascade(let cascadeDelay) = mode {
|
||||
delay = cascadeDelay
|
||||
} else {
|
||||
delay = 0
|
||||
}
|
||||
withAnimation(
|
||||
ChatMessageEntrancePolicy.appendAnimation.delay(delay)
|
||||
) {
|
||||
isRevealed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user