Skip to content

Instantly share code, notes, and snippets.

View RohitSurwase's full-sized avatar
💭
Slightly Busy

Rohit Surwase RohitSurwase

💭
Slightly Busy
View GitHub Profile
@RohitSurwase
RohitSurwase / AacMviDH.kt
Created February 13, 2021 08:32
Parent DataHolder for easy implementation of DataHolder for Activities.
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 February 13, 2021 08:27
AacMviViewModel for Activities with DataHolder
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")
@RohitSurwase
RohitSurwase / MainViewEvent.kt
Created March 25, 2020 13:57
ViewEvent example
sealed class MainViewEvent {
data class NewsItemClicked(val newsItem: NewsItem) : MainViewEvent()
object FabClicked : MainViewEvent()
object OnSwipeRefresh : MainViewEvent()
object FetchNews : MainViewEvent()
}
@RohitSurwase
RohitSurwase / MainViewEffect.kt
Created March 25, 2020 13:54
ViewEffect example
sealed class MainViewEffect {
data class ShowSnackbar(val message: String) : MainViewEffect()
data class ShowToast(val message: String) : MainViewEffect()
}
@RohitSurwase
RohitSurwase / MainViewState.kt
Last active March 25, 2020 13:58
MainViewState : ViewState example
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 March 4, 2020 23:16
Implementation of AacMviActivity abstract activity class,
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 March 4, 2020 23:10
Implementation of AacMviViewModel generic base class.
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 March 2, 2020 16:33
Base AndroidViewModel for MVI architecture using LiveData and ViewModel.
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 March 2, 2020 16:32
Abstract Activity for MVI architecture using LiveData and ViewModel.
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 February 25, 2020 02:09
UI observes MainViewState model for changes.
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()