Rename the state flow. Also let stateFlow be a real val.
This commit is contained in:
parent
017894201c
commit
caf5ab1085
3 changed files with 17 additions and 19 deletions
|
|
@ -37,7 +37,7 @@ interface ElementClassicConnection {
|
||||||
fun start()
|
fun start()
|
||||||
fun stop()
|
fun stop()
|
||||||
fun requestData()
|
fun requestData()
|
||||||
val state: StateFlow<ElementClassicConnectionState>
|
val stateFlow: StateFlow<ElementClassicConnectionState>
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed interface ElementClassicConnectionState {
|
sealed interface ElementClassicConnectionState {
|
||||||
|
|
@ -107,11 +107,11 @@ class DefaultElementClassicConnection(
|
||||||
} else {
|
} else {
|
||||||
// This happen when the app is not installed
|
// This happen when the app is not installed
|
||||||
Timber.tag(loggerTag.value).d("Binding returned false")
|
Timber.tag(loggerTag.value).d("Binding returned false")
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.ElementClassicNotFound)
|
mutableStateFlow.emit(ElementClassicConnectionState.ElementClassicNotFound)
|
||||||
}
|
}
|
||||||
} catch (e: SecurityException) {
|
} catch (e: SecurityException) {
|
||||||
Timber.tag(loggerTag.value).e(e, "Can't bind to Service")
|
Timber.tag(loggerTag.value).e(e, "Can't bind to Service")
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.Error(e.localizedMessage.orEmpty()))
|
mutableStateFlow.emit(ElementClassicConnectionState.Error(e.localizedMessage.orEmpty()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -124,7 +124,7 @@ class DefaultElementClassicConnection(
|
||||||
bound = false
|
bound = false
|
||||||
}
|
}
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.Idle)
|
mutableStateFlow.emit(ElementClassicConnectionState.Idle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,7 +134,7 @@ class DefaultElementClassicConnection(
|
||||||
val finalMessenger = messenger
|
val finalMessenger = messenger
|
||||||
if (finalMessenger == null) {
|
if (finalMessenger == null) {
|
||||||
Timber.tag(loggerTag.value).w("The messenger is null, can't request data")
|
Timber.tag(loggerTag.value).w("The messenger is null, can't request data")
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.Error("The messenger is null, can't request data"))
|
mutableStateFlow.emit(ElementClassicConnectionState.Error("The messenger is null, can't request data"))
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
// Get the data
|
// Get the data
|
||||||
|
|
@ -147,16 +147,14 @@ class DefaultElementClassicConnection(
|
||||||
// disconnected (and then reconnected if it can be restarted)
|
// disconnected (and then reconnected if it can be restarted)
|
||||||
// so there is no need to do anything here.
|
// so there is no need to do anything here.
|
||||||
Timber.tag(loggerTag.value).e(e, "RemoteException")
|
Timber.tag(loggerTag.value).e(e, "RemoteException")
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.Error(e.localizedMessage.orEmpty()))
|
mutableStateFlow.emit(ElementClassicConnectionState.Error(e.localizedMessage.orEmpty()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val elementClassicConnectionStateFlow = MutableStateFlow<ElementClassicConnectionState>(ElementClassicConnectionState.Idle)
|
private val mutableStateFlow = MutableStateFlow<ElementClassicConnectionState>(ElementClassicConnectionState.Idle)
|
||||||
|
override val stateFlow = mutableStateFlow.asStateFlow()
|
||||||
override val state: StateFlow<ElementClassicConnectionState>
|
|
||||||
get() = elementClassicConnectionStateFlow.asStateFlow()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler of incoming messages from service.
|
* Handler of incoming messages from service.
|
||||||
|
|
@ -182,20 +180,20 @@ class DefaultElementClassicConnection(
|
||||||
when (state) {
|
when (state) {
|
||||||
is ElementClassicConnectionState.Error -> {
|
is ElementClassicConnectionState.Error -> {
|
||||||
Timber.tag(loggerTag.value).w("Received error from Element Classic: %s", state.error)
|
Timber.tag(loggerTag.value).w("Received error from Element Classic: %s", state.error)
|
||||||
elementClassicConnectionStateFlow.emit(state)
|
mutableStateFlow.emit(state)
|
||||||
}
|
}
|
||||||
is ElementClassicConnectionState.ElementClassicReady -> {
|
is ElementClassicConnectionState.ElementClassicReady -> {
|
||||||
Timber.tag(loggerTag.value).d("Received userId from Element Classic: %s", state.userId)
|
Timber.tag(loggerTag.value).d("Received userId from Element Classic: %s", state.userId)
|
||||||
elementClassicConnectionStateFlow.emit(state)
|
mutableStateFlow.emit(state)
|
||||||
}
|
}
|
||||||
ElementClassicConnectionState.ElementClassicReadyNoSession -> {
|
ElementClassicConnectionState.ElementClassicReadyNoSession -> {
|
||||||
Timber.tag(loggerTag.value).d("Received no session from Element Classic")
|
Timber.tag(loggerTag.value).d("Received no session from Element Classic")
|
||||||
elementClassicConnectionStateFlow.emit(state)
|
mutableStateFlow.emit(state)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// Should not happen
|
// Should not happen
|
||||||
Timber.tag(loggerTag.value).w("Received unexpected state from Element Classic: %s", state)
|
Timber.tag(loggerTag.value).w("Received unexpected state from Element Classic: %s", state)
|
||||||
elementClassicConnectionStateFlow.emit(ElementClassicConnectionState.Idle)
|
mutableStateFlow.emit(ElementClassicConnectionState.Idle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ class LoginWithClassicPresenter(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val state by elementClassicConnection.state.collectAsState()
|
val state by elementClassicConnection.stateFlow.collectAsState()
|
||||||
val loginWithClassicAction = remember { mutableStateOf<AsyncAction<Unit>>(AsyncAction.Uninitialized) }
|
val loginWithClassicAction = remember { mutableStateOf<AsyncAction<Unit>>(AsyncAction.Uninitialized) }
|
||||||
|
|
||||||
val existingSession by remember {
|
val existingSession by remember {
|
||||||
|
|
@ -73,7 +73,7 @@ class LoginWithClassicPresenter(
|
||||||
elementClassicConnection.requestData()
|
elementClassicConnection.requestData()
|
||||||
}
|
}
|
||||||
LoginWithClassicEvent.StartLoginWithClassic -> {
|
LoginWithClassicEvent.StartLoginWithClassic -> {
|
||||||
val currentState = elementClassicConnection.state.value
|
val currentState = elementClassicConnection.stateFlow.value
|
||||||
if (currentState is ElementClassicConnectionState.ElementClassicReady) {
|
if (currentState is ElementClassicConnectionState.ElementClassicReady) {
|
||||||
loginWithClassicAction.value = ConfirmingLoginWithElementClassic(
|
loginWithClassicAction.value = ConfirmingLoginWithElementClassic(
|
||||||
userId = currentState.userId,
|
userId = currentState.userId,
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,9 @@ class FakeElementClassicConnection(
|
||||||
override fun start() = startResult()
|
override fun start() = startResult()
|
||||||
override fun stop() = stopResult()
|
override fun stop() = stopResult()
|
||||||
override fun requestData() = requestDataResult()
|
override fun requestData() = requestDataResult()
|
||||||
private val _state = MutableStateFlow(initialState)
|
private val mutableStateFlow = MutableStateFlow(initialState)
|
||||||
override val state: StateFlow<ElementClassicConnectionState> = _state.asStateFlow()
|
override val stateFlow: StateFlow<ElementClassicConnectionState> = mutableStateFlow.asStateFlow()
|
||||||
suspend fun emitState(state: ElementClassicConnectionState) {
|
suspend fun emitState(state: ElementClassicConnectionState) {
|
||||||
_state.emit(state)
|
mutableStateFlow.emit(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue