From 30827d5815d02552919c2314da62ebbabe856cbb Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Fri, 6 Dec 2019 10:28:42 +0000 Subject: [PATCH] #1173 Do not filter out files over 4GB when storage is fat32, Grey them out instead --- .../zim_manager/ZimManageViewModel.kt | 30 +-- .../library_view/adapter/LibraryListItem.kt | 24 ++- .../library_view/adapter/LibraryViewHolder.kt | 7 +- app/src/main/res/layout/item_library.xml | 172 +++++++++--------- .../zim_manager/ZimManageViewModelTest.kt | 14 +- .../adapter/LibraryListItemTest.kt | 81 +++++++++ 6 files changed, 214 insertions(+), 114 deletions(-) create mode 100644 app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItemTest.kt 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 a65470a50..7cce3e810 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 @@ -32,6 +32,7 @@ import io.reactivex.processors.PublishProcessor import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.StorageObserver +import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao @@ -50,10 +51,6 @@ import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.SelectionMode.NORM import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState -import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CanWrite4GbFile -import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CannotWrite4GbFile -import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.NotEnoughSpaceFor4GbFile -import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.Unknown import org.kiwix.kiwixmobile.zim_manager.NetworkState.CONNECTED import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.MultiModeFinished import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestDeleteMultiSelection @@ -66,7 +63,6 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.DeleteFiles import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.None import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.OpenFile import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.ShareFiles -import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.StartMultiSelection import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem @@ -354,14 +350,6 @@ class ZimManageViewModel @Inject constructor( val booksUnfilteredByLanguage = applyUserFilter( libraryNetworkEntity.books - .filter { - when (fileSystemState) { - Unknown, - CannotWrite4GbFile -> isLessThan4GB(it) - NotEnoughSpaceFor4GbFile, - CanWrite4GbFile -> true - } - } .filterNot { downloadedBooksIds.contains(it.id) } .filterNot { downloadingBookIds.contains(it.id) }, filter @@ -370,30 +358,29 @@ class ZimManageViewModel @Inject constructor( return listOf( *createLibrarySection( booksUnfilteredByLanguage.filter { activeLanguageCodes.contains(it.language) }, + fileSystemState, R.string.your_languages, Long.MAX_VALUE ), *createLibrarySection( booksUnfilteredByLanguage.filterNot { activeLanguageCodes.contains(it.language) }, + fileSystemState, R.string.other_languages, Long.MIN_VALUE ) ) } - private fun isLessThan4GB(it: Book) = - it.size.toLongOrNull() ?: 0L < - Fat32Checker.FOUR_GIGABYTES_IN_KILOBYTES - private fun createLibrarySection( books: List, + fileSystemState: FileSystemState, sectionStringId: Int, sectionId: Long ) = if (books.isNotEmpty()) arrayOf( DividerItem(sectionId, context.getString(sectionStringId)), - *toBookItems(books) + *toBookItems(books, fileSystemState) ) else emptyArray() @@ -407,8 +394,11 @@ class ZimManageViewModel @Inject constructor( booksUnfilteredByLanguage.filter { it.searchMatches > 0 } } - private fun toBookItems(books: List) = - books.map { BookItem(it) }.toTypedArray() + private fun toBookItems( + books: List, + fileSystemState: FileSystemState + ) = + books.map { BookItem(it, fileSystemState) }.toTypedArray() private fun checkFileSystemForBooksOnRequest(booksFromDao: Flowable>) = requestFileSystemCheck 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 a39b2b296..477b3ced8 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 @@ -19,6 +19,12 @@ package org.kiwix.kiwixmobile.zim_manager.library_view.adapter import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CanWrite4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CannotWrite4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.NotEnoughSpaceFor4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.Unknown sealed class LibraryListItem { abstract val id: Long @@ -28,8 +34,22 @@ sealed class LibraryListItem { val text: String ) : LibraryListItem() - data class BookItem( + data class BookItem constructor( val book: Book, + val canBeDownloaded: Boolean, override val id: Long = book.id.hashCode().toLong() - ) : LibraryListItem() + ) : LibraryListItem() { + constructor(book: Book, fileSystemState: FileSystemState) : this( + book, + when (fileSystemState) { + Unknown, CannotWrite4GbFile -> book.isLessThan4GB() + NotEnoughSpaceFor4GbFile, CanWrite4GbFile -> true + } + ) + + companion object { + private fun Book.isLessThan4GB() = + size.toLongOrNull() ?: 0L < Fat32Checker.FOUR_GIGABYTES_IN_KILOBYTES + } + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt index fa48321a2..f686375d1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryViewHolder.kt @@ -19,7 +19,6 @@ package org.kiwix.kiwixmobile.zim_manager.library_view.adapter import android.view.View -import kotlinx.android.synthetic.main.library_divider.divider_text import kotlinx.android.synthetic.main.item_library.creator import kotlinx.android.synthetic.main.item_library.date import kotlinx.android.synthetic.main.item_library.description @@ -29,14 +28,16 @@ import kotlinx.android.synthetic.main.item_library.language import kotlinx.android.synthetic.main.item_library.publisher import kotlinx.android.synthetic.main.item_library.size import kotlinx.android.synthetic.main.item_library.title +import kotlinx.android.synthetic.main.item_library.unableToDownload +import kotlinx.android.synthetic.main.library_divider.divider_text import org.kiwix.kiwixmobile.core.CoreApp +import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder import org.kiwix.kiwixmobile.core.downloader.model.Base64String import org.kiwix.kiwixmobile.core.extensions.setBitmap import org.kiwix.kiwixmobile.core.extensions.setTextAndVisibility import org.kiwix.kiwixmobile.core.utils.BookUtils import org.kiwix.kiwixmobile.core.utils.NetworkUtils import org.kiwix.kiwixmobile.core.zim_manager.KiloByte -import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.DividerItem @@ -64,6 +65,8 @@ sealed class LibraryViewHolder(containerView: View) : containerView.setOnClickListener { clickAction.invoke(item) } + unableToDownload.setOnTouchListener { _, _ -> true } + unableToDownload.visibility = if (item.canBeDownloaded) View.GONE else View.VISIBLE } } diff --git a/app/src/main/res/layout/item_library.xml b/app/src/main/res/layout/item_library.xml index b8126d968..7f4eb00d9 100644 --- a/app/src/main/res/layout/item_library.xml +++ b/app/src/main/res/layout/item_library.xml @@ -1,115 +1,115 @@ - + tools:ignore="Overdraw, RTLHardcoded"> - + android:layout_marginLeft="@dimen/favicon_margin_right" + android:layout_marginTop="@dimen/activity_horizontal_margin" + app:layout_constraintStart_toEndOf="@+id/favicon" + app:layout_constraintTop_toTopOf="parent" + tools:text="Title" /> - + - + - + - + - - + - + - + - + - - - - - - - - - - + diff --git a/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModelTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModelTest.kt index f4bd49d33..68f78d2ba 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModelTest.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/ZimManageViewModelTest.kt @@ -392,15 +392,16 @@ class ZimManageViewModelTest { .assertValue( listOf( LibraryListItem.DividerItem(Long.MAX_VALUE, "1"), - LibraryListItem.BookItem(bookWithActiveLanguage), + LibraryListItem.BookItem(bookWithActiveLanguage, true), LibraryListItem.DividerItem(Long.MIN_VALUE, "2"), - LibraryListItem.BookItem(bookWithInactiveLanguage) + LibraryListItem.BookItem(bookWithInactiveLanguage, true) ) ) } @Test - fun `library filters out files over 4GB if file system state says to`() { + fun `library marks files over 4GB as can't download if file system state says to`() { + every { application.getString(R.string.other_languages) } returns "2" val bookOver4Gb = book( id = "0", url = "", @@ -423,6 +424,11 @@ class ZimManageViewModelTest { testScheduler.advanceTimeBy(500, MILLISECONDS) testScheduler.triggerActions() viewModel.libraryItems.test() - .assertValue(listOf()) + .assertValue( + listOf( + LibraryListItem.DividerItem(Long.MIN_VALUE, "2"), + LibraryListItem.BookItem(bookOver4Gb, false) + ) + ) } } diff --git a/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItemTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItemTest.kt new file mode 100644 index 000000000..b84b7c8eb --- /dev/null +++ b/app/src/test/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItemTest.kt @@ -0,0 +1,81 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.zim_manager.library_view.adapter + +import io.mockk.clearAllMocks +import io.mockk.every +import io.mockk.mockk +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CanWrite4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CannotWrite4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.NotEnoughSpaceFor4GbFile +import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.Unknown +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.BookItem + +internal class LibraryListItemTest { + + val book = mockk() + + @BeforeEach + fun init() { + clearAllMocks() + every { book.getId() } returns "0" + every { book.getSize() } returns "0" + } + + @Test + internal fun `Unknown file system state files under 4GB can be downloaded`() { + assertThat(canBeDownloaded(book, Unknown)).isTrue() + } + + @Test + internal fun `Unknown file system state greater than 4GB can't be downloaded`() { + every { book.getSize() } returns (Fat32Checker.FOUR_GIGABYTES_IN_KILOBYTES + 1).toString() + assertThat(canBeDownloaded(book, Unknown)).isFalse() + } + + @Test + internal fun `Unknown file system state empty size can be downloaded`() { + every { book.getSize() } returns "" + assertThat(canBeDownloaded(book, Unknown)).isTrue() + } + + @Test + internal fun `CannotWrite4GB file system state can be downloaded`() { + assertThat(canBeDownloaded(book, CannotWrite4GbFile)).isTrue() + } + + @Test + internal fun `CanWrite4GbFile file system state can be downloaded`() { + assertThat(canBeDownloaded(book, CanWrite4GbFile)).isTrue() + } + + @Test + internal fun `NotEnoughSpaceFor4GbFile file system state can be downloaded`() { + assertThat(canBeDownloaded(book, NotEnoughSpaceFor4GbFile)).isTrue() + } + + private fun canBeDownloaded(book: Book, fileSystemState: FileSystemState) = + BookItem(book, fileSystemState).canBeDownloaded +}