mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 17:29:32 +01:00
Observe sharedprefs for banner updates.
This commit is contained in:
committed by
mtang-signal
parent
244a81ef24
commit
d15bb05ae3
@@ -9,7 +9,9 @@ import android.content.Context
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.banner.Banner
|
||||
import org.thoughtcrime.securesms.banner.ui.compose.DefaultBanner
|
||||
@@ -31,17 +33,24 @@ class ServiceOutageBanner(outageInProgress: Boolean) : Banner() {
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* A class that can be held by a listener but still produce new [ServiceOutageBanner] in its flow.
|
||||
* Designed for being called upon by a listener that is listening to changes in [TextSecurePreferences]
|
||||
*/
|
||||
class Producer(private val context: Context) {
|
||||
private val _flow = MutableSharedFlow<Boolean>(replay = 1)
|
||||
val flow: Flow<ServiceOutageBanner> = _flow.map { ServiceOutageBanner(context) }
|
||||
|
||||
@JvmStatic
|
||||
fun createOneShotFlow(context: Context): Flow<ServiceOutageBanner> = createAndEmit {
|
||||
ServiceOutageBanner(context)
|
||||
init {
|
||||
queryAndEmit()
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a [Flow] of [Boolean] values representing the service status and map it into a [Flow] of [ServiceOutageBanner]
|
||||
*/
|
||||
@JvmStatic
|
||||
fun fromFlow(statusFlow: Flow<Boolean>): Flow<ServiceOutageBanner> = statusFlow.map { ServiceOutageBanner(it) }
|
||||
fun queryAndEmit() {
|
||||
_flow.tryEmit(TextSecurePreferences.getServiceOutage(context))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = Log.tag(ServiceOutageBanner::class)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import android.content.Context
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.banner.Banner
|
||||
import org.thoughtcrime.securesms.banner.ui.compose.Action
|
||||
@@ -18,6 +21,9 @@ import org.thoughtcrime.securesms.keyvalue.SignalStore
|
||||
import org.thoughtcrime.securesms.registration.ui.RegistrationActivity
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||
|
||||
/**
|
||||
* A banner displayed when the client is unauthorized (deregistered).
|
||||
*/
|
||||
class UnauthorizedBanner(val context: Context) : Banner() {
|
||||
|
||||
override val enabled = TextSecurePreferences.isUnauthorizedReceived(context) || !SignalStore.account.isRegistered
|
||||
@@ -37,11 +43,24 @@ class UnauthorizedBanner(val context: Context) : Banner() {
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* A class that can be held by a listener but still produce new [UnauthorizedBanner] in its flow.
|
||||
* Designed for being called upon by a listener that is listening to changes in [TextSecurePreferences]
|
||||
*/
|
||||
class Producer(private val context: Context) {
|
||||
private val _flow = MutableSharedFlow<Boolean>(replay = 1)
|
||||
val flow: Flow<UnauthorizedBanner> = _flow.map { UnauthorizedBanner(context) }
|
||||
|
||||
@JvmStatic
|
||||
fun createFlow(context: Context): Flow<UnauthorizedBanner> = createAndEmit {
|
||||
UnauthorizedBanner(context)
|
||||
init {
|
||||
queryAndEmit()
|
||||
}
|
||||
|
||||
fun queryAndEmit() {
|
||||
_flow.tryEmit(TextSecurePreferences.isUnauthorizedReceived(context))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = Log.tag(UnauthorizedBanner::class)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user