|
7 | 7 |
|
8 | 8 | package io.element.android.features.messages.impl.crypto.identity |
9 | 9 |
|
| 10 | +import androidx.annotation.StringRes |
10 | 11 | import androidx.compose.runtime.Composable |
11 | 12 | import androidx.compose.ui.Modifier |
12 | 13 | import androidx.compose.ui.res.stringResource |
@@ -35,79 +36,86 @@ fun IdentityChangeStateView( |
35 | 36 | val identityChangeViolation = state.roomMemberIdentityStateChanges.firstOrNull { |
36 | 37 | it.identityState.isAViolation() |
37 | 38 | } |
38 | | - if (identityChangeViolation != null) { |
39 | | - ComposerAlertMolecule( |
| 39 | + when (identityChangeViolation?.identityState) { |
| 40 | + IdentityState.PinViolation -> ViolationAlert( |
| 41 | + identityChangeViolation = identityChangeViolation, |
| 42 | + onLinkClick = onLinkClick, |
| 43 | + textId = CommonStrings.crypto_identity_change_pin_violation_new, |
| 44 | + isCritical = false, |
| 45 | + submitTextId = CommonStrings.action_ok, |
| 46 | + onSubmitClick = { state.eventSink(IdentityChangeEvent.PinIdentity(identityChangeViolation.identityRoomMember.userId)) }, |
40 | 47 | modifier = modifier, |
41 | | - avatar = identityChangeViolation.identityRoomMember.avatarData, |
42 | | - content = buildAnnotatedString { |
43 | | - val learnMoreStr = stringResource(CommonStrings.action_learn_more) |
44 | | - val displayName = identityChangeViolation.identityRoomMember.displayNameOrDefault |
45 | | - val userIdStr = stringResource( |
46 | | - CommonStrings.crypto_identity_change_pin_violation_new_user_id, |
47 | | - identityChangeViolation.identityRoomMember.userId, |
48 | | - ) |
49 | | - val fullText = if (identityChangeViolation.identityState == IdentityState.PinViolation) { |
50 | | - stringResource( |
51 | | - id = CommonStrings.crypto_identity_change_pin_violation_new, |
52 | | - displayName, |
53 | | - userIdStr, |
54 | | - learnMoreStr, |
55 | | - ) |
56 | | - } else { |
57 | | - stringResource( |
58 | | - id = CommonStrings.crypto_identity_change_verification_violation_new, |
59 | | - displayName, |
60 | | - userIdStr, |
61 | | - learnMoreStr, |
62 | | - ) |
63 | | - } |
64 | | - append(fullText) |
65 | | - val userIdStartIndex = fullText.indexOf(userIdStr) |
66 | | - addStyle( |
67 | | - style = SpanStyle( |
68 | | - fontWeight = FontWeight.Bold, |
69 | | - ), |
70 | | - start = userIdStartIndex, |
71 | | - end = userIdStartIndex + userIdStr.length, |
72 | | - ) |
73 | | - val learnMoreStartIndex = fullText.lastIndexOf(learnMoreStr) |
74 | | - addStyle( |
75 | | - style = SpanStyle( |
76 | | - textDecoration = TextDecoration.Underline, |
77 | | - fontWeight = FontWeight.Bold, |
78 | | - color = ElementTheme.colors.textPrimary |
79 | | - ), |
80 | | - start = learnMoreStartIndex, |
81 | | - end = learnMoreStartIndex + learnMoreStr.length, |
82 | | - ) |
83 | | - addLink( |
84 | | - url = LinkAnnotation.Url( |
85 | | - url = LearnMoreConfig.IDENTITY_CHANGE_URL, |
86 | | - linkInteractionListener = { |
87 | | - onLinkClick(LearnMoreConfig.IDENTITY_CHANGE_URL, true) |
88 | | - } |
89 | | - ), |
90 | | - start = learnMoreStartIndex, |
91 | | - end = learnMoreStartIndex + learnMoreStr.length, |
92 | | - ) |
93 | | - }, |
94 | | - submitText = if (identityChangeViolation.identityState == IdentityState.VerificationViolation) { |
95 | | - stringResource(CommonStrings.crypto_identity_change_withdraw_verification_action) |
96 | | - } else { |
97 | | - stringResource(CommonStrings.action_ok) |
98 | | - }, |
99 | | - onSubmitClick = { |
100 | | - if (identityChangeViolation.identityState == IdentityState.VerificationViolation) { |
101 | | - state.eventSink(IdentityChangeEvent.WithdrawVerification(identityChangeViolation.identityRoomMember.userId)) |
102 | | - } else { |
103 | | - state.eventSink(IdentityChangeEvent.PinIdentity(identityChangeViolation.identityRoomMember.userId)) |
104 | | - } |
105 | | - }, |
106 | | - isCritical = identityChangeViolation.identityState == IdentityState.VerificationViolation, |
107 | 48 | ) |
| 49 | + IdentityState.VerificationViolation -> ViolationAlert( |
| 50 | + identityChangeViolation = identityChangeViolation, |
| 51 | + onLinkClick = onLinkClick, |
| 52 | + textId = CommonStrings.crypto_identity_change_verification_violation_new, |
| 53 | + isCritical = true, |
| 54 | + submitTextId = CommonStrings.crypto_identity_change_withdraw_verification_action, |
| 55 | + onSubmitClick = { state.eventSink(IdentityChangeEvent.WithdrawVerification(identityChangeViolation.identityRoomMember.userId)) }, |
| 56 | + modifier = modifier, |
| 57 | + ) |
| 58 | + else -> Unit |
108 | 59 | } |
109 | 60 | } |
110 | 61 |
|
| 62 | +@Composable |
| 63 | +private fun ViolationAlert( |
| 64 | + identityChangeViolation: RoomMemberIdentityStateChange, |
| 65 | + onLinkClick: (String, Boolean) -> Unit, |
| 66 | + @StringRes textId: Int, |
| 67 | + isCritical: Boolean, |
| 68 | + @StringRes submitTextId: Int, |
| 69 | + onSubmitClick: () -> Unit, |
| 70 | + modifier: Modifier = Modifier, |
| 71 | +) { |
| 72 | + ComposerAlertMolecule( |
| 73 | + modifier = modifier, |
| 74 | + avatar = identityChangeViolation.identityRoomMember.avatarData, |
| 75 | + content = buildAnnotatedString { |
| 76 | + val learnMoreStr = stringResource(CommonStrings.action_learn_more) |
| 77 | + val displayName = identityChangeViolation.identityRoomMember.displayNameOrDefault |
| 78 | + val userIdStr = stringResource( |
| 79 | + CommonStrings.crypto_identity_change_pin_violation_new_user_id, |
| 80 | + identityChangeViolation.identityRoomMember.userId, |
| 81 | + ) |
| 82 | + val fullText = stringResource(textId, displayName, userIdStr, learnMoreStr) |
| 83 | + append(fullText) |
| 84 | + val userIdStartIndex = fullText.indexOf(userIdStr) |
| 85 | + addStyle( |
| 86 | + style = SpanStyle( |
| 87 | + fontWeight = FontWeight.Bold, |
| 88 | + ), |
| 89 | + start = userIdStartIndex, |
| 90 | + end = userIdStartIndex + userIdStr.length, |
| 91 | + ) |
| 92 | + val learnMoreStartIndex = fullText.lastIndexOf(learnMoreStr) |
| 93 | + addStyle( |
| 94 | + style = SpanStyle( |
| 95 | + textDecoration = TextDecoration.Underline, |
| 96 | + fontWeight = FontWeight.Bold, |
| 97 | + color = ElementTheme.colors.textPrimary |
| 98 | + ), |
| 99 | + start = learnMoreStartIndex, |
| 100 | + end = learnMoreStartIndex + learnMoreStr.length, |
| 101 | + ) |
| 102 | + addLink( |
| 103 | + url = LinkAnnotation.Url( |
| 104 | + url = LearnMoreConfig.IDENTITY_CHANGE_URL, |
| 105 | + linkInteractionListener = { |
| 106 | + onLinkClick(LearnMoreConfig.IDENTITY_CHANGE_URL, true) |
| 107 | + } |
| 108 | + ), |
| 109 | + start = learnMoreStartIndex, |
| 110 | + end = learnMoreStartIndex + learnMoreStr.length, |
| 111 | + ) |
| 112 | + }, |
| 113 | + submitText = stringResource(submitTextId), |
| 114 | + onSubmitClick = onSubmitClick, |
| 115 | + isCritical = isCritical, |
| 116 | + ) |
| 117 | +} |
| 118 | + |
111 | 119 | @PreviewsDayNight |
112 | 120 | @Composable |
113 | 121 | internal fun IdentityChangeStateViewPreview( |
|
0 commit comments