Commit d82382d9 authored by Christophe Henry's avatar Christophe Henry

Solves #4: Display is not refreshed as the data change

parent 3bc4c92e
package fr.chenry.android.freshrss.components.subscriptions
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import fr.chenry.android.freshrss.store.*
import fr.chenry.android.freshrss.store.dao.common.Subscription
import fr.chenry.android.freshrss.store.dao.common.Subscriptions
class AllSubscriptionsFragment: SubscriptionsFragment() {
override val subscriptions: Subscriptions get() = Store.subscriptions.values
override val liveData: LiveData<Subscriptions> = MutableLiveData<Subscriptions>().apply {
this.value = Store.subscriptions.values
Store.subscriptions.observeForever {this.value = Store.subscriptions.values}
}
override fun onClick(subscription: Subscription) {
Router.navigate(RouteName.SUBSCRIPTION_ARTICLES_ALL, mapOf("id" to subscription.id))
......
package fr.chenry.android.freshrss.components.subscriptions
import android.os.Bundle
import androidx.lifecycle.*
import fr.chenry.android.freshrss.store.RouteName
import fr.chenry.android.freshrss.store.Router
import fr.chenry.android.freshrss.store.dao.common.Subscription
import fr.chenry.android.freshrss.store.dao.common.Subscriptions
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.UnreadSubscriptionsViewModel
class FavoritesSubscriptionsFragment: SubscriptionsFragment() {
override val liveData = MutableLiveData<Subscriptions>().apply {this.value = listOf()}
override fun onClick(subscription: Subscription) {
Router.navigate(RouteName.SUBSCRIPTION_ARTICLES_UNREAD, mapOf("id" to subscription.id))
}
}
......@@ -3,12 +3,14 @@ package fr.chenry.android.freshrss.components.subscriptions
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import fr.chenry.android.freshrss.R
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.dao.common.Subscription
import fr.chenry.android.freshrss.store.dao.common.Subscriptions
import fr.chenry.android.freshrss.utils.e
import fr.chenry.android.freshrss.utils.getOrDefault
import kotlinx.android.synthetic.main.fragment_main_subscription.*
import kotlin.reflect.KClass
......@@ -17,7 +19,7 @@ class MainSubscriptionFragment: Fragment() {
private val fragment: KClass<out SubscriptionsFragment>
get() = when(Store.subscriptionsSection.value) {
SubscriptionSection.ALL -> AllSubscriptionsFragment::class
SubscriptionSection.FAVORITES -> AllSubscriptionsFragment::class
SubscriptionSection.FAVORITES -> FavoritesSubscriptionsFragment::class
SubscriptionSection.UNREAD -> UnreadSubscriptionsFragment::class
else -> TODO("Handle bad section")
}
......@@ -73,14 +75,18 @@ class MainSubscriptionFragment: Fragment() {
}
abstract class SubscriptionsFragment: Fragment() {
abstract val subscriptions: Subscriptions
abstract val liveData: LiveData<Subscriptions>
val subscriptions: Subscriptions get() = liveData.value ?: listOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_subscriptions, container, false)
view.findViewById<RecyclerView>(R.id.subscriptions_list).let {
it.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
it.adapter = RecyclerViewAdapter(this)
it.adapter = RecyclerViewAdapter(this).apply {
liveData.observeForever {this.notifyDataSetChanged()}
}
}
return view
......
......@@ -9,9 +9,8 @@ import fr.chenry.android.freshrss.store.dao.common.Subscriptions
import fr.chenry.android.freshrss.store.databindingsupport.viewmodels.UnreadSubscriptionsViewModel
class UnreadSubscriptionsFragment: SubscriptionsFragment() {
private lateinit var model: UnreadSubscriptionsViewModel
override val subscriptions: Subscriptions get() = model.subscriptions
override val liveData get() = model.subscriptions
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......
package fr.chenry.android.freshrss.store.dao.common
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import fr.chenry.android.freshrss.BR
import fr.chenry.android.freshrss.store.dao.store.ItemId
import fr.chenry.android.freshrss.utils.escapeHtml4
import fr.chenry.android.freshrss.utils.unescapeHtml4
......@@ -16,13 +19,18 @@ typealias Subscriptions = List<Subscription>
@Serializable
data class Subscription(
val id: String,
@Serializable(with = HtmlEntitiesSerializer::class) val title: String,
@Serializable(with = HtmlEntitiesSerializer::class)
val title: String,
val categories: List<SubscriptionCategory>,
val url: String,
val htmlUrl: String
) {
@Transient
): BaseObservable() {
@Transient @get:Bindable
var unreadCount = 0
set(value) {
field = value
notifyPropertyChanged(BR.unreadCount)
}
@Transient
var unreadList = listOf<ItemId>()
}
......
......@@ -14,7 +14,10 @@ class UnreadSubscriptionsViewModel: ViewModel() {
}
}
val subscriptions: Subscriptions get() = liveData.value?.values ?: listOf()
val subscriptions = MutableLiveData<Subscriptions>().apply {
this.value = liveData.value?.values ?: listOf()
liveData.observeForever {this.value = liveData.value?.values ?: listOf()}
}
private fun load(): MapList<String, Subscription> {
if(Store.subscriptions.isNullOrEmpty()) return MapList()
......
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