From a845fe192f460bdda9f58e3b0b008a934e39bccc Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 7 Feb 2020 21:20:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/group/GroupManageDialog.kt | 5 ++- .../app/ui/book/group/GroupSelectDialog.kt | 5 ++- .../app/ui/book/group/GroupViewModel.kt | 34 +++++++++++++++++++ .../app/ui/main/bookshelf/BookshelfAdapter.kt | 11 ++++++ .../ui/main/bookshelf/BookshelfViewModel.kt | 25 -------------- 5 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt 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 e9892dae0..ac49119bd 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 @@ -27,7 +27,6 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton -import io.legado.app.ui.main.bookshelf.BookshelfViewModel import io.legado.app.utils.applyTint import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getViewModel @@ -38,7 +37,7 @@ import kotlinx.android.synthetic.main.item_group_manage.view.* import org.jetbrains.anko.sdk27.listeners.onClick class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { - private lateinit var viewModel: BookshelfViewModel + private lateinit var viewModel: GroupViewModel private lateinit var adapter: GroupAdapter private var callBack: CallBack? = null @@ -54,7 +53,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(BookshelfViewModel::class.java) + viewModel = getViewModel(GroupViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index aebbcb603..3541defb4 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -26,7 +26,6 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton -import io.legado.app.ui.main.bookshelf.BookshelfViewModel import io.legado.app.utils.applyTint import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getViewModel @@ -52,7 +51,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { } private var requestCode: Int = -1 - private lateinit var viewModel: BookshelfViewModel + private lateinit var viewModel: GroupViewModel private lateinit var adapter: GroupAdapter private var callBack: CallBack? = null @@ -68,7 +67,7 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(BookshelfViewModel::class.java) + viewModel = getViewModel(GroupViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt new file mode 100644 index 000000000..610eff778 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt @@ -0,0 +1,34 @@ +package io.legado.app.ui.book.group + +import android.app.Application +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.BookGroup + +class GroupViewModel(application: Application) : BaseViewModel(application) { + + fun addGroup(groupName: String) { + execute { + val bookGroup = BookGroup( + groupId = App.db.bookGroupDao().maxId.plus(1), + groupName = groupName, + order = App.db.bookGroupDao().maxOrder.plus(1) + ) + App.db.bookGroupDao().insert(bookGroup) + } + } + + fun upGroup(vararg bookGroup: BookGroup) { + execute { + App.db.bookGroupDao().update(*bookGroup) + } + } + + fun delGroup(vararg bookGroup: BookGroup) { + execute { + App.db.bookGroupDao().delete(*bookGroup) + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt index 4caa119cd..55115ad94 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt @@ -9,12 +9,23 @@ import io.legado.app.ui.main.bookshelf.books.BooksFragment class BookshelfAdapter(fragment: Fragment, val callBack: CallBack) : FragmentStateAdapter(fragment) { + private val ids = hashSetOf() + override fun getItemCount(): Int { return callBack.groupSize } + override fun getItemId(position: Int): Long { + return callBack.getGroup(position).groupId.toLong() + } + + override fun containsItem(itemId: Long): Boolean { + return ids.contains(itemId) + } + override fun createFragment(position: Int): Fragment { val groupId = callBack.getGroup(position).groupId + ids.add(groupId.toLong()) return BooksFragment.newInstance(position, groupId) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index 74c963763..d0ab47c0d 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -5,7 +5,6 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book -import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookSource import io.legado.app.model.WebBook import io.legado.app.utils.NetworkUtils @@ -13,30 +12,6 @@ import kotlinx.coroutines.Dispatchers.IO class BookshelfViewModel(application: Application) : BaseViewModel(application) { - - fun addGroup(groupName: String) { - execute { - val bookGroup = BookGroup( - groupId = App.db.bookGroupDao().maxId.plus(1), - groupName = groupName, - order = App.db.bookGroupDao().maxOrder.plus(1) - ) - App.db.bookGroupDao().insert(bookGroup) - } - } - - fun upGroup(vararg bookGroup: BookGroup) { - execute { - App.db.bookGroupDao().update(*bookGroup) - } - } - - fun delGroup(vararg bookGroup: BookGroup) { - execute { - App.db.bookGroupDao().delete(*bookGroup) - } - } - fun addBookByUrl(bookUrls: String) { var successCount = 0 execute {