Merge branch 'feature/ons/qr_code_login_ui' into feature/hughns/qr_code_login

This commit is contained in:
Hugh Nimmo-Smith 2022-10-17 13:26:35 +01:00
commit e89774e160
17 changed files with 98 additions and 127 deletions

View file

@ -3386,7 +3386,7 @@
<string name="qr_code_login_header_failed_other_description">The request failed.</string>
<string name="qr_code_login_new_device_instruction_1">Open ${app_name} on your other device</string>
<string name="qr_code_login_new_device_instruction_2">Go to Settings -> Security &amp; Privacy -> Show All Sessions</string>
<string name="qr_code_login_new_device_instruction_3">Select \'Show QR code\'</string>
<string name="qr_code_login_new_device_instruction_3">Select \'Show QR code in this device\'</string>
<string name="qr_code_login_link_a_device_scan_qr_code_instruction_1">Start at the sign in screen</string>
<string name="qr_code_login_link_a_device_scan_qr_code_instruction_2">Select \'Sign in with QR code\'</string>
<string name="qr_code_login_link_a_device_show_qr_code_instruction_1">Start at the sign in screen</string>

View file

@ -20,7 +20,6 @@ import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.orTrue
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.internal.auth.version.Versions
@ -146,8 +145,8 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
homeServerCapabilitiesEntity.lastVersionIdentityServerSupported = getVersionResult.isLoginAndRegistrationSupportedBySdk()
homeServerCapabilitiesEntity.canControlLogoutDevices = getVersionResult.doesServerSupportLogoutDevices()
homeServerCapabilitiesEntity.canUseThreading = /* capabilities?.threads?.enabled.orFalse() || */
getVersionResult.doesServerSupportThreads().orFalse()
homeServerCapabilitiesEntity.canLoginWithQrCode = getVersionResult.doesServerSupportQrCodeLogin().orFalse()
getVersionResult.doesServerSupportThreads()
homeServerCapabilitiesEntity.canLoginWithQrCode = getVersionResult.doesServerSupportQrCodeLogin()
}
if (getWellknownResult != null && getWellknownResult is WellknownResult.Prompt) {

View file

@ -59,7 +59,7 @@ class DefaultVectorFeatures : VectorFeatures {
override fun isLocationSharingEnabled() = Config.ENABLE_LOCATION_SHARING
override fun forceUsageOfOpusEncoder(): Boolean = false
override fun isNewAppLayoutFeatureEnabled(): Boolean = true
override fun isQrCodeLoginEnabled(): Boolean = false
override fun isQrCodeLoginEnabled(): Boolean = true
override fun isQrCodeLoginForAllServers(): Boolean = false
override fun isReciprocateQrCodeLogin(): Boolean = false
override fun isVoiceBroadcastEnabled(): Boolean = false

View file

@ -20,6 +20,6 @@ import im.vector.app.core.platform.VectorViewModelAction
sealed class QrCodeLoginAction : VectorViewModelAction {
data class OnQrCodeScanned(val qrCode: String) : QrCodeLoginAction()
object QrCodeViewStarted : QrCodeLoginAction()
object GenerateQrCode : QrCodeLoginAction()
object ShowQrCode : QrCodeLoginAction()
}

View file

@ -27,7 +27,6 @@ import im.vector.app.core.extensions.addFragment
import im.vector.app.core.platform.SimpleFragmentActivity
import im.vector.app.features.home.HomeActivity
import im.vector.lib.core.utils.compat.getParcelableCompat
import org.matrix.android.sdk.api.extensions.orFalse
import timber.log.Timber
@AndroidEntryPoint
@ -46,7 +45,7 @@ class QrCodeLoginActivity : SimpleFragmentActivity() {
showInstructionsFragment(qrCodeLoginArgs)
}
QrCodeLoginType.LINK_A_DEVICE -> {
if (qrCodeLoginArgs.showQrCodeByDefault.orFalse()) {
if (qrCodeLoginArgs.showQrCodeImmediately) {
handleNavigateToShowQrCodeScreen()
} else {
showInstructionsFragment(qrCodeLoginArgs)
@ -55,6 +54,7 @@ class QrCodeLoginActivity : SimpleFragmentActivity() {
null -> {
Timber.i("QrCodeLoginArgs is null. This is not expected.")
finish()
return
}
}
}

View file

@ -22,5 +22,5 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class QrCodeLoginArgs(
val loginType: QrCodeLoginType,
val showQrCodeByDefault: Boolean,
val showQrCodeImmediately: Boolean,
) : Parcelable

View file

@ -24,6 +24,7 @@ import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.use
import im.vector.app.R
import im.vector.app.core.extensions.setTextOrHide
import im.vector.app.databinding.ViewQrCodeLoginHeaderBinding
class QrCodeLoginHeaderView @JvmOverloads constructor(
@ -52,27 +53,26 @@ class QrCodeLoginHeaderView @JvmOverloads constructor(
private fun setTitle(typedArray: TypedArray) {
val title = typedArray.getString(R.styleable.QrCodeLoginHeaderView_qrCodeLoginHeaderTitle)
binding.qrCodeLoginHeaderTitleTextView.text = title
setTitle(title)
}
private fun setDescription(typedArray: TypedArray) {
val description = typedArray.getString(R.styleable.QrCodeLoginHeaderView_qrCodeLoginHeaderDescription)
binding.qrCodeLoginHeaderDescriptionTextView.text = description
setDescription(description)
}
private fun setImage(typedArray: TypedArray) {
val imageResource = typedArray.getResourceId(R.styleable.QrCodeLoginHeaderView_qrCodeLoginHeaderImageResource, 0)
val backgroundTint = typedArray.getColor(R.styleable.QrCodeLoginHeaderView_qrCodeLoginHeaderImageBackgroundTint, 0)
binding.qrCodeLoginHeaderImageView.setImageResource(imageResource)
binding.qrCodeLoginHeaderImageView.backgroundTintList = ColorStateList.valueOf(backgroundTint)
setImage(imageResource, backgroundTint)
}
fun setTitle(title: String) {
binding.qrCodeLoginHeaderTitleTextView.text = title
fun setTitle(title: String?) {
binding.qrCodeLoginHeaderTitleTextView.setTextOrHide(title)
}
fun setDescription(description: String) {
binding.qrCodeLoginHeaderDescriptionTextView.text = description
fun setDescription(description: String?) {
binding.qrCodeLoginHeaderDescriptionTextView.setTextOrHide(description)
}
fun setImage(imageResource: Int, backgroundTintColor: Int) {

View file

@ -22,6 +22,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.registerStartForActivityResult
@ -41,32 +42,10 @@ class QrCodeLoginInstructionsFragment : VectorBaseFragment<FragmentQrCodeLoginIn
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
observeViewState()
initScanQrCodeButton()
initShowQrCodeButton()
}
private fun observeViewState() {
viewModel.onEach {
if (it.loginType == QrCodeLoginType.LOGIN) {
views.qrCodeLoginInstructionsView.setInstructions(
listOf(
getString(R.string.qr_code_login_new_device_instruction_1),
getString(R.string.qr_code_login_new_device_instruction_2),
getString(R.string.qr_code_login_new_device_instruction_3),
)
)
} else {
views.qrCodeLoginInstructionsView.setInstructions(
listOf(
getString(R.string.qr_code_login_link_a_device_scan_qr_code_instruction_1),
getString(R.string.qr_code_login_link_a_device_scan_qr_code_instruction_2),
)
)
}
}
}
private fun initShowQrCodeButton() {
views.qrCodeLoginInstructionsShowQrCodeButton.debouncedClicks {
viewModel.handle(QrCodeLoginAction.ShowQrCode)
@ -99,4 +78,23 @@ class QrCodeLoginInstructionsFragment : VectorBaseFragment<FragmentQrCodeLoginIn
private fun onQrCodeScannerFailed() {
Timber.d("QrCodeLoginInstructionsFragment.onQrCodeScannerFailed")
}
override fun invalidate() = withState(viewModel) { state ->
if (state.loginType == QrCodeLoginType.LOGIN) {
views.qrCodeLoginInstructionsView.setInstructions(
listOf(
getString(R.string.qr_code_login_new_device_instruction_1),
getString(R.string.qr_code_login_new_device_instruction_2),
getString(R.string.qr_code_login_new_device_instruction_3),
)
)
} else {
views.qrCodeLoginInstructionsView.setInstructions(
listOf(
getString(R.string.qr_code_login_link_a_device_scan_qr_code_instruction_1),
getString(R.string.qr_code_login_link_a_device_scan_qr_code_instruction_2),
)
)
}
}
}

View file

@ -54,18 +54,19 @@ class QrCodeLoginInstructionsView @JvmOverloads constructor(
val instruction1 = typedArray.getString(R.styleable.QrCodeLoginInstructionsView_qrCodeLoginInstruction1)
val instruction2 = typedArray.getString(R.styleable.QrCodeLoginInstructionsView_qrCodeLoginInstruction2)
val instruction3 = typedArray.getString(R.styleable.QrCodeLoginInstructionsView_qrCodeLoginInstruction3)
binding.instructions1Layout.isVisible = instruction1 != null
binding.instructions2Layout.isVisible = instruction2 != null
binding.instructions3Layout.isVisible = instruction3 != null
binding.instruction1TextView.text = instruction1
binding.instruction2TextView.text = instruction2
binding.instruction3TextView.text = instruction3
setInstructions(
listOf(
instruction1,
instruction2,
instruction3,
)
)
}
fun setInstructions(instructions: List<String>) {
setInstruction(binding.instructions1Layout, binding.instruction1TextView, instructions.getOrNull(0))
setInstruction(binding.instructions2Layout, binding.instruction2TextView, instructions.getOrNull(1))
setInstruction(binding.instructions3Layout, binding.instruction3TextView, instructions.getOrNull(2))
fun setInstructions(instructions: List<String?>?) {
setInstruction(binding.instructions1Layout, binding.instruction1TextView, instructions?.getOrNull(0))
setInstruction(binding.instructions2Layout, binding.instruction2TextView, instructions?.getOrNull(1))
setInstruction(binding.instructions3Layout, binding.instruction3TextView, instructions?.getOrNull(2))
}
private fun setInstruction(instructionLayout: LinearLayout, instructionTextView: TextView, instruction: String?) {

View file

@ -21,6 +21,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.platform.VectorBaseFragment
@ -38,8 +39,7 @@ class QrCodeLoginShowQrCodeFragment : VectorBaseFragment<FragmentQrCodeLoginShow
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initCancelButton()
observeViewState()
viewModel.handle(QrCodeLoginAction.QrCodeViewStarted)
viewModel.handle(QrCodeLoginAction.GenerateQrCode)
}
private fun initCancelButton() {
@ -48,15 +48,6 @@ class QrCodeLoginShowQrCodeFragment : VectorBaseFragment<FragmentQrCodeLoginShow
}
}
private fun observeViewState() {
viewModel.onEach {
setInstructions(it.loginType)
it.generatedQrCodeData?.let { qrCodeData ->
showQrCode(qrCodeData)
}
}
}
private fun setInstructions(loginType: QrCodeLoginType) {
if (loginType == QrCodeLoginType.LOGIN) {
views.qrCodeLoginShowQrCodeHeaderView.setDescription(getString(R.string.qr_code_login_header_show_qr_code_new_device_description))
@ -81,4 +72,11 @@ class QrCodeLoginShowQrCodeFragment : VectorBaseFragment<FragmentQrCodeLoginShow
private fun showQrCode(qrCodeData: String) {
views.qrCodeLoginSHowQrCodeImageView.setData(qrCodeData)
}
override fun invalidate() = withState(viewModel) { state ->
state.generatedQrCodeData?.let { qrCodeData ->
showQrCode(qrCodeData)
}
setInstructions(state.loginType)
}
}

View file

@ -22,6 +22,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.platform.VectorBaseFragment
@ -41,7 +42,6 @@ class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initCancelButton()
observeViewState()
}
private fun initCancelButton() {
@ -50,18 +50,6 @@ class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBi
}
}
private fun observeViewState() {
viewModel.onEach {
when (it.connectionStatus) {
is QrCodeLoginConnectionStatus.Connected -> handleConnectionEstablished(it.connectionStatus, it.loginType)
QrCodeLoginConnectionStatus.ConnectingToDevice -> handleConnectingToDevice()
QrCodeLoginConnectionStatus.SigningIn -> handleSigningIn()
is QrCodeLoginConnectionStatus.Failed -> handleFailed(it.connectionStatus)
null -> { /* NOOP */ }
}
}
}
private fun handleFailed(connectionStatus: QrCodeLoginConnectionStatus.Failed) {
views.qrCodeLoginConfirmSecurityCodeLayout.isVisible = false
views.qrCodeLoginStatusLoadingLayout.isVisible = false
@ -131,4 +119,14 @@ class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBi
backgroundTintColor = ThemeUtils.getColor(requireContext(), R.attr.colorPrimary)
)
}
override fun invalidate() = withState(viewModel) { state ->
when (state.connectionStatus) {
is QrCodeLoginConnectionStatus.Connected -> handleConnectionEstablished(state.connectionStatus, state.loginType)
QrCodeLoginConnectionStatus.ConnectingToDevice -> handleConnectingToDevice()
QrCodeLoginConnectionStatus.SigningIn -> handleSigningIn()
is QrCodeLoginConnectionStatus.Failed -> handleFailed(state.connectionStatus)
null -> { /* NOOP */ }
}
}
}

View file

@ -50,7 +50,7 @@ class QrCodeLoginViewModel @AssistedInject constructor(
override fun handle(action: QrCodeLoginAction) {
when (action) {
is QrCodeLoginAction.OnQrCodeScanned -> handleOnQrCodeScanned(action)
QrCodeLoginAction.QrCodeViewStarted -> handleQrCodeViewStarted()
QrCodeLoginAction.GenerateQrCode -> handleQrCodeViewStarted()
QrCodeLoginAction.ShowQrCode -> handleShowQrCode()
}
}
@ -139,6 +139,6 @@ class QrCodeLoginViewModel @AssistedInject constructor(
* TODO. UI test purpose. Fixme accordingly.
*/
private fun generateQrCodeData(): String {
return "https://element.io"
return "TODO"
}
}

View file

@ -118,7 +118,7 @@ class OnboardingViewModel @AssistedInject constructor(
}
}
private fun observeQrCodeLoginCapability(homeServerUrl: String) = viewModelScope.launch {
private fun checkQrCodeLoginCapability(homeServerUrl: String) {
if (!vectorFeatures.isQrCodeLoginEnabled()) {
setState {
copy(
@ -133,6 +133,7 @@ class OnboardingViewModel @AssistedInject constructor(
)
}
} else {
viewModelScope.launch {
// check if selected server supports MSC3882 first
homeServerConnectionConfigFactory.create(homeServerUrl)?.let {
val canLoginWithQrCode = authenticationService.isQrLoginSupported(it)
@ -144,6 +145,7 @@ class OnboardingViewModel @AssistedInject constructor(
}
}
}
}
private val matrixOrgUrl = stringProvider.getString(R.string.matrix_org_server_url).ensureTrailingSlash()
private val defaultHomeserverUrl = matrixOrgUrl
@ -707,7 +709,7 @@ class OnboardingViewModel @AssistedInject constructor(
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Unable to create a HomeServerConnectionConfig")))
} else {
startAuthenticationFlow(action, homeServerConnectionConfig, serverTypeOverride, postAction)
observeQrCodeLoginCapability(homeServerConnectionConfig.homeServerUri.toString())
checkQrCodeLoginCapability(homeServerConnectionConfig.homeServerUri.toString())
}
}

View file

@ -81,20 +81,18 @@ class FtueAuthCombinedLoginFragment :
}
private fun configureQrCodeLoginButtonVisibility(canLoginWithQrCode: Boolean) {
views.loginWithQrCode.isVisible = canLoginWithQrCode
if (canLoginWithQrCode) {
views.loginWithQrCode.isVisible = true
views.loginWithQrCode.debouncedClicks {
navigator
.openLoginWithQrCode(
requireActivity(),
QrCodeLoginArgs(
loginType = QrCodeLoginType.LOGIN,
showQrCodeByDefault = false,
showQrCodeImmediately = false,
)
)
}
} else {
views.loginWithQrCode.isVisible = false
}
}

View file

@ -170,27 +170,24 @@ class VectorSettingsDevicesFragment :
views.deviceListHeaderShowQrCodeButton.isVisible = true
views.deviceListHeaderScanQrCodeButton.debouncedClicks {
navigator
.openLoginWithQrCode(
requireActivity(),
QrCodeLoginArgs(
loginType = QrCodeLoginType.LINK_A_DEVICE,
showQrCodeByDefault = false,
)
)
navigateToQrCodeScreen(showQrCodeImmediately = false)
}
views.deviceListHeaderShowQrCodeButton.debouncedClicks {
navigateToQrCodeScreen(showQrCodeImmediately = true)
}
}
private fun navigateToQrCodeScreen(showQrCodeImmediately: Boolean) {
navigator
.openLoginWithQrCode(
requireActivity(),
QrCodeLoginArgs(
loginType = QrCodeLoginType.LINK_A_DEVICE,
showQrCodeByDefault = true,
showQrCodeImmediately = showQrCodeImmediately,
)
)
}
}
override fun onDestroyView() {
cleanUpLearnMoreButtonsListeners()

View file

@ -251,10 +251,12 @@
android:layout_height="60dp"
android:layout_marginTop="12dp"
android:text="@string/login_scan_qr_code"
android:visibility="gone"
app:drawableLeftCompat="@drawable/ic_qr_code"
app:layout_constraintEnd_toEndOf="@id/loginGutterEnd"
app:layout_constraintStart_toStartOf="@id/loginGutterStart"
app:layout_constraintTop_toBottomOf="@id/ssoButtonsHeader" />
app:layout_constraintTop_toBottomOf="@id/ssoButtonsHeader"
tools:visibility="visible" />
<im.vector.app.features.login.SocialLoginButtonsView
android:id="@+id/ssoButtons"

View file

@ -98,26 +98,4 @@
tools:text="@string/qr_code_login_new_device_instruction_3" />
</LinearLayout>
<LinearLayout
android:id="@+id/instructions4Layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/instructions3Layout"
tools:visibility="visible">
<TextView
style="@style/TextAppearance.Vector.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle_qr_code_login_instruction_with_border"
android:padding="6dp"
android:text="@string/four"
android:textColor="?colorPrimary" />
</LinearLayout>
</merge>