This commit is contained in:
Horis
2022-10-26 12:46:23 +08:00
parent a434b6d428
commit e52407e7ec
16 changed files with 119 additions and 7 deletions
@@ -120,6 +120,7 @@ object PreferKey {
const val noAnimScrollPage = "noAnimScrollPage"
const val webDavDeviceName = "webDavDeviceName"
const val wakeLock = "wakeLock"
const val showLastUpdateTime = "showLastUpdateTime"
const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent"
@@ -82,6 +82,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
appCtx.putPrefBoolean(PreferKey.showUnread, value)
}
var showLastUpdateTime: Boolean
get() = appCtx.getPrefBoolean(PreferKey.showLastUpdateTime, false)
set(value) {
appCtx.putPrefBoolean(PreferKey.showLastUpdateTime, value)
}
var readBrightness: Int
get() = if (isNightTheme) {
appCtx.getPrefInt(PreferKey.nightBrightness, 100)
@@ -136,6 +136,7 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
.apply {
spGroupStyle.setSelection(AppConfig.bookGroupStyle)
swShowUnread.isChecked = AppConfig.showUnread
swShowLastUpdateTime.isChecked = AppConfig.showLastUpdateTime
rgLayout.checkByIndex(bookshelfLayout)
rgSort.checkByIndex(bookshelfSort)
}
@@ -150,6 +151,10 @@ abstract class BaseBookshelfFragment(layoutId: Int) : VMBaseFragment<BookshelfVi
AppConfig.showUnread = swShowUnread.isChecked
postEvent(EventBus.BOOKSHELF_REFRESH, "")
}
if (AppConfig.showLastUpdateTime != swShowLastUpdateTime.isChecked) {
AppConfig.showLastUpdateTime = swShowLastUpdateTime.isChecked
postEvent(EventBus.BOOKSHELF_REFRESH, "")
}
var changed = false
if (bookshelfLayout != rgLayout.getCheckedIndex()) {
putPrefInt(PreferKey.bookshelfLayout, rgLayout.getCheckedIndex())
@@ -66,13 +66,17 @@ abstract class BaseBooksAdapter<VB : ViewBinding>(context: Context) :
for (i in 0 until itemCount) {
getItem(i)?.let {
if (it.bookUrl == bookUrl) {
notifyItemChanged(i, bundleOf(Pair("refresh", null)))
notifyItemChanged(i, bundleOf(Pair("refresh", null), Pair("lastUpdateTime", null)))
return
}
}
}
}
fun upLastUpdateTime() {
notifyItemRangeChanged(0, itemCount, bundleOf(Pair("lastUpdateTime", null)))
}
interface CallBack {
fun open(book: Book)
fun openBookInfo(book: Book)
@@ -9,6 +9,7 @@ import io.legado.app.databinding.ItemBookshelfListBinding
import io.legado.app.help.book.isLocal
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.invisible
import io.legado.app.utils.toTimeAgo
import splitties.views.onLongClick
class BooksAdapterList(context: Context, private val callBack: CallBack) :
@@ -32,6 +33,7 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) :
tvLast.text = item.latestChapterTitle
ivCover.load(item.getDisplayCover(), item.name, item.author, false, item.origin)
upRefresh(binding, item)
upLastUpdateTime(binding, item)
} else {
tvRead.text = item.durChapterTitle
tvLast.text = item.latestChapterTitle
@@ -41,6 +43,7 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) :
"author" -> tvAuthor.text = item.author
"cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author, false, item.origin)
"refresh" -> upRefresh(binding, item)
"lastUpdateTime" -> upLastUpdateTime(binding, item)
}
}
}
@@ -61,6 +64,14 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) :
}
}
private fun upLastUpdateTime(binding: ItemBookshelfListBinding, item: Book) {
if (AppConfig.showLastUpdateTime && !item.isLocal) {
binding.tvLastUpdateTime.text = item.latestChapterTime.toTimeAgo()
} else {
binding.tvLastUpdateTime.text = ""
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemBookshelfListBinding) {
holder.itemView.apply {
setOnClickListener {
@@ -27,14 +27,11 @@ import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.main.MainViewModel
import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlin.math.max
/**
@@ -66,6 +63,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
private var savedInstanceState: Bundle? = null
private var position = 0
private var groupId = -1L
private var upLastUpdateTimeJob : Job? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
this.savedInstanceState = savedInstanceState
@@ -107,6 +105,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
}
}
})
startLastUpdateTimeJob()
}
private fun upRecyclerData() {
@@ -153,6 +152,19 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
}
}
private fun startLastUpdateTimeJob() {
upLastUpdateTimeJob?.cancel()
if (!AppConfig.showLastUpdateTime) {
return
}
upLastUpdateTimeJob = launch {
while (isActive) {
booksAdapter.upLastUpdateTime()
delay(30 * 1000)
}
}
}
fun getBooks(): List<Book> {
return booksAdapter.getItems()
}
@@ -222,6 +234,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
}
observeEvent<String>(EventBus.BOOKSHELF_REFRESH) {
booksAdapter.notifyDataSetChanged()
startLastUpdateTimeJob()
}
}
}
@@ -0,0 +1,39 @@
package io.legado.app.utils
import kotlin.math.abs
fun Long.toTimeAgo(): String {
val curTime = System.currentTimeMillis()
val time = this
val seconds = abs(System.currentTimeMillis() - time) / 1000f
val end = if (time < curTime) "" else ""
val start = when {
seconds < 60 -> "${seconds.toInt()}"
seconds < 3600 -> {
val minutes = seconds / 60f
"${minutes.toInt()}分钟"
}
seconds < 86400 -> {
val hours = seconds / 3600f
"${hours.toInt()}小时"
}
seconds < 604800 -> {
val days = seconds / 86400f
"${days.toInt()}"
}
seconds < 2_628_000 -> {
val weeks = seconds / 604800f
"${weeks.toInt()}"
}
seconds < 31_536_000 -> {
val months = seconds / 2_628_000f
"${months.toInt()}"
}
else -> {
val years = seconds / 31_536_000f
"${years.toInt()}"
}
}
return start + end
}