diff --git a/yobble/Resources/Localizable.xcstrings b/yobble/Resources/Localizable.xcstrings index e63ed04..8b59e96 100644 --- a/yobble/Resources/Localizable.xcstrings +++ b/yobble/Resources/Localizable.xcstrings @@ -280,6 +280,9 @@ "Блокировка контакта \"%1$@\" появится позже." : { "comment" : "Contacts block placeholder message" }, + "Больше сообщений нет" : { + "comment" : "Chat history top reached" + }, "Бот" : { "comment" : "Тип сессии — бот" }, diff --git a/yobble/ViewModels/PrivateChatViewModel.swift b/yobble/ViewModels/PrivateChatViewModel.swift index b1ebc77..e4766ff 100644 --- a/yobble/ViewModels/PrivateChatViewModel.swift +++ b/yobble/ViewModels/PrivateChatViewModel.swift @@ -7,13 +7,13 @@ final class PrivateChatViewModel: ObservableObject { @Published private(set) var isLoadingMore: Bool = false @Published var errorMessage: String? @Published private(set) var isSending: Bool = false + @Published private(set) var hasMore: Bool = true private let chatService: ChatService private let chatId: String private let currentUserId: String? private let pageSize: Int private let maxMessageLength: Int = 4096 - private var hasMore: Bool = true private var didLoadInitially: Bool = false private var messageObserver: NSObjectProtocol? @@ -127,11 +127,21 @@ final class PrivateChatViewModel: ObservableObject { func loadMoreIfNeeded(for message: MessageItem) { guard didLoadInitially, !isInitialLoading, hasMore, !isLoadingMore else { return } - guard let first = messages.first, first.id == message.id else { return } + + guard let messageIndex = messages.firstIndex(where: { $0.id == message.id }) else { + return + } + + let threshold = 10 + guard messageIndex < threshold else { + return + } + + guard let oldestMessage = messages.first else { return } isLoadingMore = true - chatService.fetchPrivateChatHistory(chatId: chatId, beforeMessageId: message.id, limit: pageSize) { [weak self] result in + chatService.fetchPrivateChatHistory(chatId: chatId, beforeMessageId: oldestMessage.id, limit: pageSize) { [weak self] result in guard let self else { return } switch result { diff --git a/yobble/Views/Chat/PrivateChatView.swift b/yobble/Views/Chat/PrivateChatView.swift index f478c3c..cb3a2e2 100644 --- a/yobble/Views/Chat/PrivateChatView.swift +++ b/yobble/Views/Chat/PrivateChatView.swift @@ -153,6 +153,9 @@ struct PrivateChatView: View { if viewModel.isLoadingMore { loadingMoreView .scaleEffect(x: 1, y: -1, anchor: .center) + } else if !viewModel.hasMore && !viewModel.messages.isEmpty { + noMoreMessagesView + .scaleEffect(x: 1, y: -1, anchor: .center) } else if viewModel.messages.isEmpty { emptyState .scaleEffect(x: 1, y: -1, anchor: .center) @@ -187,6 +190,14 @@ struct PrivateChatView: View { .padding(.vertical, 8) } + private var noMoreMessagesView: some View { + Text(NSLocalizedString("Больше сообщений нет", comment: "Chat history top reached")) + .font(.caption) + .foregroundColor(.secondary) + .padding(.vertical, 16) + .frame(maxWidth: .infinity) + } + private func errorView(message: String) -> some View { VStack(spacing: 12) { Text(message)