Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Slightly Busy

Rohit Surwase RohitSurwase

💭
Slightly Busy
View GitHub Profile
@RohitSurwase
RohitSurwase / ViewPagerFirstFragment.class
Last active Dec 4, 2017
Calling Viewpager Fragment's Function on it's Visibility - Solid Solution.
View ViewPagerFirstFragment.class
private boolean notFirstTime;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && notFirstTime) {
//TODO: Call function to be executed every time.
requestData();
}
}
@RohitSurwase
RohitSurwase / Robo_Script.json
Created Oct 20, 2019
Example of a Robo Script.
View Robo_Script.json
[
{
"eventType": "VIEW_CLICKED",
"timestamp": 1570986000016,
"replacementText": "",
"actionCode": -1,
"delayTime": 0,
"canScrollTo": false,
"elementDescriptors": [
{
@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()
@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 / 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 / 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
}
View MainViewEffect.kt
sealed class MainViewEffect {
data class ShowSnackbar(val message: String) : MainViewEffect()
data class ShowToast(val message: String) : MainViewEffect()
}
View MainViewEvent.kt
sealed class MainViewEvent {
data class NewsItemClicked(val newsItem: NewsItem) : MainViewEvent()
object FabClicked : MainViewEvent()
object OnSwipeRefresh : MainViewEvent()
object FetchNews : MainViewEvent()
}
@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 / 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")