Skip to content

Instantly share code, notes, and snippets.

View ericntd's full-sized avatar

Eric Nguyen ericntd

View GitHub Profile
@ericntd
ericntd / Android Studio .gitignore
Created July 5, 2016 03:24 — forked from iainconnor/Android Studio .gitignore
A .gitignore for use in Android Studio
# Built application files
/*/build/
# Crashlytics configuations
com_crashlytics_export_strings.xml
# Local configuration file (sdk path, etc)
local.properties
# Gradle generated files
@ericntd
ericntd / MainActivity.java
Last active November 21, 2017 06:26
MainActivity in MVC
/* Full source [here](https://github.com/ericn37/Github-Search/blob/master/app/src/main/java/tech/ericntd/githubsearch/MainActivity.java) */
public class MainActivity extends AppCompatActivity {
private ReposRvAdapter rvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@ericntd
ericntd / SearchPresenter.java
Last active November 21, 2017 06:27
SearchPresenter in MVP
/* Full source [here](https://github.com/ericn37/Github-Search/blob/step-1-mvp/app/src/main/java/tech/ericntd/githubsearch/search/SearchPresenter.java) */
public class SearchPresenter implements SearchPresenterContract, GitHubRepository
.GitHubRepositoryCallback {
private final SearchViewContract viewContract;
private final GitHubRepository repository;
SearchPresenter(@NonNull final SearchViewContract viewContract,
@NonNull final GitHubRepository repository) {
this.viewContract = viewContract;
@ericntd
ericntd / MainActivity.java
Last active November 21, 2017 06:22
MainActivity in MVP
/* Full source [here](https://github.com/ericn37/Github-Search/blob/step-1-mvp/app/src/main/java/tech/ericntd/githubsearch/search/MainActivity.java) */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
@ericntd
ericntd / SearchPresenterTest.java
Last active November 22, 2017 08:55
SearchPresenterTest in MVP
/* Full source [here](https://github.com/ericn37/Github-Search/blob/step-2-unit-tests/app/src/test/java/tech/ericntd/githubsearch/search/SearchPresenterTest.java) */
public class SearchPresenterTest {
private SearchPresenter presenter;
@Mock
private GitHubRepository repository;
@Mock
private SearchViewContract viewContract;
@Before
@ericntd
ericntd / activity_some.xml
Last active April 5, 2018 09:35
Dummy xml layout with data binding
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="vm"
type="SomeViewModel" />
</data>
<LinearLayout>
@ericntd
ericntd / rxjava-testing.md
Created April 30, 2018 08:05
How to test RxJava chain single emission

Given the following SomePresenter/ SomeViewModel.java

void someAction(){
  subscription = someObservable()
                    .subscribeOn(getBgThread())
                    .compose(screenProgressDialog.bindLoading())
                    .observeOn(getUiThread())
                    .subscribe(new Subscriber<GetDailyHistoryEvent>() {
                        @Override
@ericntd
ericntd / RxJava interval tests.java
Created October 29, 2018 08:44
How to test repetitive actions done using RxJava 2?
@Test
public void doReps() {
// Make sure you are swapping the actual Scheduler with our TestScheduler object here
TestScheduler testScheduler = new TestScheduler();
Mockito.doReturn(testScheduler)
.when(viewModel)
.getSchedulerIo();
// Other preparations
Mockito.doReturn(Single.just(123))
.when(viewModel)
@ericntd
ericntd / naive-viewmodel.kt
Last active September 9, 2021 17:05
Supercharged MVVM part 1 - naive ViewModel
val isLoading = MutableLiveData<Boolean>() // for loading animation
val userName = MutableLiveData<String>()
val avatarSrc = MutableLiveData<String>()
val errorMessage = MutableLiveData<String>()
init {
isLoading.value = true
interactor.getProfile().let { getProfileState ->
when (getProfileState) {
is GetProfileInteractor.State.Success -> {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpObservation()
}
private fun setUpObservation() {
viewModel.isLoading.observe(this, Observer {
loader.visibility = if (it) View.VISIBLE else View.GONE