2023-10-03 22:34:51 +03:00
|
|
|
package com.x8bit.bitwarden
|
|
|
|
|
|
|
|
import android.content.Intent
|
2024-01-17 02:41:34 +03:00
|
|
|
import android.os.Parcelable
|
|
|
|
import androidx.lifecycle.viewModelScope
|
2023-10-03 22:34:51 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
2023-10-24 17:11:07 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.util.getCaptchaCallbackTokenResult
|
2024-01-17 02:41:34 +03:00
|
|
|
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
|
|
|
|
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
|
|
|
|
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
|
2023-10-03 22:34:51 +03:00
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
2024-01-17 02:41:34 +03:00
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
|
import kotlinx.parcelize.Parcelize
|
2023-10-03 22:34:51 +03:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A view model that helps launch actions for the [MainActivity].
|
|
|
|
*/
|
|
|
|
@HiltViewModel
|
|
|
|
class MainViewModel @Inject constructor(
|
|
|
|
private val authRepository: AuthRepository,
|
2024-01-17 02:41:34 +03:00
|
|
|
settingsRepository: SettingsRepository,
|
|
|
|
) : BaseViewModel<MainState, Unit, MainAction>(
|
|
|
|
MainState(
|
|
|
|
theme = settingsRepository.appTheme,
|
|
|
|
),
|
|
|
|
) {
|
|
|
|
init {
|
|
|
|
settingsRepository
|
|
|
|
.appThemeStateFlow
|
|
|
|
.onEach { trySendAction(MainAction.Internal.ThemeUpdate(it)) }
|
|
|
|
.launchIn(viewModelScope)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleAction(action: MainAction) {
|
2023-10-03 22:34:51 +03:00
|
|
|
when (action) {
|
2024-01-17 02:41:34 +03:00
|
|
|
is MainAction.Internal.ThemeUpdate -> handleAppThemeUpdated(action)
|
|
|
|
is MainAction.ReceiveNewIntent -> handleNewIntentReceived(action)
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
private fun handleAppThemeUpdated(action: MainAction.Internal.ThemeUpdate) {
|
|
|
|
mutableStateFlow.update { it.copy(theme = action.theme) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handleNewIntentReceived(action: MainAction.ReceiveNewIntent) {
|
|
|
|
val captchaCallbackTokenResult = action.intent.getCaptchaCallbackTokenResult()
|
2023-10-03 22:34:51 +03:00
|
|
|
when {
|
|
|
|
captchaCallbackTokenResult != null -> {
|
|
|
|
authRepository.setCaptchaCallbackTokenResult(
|
|
|
|
tokenResult = captchaCallbackTokenResult,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
else -> Unit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
/**
|
|
|
|
* Models state for the [MainActivity].
|
|
|
|
*/
|
|
|
|
@Parcelize
|
|
|
|
data class MainState(
|
|
|
|
val theme: AppTheme,
|
|
|
|
) : Parcelable
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
/**
|
|
|
|
* Models actions for the [MainActivity].
|
|
|
|
*/
|
|
|
|
sealed class MainAction {
|
|
|
|
/**
|
|
|
|
* Receive Intent by the application.
|
|
|
|
*/
|
|
|
|
data class ReceiveNewIntent(val intent: Intent) : MainAction()
|
2024-01-17 02:41:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions for internal use by the ViewModel.
|
|
|
|
*/
|
|
|
|
sealed class Internal : MainAction() {
|
|
|
|
/**
|
|
|
|
* Indicates that the app theme has changed.
|
|
|
|
*/
|
|
|
|
data class ThemeUpdate(
|
|
|
|
val theme: AppTheme,
|
|
|
|
) : Internal()
|
|
|
|
}
|
2023-10-03 22:34:51 +03:00
|
|
|
}
|