Add pagination to media and document lists

This commit is contained in:
Benoit Martins 2026-02-12 12:00:52 +01:00
parent 304651d776
commit cf97da11b1
5 changed files with 80 additions and 38 deletions

View file

@ -2,6 +2,6 @@ import Foundation
public enum AppGitInfo {
public static let branch = "master"
public static let commit = "b753b5925"
public static let commit = "304651d77"
public static let tag = "6.1.0-alpha"
}

View file

@ -75,6 +75,11 @@ struct ConversationDocumentsListFragment: View {
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.onAppear {
if file == conversationDocumentsListViewModel.documentsList.last {
conversationDocumentsListViewModel.loadMoreData(totalItemsCount: conversationDocumentsListViewModel.documentsList.count)
}
}
}
}
.safeAreaInset(edge: .top, content: {
@ -98,6 +103,11 @@ struct ConversationDocumentsListFragment: View {
.frame(maxWidth: .infinity)
}
.background(Color.gray100)
if conversationDocumentsListViewModel.operationInProgress {
PopupLoadingView()
.background(.black.opacity(0.65))
}
}
.navigationTitle("")
.navigationBarHidden(true)

View file

@ -69,6 +69,11 @@ struct ConversationMediaListFragment: View {
ConversationMediaGridView(viewModel: conversationMediaListViewModel)
}
.background(Color.gray100)
if conversationMediaListViewModel.operationInProgress {
PopupLoadingView()
.background(.black.opacity(0.65))
}
}
.navigationTitle("")
.navigationBarHidden(true)
@ -92,7 +97,7 @@ struct ConversationMediaGridView: View {
var body: some View {
VStack(spacing: 0) {
if !viewModel.mediaList.isEmpty && !viewModel.operationInProgress {
if !viewModel.mediaList.isEmpty {
GeometryReader { geometry in
let totalSpacing = spacing * CGFloat(columns - 1)
let itemWidth = (geometry.size.width - totalSpacing) / CGFloat(columns)
@ -128,6 +133,8 @@ struct ConversationMediaGridView: View {
.multilineTextAlignment(.center)
.default_text_style_800(styleSize: 16)
Spacer()
} else {
Spacer()
}
}
}

View file

@ -41,30 +41,46 @@ final class ConversationDocumentsListViewModel: ObservableObject {
// MARK: - Loading
private func loadDocumentsList() {
operationInProgress = true
totalDocumentsCount = self.conversationModel.chatRoom.documentContentsSize
let contentsToLoad = min(totalDocumentsCount, Self.CONTENTS_PER_PAGE)
let contents = self.conversationModel.chatRoom.getDocumentContentsRange(begin: 0, end: contentsToLoad)
documentsList = getFileModelsList(from: contents)
operationInProgress = false
self.operationInProgress = true
CoreContext.shared.doOnCoreQueue { _ in
self.totalDocumentsCount = self.conversationModel.chatRoom.documentContentsSize
let contentsToLoad = min(self.totalDocumentsCount, Self.CONTENTS_PER_PAGE)
let contents = self.conversationModel.chatRoom.getDocumentContentsRange(begin: 0, end: contentsToLoad)
let documentsListTmp = self.getFileModelsList(from: contents)
DispatchQueue.main.async {
self.documentsList = documentsListTmp
self.operationInProgress = false
}
}
}
func loadMoreData(totalItemsCount: Int) {
guard totalItemsCount < totalDocumentsCount else { return }
var upperBound = totalItemsCount + Self.CONTENTS_PER_PAGE
if upperBound > totalDocumentsCount {
upperBound = totalDocumentsCount
}
let contents = self.conversationModel.chatRoom.getDocumentContentsRange(begin: totalItemsCount, end: upperBound)
let newModels = getFileModelsList(from: contents)
DispatchQueue.main.async {
self.documentsList.append(contentsOf: newModels)
self.operationInProgress = true
CoreContext.shared.doOnCoreQueue { _ in
guard totalItemsCount < self.totalDocumentsCount else {
DispatchQueue.main.async {
self.operationInProgress = false
}
return
}
var upperBound = totalItemsCount + Self.CONTENTS_PER_PAGE
if upperBound > self.totalDocumentsCount {
upperBound = self.totalDocumentsCount
}
let contents = self.conversationModel.chatRoom.getDocumentContentsRange(begin: totalItemsCount, end: upperBound)
let newModels = self.getFileModelsList(from: contents)
DispatchQueue.main.async {
self.documentsList.append(contentsOf: newModels)
self.operationInProgress = false
}
}
}
// MARK: - Mapping Content -> FileModel

View file

@ -42,28 +42,36 @@ final class ConversationMediaListViewModel: ObservableObject {
// MARK: - Loading
private func loadMediaList() {
operationInProgress = true
Log.info("\(Self.TAG) Loading media contents for conversation \(conversationModel.chatRoom.identifier ?? "No ID")")
totalMediaCount = conversationModel.chatRoom.mediaContentsSize
Log.info("\(Self.TAG) Media contents size is [\(totalMediaCount)]")
let contentsToLoad = min(totalMediaCount, Self.CONTENTS_PER_PAGE)
let contents = conversationModel.chatRoom.getMediaContentsRange(begin: 0, end: contentsToLoad)
Log.info("\(Self.TAG) \(contents.count) media have been fetched")
DispatchQueue.main.async {
self.mediaList = self.getFileModelsList(from: contents)
self.operationInProgress = false
self.operationInProgress = true
CoreContext.shared.doOnCoreQueue { _ in
Log.info("\(Self.TAG) Loading media contents for conversation \(self.conversationModel.chatRoom.identifier ?? "No ID")")
self.totalMediaCount = self.conversationModel.chatRoom.mediaContentsSize
Log.info("\(Self.TAG) Media contents size is [\(self.totalMediaCount)]")
let contentsToLoad = min(self.totalMediaCount, Self.CONTENTS_PER_PAGE)
let contents = self.conversationModel.chatRoom.getMediaContentsRange(begin: 0, end: contentsToLoad)
Log.info("\(Self.TAG) \(contents.count) media have been fetched")
DispatchQueue.main.async {
self.mediaList = self.getFileModelsList(from: contents)
self.operationInProgress = false
}
}
}
func loadMoreData(totalItemsCount: Int) {
CoreContext.shared.doOnCoreQueue { core in
self.operationInProgress = true
CoreContext.shared.doOnCoreQueue { _ in
Log.info("\(Self.TAG) Loading more data, current total is \(totalItemsCount), max size is \(self.totalMediaCount)")
guard totalItemsCount < self.totalMediaCount else { return }
guard totalItemsCount < self.totalMediaCount else {
DispatchQueue.main.async {
self.operationInProgress = false
}
return
}
var upperBound = totalItemsCount + Self.CONTENTS_PER_PAGE
if upperBound > self.totalMediaCount {
@ -77,6 +85,7 @@ final class ConversationMediaListViewModel: ObservableObject {
DispatchQueue.main.async {
self.mediaList.append(contentsOf: newModels)
self.operationInProgress = false
}
}
}