diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModel.kt index 7cce3e810..7574ffb04 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModel.kt @@ -38,7 +38,6 @@ import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.data.remote.KiwixService -import org.kiwix.kiwixmobile.core.downloader.model.DownloadItem import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book @@ -67,6 +66,7 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.StartMultiSelec import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.DividerItem +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.LibraryDownloadItem import java.util.LinkedList import java.util.Locale import java.util.concurrent.TimeUnit.MILLISECONDS @@ -97,7 +97,6 @@ class ZimManageViewModel @Inject constructor( val sideEffects = PublishProcessor.create>() val libraryItems: MutableLiveData> = MutableLiveData() - val downloadItems: MutableLiveData> = MutableLiveData() val fileSelectListStates: MutableLiveData = MutableLiveData() val deviceListIsRefreshing = MutableLiveData() val libraryListIsRefreshing = MutableLiveData() @@ -132,7 +131,6 @@ class ZimManageViewModel @Inject constructor( val networkLibrary = PublishProcessor.create() val languages = languageDao.languages() return arrayOf( - updateDownloadItems(downloads), updateBookItems(), checkFileSystemForBooksOnRequest(booksFromDao), updateLibraryItems(booksFromDao, downloads, networkLibrary, languages), @@ -343,64 +341,65 @@ class ZimManageViewModel @Inject constructor( filter: String, fileSystemState: FileSystemState ): List { - val downloadedBooksIds = booksOnFileSystem.map { it.book.id } - val downloadingBookIds = activeDownloads.map { it.book.id } val activeLanguageCodes = allLanguages.filter(Language::active) .map(Language::languageCode) val booksUnfilteredByLanguage = - applyUserFilter( - libraryNetworkEntity.books - .filterNot { downloadedBooksIds.contains(it.id) } - .filterNot { downloadingBookIds.contains(it.id) }, + applySearchFilter( + libraryNetworkEntity.books - booksOnFileSystem.map(BookOnDisk::book), filter ) - return listOf( - *createLibrarySection( - booksUnfilteredByLanguage.filter { activeLanguageCodes.contains(it.language) }, - fileSystemState, - R.string.your_languages, - Long.MAX_VALUE - ), - *createLibrarySection( - booksUnfilteredByLanguage.filterNot { activeLanguageCodes.contains(it.language) }, + val booksWithActiveLanguages = + booksUnfilteredByLanguage.filter { activeLanguageCodes.contains(it.language) } + return createLibrarySection( + booksWithActiveLanguages, + activeDownloads, + fileSystemState, + R.string.your_languages, + Long.MAX_VALUE + ) + + createLibrarySection( + booksUnfilteredByLanguage - booksWithActiveLanguages, + activeDownloads, fileSystemState, R.string.other_languages, Long.MIN_VALUE ) - ) } private fun createLibrarySection( books: List, + activeDownloads: List, fileSystemState: FileSystemState, sectionStringId: Int, sectionId: Long ) = if (books.isNotEmpty()) - arrayOf( - DividerItem(sectionId, context.getString(sectionStringId)), - *toBookItems(books, fileSystemState) - ) - else emptyArray() + listOf(DividerItem(sectionId, context.getString(sectionStringId))) + + books.asLibraryItems(activeDownloads, fileSystemState) + else emptyList() - private fun applyUserFilter( - booksUnfilteredByLanguage: List, + private fun applySearchFilter( + unDownloadedBooks: List, filter: String ) = if (filter.isEmpty()) { - booksUnfilteredByLanguage + unDownloadedBooks } else { - booksUnfilteredByLanguage.forEach { it.calculateSearchMatches(filter, bookUtils) } - booksUnfilteredByLanguage.filter { it.searchMatches > 0 } + unDownloadedBooks.forEach { it.calculateSearchMatches(filter, bookUtils) } + unDownloadedBooks.filter { it.searchMatches > 0 } } - private fun toBookItems( - books: List, + private fun List.asLibraryItems( + activeDownloads: List, fileSystemState: FileSystemState - ) = - books.map { BookItem(it, fileSystemState) }.toTypedArray() + ) = map { book -> + activeDownloads.firstOrNull { download -> download.book == book } + ?.let(::LibraryDownloadItem) + ?: BookItem(book, fileSystemState) + } - private fun checkFileSystemForBooksOnRequest(booksFromDao: Flowable>) = + private fun checkFileSystemForBooksOnRequest(booksFromDao: Flowable>): + Disposable = requestFileSystemCheck .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) @@ -462,12 +461,4 @@ class ZimManageViewModel @Inject constructor( newBookOnDisk.apply { isSelected = firstOrNull?.isSelected ?: false } }) } - - private fun updateDownloadItems(downloadModels: Flowable>) = - downloadModels - .map { it.map(::DownloadItem) } - .subscribe( - downloadItems::postValue, - Throwable::printStackTrace - ) } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt index d49593bfb..10913c4d3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/LibraryFragment.kt @@ -49,7 +49,6 @@ import org.kiwix.kiwixmobile.core.utils.KiwixDialog.YesNoDialog.StopDownload import org.kiwix.kiwixmobile.core.utils.KiwixDialog.YesNoDialog.WifiOnly import org.kiwix.kiwixmobile.core.utils.NetworkUtils import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil -import org.kiwix.kiwixmobile.core.utils.TestingUtils import org.kiwix.kiwixmobile.zim_manager.NetworkState import org.kiwix.kiwixmobile.zim_manager.NetworkState.CONNECTED import org.kiwix.kiwixmobile.zim_manager.NetworkState.NOT_CONNECTED @@ -104,10 +103,7 @@ class LibraryFragment : BaseFragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - TestingUtils.bindResource(LibraryFragment::class.java) - return inflater.inflate(R.layout.activity_library, container, false) - } + ): View = inflater.inflate(R.layout.activity_library, container, false) override fun onViewCreated( view: View, @@ -154,7 +150,6 @@ class LibraryFragment : BaseFragment() { else string.no_items_msg ) libraryErrorText.visibility = VISIBLE - TestingUtils.unbindResource(LibraryFragment::class.java) } else { libraryErrorText.visibility = GONE } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt index 429eeaf35..b9884f7e5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt @@ -68,7 +68,7 @@ sealed class LibraryListItem { val progress: Int, val eta: Seconds, val downloadState: DownloadState, - override val id: Long = downloadId + override val id: Long ) : LibraryListItem() { val readableEta: CharSequence = eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: "" @@ -82,7 +82,8 @@ sealed class LibraryListItem { downloadModel.totalSizeOfDownload, downloadModel.progress, Seconds(downloadModel.etaInMilliSeconds / 1000L), - DownloadState.from(downloadModel.state, downloadModel.error) + DownloadState.from(downloadModel.state, downloadModel.error), + downloadModel.book.id.hashCode().toLong() ) } }