diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index 01b02eac5..5a381cf73 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -143,7 +143,7 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { get() = appCtx.getPrefBoolean(PreferKey.autoRefresh) var threadCount: Int - get() = appCtx.getPrefInt(PreferKey.threadCount, 16) + get() = appCtx.getPrefInt(PreferKey.threadCount, 8) set(value) { appCtx.putPrefInt(PreferKey.threadCount, value) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupEdit.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupEdit.kt new file mode 100644 index 000000000..9902ee15d --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupEdit.kt @@ -0,0 +1,54 @@ +package io.legado.app.ui.book.group + +import android.content.Context +import android.view.LayoutInflater +import io.legado.app.R +import io.legado.app.data.appDb +import io.legado.app.data.entities.BookGroup +import io.legado.app.databinding.DialogEditTextBinding +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.lib.dialogs.alert +import io.legado.app.utils.requestInputMethod + +object GroupEdit { + + fun show(context: Context, layoutInflater: LayoutInflater, bookGroup: BookGroup) = context.run { + alert(title = getString(R.string.group_edit)) { + val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { + editView.setHint(R.string.group_name) + editView.setText(bookGroup.groupName) + } + if (bookGroup.groupId >= 0) { + neutralButton(R.string.delete) { + deleteGroup(context, bookGroup) + } + } + customView { alertBinding.root } + yesButton { + alertBinding.editView.text?.toString()?.let { + Coroutine.async { + appDb.bookGroupDao.update(bookGroup) + } + } + } + noButton() + }.show().requestInputMethod() + } + + private fun deleteGroup(context: Context, bookGroup: BookGroup) = context.run { + alert(R.string.delete, R.string.sure_del) { + okButton { + Coroutine.async { + appDb.bookGroupDao.delete(bookGroup) + val books = appDb.bookDao.getBooksByGroup(bookGroup.groupId) + books.forEach { + it.group = it.group - bookGroup.groupId + } + appDb.bookDao.update(*books.toTypedArray()) + } + } + noButton() + }.show() + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index af5de0d90..ca6271a15 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -117,37 +117,6 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener }.show().requestInputMethod() } - @SuppressLint("InflateParams") - private fun editGroup(bookGroup: BookGroup) { - alert(title = getString(R.string.group_edit)) { - val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { - editView.setHint(R.string.group_name) - editView.setText(bookGroup.groupName) - } - if (bookGroup.groupId >= 0) { - neutralButton(R.string.delete) { - deleteGroup(bookGroup) - } - } - customView { alertBinding.root } - yesButton { - alertBinding.editView.text?.toString()?.let { - viewModel.upGroup(bookGroup.copy(groupName = it)) - } - } - noButton() - }.show().requestInputMethod() - } - - private fun deleteGroup(bookGroup: BookGroup) { - alert(R.string.delete, R.string.sure_del) { - okButton { - viewModel.delGroup(bookGroup) - } - noButton() - }.show() - } - private inner class GroupAdapter(context: Context) : RecyclerAdapter(context), ItemTouchCallback.Callback { @@ -173,7 +142,11 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener override fun registerListener(holder: ItemViewHolder, binding: ItemBookGroupManageBinding) { binding.run { - tvEdit.setOnClickListener { getItem(holder.layoutPosition)?.let { editGroup(it) } } + tvEdit.setOnClickListener { + getItem(holder.layoutPosition)?.let { bookGroup -> + GroupEdit.show(context, layoutInflater, bookGroup) + } + } swShow.setOnCheckedChangeListener { buttonView, isChecked -> if (buttonView.isPressed) { getItem(holder.layoutPosition)?.let { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt index aada477e8..6e433dec9 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt @@ -22,6 +22,7 @@ import io.legado.app.help.AppConfig import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.ui.book.audio.AudioPlayActivity +import io.legado.app.ui.book.group.GroupEdit import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.search.SearchActivity @@ -52,7 +53,6 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), override var books: List = emptyList() override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - searchView = binding.titleBar.findViewById(R.id.search_view) setSupportToolbar(binding.titleBar.toolbar) initRecyclerView() initGroupData() @@ -159,34 +159,30 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1), } override fun onItemClick(position: Int) { - if (position < bookGroups.size) { - val bookGroup = bookGroups[position] - groupId = bookGroup.groupId - initBooksData() - } else { - val book = books[position - bookGroups.size] - when (book.type) { + when (val item = getItem(position)) { + is Book -> when (item.type) { BookType.audio -> startActivity { - putExtra("bookUrl", book.bookUrl) + putExtra("bookUrl", item.bookUrl) } else -> startActivity { - putExtra("bookUrl", book.bookUrl) + putExtra("bookUrl", item.bookUrl) } } + is BookGroup -> { + groupId = item.groupId + initBooksData() + } } } override fun onItemLongClick(position: Int) { - if (position < bookGroups.size) { - groupId = bookGroups[position].groupId - initBooksData() - } else { - val book = books[position - bookGroups.size] - startActivity { - putExtra("name", book.name) - putExtra("author", book.author) + when (val item = getItem(position)) { + is Book -> startActivity { + putExtra("name", item.name) + putExtra("author", item.author) } + is BookGroup -> GroupEdit.show(requireContext(), layoutInflater, item) } }