Commit 353f37d1 authored by Christophe Henry's avatar Christophe Henry

Cleanup data binding support classes

parent 4f84e6b5
......@@ -15,8 +15,8 @@ import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.database.models.Article
import fr.chenry.android.freshrss.store.database.models.ReadStatus
import fr.chenry.android.freshrss.store.database.models.ReadStatus.UNREAD
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.SubscriptionArticlesVM
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.SubscriptionArticlesVMF
import fr.chenry.android.freshrss.store.viewmodels.SubscriptionArticlesVM
import fr.chenry.android.freshrss.store.viewmodels.SubscriptionArticlesVMF
import fr.chenry.android.freshrss.utils.capitalizeFull
import fr.chenry.android.freshrss.utils.e
import kotlinx.android.synthetic.main.fragment_subscription_article_detail.*
......
......@@ -14,7 +14,7 @@ import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection.*
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.database.models.*
import fr.chenry.android.freshrss.store.database.models.ReadStatus.READ
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.*
import fr.chenry.android.freshrss.store.viewmodels.*
import kotlinx.android.synthetic.main.fragment_subscriptions.*
class SubscriptionsFragment: Fragment(), Observer<Subscriptions> {
......
package fr.chenry.android.freshrss.store.databindingsupport
import androidx.lifecycle.LiveData
class GenericLiveData<T, U>(items: Map<T, U>): LiveData<MapList<T, U>>(){
private val mapList = MapList(items)
constructor(): this(mutableMapOf<T, U>())
fun emit() = super.postValue(mapList)
fun emitUi() = super.setValue(mapList)
fun isNullOrEmpty() = mapList.isNullOrEmpty()
fun filter(predicate: (Map.Entry<T, U>) -> Boolean) = GenericLiveData(mapList.filter(predicate))
fun addAll(items: Map<T, U>) {
mapList.addAll(items)
this.emit()
}
fun addAll(items: List<Pair<T, U>>) = addAll(items.toMap())
fun addAll(vararg item: Pair<T, U>) = addAll(item.toMap())
operator fun get(key: T) = mapList[key]
operator fun get(index: Int) = mapList[index]
operator fun set(key: T, value: U) = addAll(mapOf(key to value))
val size get() = mapList.size
val entries get() = mapList.entries
val keys get() = mapList.keys
val values get() = mapList.values
}
package fr.chenry.android.freshrss.store.databindingsupport
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
class MapList<T, U>(items: Map<T, U>): List<U>, Map<T, U> {
private val lock = ReentrantLock()
private var list = items.values.toMutableList()
private var map = mutableMapOf<T, U>().apply { this.putAll(items) }
constructor(vararg items: Pair<T, U>): this(items.toList().toMap())
constructor(items: Collection<Map.Entry<T, U>>): this(items.map {it.key to it.value}.toMap())
constructor(): this(mutableMapOf<T, U>())
override operator fun get(key: T) = lock.withLock {map[key]}
operator fun set(key: T, value: U) { addAll(mapOf(key to value)) }
fun addAll(items: Map<T, U>) {
lock.withLock {
items.forEach {
if(map.contains(it.key)) map[it.key] = it.value
else {
list.add(it.value)
map[it.key] = it.value
}
}
}
}
fun addAll(items: List<Pair<T, U>>) = addAll(items.toMap())
fun addAll(vararg item: Pair<T, U>) = addAll(item.toMap())
fun filter(predicate: (Map.Entry<T, U>) -> Boolean): MapList<T, U> = lock.withLock {MapList(map.filter(predicate))}
fun isNullOrEmpty() = lock.withLock {map.isNullOrEmpty()}
override val size get() = lock.withLock {list.size}
override fun contains(element: U) = lock.withLock {list.contains(element)}
override fun containsAll(elements: Collection<U>) = lock.withLock {list.containsAll(elements)}
override fun get(index: Int) = lock.withLock {list[index]}
override fun indexOf(element: U) = lock.withLock {list.indexOf(element)}
override fun isEmpty() = lock.withLock {list.isEmpty()}
override fun iterator() = lock.withLock {list.iterator()}
override fun lastIndexOf(element: U) = lock.withLock {list.lastIndexOf(element)}
override fun listIterator() = lock.withLock {list.listIterator()}
override fun listIterator(index: Int) = lock.withLock {list.listIterator(index)}
override fun subList(fromIndex: Int, toIndex: Int) = lock.withLock {list.subList(fromIndex, toIndex)}
override val entries: Set<Map.Entry<T, U>> get() = lock.withLock {map.entries}
override val keys: Set<T> get() = lock.withLock {map.keys}
override val values: List<U> get() = lock.withLock {list.toList()}
override fun containsKey(key: T) = lock.withLock {map.containsKey(key)}
override fun containsValue(value: U) = lock.withLock {map.containsValue(value)}
}
\ No newline at end of file
package fr.chenry.android.freshrss.store.databindingsupport.viewmodels
package fr.chenry.android.freshrss.store.viewmodels
import androidx.lifecycle.*
import fr.chenry.android.freshrss.store.database.FreshRSSDabatabase
......
package fr.chenry.android.freshrss.store.databindingsupport.viewmodels
package fr.chenry.android.freshrss.store.viewmodels
import androidx.lifecycle.*
import fr.chenry.android.freshrss.FreshRSSApplication
......
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