@@ -4,6 +4,7 @@ import android.content.Context
44import com.uid2.UID2ManagerState.Established
55import com.uid2.UID2ManagerState.Expired
66import com.uid2.UID2ManagerState.Invalid
7+ import com.uid2.UID2ManagerState.Loading
78import com.uid2.UID2ManagerState.NoIdentity
89import com.uid2.UID2ManagerState.OptOut
910import com.uid2.UID2ManagerState.RefreshExpired
@@ -58,6 +59,7 @@ public interface UID2ManagerIdentityChangedListener {
5859 * A interface defining the flow of state communicated by the [UID2Manager].
5960 */
6061public sealed interface UID2ManagerState {
62+ public data object Loading : UID2ManagerState
6163 public data class Established (val identity : UID2Identity ) : UID2ManagerState
6264 public data class Refreshed (val identity : UID2Identity ) : UID2ManagerState
6365 public data object NoIdentity : UID2ManagerState
@@ -93,7 +95,23 @@ public class UID2Manager internal constructor(
9395 */
9496 public var onIdentityChangedListener: UID2ManagerIdentityChangedListener ? = null
9597
96- private val _state = MutableStateFlow <UID2ManagerState >(NoIdentity )
98+ /* *
99+ * Gets or sets a listener which can be used to determine if the [UID2Manager] instance has finished initializing.
100+ * Initializing includes any time required to restore a previously persisted [UID2Identity] from storage.
101+ *
102+ * If this property is set *after* initialization is complete, the callback will be invoked immediately.
103+ */
104+ public var onInitialized: (() -> Unit )? = null
105+ set(value) {
106+ field = value
107+
108+ // If we've already finished initializing, we should immediately invoke the callback.
109+ if (initialized.isCompleted) {
110+ value?.invoke()
111+ }
112+ }
113+
114+ private val _state = MutableStateFlow <UID2ManagerState >(Loading )
97115
98116 /* *
99117 * The flow representing the state of the UID2Manager.
@@ -135,6 +153,7 @@ public class UID2Manager internal constructor(
135153 */
136154 public val currentIdentityStatus: IdentityStatus
137155 get() = when (_state .value) {
156+ is Loading -> NO_IDENTITY // Not available yet.
138157 is Established -> ESTABLISHED
139158 is Refreshed -> REFRESHED
140159 is NoIdentity -> NO_IDENTITY
@@ -164,6 +183,9 @@ public class UID2Manager internal constructor(
164183
165184 validateAndSetIdentity(it.first, it.second, false )
166185 }
186+
187+ // If we have a callback provided, invoke it.
188+ onInitialized?.invoke()
167189 }
168190 }
169191
0 commit comments