From 8440c5a4dde01a39a3939888da97981e6114762a Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 4 Dec 2020 12:15:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookshelfController.kt | 5 ++- .../legado/app/ui/about/ReadRecordActivity.kt | 34 ++++++++++++++++++- .../ui/main/bookshelf/books/BooksFragment.kt | 9 +++-- .../io/legado/app/utils/StringExtensions.kt | 7 ++++ app/src/main/res/menu/book_read_record.xml | 12 +++++++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/menu/book_read_record.xml diff --git a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt index 2130340c3..c8c4d56ab 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt @@ -7,6 +7,7 @@ import io.legado.app.help.BookHelp import io.legado.app.model.webBook.WebBook import io.legado.app.service.help.ReadBook import io.legado.app.utils.GSON +import io.legado.app.utils.cnCompare import io.legado.app.utils.fromJsonObject import io.legado.app.utils.getPrefInt import io.legado.app.web.utils.ReturnData @@ -23,7 +24,9 @@ object BookshelfController { } else { val data = when (App.INSTANCE.getPrefInt(PreferKey.bookshelfSort)) { 1 -> books.sortedByDescending { it.latestChapterTime } - 2 -> books.sortedBy { it.name } + 2 -> books.sortedWith { o1, o2 -> + o1.name.cnCompare(o2.name) + } 3 -> books.sortedBy { it.order } else -> books.sortedByDescending { it.durChapterTime } } diff --git a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt index 2ccde21b8..9ad3a386c 100644 --- a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt @@ -2,6 +2,8 @@ package io.legado.app.ui.about import android.content.Context import android.os.Bundle +import android.view.Menu +import android.view.MenuItem import android.view.ViewGroup import io.legado.app.App import io.legado.app.R @@ -12,15 +14,18 @@ import io.legado.app.data.entities.ReadRecordShow import io.legado.app.databinding.ActivityReadRecordBinding import io.legado.app.databinding.ItemReadRecordBinding import io.legado.app.lib.dialogs.alert +import io.legado.app.utils.cnCompare import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class ReadRecordActivity : BaseActivity() { lateinit var adapter: RecordAdapter + private var sortMode = 0 override fun getViewBinding(): ActivityReadRecordBinding { return ActivityReadRecordBinding.inflate(layoutInflater) @@ -31,6 +36,25 @@ class ReadRecordActivity : BaseActivity() { initData() } + override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.book_read_record, menu) + return super.onCompatCreateOptionsMenu(menu) + } + + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_sort_name -> { + sortMode = 0 + initData() + } + R.id.menu_sort_time -> { + sortMode = 1 + initData() + } + } + return super.onCompatOptionsItemSelected(item) + } + private fun initView() = with(binding) { readRecord.tvBookName.setText(R.string.all_read_time) adapter = RecordAdapter(this@ReadRecordActivity) @@ -52,7 +76,15 @@ class ReadRecordActivity : BaseActivity() { withContext(Main) { binding.readRecord.tvReadTime.text = formatDuring(allTime) } - val readRecords = App.db.readRecordDao().allShow + var readRecords = App.db.readRecordDao().allShow + readRecords = when (sortMode) { + 1 -> readRecords.sortedBy { it.readTime } + else -> { + readRecords.sortedWith { o1, o2 -> + o1.bookName.cnCompare(o2.bookName) + } + } + } withContext(Main) { adapter.setItems(readRecords) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index 508581705..bc9b91de9 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -25,10 +25,7 @@ import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainViewModel -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.getViewModelOfActivity -import io.legado.app.utils.observeEvent -import io.legado.app.utils.startActivity +import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlin.math.max @@ -114,7 +111,9 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), binding.tvEmptyMsg.isGone = list.isNotEmpty() val books = when (getPrefInt(PreferKey.bookshelfSort)) { 1 -> list.sortedByDescending { it.latestChapterTime } - 2 -> list.sortedBy { it.name } + 2 -> list.sortedWith { o1, o2 -> + o1.name.cnCompare(o2.name) + } 3 -> list.sortedBy { it.order } else -> list.sortedByDescending { it.durChapterTime } } diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index f70ab447e..a75811931 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -4,10 +4,13 @@ package io.legado.app.utils import android.net.Uri import java.io.File +import java.text.Collator +import java.util.* val removeHtmlRegex = "]*>".toRegex() val imgRegex = "]*>".toRegex() val notImgHtmlRegex = "]*>".toRegex() +val cnCollator: Collator = Collator.getInstance(Locale.CHINA) fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() @@ -67,6 +70,10 @@ fun String.splitNotBlank(regex: Regex, limit: Int = 0): Array = run { this.split(regex, limit).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray() } +fun String.cnCompare(other: String): Int { + return cnCollator.compare(this, other) +} + /** * 将字符串拆分为单个字符,包含emoji */ diff --git a/app/src/main/res/menu/book_read_record.xml b/app/src/main/res/menu/book_read_record.xml new file mode 100644 index 000000000..6ba105d38 --- /dev/null +++ b/app/src/main/res/menu/book_read_record.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file