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

Refacto Store to simplify

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