Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / SignalLineSession.kt

SignalLineSession.kt87 lines · 3.4 KiBRaw
1package com.vayunmathur.communicate.data.signal
2
3import android.content.Context
4import com.vayunmathur.library.util.DataStoreUtils
5import kotlinx.coroutines.flow.Flow
6import kotlinx.coroutines.flow.SharedFlow
7import kotlinx.coroutines.flow.map
8
9/**
10 * Session facade for the Signal primary line, mirroring
11 * [com.vayunmathur.communicate.data.whatsapp.WhatsAppLineSession]'s
12 * shape so the UI and the sync service treat all lines uniformly.
13 *
14 * Owns the reactive sign-in flag + phone-number mirror in DataStore; the authoritative credentials
15 * live in [SignalAuthData] (private prefs). Bridges the [SignalClient] singleton's [state] + [events]
16 * out to callers.
17 */
18class SignalLineSession private constructor(
19 private val store: DataStoreUtils,
20) {
21 /** Reactive sign-in state for the Accounts screen + line pickers. */
22 val signedInFlow: Flow<Boolean>
23 get() = store.booleanFlow(KEY_SIGNED_IN)
24
25 suspend fun isSignedIn(): Boolean = store.getBooleanAwait(KEY_SIGNED_IN, default = false)
26
27 val phoneNumberFlow: Flow<String?>
28 get() = store.stringFlow(KEY_PHONE_NUMBER).map { it }
29
30 suspend fun phoneNumber(): String? = store.getStringAwait(KEY_PHONE_NUMBER)
31
32 /** True when a completed registration is persisted (primary line is live). */
33 fun hasUsableCredentials(context: Context): Boolean =
34 SignalAuthData.load(context)?.registered == true
35
36 /** Lifecycle state mapped from the client's internal handshake state machine. */
37 val state: Flow<SignalState>
38 get() = SignalClient.state.map { it.toPublic() }
39
40 /** The client's event stream. */
41 val events: SharedFlow<SignalEvent>
42 get() = SignalClient.events
43
44 /**
45 * Initialize + connect the primary client if we have usable credentials. Safe to call repeatedly
46 * (SignalClient.init is idempotent). Call from the sync service / MainActivity.
47 */
48 fun init(context: Context) {
49 SignalClient.init(context.applicationContext)
50 if (hasUsableCredentials(context)) {
51 SignalClient.start()
52 }
53 }
54
55 /** Mark the line signed-in after a successful registration and start the client. */
56 suspend fun markRegistered(context: Context, auth: SignalAuthData) {
57 store.setString(KEY_PHONE_NUMBER, auth.phoneNumber)
58 store.setBoolean(KEY_SIGNED_IN, true)
59 SignalClient.init(context.applicationContext)
60 SignalClient.start()
61 }
62
63 suspend fun signOut(context: Context) {
64 SignalClient.stop()
65 SignalAuthData.clear(context)
66 store.removeKeys(listOf(KEY_PHONE_NUMBER))
67 store.setBoolean(KEY_SIGNED_IN, false)
68 }
69
70 /** Preference keys for the Signal line session (signed-in flag, phone number). */
71 companion object {
72 private const val KEY_SIGNED_IN = "sig_signed_in"
73 private const val KEY_PHONE_NUMBER = "sig_phone_number"
74
75 fun get(context: Context): SignalLineSession =
76 SignalLineSession(DataStoreUtils.getInstance(context.applicationContext))
77 }
78}
79
80/** Map the client's internal [SignalClient.State] to the public [SignalState]. */
81private fun SignalClient.State.toPublic(): SignalState = when (this) {
82 is SignalClient.State.Idle -> SignalState.Disconnected
83 is SignalClient.State.NeedsSetup -> SignalState.Disconnected
84 is SignalClient.State.Connecting -> SignalState.Connecting
85 is SignalClient.State.Connected -> SignalState.Ready
86 is SignalClient.State.Disconnected -> SignalState.Disconnected
87}