Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / LoginUserCompiled.java
Last active March 18, 2020 11:13
LoginUser suspend fun (compiler generated code) - complete
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
fun loginUser(userId: String?, password: String?, completion: Continuation<Any?>) {
class LoginUserStateMachine(
// completion parameter is the callback to the function that called loginUser
completion: Continuation<Any?>
): CoroutineImpl(completion) {
// objects to store across the suspend function
var user: User? = null
@manuelvicnt
manuelvicnt / LoginUserCompiled.java
Last active March 18, 2020 11:13
LoginUser suspend fun (compiler generated code) - part 2
/* Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
fun loginUser(userId: String?, password: String?, completion: Continuation<Any?>) {
...
val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)
when(continuation.label) {
...
2 -> {