分组支持更多数量

pull/416/head
gedoor 4 years ago
parent d357954635
commit a16ff33b4d
  1. 6
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 7
      app/src/main/java/io/legado/app/data/dao/BookGroupDao.kt
  3. 2
      app/src/main/java/io/legado/app/data/entities/Book.kt
  4. 2
      app/src/main/java/io/legado/app/data/entities/BookGroup.kt
  5. 16
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  6. 8
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  7. 8
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  8. 10
      app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt
  9. 4
      app/src/main/java/io/legado/app/ui/book/group/GroupViewModel.kt
  10. 4
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  11. 2
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  12. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  13. 6
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  14. 8
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt

@ -22,7 +22,7 @@ interface BookDao {
fun observeLocalUri(): LiveData<List<String>> fun observeLocalUri(): LiveData<List<String>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0") @Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun observeByGroup(group: Int): LiveData<List<Book>> fun observeByGroup(group: Long): LiveData<List<Book>>
@Query("select * from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0") @Query("select * from books where (SELECT sum(groupId) FROM book_groups) & `group` = 0")
fun observeNoGroup(): LiveData<List<Book>> fun observeNoGroup(): LiveData<List<Book>>
@ -34,7 +34,7 @@ interface BookDao {
fun liveDataSearch(key: String): LiveData<List<Book>> fun liveDataSearch(key: String): LiveData<List<Book>>
@Query("SELECT * FROM books WHERE (`group` & :group) > 0") @Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun getBooksByGroup(group: Int): List<Book> fun getBooksByGroup(group: Long): List<Book>
@Query("SELECT * FROM books WHERE `name` in (:names)") @Query("SELECT * FROM books WHERE `name` in (:names)")
fun findByName(vararg names: String): List<Book> fun findByName(vararg names: String): List<Book>
@ -82,7 +82,7 @@ interface BookDao {
fun upProgress(bookUrl: String, pos: Int) fun upProgress(bookUrl: String, pos: Int)
@Query("update books set `group` = :newGroupId where `group` = :oldGroupId") @Query("update books set `group` = :newGroupId where `group` = :oldGroupId")
fun upGroup(oldGroupId: Int, newGroupId: Int) fun upGroup(oldGroupId: Long, newGroupId: Long)
@get:Query("select bookUrl, tocUrl, origin, originName, durChapterIndex, durChapterPos, durChapterTime, durChapterTitle from books") @get:Query("select bookUrl, tocUrl, origin, originName, durChapterIndex, durChapterPos, durChapterTime, durChapterTitle from books")
val allBookProgress: List<BookProgress> val allBookProgress: List<BookProgress>

@ -8,13 +8,16 @@ import io.legado.app.data.entities.BookGroup
interface BookGroupDao { interface BookGroupDao {
@Query("select * from book_groups where groupId = :id") @Query("select * from book_groups where groupId = :id")
fun getByID(id: Int): BookGroup? fun getByID(id: Long): BookGroup?
@Query("select * from book_groups where groupName = :groupName")
fun getByName(groupName: String): BookGroup?
@Query("SELECT * FROM book_groups ORDER BY `order`") @Query("SELECT * FROM book_groups ORDER BY `order`")
fun liveDataAll(): LiveData<List<BookGroup>> fun liveDataAll(): LiveData<List<BookGroup>>
@get:Query("SELECT sum(groupId) FROM book_groups") @get:Query("SELECT sum(groupId) FROM book_groups")
val idsSum: Int val idsSum: Long
@get:Query("SELECT MAX(`order`) FROM book_groups") @get:Query("SELECT MAX(`order`) FROM book_groups")
val maxOrder: Int val maxOrder: Int

@ -39,7 +39,7 @@ data class Book(
var customIntro: String? = null, // 简介内容(用户修改) var customIntro: String? = null, // 简介内容(用户修改)
var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍) var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍)
var type: Int = 0, // 0:text 1:audio var type: Int = 0, // 0:text 1:audio
var group: Int = 0, // 自定义分组索引号 var group: Long = 0, // 自定义分组索引号
var latestChapterTitle: String? = null, // 最新章节标题 var latestChapterTitle: String? = null, // 最新章节标题
var latestChapterTime: Long = System.currentTimeMillis(), // 最新章节标题更新时间 var latestChapterTime: Long = System.currentTimeMillis(), // 最新章节标题更新时间
var lastCheckTime: Long = System.currentTimeMillis(), // 最近一次更新书籍信息的时间 var lastCheckTime: Long = System.currentTimeMillis(), // 最近一次更新书籍信息的时间

@ -9,7 +9,7 @@ import kotlinx.android.parcel.Parcelize
@Entity(tableName = "book_groups") @Entity(tableName = "book_groups")
data class BookGroup( data class BookGroup(
@PrimaryKey @PrimaryKey
val groupId: Int = 0b1, val groupId: Long = 0b1,
var groupName: String, var groupName: String,
var order: Int = 0 var order: Int = 0
) : Parcelable ) : Parcelable

@ -41,10 +41,10 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
private var groupLiveData: LiveData<List<BookGroup>>? = null private var groupLiveData: LiveData<List<BookGroup>>? = null
private var booksLiveData: LiveData<List<Book>>? = null private var booksLiveData: LiveData<List<Book>>? = null
private var menu: Menu? = null private var menu: Menu? = null
private var groupId: Int = -1 private var groupId: Long = -1
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
groupId = intent.getIntExtra("groupId", -1) groupId = intent.getLongExtra("groupId", -1)
title_bar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all) title_bar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all)
initView() initView()
initGroupData() initGroupData()
@ -85,7 +85,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
} }
override fun onClickMainAction() { override fun onClickMainAction() {
selectGroup(0, groupRequestCode) selectGroup(groupRequestCode, 0)
} }
}) })
} }
@ -149,7 +149,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
} }
else -> if (item.groupId == R.id.menu_group) { else -> if (item.groupId == R.id.menu_group) {
title_bar.subtitle = item.title title_bar.subtitle = item.title
groupId = item.itemId groupId = App.db.bookGroupDao().getByName(item.title.toString())?.groupId ?: 0
initBookData() initBookData()
} }
} }
@ -167,7 +167,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
viewModel.upCanUpdate(adapter.selectedBooks(), true) viewModel.upCanUpdate(adapter.selectedBooks(), true)
R.id.menu_update_disable -> R.id.menu_update_disable ->
viewModel.upCanUpdate(adapter.selectedBooks(), false) viewModel.upCanUpdate(adapter.selectedBooks(), false)
R.id.menu_add_to_group -> selectGroup(0, addToGroupRequestCode) R.id.menu_add_to_group -> selectGroup(addToGroupRequestCode, 0)
} }
return false return false
} }
@ -176,16 +176,16 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
menu?.findItem(R.id.menu_book_group)?.subMenu?.let { subMenu -> menu?.findItem(R.id.menu_book_group)?.subMenu?.let { subMenu ->
subMenu.removeGroup(R.id.menu_group) subMenu.removeGroup(R.id.menu_group)
groupList.forEach { bookGroup -> groupList.forEach { bookGroup ->
subMenu.add(R.id.menu_group, bookGroup.groupId, Menu.NONE, bookGroup.groupName) subMenu.add(R.id.menu_group, bookGroup.order, Menu.NONE, bookGroup.groupName)
} }
} }
} }
override fun selectGroup(groupId: Int, requestCode: Int) { override fun selectGroup(requestCode: Int, groupId: Long) {
GroupSelectDialog.show(supportFragmentManager, groupId, requestCode) GroupSelectDialog.show(supportFragmentManager, groupId, requestCode)
} }
override fun upGroup(requestCode: Int, groupId: Int) { override fun upGroup(requestCode: Int, groupId: Long) {
when (requestCode) { when (requestCode) {
groupRequestCode -> { groupRequestCode -> {
val books = arrayListOf<Book>() val books = arrayListOf<Book>()

@ -101,13 +101,13 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
tv_group.onClick { tv_group.onClick {
getItem(holder.layoutPosition)?.let { getItem(holder.layoutPosition)?.let {
actionItem = it actionItem = it
callBack.selectGroup(it.group, groupRequestCode) callBack.selectGroup(groupRequestCode, it.group)
} }
} }
} }
} }
private fun getGroupList(groupId: Int): List<String> { private fun getGroupList(groupId: Long): List<String> {
val groupNames = arrayListOf<String>() val groupNames = arrayListOf<String>()
callBack.groupList.forEach { callBack.groupList.forEach {
if (it.groupId and groupId > 0) { if (it.groupId and groupId > 0) {
@ -117,7 +117,7 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
return groupNames return groupNames
} }
private fun getGroupName(groupId: Int): String { private fun getGroupName(groupId: Long): String {
val groupNames = getGroupList(groupId) val groupNames = getGroupList(groupId)
if (groupNames.isEmpty()) { if (groupNames.isEmpty()) {
return "" return ""
@ -159,6 +159,6 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
fun upSelectCount() fun upSelectCount()
fun updateBook(vararg book: Book) fun updateBook(vararg book: Book)
fun deleteBook(book: Book) fun deleteBook(book: Book)
fun selectGroup(groupId: Int, requestCode: Int) fun selectGroup(requestCode: Int, groupId: Long)
} }
} }

@ -43,13 +43,13 @@ class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download)
private var menu: Menu? = null private var menu: Menu? = null
private var exportPosition = -1 private var exportPosition = -1
private val groupList: ArrayList<BookGroup> = arrayListOf() private val groupList: ArrayList<BookGroup> = arrayListOf()
private var groupId: Int = -1 private var groupId: Long = -1
override val viewModel: CacheViewModel override val viewModel: CacheViewModel
get() = getViewModel(CacheViewModel::class.java) get() = getViewModel(CacheViewModel::class.java)
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
groupId = intent.getIntExtra("groupId", -1) groupId = intent.getLongExtra("groupId", -1)
title_bar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all) title_bar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all)
initRecyclerView() initRecyclerView()
initGroupData() initGroupData()
@ -71,7 +71,7 @@ class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download)
menu?.findItem(R.id.menu_book_group)?.subMenu?.let { subMenu -> menu?.findItem(R.id.menu_book_group)?.subMenu?.let { subMenu ->
subMenu.removeGroup(R.id.menu_group) subMenu.removeGroup(R.id.menu_group)
groupList.forEach { bookGroup -> groupList.forEach { bookGroup ->
subMenu.add(R.id.menu_group, bookGroup.groupId, Menu.NONE, bookGroup.groupName) subMenu.add(R.id.menu_group, bookGroup.order, Menu.NONE, bookGroup.groupName)
} }
} }
} }
@ -107,7 +107,7 @@ class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download)
} }
else -> if (item.groupId == R.id.menu_group) { else -> if (item.groupId == R.id.menu_group) {
title_bar.subtitle = item.title title_bar.subtitle = item.title
groupId = item.itemId groupId = App.db.bookGroupDao().getByName(item.title.toString())?.groupId ?: 0
initBookData() initBookData()
} }
} }

@ -44,10 +44,10 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
companion object { companion object {
const val tag = "groupSelectDialog" const val tag = "groupSelectDialog"
fun show(manager: FragmentManager, groupId: Int, requestCode: Int = -1) { fun show(manager: FragmentManager, groupId: Long, requestCode: Int = -1) {
val fragment = GroupSelectDialog().apply { val fragment = GroupSelectDialog().apply {
val bundle = Bundle() val bundle = Bundle()
bundle.putInt("groupId", groupId) bundle.putLong("groupId", groupId)
bundle.putInt("requestCode", requestCode) bundle.putInt("requestCode", requestCode)
arguments = bundle arguments = bundle
} }
@ -59,7 +59,7 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
private lateinit var viewModel: GroupViewModel private lateinit var viewModel: GroupViewModel
private lateinit var adapter: GroupAdapter private lateinit var adapter: GroupAdapter
private var callBack: CallBack? = null private var callBack: CallBack? = null
private var groupId = 0 private var groupId: Long = 0
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -81,7 +81,7 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
tool_bar.setBackgroundColor(primaryColor) tool_bar.setBackgroundColor(primaryColor)
callBack = activity as? CallBack callBack = activity as? CallBack
arguments?.let { arguments?.let {
groupId = it.getInt("groupId") groupId = it.getLong("groupId")
requestCode = it.getInt("requestCode", -1) requestCode = it.getInt("requestCode", -1)
} }
initView() initView()
@ -214,6 +214,6 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener
} }
interface CallBack { interface CallBack {
fun upGroup(requestCode: Int, groupId: Int) fun upGroup(requestCode: Int, groupId: Long)
} }
} }

@ -9,9 +9,9 @@ class GroupViewModel(application: Application) : BaseViewModel(application) {
fun addGroup(groupName: String) { fun addGroup(groupName: String) {
execute { execute {
var id = 1 var id = 1L
val idsSum = App.db.bookGroupDao().idsSum val idsSum = App.db.bookGroupDao().idsSum
while (id and idsSum != 0) { while (id and idsSum != 0L) {
id = id.shl(1) id = id.shl(1)
} }
val bookGroup = BookGroup( val bookGroup = BookGroup(

@ -182,7 +182,7 @@ class BookInfoActivity :
} }
} }
private fun upGroup(groupId: Int) { private fun upGroup(groupId: Long) {
viewModel.loadGroup(groupId) { viewModel.loadGroup(groupId) {
if (it.isNullOrEmpty()) { if (it.isNullOrEmpty()) {
tv_group.text = getString(R.string.group_s, getString(R.string.no_group)) tv_group.text = getString(R.string.group_s, getString(R.string.no_group))
@ -350,7 +350,7 @@ class BookInfoActivity :
return viewModel.durChapterIndex return viewModel.durChapterIndex
} }
override fun upGroup(requestCode: Int, groupId: Int) { override fun upGroup(requestCode: Int, groupId: Long) {
upGroup(groupId) upGroup(groupId)
viewModel.bookData.value?.group = groupId viewModel.bookData.value?.group = groupId
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {

@ -117,7 +117,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun loadGroup(groupId: Int, success: ((groupNames: String?) -> Unit)) { fun loadGroup(groupId: Long, success: ((groupNames: String?) -> Unit)) {
execute { execute {
val groupNames = arrayListOf<String>() val groupNames = arrayListOf<String>()
App.db.bookGroupDao().all.forEach { App.db.bookGroupDao().all.forEach {

@ -54,7 +54,7 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
private var bookGroupLiveData: LiveData<List<BookGroup>>? = null private var bookGroupLiveData: LiveData<List<BookGroup>>? = null
private var noGroupLiveData: LiveData<Int>? = null private var noGroupLiveData: LiveData<Int>? = null
private val bookGroups = mutableListOf<BookGroup>() private val bookGroups = mutableListOf<BookGroup>()
private val fragmentMap = hashMapOf<Int, BooksFragment>() private val fragmentMap = hashMapOf<Long, BooksFragment>()
private var showGroupNone = false private var showGroupNone = false
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

@ -65,10 +65,10 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
fun checkGroup(groups: List<BookGroup>) { fun checkGroup(groups: List<BookGroup>) {
execute { execute {
groups.forEach { group -> groups.forEach { group ->
if (group.groupId and (group.groupId - 1) != 0) { if (group.groupId and (group.groupId - 1) != 0L) {
var id = 1 var id = 1L
val idsSum = App.db.bookGroupDao().idsSum val idsSum = App.db.bookGroupDao().idsSum
while (id and idsSum != 0) { while (id and idsSum != 0L) {
id = id.shl(1) id = id.shl(1)
} }
App.db.bookGroupDao().delete(group) App.db.bookGroupDao().delete(group)

@ -35,11 +35,11 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
BaseBooksAdapter.CallBack { BaseBooksAdapter.CallBack {
companion object { companion object {
fun newInstance(position: Int, groupId: Int): BooksFragment { fun newInstance(position: Int, groupId: Long): BooksFragment {
return BooksFragment().apply { return BooksFragment().apply {
val bundle = Bundle() val bundle = Bundle()
bundle.putInt("position", position) bundle.putInt("position", position)
bundle.putInt("groupId", groupId) bundle.putLong("groupId", groupId)
arguments = bundle arguments = bundle
} }
} }
@ -50,12 +50,12 @@ class BooksFragment : BaseFragment(R.layout.fragment_books),
private lateinit var booksAdapter: BaseBooksAdapter private lateinit var booksAdapter: BaseBooksAdapter
private var bookshelfLiveData: LiveData<List<Book>>? = null private var bookshelfLiveData: LiveData<List<Book>>? = null
private var position = 0 private var position = 0
private var groupId = -1 private var groupId = -1L
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let { arguments?.let {
position = it.getInt("position", 0) position = it.getInt("position", 0)
groupId = it.getInt("groupId", -1) groupId = it.getLong("groupId", -1)
} }
initRecyclerView() initRecyclerView()
upRecyclerData() upRecyclerData()

Loading…
Cancel
Save