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 MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: LiveData<String?> = | |
authManager.observeUser().map { user -> user.id }.asLiveData() | |
val result: LiveData<Result<Item>> = userId.switchMap { newUserId -> | |
liveData { emit(repository.fetchItem(newUserId)) } | |
} | |
} |
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 MyViewModel(...) : ViewModel() { | |
val result: StateFlow<Result<UiState>> = flow { | |
emit(repository.fetchItem()) | |
}.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), // Or Lazily because it's a one-shot | |
initialValue = Result.Loading | |
) | |
} |
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 MyViewModel(...) : ViewModel() { | |
val result: LiveData<Result<UiState>> = liveData { | |
emit(Result.Loading) | |
emit(repository.fetchItem()) | |
} | |
} |
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 MyViewModel { | |
private val _myUiState = MutableStateFlow<Result<UiState>>(Result.Loading) | |
val myUiState: StateFlow<Result<UiState>> = _myUiState | |
// Load data from a suspend fun and mutate state | |
init { | |
viewModelScope.launch { | |
val result = ... | |
_myUiState.value = result | |
} |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
class MyViewModel { | |
private val _myUiState = MutableLiveData<Result<UiState>>(Result.Loading) | |
val myUiState: LiveData<Result<UiState>> = _myUiState | |
// Load data from a suspend fun and mutate state | |
init { | |
viewModelScope.launch { |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
@HiltAndroidTest | |
@UninstallModules(CoroutinesModule::class) | |
@RunWith(AndroidJUnit4::class) | |
class AgendaTest { | |
@get:Rule(order = 0) | |
var hiltRule = HiltAndroidRule(this) |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
@InstallIn(ApplicationComponent::class) | |
@Module | |
object TestCoroutinesModule { | |
@DefaultDispatcher | |
@Provides | |
fun providesDefaultDispatcher(): CoroutineDispatcher = | |
AsyncTask.THREAD_POOL_EXECUTOR.asCoroutineDispatcher() |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
open class MainTestApplication : Application() { | |
override fun onCreate() { | |
// ThreeTenBP for times and dates, called before super to be available for objects | |
AndroidThreeTen.init(this) | |
Timber.plant(Timber.DebugTree()) |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
// With Dagger2 or dagger.android | |
class MainTestApplication : MainApplication() { | |
override fun applicationInjector(): AndroidInjector<out DaggerApplication> { | |
return DaggerTestAppComponent.builder().create(this) | |
} | |
} |
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
<!-- Copyright 2020 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 --> | |
@CustomTestApplication(MainTestApplication::class) | |
class CustomTestRunner : AndroidJUnitRunner() { | |
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application { | |
return super.newApplication(cl, CustomTestRunner_Application::class.java.name, context) | |
} | |
} |