Skip to content

Commit

Permalink
PM-14963: Add toast when login via device succeeds (#4351)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored Nov 21, 2024
1 parent dbeb00b commit 245fcd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fun LoginWithDeviceScreen(
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
val context = LocalContext.current
val resources = context.resources
EventsEffect(viewModel = viewModel) { event ->
when (event) {
LoginWithDeviceEvent.NavigateBack -> onNavigateBack()
Expand All @@ -73,7 +74,7 @@ fun LoginWithDeviceScreen(
}

is LoginWithDeviceEvent.ShowToast -> {
Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show()
Toast.makeText(context, event.message(resources), Toast.LENGTH_SHORT).show()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.x8bit.bitwarden.data.auth.repository.util.generateUriForCaptcha
import com.x8bit.bitwarden.ui.auth.feature.loginwithdevice.model.LoginWithDeviceType
import com.x8bit.bitwarden.ui.auth.feature.loginwithdevice.util.toAuthRequestType
import com.x8bit.bitwarden.ui.platform.base.BaseViewModel
import com.x8bit.bitwarden.ui.platform.base.util.BackgroundEvent
import com.x8bit.bitwarden.ui.platform.base.util.Text
import com.x8bit.bitwarden.ui.platform.base.util.asText
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -252,6 +253,7 @@ class LoginWithDeviceViewModel @Inject constructor(
}

is LoginResult.Success -> {
sendEvent(LoginWithDeviceEvent.ShowToast(R.string.login_approved.asText()))
mutableStateFlow.update { it.copy(dialogState = null) }
}
}
Expand Down Expand Up @@ -494,8 +496,8 @@ sealed class LoginWithDeviceEvent {
* Shows a toast with the given [message].
*/
data class ShowToast(
val message: String,
) : LoginWithDeviceEvent()
val message: Text,
) : LoginWithDeviceEvent(), BackgroundEvent
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
)
} returns LoginResult.Success
val viewModel = createViewModel()
viewModel.stateFlow.test {
assertEquals(DEFAULT_STATE, awaitItem())
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(DEFAULT_STATE, stateFlow.awaitItem())
mutableCreateAuthRequestWithUpdatesFlow.tryEmit(
CreateAuthRequestResult.Success(
authRequest = AUTH_REQUEST,
Expand All @@ -181,7 +181,7 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
message = R.string.logging_in.asText(),
),
),
awaitItem(),
stateFlow.awaitItem(),
)
assertEquals(
DEFAULT_STATE.copy(
Expand All @@ -191,7 +191,11 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
dialogState = null,
loginData = DEFAULT_LOGIN_DATA,
),
awaitItem(),
stateFlow.awaitItem(),
)
assertEquals(
LoginWithDeviceEvent.ShowToast(R.string.login_approved.asText()),
eventFlow.awaitItem(),
)
}

Expand Down Expand Up @@ -227,8 +231,8 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
)
val viewModel = createViewModel(initialState)

viewModel.stateFlow.test {
assertEquals(initialState, awaitItem())
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(initialState, stateFlow.awaitItem())
mutableCreateAuthRequestWithUpdatesFlow.tryEmit(
CreateAuthRequestResult.Success(
authRequest = AUTH_REQUEST,
Expand All @@ -246,7 +250,7 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
),
loginData = DEFAULT_LOGIN_DATA,
),
awaitItem(),
stateFlow.awaitItem(),
)
assertEquals(
initialState.copy(
Expand All @@ -256,7 +260,11 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
dialogState = null,
loginData = DEFAULT_LOGIN_DATA,
),
awaitItem(),
stateFlow.awaitItem(),
)
assertEquals(
LoginWithDeviceEvent.ShowToast(R.string.login_approved.asText()),
eventFlow.awaitItem(),
)
}

Expand Down

0 comments on commit 245fcd7

Please sign in to comment.