From 0ab76a176f7ff9ecd0072d21929952803ea71eb3 Mon Sep 17 00:00:00 2001 From: 821938089 <821938089@qq.com> Date: Tue, 28 Jun 2022 23:04:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DUI=E7=9A=84bug=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B7=BB=E5=8A=A0=E7=BD=91=E5=9D=80=E7=9A=84?= =?UTF-8?q?order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + app/src/main/java/io/legado/app/App.kt | 11 ++++-- .../ui/main/bookshelf/BookshelfViewModel.kt | 2 +- .../bookshelf/style1/books/BooksFragment.kt | 38 +++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2275e93eb..cc0d44b44 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -147,6 +147,7 @@ diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index ff64c1024..40466a446 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -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) } /** 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 43fddca9d..42c4f5194 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 @@ -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 { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt index d897ed4c8..2e836422b 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt @@ -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 { 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 ->