pull/517/head
gedoor 4 years ago
parent c58f962805
commit 8440c5a4dd
  1. 5
      app/src/main/java/io/legado/app/api/controller/BookshelfController.kt
  2. 34
      app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt
  3. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt
  4. 7
      app/src/main/java/io/legado/app/utils/StringExtensions.kt
  5. 12
      app/src/main/res/menu/book_read_record.xml

@ -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 }
}

@ -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<ActivityReadRecordBinding>() {
lateinit var adapter: RecordAdapter
private var sortMode = 0
override fun getViewBinding(): ActivityReadRecordBinding {
return ActivityReadRecordBinding.inflate(layoutInflater)
@ -31,6 +36,25 @@ class ReadRecordActivity : BaseActivity<ActivityReadRecordBinding>() {
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<ActivityReadRecordBinding>() {
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)
}

@ -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 }
}

@ -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 = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex()
val imgRegex = "<img[^>]*>".toRegex()
val notImgHtmlRegex = "</?(?!img)\\w+[^>]*>".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<String> = run {
this.split(regex, limit).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray()
}
fun String.cnCompare(other: String): Int {
return cnCollator.compare(this, other)
}
/**
* 将字符串拆分为单个字符,包含emoji
*/

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_sort_name"
android:title="按书名排序" />
<item
android:id="@+id/menu_sort_time"
android:title="按时间排序" />
</menu>
Loading…
Cancel
Save