pull/2520/head
kunfei 2 years ago
parent a31ed9d631
commit 6040dfe059
  1. 29
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt
  3. 45
      app/src/main/java/io/legado/app/ui/book/manage/ComposeView.kt

@ -23,8 +23,8 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.book.group.GroupManageDialog import io.legado.app.ui.book.group.GroupManageDialog
import io.legado.app.ui.book.group.GroupSelectDialog import io.legado.app.ui.book.group.GroupSelectDialog
import io.legado.app.ui.theme.AppTheme
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.ui.widget.recycler.DragSelectTouchHelper import io.legado.app.ui.widget.recycler.DragSelectTouchHelper
import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.ItemTouchCallback
import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.ui.widget.recycler.VerticalDivider
@ -59,6 +59,7 @@ class BookshelfManageActivity :
private var menu: Menu? = null private var menu: Menu? = null
private var searchView: SearchView? = null private var searchView: SearchView? = null
private var books: List<Book>? = null private var books: List<Book>? = null
private val waitDialog by lazy { WaitDialog(this) }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.groupId = intent.getLongExtra("groupId", -1) viewModel.groupId = intent.getLongExtra("groupId", -1)
@ -75,6 +76,20 @@ class BookshelfManageActivity :
upBookDataByGroupId() upBookDataByGroupId()
} }
override fun observeLiveBus() {
viewModel.batchChangeSourceState.observe(this) {
if (it) {
waitDialog.setText(R.string.change_source_batch)
waitDialog.show()
} else {
waitDialog.dismiss()
}
}
viewModel.batchChangeSourceProcessLiveData.observe(this) {
waitDialog.setText(it)
}
}
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.bookshelf_manage, menu) menuInflater.inflate(R.menu.bookshelf_manage, menu)
initSearchView(menu) initSearchView(menu)
@ -150,16 +165,8 @@ class BookshelfManageActivity :
binding.selectActionBar.inflateMenu(R.menu.bookshelf_menage_sel) binding.selectActionBar.inflateMenu(R.menu.bookshelf_menage_sel)
binding.selectActionBar.setOnMenuItemClickListener(this) binding.selectActionBar.setOnMenuItemClickListener(this)
binding.selectActionBar.setCallBack(this) binding.selectActionBar.setCallBack(this)
binding.composeView.setContent { waitDialog.setOnCancelListener {
AppTheme { viewModel.batchChangeSourceCoroutine?.cancel()
BatchChangeSourceDialog(
state = viewModel.batchChangeSourceState,
size = viewModel.batchChangeSourceSize,
position = viewModel.batchChangeSourcePosition
) {
viewModel.batchChangeSourceCoroutine?.cancel()
}
}
} }
} }

@ -1,7 +1,7 @@
package io.legado.app.ui.book.manage package io.legado.app.ui.book.manage
import android.app.Application import android.app.Application
import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.appDb import io.legado.app.data.appDb
@ -17,9 +17,8 @@ import io.legado.app.utils.toastOnUi
class BookshelfManageViewModel(application: Application) : BaseViewModel(application) { class BookshelfManageViewModel(application: Application) : BaseViewModel(application) {
var groupId: Long = -1L var groupId: Long = -1L
var groupName: String? = null var groupName: String? = null
val batchChangeSourceState = mutableStateOf(false) val batchChangeSourceState = MutableLiveData<Boolean>()
val batchChangeSourceSize = mutableStateOf(0) val batchChangeSourceProcessLiveData = MutableLiveData<String>()
val batchChangeSourcePosition = mutableStateOf(0)
var batchChangeSourceCoroutine: Coroutine<Unit>? = null var batchChangeSourceCoroutine: Coroutine<Unit>? = null
fun upCanUpdate(books: List<Book>, canUpdate: Boolean) { fun upCanUpdate(books: List<Book>, canUpdate: Boolean) {
@ -46,9 +45,8 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
fun changeSource(books: List<Book>, source: BookSource) { fun changeSource(books: List<Book>, source: BookSource) {
batchChangeSourceCoroutine?.cancel() batchChangeSourceCoroutine?.cancel()
batchChangeSourceCoroutine = execute { batchChangeSourceCoroutine = execute {
batchChangeSourceSize.value = books.size
books.forEachIndexed { index, book -> books.forEachIndexed { index, book ->
batchChangeSourcePosition.value = index + 1 batchChangeSourceProcessLiveData.postValue("${index + 1}/${books.size}")
if (book.isLocal) return@forEachIndexed if (book.isLocal) return@forEachIndexed
if (book.origin == source.bookSourceUrl) return@forEachIndexed if (book.origin == source.bookSourceUrl) return@forEachIndexed
WebBook.preciseSearchAwait(this, source, book.name, book.author) WebBook.preciseSearchAwait(this, source, book.name, book.author)
@ -66,8 +64,10 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
} }
} }
} }
}.onStart {
batchChangeSourceState.postValue(true)
}.onFinally { }.onFinally {
batchChangeSourceState.value = false batchChangeSourceState.postValue(false)
} }
} }

@ -1,45 +0,0 @@
package io.legado.app.ui.book.manage
import androidx.compose.foundation.layout.Column
import androidx.compose.material.AlertDialog
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import io.legado.app.R
import splitties.init.appCtx
@Composable
fun BatchChangeSourceDialog(
state: MutableState<Boolean>,
size: MutableState<Int>,
position: MutableState<Int>,
cancel: () -> Unit
) {
if (state.value) {
AlertDialog(
onDismissRequest = { },
confirmButton = {
TextButton(onClick = {
cancel.invoke()
state.value = false
}, content = {
Text(text = appCtx.getString(R.string.cancel))
})
},
title = {
Text(text = appCtx.getString(R.string.change_source_batch))
},
text = {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(text = "${position.value}/${size.value}")
LinearProgressIndicator(
progress = position.value / size.value.toFloat()
)
}
}
)
}
}
Loading…
Cancel
Save