Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Slightly Busy

Rohit Surwase RohitSurwase

💭
Slightly Busy
View GitHub Profile
@RohitSurwase
RohitSurwase / AacMviDH.kt
Created Feb 13, 2021
Parent DataHolder for easy implementation of DataHolder for Activities.
View AacMviDH.kt
open class AacMviDH<STATE, EFFECT> {
private val _states: MutableLiveData<STATE> = MutableLiveData()
val stateLiveData: LiveData<STATE>
get() = _states
private var _state: STATE? = null
var state: STATE
get() = _state
?: throw UninitializedPropertyAccessException("\"state\" was queried before being initialized")
@RohitSurwase
RohitSurwase / AacMviViewModelDH.kt
Created Feb 13, 2021
AacMviViewModel for Activities with DataHolder
View AacMviViewModelDH.kt
open class AacMviViewModelDH<STATE, EFFECT, EVENT>(application: Application, private val aacMviDH: AacMviDH<STATE, EFFECT>) :
AndroidViewModel(application), ViewModelContract<EVENT> {
fun viewStates(): LiveData<STATE> = aacMviDH.stateLiveData
fun viewEffects(): LiveData<EFFECT> = aacMviDH.effectLiveData
@CallSuper
override fun process(viewEvent: EVENT) {
Log.d(TAG, "processing viewEvent : $viewEvent")
View MainViewEvent.kt
sealed class MainViewEvent {
data class NewsItemClicked(val newsItem: NewsItem) : MainViewEvent()
object FabClicked : MainViewEvent()
object OnSwipeRefresh : MainViewEvent()
object FetchNews : MainViewEvent()
}
View MainViewEffect.kt
sealed class MainViewEffect {
data class ShowSnackbar(val message: String) : MainViewEffect()
data class ShowToast(val message: String) : MainViewEffect()
}
@RohitSurwase
RohitSurwase / MainViewState.kt
Last active Mar 25, 2020
MainViewState : ViewState example
View MainViewState.kt
data class MainViewState(val fetchStatus: FetchStatus, val newsList: List<NewsItem>)
sealed class FetchStatus {
object Fetching : FetchStatus()
object Fetched : FetchStatus()
object NotFetched : FetchStatus()
}
@RohitSurwase
RohitSurwase / MainActivity.kt
Created Mar 4, 2020
Implementation of AacMviActivity abstract activity class,
View MainActivity.kt
class MainActivity : AacMviActivity<MainViewState, MainViewEffect, MainViewEvent, MainActVM>() {
override val viewModel: MainActVM by viewModels()
override fun renderViewState(viewState: MainViewState) {
//Handle new viewState
}
override fun renderViewEffect(viewEffect: MainViewEffect) {
//Show effects
}
@RohitSurwase
RohitSurwase / MainActVM.kt
Created Mar 4, 2020
Implementation of AacMviViewModel generic base class.
View MainActVM.kt
class MainActVM(application: Application) :
AacMviViewModel<MainViewState, MainViewEffect, MainViewEvent>(application) {
init {
viewState = MainViewState(fetchStatus = FetchStatus.NotFetched, newsList = emptyList())
}
override fun process(viewEvent: MainViewEvent) {
super.process(viewEvent)
}
@RohitSurwase
RohitSurwase / AacMviViewModel.kt
Created Mar 2, 2020
Base AndroidViewModel for MVI architecture using LiveData and ViewModel.
View AacMviViewModel.kt
open class AacMviViewModel<STATE, EFFECT, EVENT>(application: Application) :
AndroidViewModel(application), ViewModelContract<EVENT> {
private val _viewStates: MutableLiveData<STATE> = MutableLiveData()
fun viewStates(): LiveData<STATE> = _viewStates
private var _viewState: STATE? = null
protected var viewState: STATE
get() = _viewState
?: throw UninitializedPropertyAccessException("\"viewState\" was queried before being initialized")
@RohitSurwase
RohitSurwase / AacMviActivity.kt
Created Mar 2, 2020
Abstract Activity for MVI architecture using LiveData and ViewModel.
View AacMviActivity.kt
abstract class AacMviActivity<STATE, EFFECT, EVENT, ViewModel : AacMviViewModel<STATE, EFFECT, EVENT>> :
AppCompatActivity() {
abstract val viewModel: ViewModel
private val viewStateObserver = Observer<STATE> {
Log.d(TAG, "observed viewState : $it")
renderViewState(it)
}
@RohitSurwase
RohitSurwase / MainActivityStateModel.kt
Created Feb 25, 2020
UI observes MainViewState model for changes.
View MainActivityStateModel.kt
data class MainViewState(val fetchStatus: FetchStatus, val newsList: List<NewsItem>)
sealed class MainViewEffect {
data class ShowSnackbar(val message: String) : MainViewEffect()
data class ShowToast(val message: String) : MainViewEffect()
}
sealed class MainViewEvent {
data class NewsItemClicked(val newsItem: NewsItem) : MainViewEvent()
object FabClicked : MainViewEvent()