This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ... | |
@RunWith(AndroidJUnit4ClassRunner::class) | |
class HomeActivityTest: KoinTest { | |
@get:Rule | |
var intentRule = IntentsTestRule(HomeActivity::class.java, true, false) | |
lateinit var mockModule: Module | |
lateinit var mockedInteractor: HomeInteractor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeViewModel(val homeInteractor: HomeInteractor) { | |
private val resultLiveData = MutableLiveData<Result<String>>() | |
fun getResultFromApi(): LiveData<Result<String>> { | |
homeInteractor.getResultFromApi() | |
.startWith(Result.InProgress) | |
.subscribe({ someString -> | |
resultLiveData.value = Result.Success(someString) | |
}, { err -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.koin.androidx.viewmodel.ext.android.viewModel | |
... | |
class HomeActivity: AppCompatActivity() { | |
private val homeViewModel by viewModel<HomeViewModel>() | |
override fun onCreate(..) { | |
... | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeApplication: Application() { | |
internal lateinit var koinApplication: KoinApplication | |
override fun onCreate() { | |
super.onCreate() | |
koinApplication = startKoin { | |
androidContext(this@HomeApplication) | |
modules(apiModule, interactorModule, viewModelModule) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
val mainFragment by fragmentByTagOrNew(MainFragment.TAG) { | |
MainFragment().withArgs { | |
putString(MainFragment.KEY_TEXT, "Now this won't crash") | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Fragment> AppCompatActivity.fragmentByTagOrNew( | |
tag: String, noinline factory: () -> T | |
): Lazy<T> { | |
return lazy(LazyThreadSafetyMode.NONE) { | |
supportFragmentManager.findFragmentByTag(tag) as? T ?: factory() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
val bundle = Bundle().apply { | |
putString(MainFragment.KEY_TEXT, "Now this won't crash") | |
} | |
val mainFragment = MainFragment().apply { | |
arguements = bundle | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val appModule = module { | |
single { //Existing koin dependencies } | |
fragment { MainFragment("Now this won't crash") } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private val mainFragment by fragmentFromFactory<MainFragment>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
setupKoinFragmentFactory() //This has to be done before super call | |
super.onCreate(savesInstanceState) | |
supportFragmentManager.beginTransaction() | |
.add(R.layout.fragmentContainerView, mainFragment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Fragment> AppCompatActivity.fragmentFromFactory(): Lazy<T> { | |
return lazy { | |
supportFragmentManager.fragmentFactory.instantiate(classLoader, T::class.java.name) as T | |
} | |
} |