Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
manuelvicnt / AnAndroidApp.kt
Last active June 22, 2022 12:03
Hilt and AssistedInject working together in Hilt v2.28-alpha times
// 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
@manuelvicnt
manuelvicnt / AnAndroidApp.kt
Last active January 1, 2023 17:05
Hilt and AssistedInject working together in Hilt v2.28-alpha times - ViewModel version
// 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
@manuelvicnt
manuelvicnt / MainActivityAnalyticsAdapter.kt
Last active July 9, 2020 15:42
Adding components to the Hilt hierarchy - 1
/* 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!
@manuelvicnt
manuelvicnt / UserComponent.kt
Last active July 9, 2020 15:40
Adding components to the Hilt hierarchy - 2
/* 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
@manuelvicnt
manuelvicnt / UserManager.kt
Last active July 9, 2020 22:11
Adding components to the Hilt hierarchy - 3
/* 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>
) {
@manuelvicnt
manuelvicnt / MainActivity.kt
Created July 2, 2020 07:34
Adding components to the Hilt hierarchy - 4
@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?) {
@manuelvicnt
manuelvicnt / ComposeBaseTextFieldCrash.kt
Last active September 29, 2020 07:14
Compose BaseTextField crash
// 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
@manuelvicnt
manuelvicnt / FusedLocationProviderClientUtils.kt
Created December 9, 2020 11:34
FusedLocationProviderClient.awaitLastLocation() code
// 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)
@manuelvicnt
manuelvicnt / suspendCancellableCoroutine.kt
Created December 9, 2020 11:37
suspendCancellableCoroutine
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(), ...)
/* ... */
@manuelvicnt
manuelvicnt / FusedLocationProviderClientUtils.kt
Created December 9, 2020 11:41
FusedLocationProviderClient.locationFlow
// 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 {