Merge pull request #2001 from 821938089/master

修复UI的一些bug,保存书架状态,修改添加网址的order
pull/2005/head
kunfei 2 years ago committed by GitHub
commit 9457504ebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/src/main/AndroidManifest.xml
  2. 11
      app/src/main/java/io/legado/app/App.kt
  3. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  4. 38
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt

@ -147,6 +147,7 @@
<!-- 主界面 -->
<activity
android:name=".ui.main.MainActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|uiMode"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask" />
<!-- 阅读界面 -->

@ -3,6 +3,7 @@ package io.legado.app
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import androidx.multidex.MultiDexApplication
@ -28,8 +29,11 @@ import java.util.concurrent.TimeUnit
class App : MultiDexApplication() {
private lateinit var oldConfig: Configuration
override fun onCreate() {
super.onCreate()
oldConfig = Configuration(resources.configuration)
CrashHandler(this)
//预下载Cronet so
CronetLoader.preDownload()
@ -69,10 +73,11 @@ class App : MultiDexApplication() {
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES,
Configuration.UI_MODE_NIGHT_NO -> applyDayNight(this)
val diff = newConfig.diff(oldConfig)
if ((diff and ActivityInfo.CONFIG_UI_MODE) != 0) {
applyDayNight(this)
}
oldConfig = Configuration(newConfig)
}
/**

@ -50,7 +50,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
)
WebBook.getBookInfo(this, bookSource, book)
.onSuccess(IO) {
it.order = appDb.bookDao.maxOrder + 1
it.order = appDb.bookDao.minOrder - 1
it.save()
successCount++
}.onError {

@ -59,10 +59,12 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
}
}
private var booksFlowJob: Job? = null
private var savedInstanceState: Bundle? = null
private var position = 0
private var groupId = -1L
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
this.savedInstanceState = savedInstanceState
arguments?.let {
position = it.getInt("position", 0)
groupId = it.getLong("groupId", -1)
@ -126,11 +128,25 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
}.conflate().collect { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty()
booksAdapter.setItems(list)
recoverPositionState()
delay(100)
}
}
}
private fun recoverPositionState() {
// 恢复书架位置状态
if (savedInstanceState?.getBoolean("needRecoverState") == true) {
val layoutManager = binding.rvBookshelf.layoutManager
if (layoutManager is LinearLayoutManager) {
val leavePosition = savedInstanceState!!.getInt("leavePosition")
val leaveOffset = savedInstanceState!!.getInt("leaveOffset")
layoutManager.scrollToPositionWithOffset(leavePosition, leaveOffset)
}
savedInstanceState!!.putBoolean("needRecoverState", false)
}
}
fun getBooks(): List<Book> {
return booksAdapter.getItems()
}
@ -147,6 +163,28 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
return booksAdapter.itemCount
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
// 保存书架位置状态
val layoutManager = binding.rvBookshelf.layoutManager
if (layoutManager is LinearLayoutManager) {
val itemPosition = layoutManager.findFirstVisibleItemPosition()
val currentView = layoutManager.findViewByPosition(itemPosition)
val viewOffset = currentView?.top
if (viewOffset != null) {
outState.putInt("leavePosition", itemPosition)
outState.putInt("leaveOffset", viewOffset)
outState.putBoolean("needRecoverState", true)
} else if (savedInstanceState != null) {
val leavePosition = savedInstanceState!!.getInt("leavePosition")
val leaveOffset = savedInstanceState!!.getInt("leaveOffset")
outState.putInt("leavePosition", leavePosition)
outState.putInt("leaveOffset", leaveOffset)
outState.putBoolean("needRecoverState", true)
}
}
}
override fun open(book: Book) {
when (book.type) {
BookType.audio ->

Loading…
Cancel
Save