Skip to content

Commit

Permalink
Use a single I/O thread when deleting multiple notebooks
Browse files Browse the repository at this point in the history
This resolves issue #315.
  • Loading branch information
amberin committed Sep 1, 2024
1 parent 26e8126 commit 9d3c37b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@ class BooksFragment : CommonFragment(), DrawerItem, OnViewHolderClickListener<Bo
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
val deleteLinked = dialogBinding.deleteLinkedCheckbox.isChecked
for (book in books) {
viewModel.deleteBook(book.book.id, deleteLinked)
}
val bookIds = books.map { it.book.id }.toSet()
viewModel.deleteBooks(bookIds, deleteLinked)
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ import com.orgzly.android.db.entity.Repo
import com.orgzly.android.ui.AppBar
import com.orgzly.android.ui.CommonViewModel
import com.orgzly.android.ui.SingleLiveEvent
import com.orgzly.android.usecase.*
import com.orgzly.android.usecase.BookCreate
import com.orgzly.android.usecase.BookDelete
import com.orgzly.android.usecase.BookExportToUri
import com.orgzly.android.usecase.BookForceLoad
import com.orgzly.android.usecase.BookForceSave
import com.orgzly.android.usecase.BookImportFromUri
import com.orgzly.android.usecase.BookLinkUpdate
import com.orgzly.android.usecase.BookRename
import com.orgzly.android.usecase.UseCaseResult
import com.orgzly.android.usecase.UseCaseRunner
import com.orgzly.android.util.LogUtils


Expand Down Expand Up @@ -70,10 +79,10 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod
}
}

fun deleteBook(bookId: Long, deleteLinked: Boolean) {
fun deleteBooks(bookIds: Set<Long>, deleteLinked: Boolean) {
App.EXECUTORS.diskIO().execute {
catchAndPostError {
val result = UseCaseRunner.run(BookDelete(bookId, deleteLinked))
val result = UseCaseRunner.run(BookDelete(bookIds, deleteLinked))
bookDeletedEvent.postValue(result)
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/orgzly/android/usecase/BookDelete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.orgzly.android.usecase

import com.orgzly.android.data.DataRepository

class BookDelete(val bookId: Long, val deleteLinked: Boolean) : UseCase() {
class BookDelete(val bookIds: Set<Long>, val deleteLinked: Boolean) : UseCase() {
override fun run(dataRepository: DataRepository): UseCaseResult {
val book = dataRepository.getBookView(bookId) ?: throw NotFound()

dataRepository.deleteBook(book, deleteLinked)

for (bookId in bookIds) {
val book = dataRepository.getBookView(bookId) ?: throw NotFound()
dataRepository.deleteBook(book, deleteLinked)
}
return UseCaseResult(
modifiesLocalData = true,
triggersSync = SYNC_DATA_MODIFIED
Expand Down

0 comments on commit 9d3c37b

Please sign in to comment.