Handle backpresses in onboarding
parent
d621036af6
commit
9cf3a37a2b
@ -1,4 +1,4 @@
|
||||
package org.thoughtcrime.securesms.onboarding.loading
|
||||
package org.thoughtcrime.securesms.onboarding.manager
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.CoroutineScope
|
@ -1,22 +1,83 @@
|
||||
package org.thoughtcrime.securesms.onboarding.messagenotifications
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.launch
|
||||
import org.thoughtcrime.securesms.ApplicationContext
|
||||
|
||||
@HiltViewModel
|
||||
internal class MessageNotificationsViewModel @Inject constructor(): ViewModel() {
|
||||
private val _states = MutableStateFlow(MessageNotificationsState())
|
||||
val states = _states.asStateFlow()
|
||||
internal class MessageNotificationsViewModel(
|
||||
private val state: State,
|
||||
private val application: Application
|
||||
): AndroidViewModel(application) {
|
||||
private val _uiStates = MutableStateFlow(UiState())
|
||||
val uiStates = _uiStates.asStateFlow()
|
||||
|
||||
fun setEnabled(enabled: Boolean) {
|
||||
_states.update { MessageNotificationsState(pushEnabled = enabled) }
|
||||
_uiStates.update { UiState(pushEnabled = enabled) }
|
||||
}
|
||||
|
||||
/**
|
||||
* @return [true] if the back press was handled.
|
||||
*/
|
||||
fun onBackPressed(): Boolean = when (state) {
|
||||
is State.CreateAccount -> false
|
||||
is State.LoadAccount -> {
|
||||
_uiStates.update { it.copy(showDialog = true) }
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissDialog() {
|
||||
_uiStates.update { it.copy(showDialog = false) }
|
||||
}
|
||||
|
||||
fun quit() {
|
||||
_uiStates.update { it.copy(clearData = true) }
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
ApplicationContext.getInstance(application).clearAllData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class MessageNotificationsState(val pushEnabled: Boolean = true) {
|
||||
val pushDisabled get() = !pushEnabled
|
||||
data class UiState(
|
||||
val pushEnabled: Boolean = true,
|
||||
val showDialog: Boolean = false,
|
||||
val clearData: Boolean = false
|
||||
) {
|
||||
val pushDisabled get() = !pushEnabled
|
||||
}
|
||||
|
||||
sealed interface State {
|
||||
class CreateAccount(val displayName: String): State
|
||||
object LoadAccount: State
|
||||
}
|
||||
|
||||
@dagger.assisted.AssistedFactory
|
||||
interface AssistedFactory {
|
||||
fun create(profileName: String?): Factory
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class Factory @AssistedInject constructor(
|
||||
@Assisted private val profileName: String?,
|
||||
private val application: Application
|
||||
) : ViewModelProvider.Factory {
|
||||
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
return MessageNotificationsViewModel(
|
||||
state = profileName?.let(State::CreateAccount) ?: State.LoadAccount,
|
||||
application = application
|
||||
) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package org.thoughtcrime.securesms.ui.components
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material.LocalContentColor
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@Composable
|
||||
fun CircularProgressIndicator(color: Color = LocalContentColor.current) {
|
||||
androidx.compose.material.CircularProgressIndicator(
|
||||
modifier = Modifier.size(40.dp),
|
||||
color = color,
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SmallCircularProgressIndicator(color: Color = LocalContentColor.current) {
|
||||
androidx.compose.material.CircularProgressIndicator(
|
||||
modifier = Modifier.size(20.dp),
|
||||
color = color,
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue