Commit 1ebb4357 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Add AccountMainBadges entity and reactive cache for it.

parent dce33b65
......@@ -13,6 +13,7 @@ import ru.terrakok.gitlabclient.entity.app.session.OAuthParams
import ru.terrakok.gitlabclient.entity.app.session.UserAccount
import ru.terrakok.gitlabclient.model.data.cache.ProjectCache
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.data.state.AccountMainBadgesStateModel
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.MarkDownConverter
import toothpick.config.Module
......@@ -52,5 +53,8 @@ class ServerModule(userAccount: UserAccount?) : Module() {
//Error handler with logout logic
bind(ErrorHandler::class.java).singletonInScope()
//Account badges
bind(AccountMainBadgesStateModel::class.java).toInstance(AccountMainBadgesStateModel())
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.entity.app
data class AccountMainBadges(
val issueCount: Int,
val mergeRequestCount: Int,
val todoCount: Int
)
\ No newline at end of file
package ru.terrakok.gitlabclient.model.data.state
import com.jakewharton.rxrelay2.BehaviorRelay
import io.reactivex.Observable
import ru.terrakok.gitlabclient.entity.app.AccountMainBadges
class AccountMainBadgesStateModel {
private val relay = BehaviorRelay.create<AccountMainBadges>()
val observable: Observable<AccountMainBadges> = relay.hide()
fun accept(value: AccountMainBadges) {
relay.accept(value)
}
fun acceptIssueCount(value: Int) {
relay.value?.let { current ->
relay.accept(current.copy(issueCount = value))
}
}
fun acceptMrCount(value: Int) {
relay.value?.let { current ->
relay.accept(current.copy(mergeRequestCount = value))
}
}
fun acceptTodoCount(value: Int) {
relay.value?.let { current ->
relay.accept(current.copy(todoCount = value))
}
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.interactor.account
import io.reactivex.Observable
import ru.terrakok.gitlabclient.entity.app.AccountMainBadges
import ru.terrakok.gitlabclient.model.repository.account.AccountRepository
import javax.inject.Inject
class AccountInteractor @Inject constructor(
private val accountRepository: AccountRepository
) {
fun getAccountMainBadges(): Observable<AccountMainBadges> =
accountRepository.getAccountMainBadges()
}
\ No newline at end of file
......@@ -59,6 +59,4 @@ class IssueInteractor @Inject constructor(
issueId: Long,
body: String
) = issueRepository.createIssueNote(projectId, issueId, body)
fun getMyAssignedIssueCount() = issueRepository.getMyAssignedIssueCount()
}
\ No newline at end of file
......@@ -66,6 +66,4 @@ class MergeRequestInteractor @Inject constructor(
projectId: Long,
mergeRequestId: Long
) = mergeRequestRepository.getMergeRequestChanges(projectId, mergeRequestId)
fun getMyAssignedMergeRequestCount() = mergeRequestRepository.getMyAssignedMergeRequestCount()
}
\ No newline at end of file
......@@ -24,6 +24,4 @@ class TodoInteractor @Inject constructor(
page = page
)
}
fun getMyAssignedTodoCount() = todoRepository.getMyAssignedTodoCount()
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.account
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.functions.Function3
import ru.terrakok.gitlabclient.entity.app.AccountMainBadges
import ru.terrakok.gitlabclient.extension.getXTotalHeader
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.data.state.AccountMainBadgesStateModel
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
import javax.inject.Inject
class AccountRepository @Inject constructor(
private val api: GitlabApi,
private val badgesStateModel: AccountMainBadgesStateModel,
private val schedulers: SchedulersProvider
) {
fun getAccountMainBadges(): Observable<AccountMainBadges> =
badgesStateModel.observable
.startWith(refreshAllBadges().onErrorComplete().toObservable())
fun refreshAllBadges(): Completable =
Single
.zip<Int, Int, Int, AccountMainBadges>(
api.getMyAssignedIssueHeaders().map { it.getXTotalHeader() },
api.getMyAssignedMergeRequestHeaders().map { it.getXTotalHeader() },
api.getMyAssignedTodoHeaders().map { it.getXTotalHeader() },
Function3<Int, Int, Int, AccountMainBadges> { t1, t2, t3 ->
AccountMainBadges(t1, t2, t3)
}
)
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
.doOnSuccess { badgesStateModel.accept(it) }
.ignoreElement()
fun refreshIssueCount(): Completable =
api
.getMyAssignedIssueHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
.doOnSuccess { badgesStateModel.acceptIssueCount(it) }
.ignoreElement()
fun refreshMrCount(): Completable =
api
.getMyAssignedMergeRequestHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
.doOnSuccess { badgesStateModel.acceptMrCount(it) }
.ignoreElement()
fun refreshTodoCount(): Completable =
api
.getMyAssignedTodoHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
.doOnSuccess { badgesStateModel.acceptTodoCount(it) }
.ignoreElement()
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ import ru.terrakok.gitlabclient.entity.event.EventAction
import ru.terrakok.gitlabclient.entity.issue.Issue
import ru.terrakok.gitlabclient.entity.issue.IssueScope
import ru.terrakok.gitlabclient.entity.issue.IssueState
import ru.terrakok.gitlabclient.extension.getXTotalHeader
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
......@@ -219,10 +218,4 @@ class IssueRepository @Inject constructor(
.getMilestoneIssues(projectId, milestoneId, page, pageSize)
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
fun getMyAssignedIssueCount(): Single<Int> =
api.getMyAssignedIssueHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequest
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestScope
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestState
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestViewType
import ru.terrakok.gitlabclient.extension.getXTotalHeader
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
......@@ -267,10 +266,4 @@ class MergeRequestRepository @Inject constructor(
.getMilestoneMergeRequests(projectId, milestoneId, page, pageSize)
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
fun getMyAssignedMergeRequestCount(): Single<Int> =
api.getMyAssignedMergeRequestHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.todo
import io.reactivex.Single
import ru.terrakok.gitlabclient.di.DefaultPageSize
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.ShortUser
......@@ -11,7 +10,6 @@ import ru.terrakok.gitlabclient.entity.target.TargetType
import ru.terrakok.gitlabclient.entity.todo.Todo
import ru.terrakok.gitlabclient.entity.todo.TodoAction
import ru.terrakok.gitlabclient.entity.todo.TodoState
import ru.terrakok.gitlabclient.extension.getXTotalHeader
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
import javax.inject.Inject
......@@ -96,10 +94,4 @@ class TodoRepository @Inject constructor(
fun markPendingTodoAsDone(id: Int) = api.markPendingTodoAsDone(id)
fun markAllPendingTodosAsDone() = api.markAllPendingTodosAsDone()
fun getMyAssignedTodoCount(): Single<Int> =
api.getMyAssignedTodoHeaders()
.map { it.getXTotalHeader() }
.subscribeOn(schedulers.io())
.observeOn(schedulers.ui())
}
\ No newline at end of file
package ru.terrakok.gitlabclient.presentation.main
import com.arellomobile.mvp.InjectViewState
import io.reactivex.Single
import io.reactivex.functions.Function3
import ru.terrakok.gitlabclient.model.interactor.issue.IssueInteractor
import ru.terrakok.gitlabclient.model.interactor.mergerequest.MergeRequestInteractor
import ru.terrakok.gitlabclient.model.interactor.todo.TodoInteractor
import ru.terrakok.gitlabclient.model.interactor.account.AccountInteractor
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
import javax.inject.Inject
......@@ -14,25 +10,15 @@ import javax.inject.Inject
*/
@InjectViewState
class MainPresenter @Inject constructor(
private val issueInteractor: IssueInteractor,
private val mergeRequestInteractor: MergeRequestInteractor,
private val todoInteractor: TodoInteractor
private val accountInteractor: AccountInteractor
) : BasePresenter<MainView>() {
override fun onFirstViewAttach() {
super.onFirstViewAttach()
Single
.zip(
issueInteractor.getMyAssignedIssueCount(),
mergeRequestInteractor.getMyAssignedMergeRequestCount(),
todoInteractor.getMyAssignedTodoCount(),
Function3<Int, Int, Int, Triple<Int, Int, Int>> { issueCount, mergeRequestCount, todoCount ->
Triple(issueCount, mergeRequestCount, todoCount)
}
)
accountInteractor.getAccountMainBadges()
.subscribe(
{ viewState.setAssignedNotifications(it.first, it.second, it.third) },
{ viewState.setAssignedNotifications(it) },
{
// TODO: user activity badges (Maybe we can retry this request, until it finishes correctly?).
}
......
......@@ -3,11 +3,12 @@ package ru.terrakok.gitlabclient.presentation.main
import com.arellomobile.mvp.MvpView
import com.arellomobile.mvp.viewstate.strategy.OneExecutionStateStrategy
import com.arellomobile.mvp.viewstate.strategy.StateStrategyType
import ru.terrakok.gitlabclient.entity.app.AccountMainBadges
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 20.05.19.
*/
@StateStrategyType(OneExecutionStateStrategy::class)
interface MainView : MvpView {
fun setAssignedNotifications(issueCount: Int, mergeRequestCount: Int, todoCount: Int)
fun setAssignedNotifications(badges: AccountMainBadges)
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import kotlinx.android.synthetic.main.fragment_main.*
import ru.terrakok.cicerone.android.support.SupportAppScreen
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.entity.app.AccountMainBadges
import ru.terrakok.gitlabclient.extension.color
import ru.terrakok.gitlabclient.presentation.main.MainPresenter
import ru.terrakok.gitlabclient.presentation.main.MainView
......@@ -93,11 +94,16 @@ class MainFragment : BaseFragment(), MainView {
currentTabFragment?.onBackPressed()
}
override fun setAssignedNotifications(issueCount: Int, mergeRequestCount: Int, todoCount: Int) {
override fun setAssignedNotifications(badges: AccountMainBadges) {
with(bottomBar) {
setNotification(buildBottomBarNotification(R.color.fruit_salad, issueCount), 1)
setNotification(buildBottomBarNotification(R.color.brandy_punch, mergeRequestCount), 2)
setNotification(buildBottomBarNotification(R.color.mariner, todoCount), 3)
setNotification(buildBottomBarNotification(R.color.fruit_salad, badges.issueCount), 1)
setNotification(
buildBottomBarNotification(
R.color.brandy_punch,
badges.mergeRequestCount
), 2
)
setNotification(buildBottomBarNotification(R.color.mariner, badges.todoCount), 3)
}
}
......
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