|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The Dagger Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dagger.hilt.android.lifecycle |
| 18 | + |
| 19 | +import androidx.lifecycle.viewmodel.CreationExtras |
| 20 | +import dagger.hilt.android.lifecycle.HiltViewModelExtras.Key |
| 21 | +import kotlin.jvm.JvmOverloads |
| 22 | +import kotlin.jvm.JvmStatic |
| 23 | + |
| 24 | +/** |
| 25 | + * A map-like object holding pairs of [HiltViewModelExtras.Key] and [Any], enabling efficient value |
| 26 | + * retrieval for each key. Each key in [HiltViewModelExtras] is unique, storing only one value per |
| 27 | + * key. |
| 28 | + * |
| 29 | + * [HiltViewModelExtras] are bound in the [dagger.hilt.android.components.ViewModelComponent] if set |
| 30 | + * in the view model's [CreationExtras] with [HiltViewModelExtras.CreationExtrasKey]. |
| 31 | + * |
| 32 | + * This abstract class supports read-only access; use [MutableHiltViewModelExtras] for read-write |
| 33 | + * access. |
| 34 | + */ |
| 35 | +abstract class HiltViewModelExtras internal constructor() { |
| 36 | + internal val extras: MutableMap<Key<*>, Any?> = mutableMapOf() |
| 37 | + |
| 38 | + /** |
| 39 | + * Key for the elements of [HiltViewModelExtras]. [T] represents the type of element associated |
| 40 | + * with this key. |
| 41 | + */ |
| 42 | + interface Key<T> |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns the value to which the specified [key] is associated, or null if this |
| 46 | + * [HiltViewModelExtras] contains no mapping for the key. |
| 47 | + */ |
| 48 | + abstract operator fun <T> get(key: Key<T>): T? |
| 49 | + |
| 50 | + /** Compares the specified object with this [HiltViewModelExtras] for equality. */ |
| 51 | + override fun equals(other: Any?): Boolean = other is HiltViewModelExtras && extras == other.extras |
| 52 | + |
| 53 | + /** Returns the hash code value for this [HiltViewModelExtras]. */ |
| 54 | + override fun hashCode(): Int = extras.hashCode() |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns a string representation of this [HiltViewModelExtras]. The string representation |
| 58 | + * consists of a list of key-value mappings in the order returned by the [HiltViewModelExtras]'s |
| 59 | + * iterator. |
| 60 | + */ |
| 61 | + override fun toString(): String = "HiltViewModelExtras(extras=$extras)" |
| 62 | + |
| 63 | + /** An empty read-only [HiltViewModelExtras]. */ |
| 64 | + object Empty : HiltViewModelExtras() { |
| 65 | + override fun <T> get(key: Key<T>): T? = null |
| 66 | + } |
| 67 | + |
| 68 | + companion object { |
| 69 | + /** The [CreationExtras.Key] used to store a [HiltViewModelExtras] in [CreationExtras]. */ |
| 70 | + @JvmField val HILT_VIEW_MODEL_EXTRAS_KEY = CreationExtras.Key<HiltViewModelExtras>() |
| 71 | + |
| 72 | + /** Returns a unique [Key] to be associated with an extra. */ |
| 73 | + @JvmStatic inline fun <reified T> Key(): Key<T> = object : Key<T> {} |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * A modifiable [HiltViewModelExtras] that holds pairs of [HiltViewModelExtras.Key] and [Any], |
| 79 | + * allowing efficient value retrieval for each key. |
| 80 | + * |
| 81 | + * Each key in [HiltViewModelExtras] is unique, storing only one value per key. |
| 82 | + * |
| 83 | + * @see [HiltViewModelExtras] |
| 84 | + */ |
| 85 | +class MutableHiltViewModelExtras |
| 86 | +/** |
| 87 | + * Constructs a [MutableHiltViewModelExtras] containing the elements of the specified |
| 88 | + * [initialExtras], in the order they are returned by the [Map]'s iterator. |
| 89 | + */ |
| 90 | +internal constructor(initialExtras: Map<Key<*>, Any?>) : HiltViewModelExtras() { |
| 91 | + |
| 92 | + /** |
| 93 | + * Constructs a [MutableHiltViewModelExtras] containing the elements of the specified |
| 94 | + * [initialExtras], in the order they are returned by the [HiltViewModelExtras]'s iterator. |
| 95 | + */ |
| 96 | + @JvmOverloads constructor(initialExtras: HiltViewModelExtras = Empty) : this(initialExtras.extras) |
| 97 | + |
| 98 | + init { |
| 99 | + extras += initialExtras |
| 100 | + } |
| 101 | + |
| 102 | + /** Associates the specified [t] with the specified [key] in this [HiltViewModelExtras]. */ |
| 103 | + operator fun <T> set(key: Key<T>, t: T) { |
| 104 | + extras[key] = t |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the value to which the specified [key] is associated, or null if this |
| 109 | + * [HiltViewModelExtras] contains no mapping for the key. |
| 110 | + */ |
| 111 | + @Suppress("UNCHECKED_CAST") override fun <T> get(key: Key<T>): T? = extras[key] as T? |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Checks if the [HiltViewModelExtras] contains the given [key]. |
| 116 | + * |
| 117 | + * This method allows to use the `key in hiltViewModelExtras` syntax for checking whether an [key] |
| 118 | + * is contained in the [HiltViewModelExtras]. |
| 119 | + */ |
| 120 | +operator fun HiltViewModelExtras.contains(key: Key<*>): Boolean = key in extras |
| 121 | + |
| 122 | +/** |
| 123 | + * Creates a new read-only [HiltViewModelExtras] by replacing or adding entries to [this] extras |
| 124 | + * from another [hiltViewModelExtras]. |
| 125 | + * |
| 126 | + * The returned [HiltViewModelExtras] preserves the entry iteration order of the original |
| 127 | + * [HiltViewModelExtras]. |
| 128 | + * |
| 129 | + * Those entries of another [hiltViewModelExtras] that are missing in [this] extras are iterated in |
| 130 | + * the end in the order of that [hiltViewModelExtras]. |
| 131 | + */ |
| 132 | +operator fun HiltViewModelExtras.plus( |
| 133 | + hiltViewModelExtras: HiltViewModelExtras |
| 134 | +): MutableHiltViewModelExtras = |
| 135 | + MutableHiltViewModelExtras(initialExtras = extras + hiltViewModelExtras.extras) |
| 136 | + |
| 137 | +/** |
| 138 | + * Appends or replaces all entries from the given [hiltViewModelExtras] in [this] mutable extras. |
| 139 | + */ |
| 140 | +operator fun MutableHiltViewModelExtras.plusAssign(hiltViewModelExtras: HiltViewModelExtras) { |
| 141 | + extras += hiltViewModelExtras.extras |
| 142 | +} |
0 commit comments