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 / ViewPagerFirstFragment.class
Last active December 4, 2017 11:35
Calling Viewpager Fragment's Function on it's Visibility - Solid Solution.
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 / CoroutineIntentService.kt
Last active May 22, 2024 22:54
IntentService (Service) using Kotlin Coroutines instead of Handler+Looper.
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.support.annotation.Nullable
import android.support.annotation.WorkerThread
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.actor
import kotlin.coroutines.CoroutineContext
@RohitSurwase
RohitSurwase / Robo_Script.json
Created October 20, 2019 12:57
Example of a Robo Script.
[
{
"eventType": "VIEW_CLICKED",
"timestamp": 1570986000016,
"replacementText": "",
"actionCode": -1,
"delayTime": 0,
"canScrollTo": false,
"elementDescriptors": [
{
@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()
@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 / 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 / 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 / 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 / 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 / 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()
}