Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1253 read action mode for sharing and deleting zim files on disk #1254

Merged
merged 4 commits into from
Jul 8, 2019
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
2 changes: 1 addition & 1 deletion app/objectbox-models/default.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
},
{
"id": "2:6862771806221961183",
"name": "zimID"
"name": "zimId"
},
{
"id": "3:4312769031500860715",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dagger.Subcomponent
import org.kiwix.kiwixmobile.di.modules.ActivityModule
import org.kiwix.kiwixmobile.downloader.DownloadFragment
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.DeleteFiles
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment

@Subcomponent(modules = [ActivityModule::class])
Expand All @@ -33,6 +34,8 @@ interface ActivityComponent {

fun inject(zimFileSelectFragment: ZimFileSelectFragment)

fun inject(deleteFiles: DeleteFiles)

@Subcomponent.Builder
interface Builder {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.kiwix.kiwixmobile.extensions

import android.app.Activity
import android.view.ActionMode
import android.view.ActionMode.Callback
import android.view.Menu
import android.view.MenuItem

fun Activity.startActionMode(
menuId: Int,
idsToClickActions: Map<Int, () -> Any>,
onDestroyAction: () -> Unit
): ActionMode? {
return startActionMode(object : Callback {
override fun onActionItemClicked(
mode: ActionMode,
item: MenuItem
) = idsToClickActions[item.itemId]?.let {
it()
mode.finish()
true
} ?: false

override fun onCreateActionMode(
mode: ActionMode,
menu: Menu?
): Boolean {
mode.getMenuInflater()
.inflate(menuId, menu)
return true
}

override fun onPrepareActionMode(
mode: ActionMode?,
menu: Menu?
) = false

override fun onDestroyActionMode(mode: ActionMode?) {
onDestroyAction()
}

})
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.text.SpannableString;
Expand Down Expand Up @@ -100,11 +99,9 @@
import org.kiwix.kiwixmobile.bookmark.BookmarkItem;
import org.kiwix.kiwixmobile.bookmark.BookmarksActivity;
import org.kiwix.kiwixmobile.data.ZimContentProvider;
import org.kiwix.kiwixmobile.data.local.entity.Bookmark;
import org.kiwix.kiwixmobile.help.HelpActivity;
import org.kiwix.kiwixmobile.history.HistoryActivity;
import org.kiwix.kiwixmobile.history.HistoryListItem;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
import org.kiwix.kiwixmobile.search.SearchActivity;
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
import org.kiwix.kiwixmobile.utils.DimenUtils;
Expand Down Expand Up @@ -377,6 +374,7 @@ public void onCreate(Bundle savedInstanceState) {
open(bookOnDiskItem);
return Unit.INSTANCE;
},
null,
null),
BookOnDiskDelegate.LanguageDelegate.INSTANCE
);
Expand Down Expand Up @@ -2105,7 +2103,7 @@ public void open(BooksOnDiskListItem.BookOnDisk bookOnDisk) {

@Override
public void addBooks(List<BooksOnDiskListItem> books) {
booksAdapter.setItemList(books);
booksAdapter.setItems(books);
}

private void searchFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,24 @@ import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CanWrite4G
import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.CannotWrite4GbFile
import org.kiwix.kiwixmobile.zim_manager.Fat32Checker.FileSystemState.NotEnoughSpaceFor4GbFile
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
import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestMultiSelection
import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestOpen
import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestSelect
import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestShareMultiSelection
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.FileSelectListState
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.SelectionMode.MULTI
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.SelectionMode.NORMAL
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
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.zim_manager.fileselect_view.effects.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
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem.DividerItem
Expand All @@ -79,16 +94,26 @@ class ZimManageViewModel @Inject constructor(
private val defaultLanguageProvider: DefaultLanguageProvider,
private val dataSource: DataSource
) : ViewModel() {
sealed class FileSelectActions {
data class RequestOpen(val bookOnDisk: BookOnDisk) : FileSelectActions()
data class RequestSelect(val bookOnDisk: BookOnDisk) : FileSelectActions()
data class RequestMultiSelection(val bookOnDisk: BookOnDisk) : FileSelectActions()
object RequestDeleteMultiSelection : FileSelectActions()
object RequestShareMultiSelection : FileSelectActions()
object MultiModeFinished : FileSelectActions()
}

val sideEffects = PublishProcessor.create<SideEffect<out Any?>>()
val libraryItems: MutableLiveData<List<LibraryListItem>> = MutableLiveData()
val downloadItems: MutableLiveData<List<DownloadItem>> = MutableLiveData()
val bookItems: MutableLiveData<List<BooksOnDiskListItem>> = MutableLiveData()
val fileSelectListStates: MutableLiveData<FileSelectListState> = MutableLiveData()
val deviceListIsRefreshing = MutableLiveData<Boolean>()
val libraryListIsRefreshing = MutableLiveData<Boolean>()
val networkStates = MutableLiveData<NetworkState>()
val languageItems = MutableLiveData<List<Language>>()

val requestFileSystemCheck = PublishProcessor.create<Unit>()
val fileSelectActions = PublishProcessor.create<FileSelectActions>()
val requestDownloadLibrary = BehaviorProcessor.createDefault<Unit>(Unit)
val requestFiltering = BehaviorProcessor.createDefault<String>("")
val requestLanguagesDialog = PublishProcessor.create<Unit>()
Expand Down Expand Up @@ -127,8 +152,72 @@ class ZimManageViewModel @Inject constructor(
updateLanguagesInDao(networkLibrary, languages),
updateNetworkStates(),
updateLanguageItemsForDialog(languages),
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary)
requestsAndConnectivtyChangesToLibraryRequests(networkLibrary),
fileSelectActions()
)
}

private fun fileSelectActions() = fileSelectActions.subscribe({
sideEffects.offer(
when (it) {
is RequestOpen -> OpenFile(it.bookOnDisk)
is RequestMultiSelection -> startMultiSelectionAndSelectBook(it.bookOnDisk)
RequestDeleteMultiSelection -> DeleteFiles(selectionsFromState())
RequestShareMultiSelection -> ShareFiles(selectionsFromState())
MultiModeFinished -> noSideEffectAndClearSelectionState()
is RequestSelect -> noSideEffectSelectBook(it.bookOnDisk)
}
)
}, Throwable::printStackTrace)

private fun startMultiSelectionAndSelectBook(
bookOnDisk: BookOnDisk
): StartMultiSelection {
fileSelectListStates.value?.let {
fileSelectListStates.postValue(
it.copy(
bookOnDiskListItems = selectBook(it, bookOnDisk),
selectionMode = MULTI
)
)
}
return StartMultiSelection(bookOnDisk, fileSelectActions)
}

private fun selectBook(
it: FileSelectListState,
bookOnDisk: BookOnDisk
): List<BooksOnDiskListItem> {
return it.bookOnDiskListItems.map { listItem ->
if (listItem.id == bookOnDisk.id) listItem.apply { isSelected = !isSelected }
else listItem
}
}

private fun noSideEffectSelectBook(bookOnDisk: BookOnDisk): SideEffect<Unit> {
fileSelectListStates.value?.let {
fileSelectListStates.postValue(
it.copy(bookOnDiskListItems = it.bookOnDiskListItems.map { listItem ->
if (listItem.id == bookOnDisk.id) listItem.apply { isSelected = !isSelected }
else listItem
})
)
}
return None
}

private fun selectionsFromState() = fileSelectListStates.value?.selectedBooks ?: emptyList()

private fun noSideEffectAndClearSelectionState(): SideEffect<Unit> {
fileSelectListStates.value?.let {
fileSelectListStates.postValue(
it.copy(
bookOnDiskListItems = it.bookOnDiskListItems.map { it.apply { isSelected = false } },
selectionMode = NORMAL
)
)
}
return None
}

private fun requestsAndConnectivtyChangesToLibraryRequests(library: PublishProcessor<LibraryNetworkEntity>) =
Expand Down Expand Up @@ -411,10 +500,27 @@ class ZimManageViewModel @Inject constructor(
private fun updateBookItems() =
dataSource.booksOnDiskAsListItems()
.subscribe(
bookItems::postValue,
{ newList ->
fileSelectListStates.postValue(
fileSelectListStates.value?.let { inheritSelections(it, newList) }
?: FileSelectListState(newList)
)
},
Throwable::printStackTrace
)

private fun inheritSelections(
oldState: FileSelectListState,
newList: MutableList<BooksOnDiskListItem>
): FileSelectListState {
return oldState.copy(
bookOnDiskListItems = newList.map { newBookOnDisk ->
val firstOrNull =
oldState.bookOnDiskListItems.firstOrNull { oldBookOnDisk -> oldBookOnDisk.id == newBookOnDisk.id }
newBookOnDisk.apply { isSelected = firstOrNull?.isSelected ?: false }
})
}

private fun removeCompletedDownloadsFromDb(downloadStatuses: Flowable<List<DownloadStatus>>) =
downloadStatuses
.observeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile.zim_manager.fileselect_view

import org.kiwix.kiwixmobile.zim_manager.fileselect_view.SelectionMode.NORMAL
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk

data class FileSelectListState(
val bookOnDiskListItems: List<BooksOnDiskListItem>,
val selectionMode: SelectionMode = NORMAL
) {
val selectedBooks by lazy {
bookOnDiskListItems.filter { it.isSelected }.filterIsInstance(BookOnDisk::class.java)
}

}

enum class SelectionMode {
NORMAL,
MULTI
}
Loading