Skip to content

Instantly share code, notes, and snippets.

View JoseAlcerreca's full-sized avatar

Jose Alcérreca JoseAlcerreca

View GitHub Profile
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With Dagger2 or dagger.android
class CustomTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, MainTestApplication::class.java.name, context)
}
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With Dagger2 or dagger.android
@Binds
@IntoMap
@ViewModelKey(SessionDetailViewModel::class)
abstract fun bindSessionDetailFragmentViewModel(viewModel: SessionDetailViewModel): ViewModel
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With Dagger2 or dagger.android
@Target(
AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With Dagger2 or dagger.android
class SessionDetailFragment : ... {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var sessionDetailViewModel: SessionDetailViewModel
override fun onCreateView(...) {
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With dagger.android
@FragmentScoped
@ContributesAndroidInjector
internal abstract fun contributeOnboardingFragment(): OnboardingFragment
@FragmentScoped
@ContributesAndroidInjector
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
// With dagger.android
@ActivityScoped
@ContributesAndroidInjector(
modules = [
OnboardingModule::class,
SignInDialogModule::class
]
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
fun flowFrom(api: CallbackBasedApi): Flow<T> = callbackFlow {
val callback = object : Callback {
override fun onNextValue(value: T) {
offer(value)
}
override fun onApiError(cause: Throwable) {
close(cause)
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
override fun fetchWeatherFlow(): Flow<String> = flow {
var counter = 0
while(true) {
counter++
delay(2000)
emit(weatherConditions[counter % weatherConditions.size])
}
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
suspend fun doOneShot(param: String) : Result<String> =
suspendCancellableCoroutine { continuation ->
api.addOnCompleteListener { result ->
continuation.resume(result)
}.addOnFailureListener { error ->
continuation.resumeWithException(error)
}.fetchSomething(param)
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
suspend fun doOneShot(param: String) : String =
retrofitClient.doSomething(param)