Commit a325311f authored by Christophe Henry's avatar Christophe Henry
Browse files

Additions to previous commit

parent d7ee5478
Pipeline #4863 passed with stage
in 0 seconds
......@@ -36,7 +36,7 @@ class MainSubscriptionFragment: Fragment(), SharedPreferences.OnSharedPreference
FragmentMainSubscriptionBinding.bind(view).apply {
lifecycleOwner = this@MainSubscriptionFragment.viewLifecycleOwner
lastFetchText = Transformations.map(viewModel.lastFetchText) {
it.resolve(requireContext())
it(requireContext())
}
autoFetchDisabled = this@MainSubscriptionFragment.autoFetchDisabled
}
......
package fr.chenry.android.freshrss.store.viewmodels
import android.app.Application
import androidx.lifecycle.*
import fr.chenry.android.freshrss.F
import fr.chenry.android.freshrss.R
import fr.chenry.android.freshrss.store.database.models.Account
import fr.chenry.android.freshrss.store.database.models.VoidAccount
......@@ -15,24 +15,24 @@ import org.joda.time.format.DateTimeFormat
import org.joda.time.format.PeriodFormat
import java.util.concurrent.TimeUnit
class AccountVM: ViewModel() {
class AccountVM(application: Application): AndroidViewModel(application) {
private val ticker = Flowable
.interval(0, 30, TimeUnit.SECONDS)
.timeInterval()
.observeOn(AndroidSchedulers.mainThread())
private val source = F.db.getAccount().map {it.firstOrElse(VoidAccount)}
private val source = requireF().db.getAccount().map {it.firstOrElse(VoidAccount)}
val account: LiveData<Account> = source.toLiveData()
val unreadCount: Int inline get() = account.value?.unreadCount ?: 0
val lastFetchText: LiveData<ResourceResolver> by lazy {
val lastFetchText: LiveData<IResourceResolver> by lazy {
Flowable.combineLatest(listOf(ticker, source)) {(_, it) ->
runBlocking {
withContext(Dispatchers.IO) {
it as Account
if(it.lastFetchDate.millis == 0L) return@withContext emptyStringResourceResolver()
if(it.lastFetchDate.millis == 0L) return@withContext emptyStringRes()
val sevenDaysAgo = DateTime.now(getUserTimeZone()).minusDays(7)
......@@ -40,16 +40,16 @@ class AccountVM: ViewModel() {
val period = it.lastFetchDate.period(DateTime.now())
if(period.toStandardDuration().isShorterThan(Duration(20_000)))
idResResolver(R.string.last_sync_a_few_seconds_ago)
idResRes(R.string.last_sync_a_few_seconds_ago)
else
idResResolver(R.string.last_sync_relative_date_text) {
+functionResourceResolver {
idResRes(R.string.last_sync_relative_date_text) {
+functionRes {
PeriodFormat.wordBased(userLocale).print(period)
}
}
} else {
idResResolver(R.string.last_sync_absolute_date_text) {
+functionResourceResolver {
idResRes(R.string.last_sync_absolute_date_text) {
+functionRes {
it.lastFetchDate.toString(DateTimeFormat.patternForStyle("M-", userLocale))
}
}
......
......@@ -13,7 +13,6 @@ import kotlinx.coroutines.*
import java.util.concurrent.TimeUnit
sealed class SubscriptionsVM(application: Application): AndroidViewModel(application) {
val subscriptions: LiveData<FeedLikeViewItems> = liveData(block = ::liveDataBlock)
protected abstract val grouper: Grouper
......@@ -117,4 +116,3 @@ class SubscriptionsForCategoryVM(
override val rawCategories: Flowable<List<FeedTag>> = rawSubscriptions.map {listOf()}
}
@file:Suppress("unused")
package fr.chenry.android.freshrss.utils
import android.content.Context
import androidx.annotation.StringRes
sealed class ResourceResolver {
abstract fun resolve(context: Context): String
interface IResourceResolver {
operator fun invoke(context: Context): String
}
class FunctionResourceResolver internal constructor (private val cb: Context.() -> String): ResourceResolver() {
override fun resolve(context: Context): String = cb(context)
class FunctionResourceResolver internal constructor (private val cb: Context.() -> String): IResourceResolver {
override operator fun invoke(context: Context): String = cb(context)
}
object EmptyStringResourceResolver: ResourceResolver() {
override fun resolve(context: Context): String = ""
object EmptyStringResourceResolver: IResourceResolver {
override operator fun invoke(context: Context): String = ""
}
class IdResResolver internal constructor(
class IdResourceResolver @JvmOverloads internal constructor(
@StringRes private val resId: Int,
vararg args: Any = arrayOf()
): ResourceResolver() {
private val formatArgs: MutableList<Any> = mutableListOf()
operator fun Any.unaryPlus() = this@IdResResolver.formatArgs.add(this)
): IResourceResolver {
private val formatArgs: MutableList<Any> = mutableListOf(args)
fun add(any: Any) = this@IdResourceResolver.formatArgs.add(any)
operator fun Any.unaryPlus() = add(this)
fun addAll(all: Collection<Any>) = this@IdResourceResolver.formatArgs.addAll(all)
fun addAll(all: Iterable<Any>) = this@IdResourceResolver.formatArgs.addAll(all)
fun addAll(all: Sequence<Any>) = this@IdResourceResolver.formatArgs.addAll(all)
fun addAll(vararg all: Any) = this@IdResourceResolver.formatArgs.addAll(all)
override fun resolve(context: Context): String {
override operator fun invoke(context: Context): String {
val fargs = formatArgs.map {
if(it is ResourceResolver) it.resolve(context) else it
if(it is IResourceResolver) it(context) else it
}
return if(fargs.isEmpty()) context.getString(resId) else context.getString(resId, *fargs.toTypedArray())
}
}
fun emptyStringResourceResolver() = EmptyStringResourceResolver
fun emptyStringRes() = EmptyStringResourceResolver
fun idResResolver(
@JvmOverloads
fun idResRes(
@StringRes resId: Int,
vararg args: Any = arrayOf(),
cb: IdResResolver.() -> Unit = {}
) = IdResResolver(resId, args).apply(cb)
cb: IdResourceResolver.() -> Unit = {}
) = IdResourceResolver(resId, args).apply(cb)
fun functionResourceResolver(cb: Context.() -> String) = FunctionResourceResolver(cb)
fun functionRes(cb: Context.() -> String) = FunctionResourceResolver(cb)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment