Skip to content

Instantly share code, notes, and snippets.

View remcomokveld's full-sized avatar

Remco Mokveld remcomokveld

View GitHub Profile
public class DaggerAppComponent implements AppComponent {
public class MyActivitySubcomponentImpl implements ActivitySubcomponent {
public void inject(MyActivity activity) {...}
public class OnboardingFragmentSubcomponentImpl implements OnboardingFragmentSubcomponent {
public void inject(OnboardingFragment fragment) { ... }
}
public class WelcomeFragmentSubcomponentImpl implements WelcomeFragmentSubcomponent {
public void inject(WelcomeFragment fragment) { ... }
@RunWith(AndroidJUnit4::class)
class MyFragmentTest {
@Test
fun buttonIsEnabled() {
TestMyFragment.testViewModel = MyViewModel().also
it.updateState(enabled = true)
}
launchFragmentInContainer<TestMyFragment>()
onView(withId(R.id.button)).check(matches(isEnabled()))
}
class TestMyFragment : MyFragment() {
override fun injectMembers() {
this.viewModel = testViewModel
}
companion object {
// static property can be set before launching the Fragment
// to a mock instance
lateinit var testViewModel: MyViewModel
}
}
override fun onAttach(context: Context) {
injectMembers()
super.onAttach(context)
}
protected open fun injectMembers() =
AndroidSupportInjection.inject(this)
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
@RunWith(AndroidJUnit4::class)
class MyFragmentTest {
@Test
fun launchFragment() {
launchFragmentInContainer<MyFragment>()
}
}
import dagger.android.support.DaggerFragment
class MyFragment: DaggerFragment() {
@Inject lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.liveData.observe(this) { button.isEnabled = it }
}
}
@remcomokveld
remcomokveld / JsonClassDetector.kt
Last active March 23, 2021 15:18
Moshi Lint Check
/*
* Copyright (C) 2021 Remco Mokveld
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
package nl.remcomokveld.firebaseloaders;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.Loader;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software