Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ struct AssistChatView: View {
.onReceive(viewModel.shouldOpenKeyboardPublisher) { value in
isFocused = value
}
.onChange(of: viewModel.isRetryButtonVisible) { _, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
viewModel.scrollViewProxy?.scrollTo(retryViewId, anchor: .bottom)
}
}
.onFirstAppear { viewModel.setViewController(viewController) }
.padding(.huiSpaces.space24)
.animation(.smooth, value: [viewModel.isBackButtonVisible, viewModel.isRetryButtonVisible])
Expand Down Expand Up @@ -81,9 +76,11 @@ struct AssistChatView: View {
LazyVStack(alignment: .leading, spacing: .huiSpaces.space16) {
ForEach(viewModel.messages) { message in
AssistChatMessageView(message: message)
.id(message.id)
.id(message.id.uuidString)
.transition(.scaleAndFade)
}
.animation(.smooth, value: viewModel.isRetryButtonVisible)
.animation(.smooth, value: viewModel.messages)
if viewModel.isRetryButtonVisible {
Button {
viewModel.retry()
Expand All @@ -95,8 +92,12 @@ struct AssistChatView: View {
.id(retryViewId)
}
}
.onAppear {
viewModel.scrollViewProxy = scrollViewProxy
.onReceive(viewModel.showMoreButtonPublisher) { id in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
withAnimation {
scrollViewProxy.scrollTo(id, anchor: .top)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import Combine
import Core
import SwiftUI
import Observation
import Foundation
import CombineSchedulers

@Observable
Expand All @@ -33,10 +34,10 @@ final class AssistChatViewModel {
private(set) var messages: [AssistChatMessageViewModel] = []
private(set) var isBackButtonVisible: Bool = false
private(set) var shouldOpenKeyboardPublisher = PassthroughSubject<Bool, Never>()
private(set) var showMoreButtonPublisher = PassthroughSubject<String, Never>()
private(set) var isLoaderVisible = false
private(set) var isRetryButtonVisible = false
private let scheduler: AnySchedulerOf<DispatchQueue>
var scrollViewProxy: ScrollViewProxy?
private(set) var state: InstUI.ScreenState = .data
var isDisableSendButton: Bool {
message.trimmed().isEmpty || !canSendMessage
Expand All @@ -53,7 +54,6 @@ final class AssistChatViewModel {

// MARK: - Private

private let animationDuration = 0.35
private var chatMessages: [AssistChatMessage] = []
private var dispatchWorkItem: DispatchWorkItem?
private var canSendMessage: Bool = true
Expand Down Expand Up @@ -144,6 +144,10 @@ final class AssistChatViewModel {
self.message = ""
}

func scrollToBottom() {
showMoreButtonPublisher.send(messages.last?.id.uuidString ?? "")
}

// MARK: - Private

/// handle the response from the interactor
Expand Down Expand Up @@ -201,40 +205,22 @@ final class AssistChatViewModel {

/// add new messages to the list of messages
private func add(newMessages: [AssistChatMessageViewModel]) {
withAnimation(.easeInOut(duration: animationDuration)) { [weak self] in
guard let self = self else { return }

newMessages.filter { newMessage in
!self.messages.contains { message in
message.id == newMessage.id
}
}.forEach { message in
self.messages.append(message)
}
}
}

/// animate the addition of a message and scroll to the bottom of the list after the animation completes
private func withAnimationAndScrollToBottom(_ block: @escaping () -> Void) {
withAnimation(.easeInOut(duration: animationDuration)) {
block()
}
DispatchQueue.main.asyncAfter(deadline: .now() + animationDuration) { [weak self] in
if let lastMessage = self?.messages.last {
self?.scrollViewProxy?.scrollTo(lastMessage.id, anchor: .bottom)
newMessages.filter { newMessage in
!self.messages.contains { message in
message.id == newMessage.id
}
}.forEach { message in
self.messages.append(message)
}
showMoreButtonPublisher.send(newMessages.last?.id.uuidString ?? "")
}

/// remove any messages that are not in the new list of messages returned from the interactor
private func remove(notAppearingIn newMessages: [AssistChatMessageViewModel]) {
withAnimationAndScrollToBottom { [weak self] in
guard let self = self else { return }
self.messages.removeAll { message in
!newMessages.contains { newMessage in
message.id == newMessage.id
} || message.isLoading
}
messages.removeAll { message in
!newMessages.contains { newMessage in
message.id == newMessage.id
} || message.isLoading
}
}
}
Expand Down