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
// IMPORTANT! READ THIS FIRST | |
// Assisted Injection doesn't work with @HiltViewModel or @ViewModelInject | |
// Read more about the issue here: https://github.com/google/dagger/issues/2287 | |
// | |
// | |
// AssistedInject and Hilt working together in v2.28-alpha times | |
// Example of a Assisted Presenter injected in a Fragment by Hilt | |
// For a solution with ViewModels, check out https://gist.github.com/manuelvicnt/437668cda3a891d347e134b1de29aee1 |
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
// IMPORTANT! READ THIS FIRST | |
// Assisted Injection doesn't work with @HiltViewModel or @ViewModelInject | |
// Read more about the issue here: https://github.com/google/dagger/issues/2287 | |
// | |
// | |
// AssistedInject and Hilt working together in v2.28-alpha times | |
// Example of a ViewModel using AssistedInject injected in a Fragment by Hilt | |
// As AssistedInject isn't part of Dagger yet, we cannot use in | |
// conjuction with @ViewModelInject |
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 */ | |
@Singleton | |
class AnalyticsRepository @Inject constructor(...) { ... } | |
class MainActivityAnalyticsAdapter @Inject constructor( | |
// AnalyticsRepository is available as it's scoped to ApplicationComponent | |
analyticsRepository: AnalyticsRepository, | |
// 💥 Build time error! |
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 */ | |
// Annotation for scoping to UserComponent | |
@Scope | |
@MustBeDocumented | |
@Retention(value = AnnotationRetention.RUNTIME) | |
annotation class LoggedUserScope | |
// Types scoped to this component must be annotated with @LoggedUserScope | |
@LoggedUserScope |
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 */ | |
@Singleton | |
class UserManager @Inject constructor( | |
// Since UserManager will be in charge of managing the UserComponent's | |
// lifecycle, it needs to know how to create instances of it. We use the | |
// provider (i.e. factory) Dagger generates for us to create instances of UserComponent. | |
private val userComponentProvider: Provider<UserComponent.Builder> | |
) { |
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
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
// Injected by ActivityComponent | |
@Inject lateinit var userManager: UserManager | |
// Populated by UserComponent | |
private lateinit var userDataRepository: UserDataRepository | |
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
// SNAPSHOT used: 6868130 | |
// Libraries used: | |
// androidx.compose.navigation:navigation:0.1.0-SNAPSHOT | |
// androidx.compose.material:material-icons-extended:1.0.0-SNAPSHOT | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.compose.animation.AnimatedVisibility | |
import androidx.compose.animation.ExperimentalAnimationApi | |
import androidx.compose.animation.animate |
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
// Extension function on FusedLocationProviderClient, returns last known location | |
suspend fun FusedLocationProviderClient.awaitLastLocation(): Location = | |
// Create a new coroutine that can be cancelled | |
suspendCancellableCoroutine<Location> { continuation -> | |
// Add listeners that will resume the execution of this coroutine | |
lastLocation.addOnSuccessListener { location -> | |
// Resume coroutine and return location | |
continuation.resume(location) |
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
public suspend inline fun <T> suspendCancellableCoroutine( | |
crossinline block: (CancellableContinuation<T>) -> Unit | |
): T = | |
// Get the Continuation object of the coroutine that it's running this suspend function | |
suspendCoroutineUninterceptedOrReturn { uCont -> | |
// Take over the control of the coroutine. The Continuation's been | |
// intercepted and it follows the CancellableContinuationImpl lifecycle now | |
val cancellable = CancellableContinuationImpl(uCont.intercepted(), ...) | |
/* ... */ |
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
// Send location updates to the consumer | |
fun FusedLocationProviderClient.locationFlow() = callbackFlow<Location> { | |
// A new Flow is created. This code executes in a coroutine! | |
// 1. Create callback and add elements into the flow | |
val callback = object : LocationCallback() { | |
override fun onLocationResult(result: LocationResult?) { | |
result ?: return // Ignore null responses | |
for (location in result.locations) { | |
try { |