Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jul 16, 2023
1 parent f8171ce commit 16ff60f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/io/legado/app/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
}
}

/**
* 如果重启太快fragment不会重建,这里更新一下书架的排序
*/
override fun recreate() {
(fragmentMap[getFragmentId(0)] as? BaseBookshelfFragment)?.run {
upSort()
}
super.recreate()
}

override fun observeLiveBus() {
viewModel.onUpBooksLiveData.observe(this) {
onUpBooksBadgeView.setBadgeCount(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi

abstract fun upGroup(data: List<BookGroup>)

abstract fun upSort()

@SuppressLint("InflateParams")
fun addBookByUrl() {
alert(titleResource = R.string.add_book_url) {
Expand Down Expand Up @@ -166,16 +168,12 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
AppConfig.showWaitUpCount = swShowWaitUpBooks.isChecked
activityViewModel.postUpBooksLiveData(true)
}
var changed = false
if (bookshelfLayout != rgLayout.getCheckedIndex()) {
putPrefInt(PreferKey.bookshelfLayout, rgLayout.getCheckedIndex())
changed = true
}
if (bookshelfSort != rgSort.getCheckedIndex()) {
AppConfig.bookshelfSort = rgSort.getCheckedIndex()
changed = true
upSort()
}
if (changed) {
if (bookshelfLayout != rgLayout.getCheckedIndex()) {
putPrefInt(PreferKey.bookshelfLayout, rgLayout.getCheckedIndex())
postEvent(EventBus.RECREATE, "")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ class BookshelfFragment1() : BaseBookshelfFragment(R.layout.fragment_bookshelf1)
if (data != bookGroups) {
bookGroups.clear()
bookGroups.addAll(data)
/**
* 在书架布局对话框修改排序后,会导致书架页面刷新。但是在分组标签未修改的情况下,此处的adapter.notifyDataSetChanged()调用不会引起
* [io.legado.app.ui.main.bookshelf.style1.BookshelfFragment1.TabFragmentPageAdapter.getItemPosition]
* 被调用。
* 有关方法:
* [androidx.viewpager.widget.ViewPager.dataSetChanged]
*/
adapter.notifyDataSetChanged()
selectLastTab()
for (i in 0 until adapter.count) {
Expand All @@ -110,6 +103,10 @@ class BookshelfFragment1() : BaseBookshelfFragment(R.layout.fragment_bookshelf1)
}
}

override fun upSort() {
adapter.notifyDataSetChanged()
}

private fun selectLastTab() {
tabLayout.post {
tabLayout.removeOnTabSelectedListener(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ import kotlin.math.max
class BooksFragment() : BaseFragment(R.layout.fragment_books),
BaseBooksAdapter.CallBack {

lateinit var group: BookGroup

constructor(position: Int, group: BookGroup) : this() {
val bundle = Bundle()
this.group = group
bundle.putInt("position", position)
bundle.putLong("groupId", group.groupId)
bundle.putInt("bookSort", group.getRealBookSort())
Expand Down Expand Up @@ -83,9 +80,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
bookSort = it.getInt("bookSort", 0)
binding.refreshLayout.isEnabled = it.getBoolean("enableRefresh", true)
}
appDb.bookGroupDao.getByID(groupId)?.apply {
group = this
}
initRecyclerView()
upRecyclerData()
}
Expand Down Expand Up @@ -199,9 +193,6 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),

override fun onResume() {
super.onResume()
if (::group.isInitialized) {
bookSort = group.getRealBookSort()
}
startLastUpdateTimeJob()
upRecyclerData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
}
}

override fun upSort() {
initBooksData()
}

@SuppressLint("NotifyDataSetChanged")
private fun initBooksData() {
if (groupId == -100L) {
Expand Down

0 comments on commit 16ff60f

Please sign in to comment.