Commit 8bcbd87e authored by Christophe Henry's avatar Christophe Henry

Refacto Store to simplify

parent 3d013434
...@@ -21,8 +21,8 @@ class MainActivity: AppCompatActivity() { ...@@ -21,8 +21,8 @@ class MainActivity: AppCompatActivity() {
.bind {Store.getUnreadCount()} .bind {Store.getUnreadCount()}
.bind {all(Store.subscriptions.keys.map {Store.getStreamContents(it)}, cancelOthersOnError = false)} .bind {all(Store.subscriptions.keys.map {Store.getStreamContents(it)}, cancelOthersOnError = false)}
.bind {Store.getUnreadItems()} .bind {Store.getUnreadItems()}
.success {deferred.resolve()} .successUi {deferred.resolve()}
.fail(deferred::reject) .failUi(deferred::reject)
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
......
...@@ -5,12 +5,13 @@ import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection ...@@ -5,12 +5,13 @@ import fr.chenry.android.freshrss.components.subscriptions.SubscriptionSection
import fr.chenry.android.freshrss.store.api.Api import fr.chenry.android.freshrss.store.api.Api
import fr.chenry.android.freshrss.store.dao.common.StreamId import fr.chenry.android.freshrss.store.dao.common.StreamId
import fr.chenry.android.freshrss.store.dao.common.Subscription import fr.chenry.android.freshrss.store.dao.common.Subscription
import fr.chenry.android.freshrss.store.dao.store.* import fr.chenry.android.freshrss.store.dao.store.Article
import fr.chenry.android.freshrss.store.dao.store.ItemId
import fr.chenry.android.freshrss.store.databindingsupport.GenericLiveData import fr.chenry.android.freshrss.store.databindingsupport.GenericLiveData
import nl.komponents.kovenant.* import fr.chenry.android.freshrss.utils.e
import nl.komponents.kovenant.Kovenant.deferred import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.ui.failUi import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.ui.successUi import nl.komponents.kovenant.then
object Store { object Store {
var debugMode = false var debugMode = false
...@@ -23,43 +24,25 @@ object Store { ...@@ -23,43 +24,25 @@ object Store {
val favorites = GenericLiveData<String, Subscription>(mutableMapOf()) val favorites = GenericLiveData<String, Subscription>(mutableMapOf())
private var lastFetchTimestamp = 0L private var lastFetchTimestamp = 0L
fun login(instance: String, user: String, password: String): Promise<Unit, Exception> { fun login(instance: String, user: String, password: String): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.login(instance, user, password) bind {getToken()}
api.login(instance, user, password) success {
getToken() success (deferred::resolve) fail (deferred::reject)
} fail (deferred::reject)
return deferred.promise
}
fun getSubscriptions(): Promise<Unit, Exception> { fun getSubscriptions(): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.getSubscriptions() then {subscriptions.addAll(it.map {self -> self.id to self})}
api.getSubscriptions() successUi {
subscriptions.addAll(it.map {self -> self.id to self})
deferred.resolve(Unit)
} fail (deferred::reject)
return deferred.promise
}
fun getUnreadCount(): Promise<Unit, Exception> { fun getUnreadCount(): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.getUnreadCount() then {
api.getUnreadCount() successUi { totalUnreadCount.postValue(it.max)
totalUnreadCount.value = it.max
if(!subscriptions.isNullOrEmpty()) if(!subscriptions.isNullOrEmpty())
it.unreadcounts.forEach {self -> subscriptions[self.id]?.unreadCount = self.count} it.unreadcounts.forEach {self -> subscriptions[self.id]?.unreadCount = self.count}
subscriptions.emitUi() subscriptions.emit()
deferred.resolve(Unit) }
} fail (deferred::reject)
return deferred.promise
}
fun getStreamItems(id: String): Promise<Unit, Exception> { fun getStreamItems(id: String): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.getStreamItems(id) then {this.e("::getStreamItems: TODO"); Unit}
api.getStreamItems(id) success {} fail (deferred::reject)
return deferred.promise
}
fun getStreamContents(id: String): Promise<Unit, Exception> { fun getStreamContents(id: String): Promise<Unit, Exception> =
return api.getStreamContents(id) then { api.getStreamContents(id) then {
contentItems[id] = GenericLiveData( contentItems[id] = GenericLiveData(
it.items.map {item -> it.items.map {item ->
item.id to Article( item.id to Article(
...@@ -70,42 +53,29 @@ object Store { ...@@ -70,42 +53,29 @@ object Store {
item.author, item.author,
item.summary.content item.summary.content
) )
}.toMap()) }.toMap()
)
contentItems.emit() contentItems.emit()
} }
}
fun getTags(): Promise<Unit, Exception> { fun getTags(): Promise<Unit, Exception> = api.getTags() then {tags.postValue(it)}
val deferred = deferred<Unit, Exception>()
api.getTags() successUi {tags.value = it} failUi (deferred::reject)
return deferred.promise
}
fun getUnreadItems(): Promise<Unit, Exception> { fun getUnreadItems(): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.getUnreadItems(lastFetchTimestamp) then {
api.getUnreadItems(lastFetchTimestamp) successUi {
val unreadItems = it.items.groupBy {c -> c.origin.streamId} val unreadItems = it.items.groupBy {c -> c.origin.streamId}
unreadItems.forEach {u -> unreadItems.forEach {u ->
subscriptions[u.key]?.unreadList = u.value.sortedBy {i -> i.timestampUsec}.map {i -> i.id} subscriptions[u.key]?.unreadList = u.value.sortedBy {i -> i.timestampUsec}.map {i -> i.id}
} }
lastFetchTimestamp = System.currentTimeMillis() % 1000 lastFetchTimestamp = System.currentTimeMillis() % 1000
deferred.resolve() }
Unit
} failUi (deferred::reject)
return deferred.promise
}
fun getArticle(streamId: String, articleId: String): Promise<Article, ArticleNotFoundException> { fun getArticle(streamId: String, articleId: String): Promise<Article, ArticleNotFoundException> {
val article = contentItems[streamId]?.get(articleId) val article = contentItems[streamId]?.get(articleId)
return if(article == null) Promise.ofFail(ArticleNotFoundException(articleId)) else Promise.ofSuccess(article) return if(article == null) Promise.ofFail(ArticleNotFoundException(articleId)) else Promise.ofSuccess(article)
} }
private fun getToken(): Promise<Unit, Exception> { private fun getToken(): Promise<Unit, Exception> =
val deferred = deferred<Unit, Exception>() api.getToken() then {this.e("::getToken: TODO"); Unit}
this.api.getToken() success {deferred.resolve(Unit)} fail (deferred::reject)
return deferred.promise
}
} }
class ArticleNotFoundException(val id: String): Exception("Article with id $id cound not be found in store") class ArticleNotFoundException(val id: String): Exception("Article with id $id cound not be found in store")
\ No newline at end of file
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