@ -43,6 +43,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
BaseBooksAdapter . CallBack {
BaseBooksAdapter . CallBack {
private val binding by viewBinding ( FragmentBookshelf1Binding :: bind )
private val binding by viewBinding ( FragmentBookshelf1Binding :: bind )
private val rootGroupId = - 100L
private val bookshelfLayout by lazy {
private val bookshelfLayout by lazy {
getPrefInt ( PreferKey . bookshelfLayout )
getPrefInt ( PreferKey . bookshelfLayout )
}
}
@ -55,7 +56,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
}
private var bookGroups : List < BookGroup > = emptyList ( )
private var bookGroups : List < BookGroup > = emptyList ( )
private var booksFlowJob : Job ? = null
private var booksFlowJob : Job ? = null
override var groupId = AppConst . bookGroupNone Id
override var groupId = rootGroup Id
override var books : List < Book > = emptyList ( )
override var books : List < Book > = emptyList ( )
override fun onFragmentCreated ( view : View , savedInstanceState : Bundle ? ) {
override fun onFragmentCreated ( view : View , savedInstanceState : Bundle ? ) {
@ -108,7 +109,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
@SuppressLint ( " NotifyDataSetChanged " )
@SuppressLint ( " NotifyDataSetChanged " )
private fun initBooksData ( ) {
private fun initBooksData ( ) {
if ( groupId == AppConst . bookGroupNoneId ) {
if ( groupId == - 100L ) {
binding . titleBar . title = getString ( R . string . bookshelf )
binding . titleBar . title = getString ( R . string . bookshelf )
} else {
} else {
bookGroups . forEach {
bookGroups . forEach {
@ -120,6 +121,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
booksFlowJob ?. cancel ( )
booksFlowJob ?. cancel ( )
booksFlowJob = launch {
booksFlowJob = launch {
when ( groupId ) {
when ( groupId ) {
rootGroupId -> appDb . bookDao . flowRoot ( )
AppConst . bookGroupAllId -> appDb . bookDao . flowAll ( )
AppConst . bookGroupAllId -> appDb . bookDao . flowAll ( )
AppConst . bookGroupLocalId -> appDb . bookDao . flowLocal ( )
AppConst . bookGroupLocalId -> appDb . bookDao . flowLocal ( )
AppConst . bookGroupAudioId -> appDb . bookDao . flowAudio ( )
AppConst . bookGroupAudioId -> appDb . bookDao . flowAudio ( )
@ -152,8 +154,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
}
fun back ( ) : Boolean {
fun back ( ) : Boolean {
if ( groupId != AppConst . bookGroupNoneId ) {
if ( groupId != - 100L ) {
groupId = AppConst . bookGroupNoneId
groupId = - 100L
initBooksData ( )
initBooksData ( )
return true
return true
}
}
@ -210,7 +212,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
}
override fun getItemCount ( ) : Int {
override fun getItemCount ( ) : Int {
return if ( groupId == AppConst . bookGroupNone Id) {
return if ( groupId == rootGroup Id) {
bookGroups . size + books . size
bookGroups . size + books . size
} else {
} else {
books . size
books . size
@ -218,23 +220,23 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
}
}
override fun getItemType ( position : Int ) : Int {
override fun getItemType ( position : Int ) : Int {
return if ( groupId == AppConst . bookGroupNoneId ) {
if ( groupId != rootGroupId ) {
if ( position < bookGroups . size ) 1 else 0
return 0
} else {
}
0
if ( position < bookGroups . size ) {
return 1
}
}
return 0
}
}
override fun getItem ( position : Int ) : Any {
override fun getItem ( position : Int ) : Any ? {
return if ( groupId == AppConst . bookGroupNoneId ) {
if ( groupId != rootGroupId ) {
if ( position < bookGroups . size ) {
return books . getOrNull ( position )
bookGroups [ position ]
}
} else {
if ( position < bookGroups . size ) {
books [ position - bookGroups . size ]
return bookGroups [ position ]
}
} else {
books [ position ]
}
}
return books . getOrNull ( position - bookGroups . size )
}
}
@SuppressLint ( " NotifyDataSetChanged " )
@SuppressLint ( " NotifyDataSetChanged " )