Skip to content

Instantly share code, notes, and snippets.

Slightly Busy

Rohit Surwase RohitSurwase

Slightly Busy
View GitHub Profile
RohitSurwase / CoroutineIntentService.kt
Last active Nov 18, 2021
IntentService (Service) using Kotlin Coroutines instead of Handler+Looper.
View CoroutineIntentService.kt
import android.content.Intent
import android.os.IBinder
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlin.coroutines.CoroutineContext
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 / 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
override fun process(viewEvent: EVENT) {
Log.d(TAG, "processing viewEvent : $viewEvent")
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 / 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()
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 / 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 / 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) {
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")