Commit 66ac5bdb authored by Vladislav's avatar Vladislav

restore model;

settings sub checker
parent 9ed39b84
package com.biganto.visual.roompark.presentation.screen.settings
import com.biganto.visual.roompark.conductor.BigantoBaseContract
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import io.reactivex.Observable
/**
......@@ -11,5 +12,6 @@ interface SettingsScreen : BigantoBaseContract<SettingsScreenViewState> {
fun signOut(): Observable<Int>
fun clearCache(): Observable<Int>
fun refreshCacheInfo(): Observable<Int>
fun onSubscription(): Observable<SubscriptionModel>
}
......@@ -12,11 +12,13 @@ import com.biganto.visual.roompark.base.HeaderToolbarModel
import com.biganto.visual.roompark.base.RoomParkApplication
import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.conductor.BigantoBaseController
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.presentation.screen.settings.util.CahcedListAdapter
import com.biganto.visual.roompark.presentation.screen.settings.util.PushListAdapter
import com.biganto.visual.roompark.presentation.screen.splash.SplashScreenController
import com.biganto.visual.roompark.util.extensions.bytesToSize
import com.biganto.visual.roompark.util.extensions.setGone
import com.biganto.visual.roompark.util.monades.ExceptionString
import com.bluelinelabs.conductor.RouterTransaction
import com.bluelinelabs.conductor.changehandler.FadeChangeHandler
import com.google.android.material.textview.MaterialTextView
......@@ -50,8 +52,17 @@ class SettingsScreenController :
.observeOn(AndroidSchedulers.mainThread())
private val refreshEmitter = BehaviorRelay.create<Int>()
override fun refreshCacheInfo(): Observable<Int> = refreshEmitter
override fun onSubscription(): Observable<SubscriptionModel> =
(pushRecycler.adapter as PushListAdapter)
.onItemClicked
.map { it.subModel }
.debounce ( 600, TimeUnit.MILLISECONDS )
.observeOn(AndroidSchedulers.mainThread())
override fun injectDependencies() {
getComponent()
}
......@@ -133,6 +144,8 @@ class SettingsScreenController :
is SettingsScreenViewState.OnCacheDeleting -> render(viewState)
is SettingsScreenViewState.LoadCachInfo -> render(viewState)
is SettingsScreenViewState.LoadSubscriptions -> render(viewState)
is SettingsScreenViewState.SubscriptionStatus -> render(viewState)
is SettingsScreenViewState.SubscriptionError -> render(viewState)
}
}
......@@ -148,11 +161,21 @@ class SettingsScreenController :
(pushRecycler.adapter as PushListAdapter).setItems(viewState.list)
}
private fun render(viewState: SettingsScreenViewState.LoadCachInfo){
(cachedRecycler.adapter as CahcedListAdapter).setItems(viewState.list)
}
private fun render(viewState: SettingsScreenViewState.SubscriptionStatus){
(pushRecycler.adapter as PushListAdapter).setSubState(viewState.subId,viewState.subState)
}
private fun render(viewState: SettingsScreenViewState.SubscriptionError){
(pushRecycler.adapter as PushListAdapter).setSubState(viewState.subId,viewState.subState)
if (viewState.subState)
showError(ExceptionString(R.string.unsubscribe_error_message,null))
else showError(ExceptionString(R.string.subscribe_error_message,null))
}
private fun render(viewState: SettingsScreenViewState.OnCacheDeleting){
val isProgressed = viewState.progress>=1f
......
......@@ -3,6 +3,9 @@ package com.biganto.visual.roompark.presentation.screen.settings
import android.content.Context
import com.biganto.visual.roompark.conductor.BigantoBasePresenter
import com.biganto.visual.roompark.domain.interactor.SettingsInteractor
import com.biganto.visual.roompark.domain.model.CachedDataModel
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.TitledSubscriptionModel
import com.biganto.visual.roompark.util.monades.ExceptionString
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
......@@ -21,16 +24,54 @@ class SettingsScreenPresenter @Inject constructor(
)
: BigantoBasePresenter<SettingsScreen, SettingsScreenViewState>() {
private val restoreModel = RestoreModel(mutableListOf(), arrayListOf(), arrayListOf())
override fun detachView() {
super.detachView()
restoreStateObservable.accept(SettingsScreenViewState.RestoreView(restoreModel))
}
override fun defaultErrorViewStateHandler() =
{ e: ExceptionString -> SettingsScreenViewState.SomeError(e) }
override fun bindIntents() {
val onSubChecked = intent(SettingsScreen::onSubscription)
.flatMap { sub ->
interactor.switchSubscription(sub, !sub.state)
.andThen(
Observable.just<SettingsScreenViewState>(
SettingsScreenViewState.SubscriptionStatus(
sub.id, !sub.state
)
)
.doOnNext {
val ind = restoreModel
.subs
.indexOfFirst { it.subModel.id == sub.id }
restoreModel.subs[ind] =
TitledSubscriptionModel(
title = restoreModel.subs[ind].title,
subModel = SubscriptionModel(
topic = sub.topic,
id = restoreModel.subs[ind].subModel.id,
state = !sub.state
)
)
}
)
.onErrorReturn { SettingsScreenViewState.SubscriptionError(sub.id, sub.state) }
}
val fetchSettings = interactor.fetchSettings()
.map { SettingsScreenViewState.LoadSettingsList(it) }
val fetchCache = interactor.getCacheInfo()
.doOnNext { cached -> cached.sortBy { it.id } }
.doOnNext { restoreModel.cacheInfo = it }
.map { SettingsScreenViewState.LoadCachInfo(it) }
val fetchSubscriptions = interactor.getSubscriptions()
......@@ -64,11 +105,12 @@ class SettingsScreenPresenter @Inject constructor(
arrayListOf(
restoreStateObservable,
fetchSettings,
fetchSettings,
onSignOut,
onClearCache,
refreshInfo,
fetchSubscriptions
fetchSubscriptions,
fetchCache,
onSubChecked
)
)
.doOnError { Timber.e(it) }
......@@ -80,3 +122,11 @@ class SettingsScreenPresenter @Inject constructor(
}
}
data class RestoreModel(
var subs: MutableList<TitledSubscriptionModel>,
var cacheInfo:MutableList<CachedDataModel>,
var offlineStoreData:MutableList<CachedDataModel>
)
\ No newline at end of file
......@@ -20,4 +20,7 @@ sealed class SettingsScreenViewState : BigantoBaseViewState() {
class OnCacheDeleting(val progress:Float) : SettingsScreenViewState()
class LoadSubscriptions(val list:List<TitledSubscriptionModel>) : SettingsScreenViewState()
class LoadCachInfo(val list: List<CachedDataModel>) : SettingsScreenViewState()
class SubscriptionStatus(val subId:Int,val subState: Boolean) : SettingsScreenViewState()
class SubscriptionError(val subId:Int,val subState: Boolean) : SettingsScreenViewState()
class RestoreView(val restore:RestoreModel) : SettingsScreenViewState()
}
......@@ -9,6 +9,7 @@ import butterknife.BindView
import butterknife.ButterKnife
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.domain.model.CachedDataModel
import com.biganto.visual.roompark.domain.model.SubscriptionModel
import com.biganto.visual.roompark.domain.model.TitledSubscriptionModel
import com.biganto.visual.roompark.util.extensions.bytesToSize
import com.google.android.material.switchmaterial.SwitchMaterial
......@@ -97,6 +98,17 @@ class PushListAdapter : CommonRecyclerAdapter<PushViewHolder,TitledSubscriptionM
get() = PushViewHolder::class
override fun getVhLayout(): Int = R.layout.bell_switcher_with_text_viewholder
fun setSubState(subId:Int,state:Boolean){
val ind = list.indexOfFirst{ it.subModel.id == subId}
list[ind] = TitledSubscriptionModel(list[ind].title,SubscriptionModel(
topic = list[ind].subModel.topic,
id = list[ind].subModel.id,
state = state
))
notifyItemChanged(ind)
}
}
......
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