kunfei 4 лет назад
Родитель
Сommit
d551591348

+ 18 - 11
app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt

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

+ 7 - 7
app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt

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

+ 0 - 45
app/src/main/java/io/legado/app/ui/book/manage/ComposeView.kt

@@ -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()
-                    )
-                }
-            }
-        )
-    }
-}