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
|
2024-01-20 00:01:43 +03:00
|
|
|
import com.x8bit.bitwarden.data.auth.repository.model.UserState
|
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
|
2024-01-20 00:01:43 +03:00
|
|
|
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
|
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-20 00:01:43 +03:00
|
|
|
private val intentManager: IntentManager,
|
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)
|
2024-01-20 00:01:43 +03:00
|
|
|
is MainAction.ReceiveFirstIntent -> handleFirstIntentReceived(action)
|
2024-01-17 02:41:34 +03:00
|
|
|
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) }
|
|
|
|
}
|
|
|
|
|
2024-01-20 00:01:43 +03:00
|
|
|
private fun handleFirstIntentReceived(action: MainAction.ReceiveFirstIntent) {
|
|
|
|
val shareData = intentManager.getShareDataFromIntent(action.intent)
|
|
|
|
when {
|
|
|
|
shareData != null -> {
|
|
|
|
authRepository.specialCircumstance =
|
|
|
|
UserState.SpecialCircumstance.ShareNewSend(
|
|
|
|
data = shareData,
|
|
|
|
shouldFinishWhenComplete = true,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 02:41:34 +03:00
|
|
|
private fun handleNewIntentReceived(action: MainAction.ReceiveNewIntent) {
|
|
|
|
val captchaCallbackTokenResult = action.intent.getCaptchaCallbackTokenResult()
|
2024-01-20 00:01:43 +03:00
|
|
|
val shareData = intentManager.getShareDataFromIntent(action.intent)
|
2023-10-03 22:34:51 +03:00
|
|
|
when {
|
|
|
|
captchaCallbackTokenResult != null -> {
|
|
|
|
authRepository.setCaptchaCallbackTokenResult(
|
|
|
|
tokenResult = captchaCallbackTokenResult,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-20 00:01:43 +03:00
|
|
|
shareData != null -> {
|
|
|
|
authRepository.specialCircumstance =
|
|
|
|
UserState.SpecialCircumstance.ShareNewSend(
|
|
|
|
data = shareData,
|
|
|
|
// Allow users back into the already-running app when completing the
|
|
|
|
// Send task.
|
|
|
|
shouldFinishWhenComplete = false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
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 {
|
2024-01-20 00:01:43 +03:00
|
|
|
/**
|
|
|
|
* Receive first Intent by the application.
|
|
|
|
*/
|
|
|
|
data class ReceiveFirstIntent(val intent: Intent) : MainAction()
|
|
|
|
|
2023-10-03 22:34:51 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|