Skip to content

Instantly share code, notes, and snippets.

View SabagRonen's full-sized avatar

Ronen Sabag SabagRonen

View GitHub Profile
@SabagRonen
SabagRonen / Presentation.java
Created October 6, 2018 13:38
Why Should You Wrap LiveData With Your Own Abstraction
interface MyGeneralObserver<T> {
void notifyUpdate(T value);
}
interface ObservableViewState {
void observeTitle(LifecycleOwner lifecycleOwner, MyGeneralObserver<String> observer);
}
interface IScreenPresenter extends ObservableViewState {
void loadData();
@SabagRonen
SabagRonen / Observer.kt
Created October 1, 2018 07:27
Why Should You Wrap LiveData With Your Own Abstraction - use directly
private val backgroundColorLiveData = MutableLiveData<Int>()
override fun observeTitleBackgroundColorWithDefaultValue(lifecycle: Lifecycle, observer: (Int) -> Unit) {
observer(R.color.colorAccent)
backgroundColorLiveData.observe({lifecycle}) {
it?.let(observer)
}
}
@SabagRonen
SabagRonen / ScreenViewTests.kt
Created October 1, 2018 06:58
Why Should You Wrap LiveData With Your Own Abstraction - testing
@RunWith(AndroidJUnit4::class)
class ScreenViewTests {
@get:Rule val testRule = createTestRuleWithFakeScreenViewInjector {
presenter = mockPresenter
}
private val mockPresenter = mock(IScreenPresenter::class.java)
@Test
fun someTest() {
val captor = lambdaArgumentCaptor<(String) -> Unit>()
@SabagRonen
SabagRonen / Presentation.kt
Last active October 1, 2018 06:31
Why Should You Wrap LiveData With Your Own Abstraction
interface ObservableViewState {
fun observeTitle(lifecycle: Lifecycle, observer: (String) -> Unit)
}
interface IScreenPresenter : ObservableViewState {
fun loadData()
}
class ScreenPresentationState : ObservableViewState {
private val titleLiveData = MutableLiveData<String>()
@SabagRonen
SabagRonen / SomeDomainLayerClass.kt
Created July 7, 2018 17:54
Android Jetpack Adoption At Gett post - domain layer
class JobCanceler : LifecycleObserver {
var job: Job? = null
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun cancelJob() {
job?.cancel()
}
}
@SabagRonen
SabagRonen / Contract.kt
Created July 7, 2018 14:46
Android Jetpack Adoption At Gett post - new way to write contract
sealed class ProgressBarState
object ProgressBarShowState : ProgressBarState()
object ProgressBarHideState : ProgressBarState()
interface SomeScreenViewObservers {
fun observeProgressBarState(lifecycle: Lifecycle, observer: (ProgressBarState) -> Unit)
}
interface IPresenter {
@SabagRonen
SabagRonen / Contract.java
Last active July 7, 2018 09:19
Android Jetpack Adoption At Gett post - defining MVP contract
public class SomeScreenContract {
public interface View {
void showProgressBar();
void hideProgressBar();
}
public interface IPresenter {
void startSomethingButtonClicked();
}
}
@SabagRonen
SabagRonen / FragmentInteraction.kt
Created June 1, 2018 16:45
What Problems Exist With Multi Activities blog post
class SharedUiData
class FragmentInteractionViewModel : ViewModel() {
val interactionLiveData = MutableLiveData<SharedUiData>()
}
class FragmentA : Fragment() {
fun publishNextUiDataToNextScreen(sharedUiData: SharedUiData) {
val viewModel = ViewModelProviders.of(activity)
.get(FragmentInteractionViewModel::class.java)
@SabagRonen
SabagRonen / MockActivityInjector.kt
Created April 28, 2018 08:02
Mock Activity Injector
@RunWith(AndroidJUnit4::class)
class MainViewTests {
val mockUserAction = mock(MainContract.UserAction::class.java)
@get:Rule
val activityTestRule = object : ActivityTestRule<MainActivity>(MainActivity::class.java, true, true) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
val myApp = InstrumentationRegistry.getTargetContext().applicationContext as MyApp
myApp.dispatchingActivityInjector = createFakeFragmentInjector<MainFragment> {
@SabagRonen
SabagRonen / Utils.kt
Created March 17, 2018 13:48
response to fragment post
package com.shop.app
import android.app.Activity
import android.support.test.InstrumentationRegistry
import android.support.v4.app.Fragment
import com.shop.app.common.BaseActivity
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.DispatchingAndroidInjector_Factory
import dagger.android.dispatchingActivityInjector