Implement show qr code screen.

This commit is contained in:
Onuray Sahin 2022-10-07 17:34:41 +03:00
parent 1932edad46
commit 04fb31666b
11 changed files with 177 additions and 10 deletions

View file

@ -3333,6 +3333,8 @@
<!-- QR Code Login --> <!-- QR Code Login -->
<string name="qr_code_login_header_scan_qr_code_title">Scan QR code</string> <string name="qr_code_login_header_scan_qr_code_title">Scan QR code</string>
<string name="qr_code_login_header_scan_qr_code_description">Use the camera on this device to scan the QR code shown on your other device:</string> <string name="qr_code_login_header_scan_qr_code_description">Use the camera on this device to scan the QR code shown on your other device:</string>
<string name="qr_code_login_header_show_qr_code_title">Sign in with QR code</string>
<string name="qr_code_login_header_show_qr_code_description">Use your signed in device to scan the QR code below:</string>
<string name="qr_code_login_header_connected_title">Secure connection established</string> <string name="qr_code_login_header_connected_title">Secure connection established</string>
<string name="qr_code_login_header_connected_description">Check your signed in device, the code below should be displayed. Confirm that the code below matches with that device:</string> <string name="qr_code_login_header_connected_description">Check your signed in device, the code below should be displayed. Confirm that the code below matches with that device:</string>
<string name="qr_code_login_new_device_instruction_1">Open Element on your other device</string> <string name="qr_code_login_new_device_instruction_1">Open Element on your other device</string>

View file

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

View file

@ -23,7 +23,7 @@ import android.view.View
import com.airbnb.mvrx.Mavericks import com.airbnb.mvrx.Mavericks
import com.airbnb.mvrx.viewModel import com.airbnb.mvrx.viewModel
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.extensions.addFragment import im.vector.app.core.extensions.addFragmentToBackstack
import im.vector.app.core.platform.SimpleFragmentActivity import im.vector.app.core.platform.SimpleFragmentActivity
@AndroidEntryPoint @AndroidEntryPoint
@ -38,10 +38,11 @@ class QrCodeLoginActivity : SimpleFragmentActivity() {
val qrCodeLoginArgs: QrCodeLoginArgs? = intent?.extras?.getParcelable(Mavericks.KEY_ARG) val qrCodeLoginArgs: QrCodeLoginArgs? = intent?.extras?.getParcelable(Mavericks.KEY_ARG)
if (isFirstCreation()) { if (isFirstCreation()) {
if (qrCodeLoginArgs?.loginType == QrCodeLoginType.LOGIN) { if (qrCodeLoginArgs?.loginType == QrCodeLoginType.LOGIN) {
addFragment( addFragmentToBackstack(
views.container, views.container,
QrCodeLoginInstructionsFragment::class.java, QrCodeLoginInstructionsFragment::class.java,
qrCodeLoginArgs qrCodeLoginArgs,
tag = FRAGMENT_QR_CODE_INSTRUCTIONS_TAG
) )
} }
} }
@ -53,19 +54,33 @@ class QrCodeLoginActivity : SimpleFragmentActivity() {
viewModel.observeViewEvents { viewModel.observeViewEvents {
when (it) { when (it) {
QrCodeLoginViewEvents.NavigateToStatusScreen -> handleNavigateToStatusScreen() QrCodeLoginViewEvents.NavigateToStatusScreen -> handleNavigateToStatusScreen()
QrCodeLoginViewEvents.NavigateToShowQrCodeScreen -> handleNavigateToShowQrCodeScreen()
} }
} }
} }
private fun handleNavigateToStatusScreen() { private fun handleNavigateToShowQrCodeScreen() {
addFragment( addFragmentToBackstack(
views.container, views.container,
QrCodeLoginStatusFragment::class.java QrCodeLoginShowQrCodeFragment::class.java,
tag = FRAGMENT_SHOW_QR_CODE_TAG
)
}
private fun handleNavigateToStatusScreen() {
addFragmentToBackstack(
views.container,
QrCodeLoginStatusFragment::class.java,
tag = FRAGMENT_QR_CODE_STATUS_TAG
) )
} }
companion object { companion object {
private const val FRAGMENT_QR_CODE_INSTRUCTIONS_TAG = "FRAGMENT_QR_CODE_INSTRUCTIONS_TAG"
private const val FRAGMENT_SHOW_QR_CODE_TAG = "FRAGMENT_SHOW_QR_CODE_TAG"
private const val FRAGMENT_QR_CODE_STATUS_TAG = "FRAGMENT_QR_CODE_STATUS_TAG"
fun getIntent(context: Context, qrCodeLoginArgs: QrCodeLoginArgs): Intent { fun getIntent(context: Context, qrCodeLoginArgs: QrCodeLoginArgs): Intent {
return Intent(context, QrCodeLoginActivity::class.java).apply { return Intent(context, QrCodeLoginActivity::class.java).apply {
putExtra(Mavericks.KEY_ARG, qrCodeLoginArgs) putExtra(Mavericks.KEY_ARG, qrCodeLoginArgs)

View file

@ -23,23 +23,32 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.fragmentViewModel
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentQrCodeLoginInstructionsBinding import im.vector.app.databinding.FragmentQrCodeLoginInstructionsBinding
import im.vector.app.features.qrcode.QrCodeScannerActivity import im.vector.app.features.qrcode.QrCodeScannerActivity
import timber.log.Timber import timber.log.Timber
@AndroidEntryPoint
class QrCodeLoginInstructionsFragment : VectorBaseFragment<FragmentQrCodeLoginInstructionsBinding>() { class QrCodeLoginInstructionsFragment : VectorBaseFragment<FragmentQrCodeLoginInstructionsBinding>() {
private val viewModel: QrCodeLoginViewModel by activityViewModel() private val viewModel: QrCodeLoginViewModel by activityViewModel()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentQrCodeLoginInstructionsBinding {
return FragmentQrCodeLoginInstructionsBinding.inflate(inflater, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
initScanQrCodeButton() initScanQrCodeButton()
initShowQrCodeButton()
} }
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentQrCodeLoginInstructionsBinding { private fun initShowQrCodeButton() {
return FragmentQrCodeLoginInstructionsBinding.inflate(inflater, container, false) views.qrCodeLoginInstructionsShowQrCodeButton.debouncedClicks {
viewModel.handle(QrCodeLoginAction.ShowQrCode)
}
} }
private fun initScanQrCodeButton() { private fun initScanQrCodeButton() {

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.login.qr
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentQrCodeLoginShowQrCodeBinding
@AndroidEntryPoint
class QrCodeLoginShowQrCodeFragment : VectorBaseFragment<FragmentQrCodeLoginShowQrCodeBinding>() {
private val viewModel: QrCodeLoginViewModel by activityViewModel()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentQrCodeLoginShowQrCodeBinding {
return FragmentQrCodeLoginShowQrCodeBinding.inflate(inflater, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initCancelButton()
observeViewState()
viewModel.handle(QrCodeLoginAction.QrCodeViewStarted)
}
private fun initCancelButton() {
views.qrCodeLoginShowQrCodeCancelButton.debouncedClicks {
activity?.supportFragmentManager?.popBackStack()
}
}
private fun observeViewState() {
viewModel.onEach {
it.generatedQrCodeData?.let { qrCodeData ->
showQrCode(qrCodeData)
}
}
}
private fun showQrCode(qrCodeData: String) {
views.qrCodeLoginSHowQrCodeImageView.setData(qrCodeData)
}
}

View file

@ -22,11 +22,13 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.activityViewModel
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentQrCodeLoginStatusBinding import im.vector.app.databinding.FragmentQrCodeLoginStatusBinding
import im.vector.app.features.themes.ThemeUtils import im.vector.app.features.themes.ThemeUtils
@AndroidEntryPoint
class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBinding>() { class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBinding>() {
private val viewModel: QrCodeLoginViewModel by activityViewModel() private val viewModel: QrCodeLoginViewModel by activityViewModel()
@ -46,7 +48,7 @@ class QrCodeLoginStatusFragment : VectorBaseFragment<FragmentQrCodeLoginStatusBi
is QrCodeLoginConnectionStatus.Connected -> handleConnectionEstablished(it.connectionStatus) is QrCodeLoginConnectionStatus.Connected -> handleConnectionEstablished(it.connectionStatus)
QrCodeLoginConnectionStatus.ConnectingToDevice -> handleConnectingToDevice() QrCodeLoginConnectionStatus.ConnectingToDevice -> handleConnectingToDevice()
QrCodeLoginConnectionStatus.SigningIn -> handleSigningIn() QrCodeLoginConnectionStatus.SigningIn -> handleSigningIn()
null -> TODO() null -> { /* NOOP */ }
} }
} }
} }

View file

@ -20,4 +20,5 @@ import im.vector.app.core.platform.VectorViewEvents
sealed class QrCodeLoginViewEvents : VectorViewEvents { sealed class QrCodeLoginViewEvents : VectorViewEvents {
object NavigateToStatusScreen : QrCodeLoginViewEvents() object NavigateToStatusScreen : QrCodeLoginViewEvents()
object NavigateToShowQrCodeScreen : QrCodeLoginViewEvents()
} }

View file

@ -40,6 +40,21 @@ class QrCodeLoginViewModel @AssistedInject constructor(
override fun handle(action: QrCodeLoginAction) { override fun handle(action: QrCodeLoginAction) {
when (action) { when (action) {
is QrCodeLoginAction.OnQrCodeScanned -> handleOnQrCodeScanned(action) is QrCodeLoginAction.OnQrCodeScanned -> handleOnQrCodeScanned(action)
QrCodeLoginAction.QrCodeViewStarted -> handleQrCodeViewStarted()
QrCodeLoginAction.ShowQrCode -> handleShowQrCode()
}
}
private fun handleShowQrCode() {
_viewEvents.post(QrCodeLoginViewEvents.NavigateToShowQrCodeScreen)
}
private fun handleQrCodeViewStarted() {
val qrCodeData = generateQrCodeData()
setState {
copy(
generatedQrCodeData = qrCodeData
)
} }
} }
@ -84,4 +99,11 @@ class QrCodeLoginViewModel @AssistedInject constructor(
private fun isValidQrCode(qrCode: String): Boolean { private fun isValidQrCode(qrCode: String): Boolean {
return qrCode.startsWith("http") return qrCode.startsWith("http")
} }
/**
* TODO. UI test purpose. Fixme accordingly.
*/
private fun generateQrCodeData(): String {
return "https://element.io"
}
} }

View file

@ -21,6 +21,7 @@ import com.airbnb.mvrx.MavericksState
data class QrCodeLoginViewState( data class QrCodeLoginViewState(
val loginType: QrCodeLoginType, val loginType: QrCodeLoginType,
val connectionStatus: QrCodeLoginConnectionStatus? = null, val connectionStatus: QrCodeLoginConnectionStatus? = null,
val generatedQrCodeData: String? = null,
) : MavericksState { ) : MavericksState {
constructor(args: QrCodeLoginArgs) : this( constructor(args: QrCodeLoginArgs) : this(

View file

@ -21,7 +21,6 @@
android:id="@+id/qrCodeLoginInstructionsView" android:id="@+id/qrCodeLoginInstructionsView"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="24dp" android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"

View file

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="16dp"
android:background="?android:colorBackground">
<im.vector.app.features.login.qr.QrCodeLoginHeaderView
android:id="@+id/qrCodeLoginShowQrCodeHeaderView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:qrCodeLoginHeaderDescription="@string/qr_code_login_header_show_qr_code_description"
app:qrCodeLoginHeaderImageBackgroundTint="?colorPrimary"
app:qrCodeLoginHeaderImageResource="@drawable/ic_camera"
app:qrCodeLoginHeaderTitle="@string/qr_code_login_header_show_qr_code_title" />
<im.vector.app.features.login.qr.QrCodeLoginInstructionsView
android:id="@+id/qrCodeLoginShowQrCodeView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/qrCodeLoginShowQrCodeHeaderView"
app:qrCodeLoginInstruction1="@string/qr_code_login_new_device_instruction_1"
app:qrCodeLoginInstruction2="@string/qr_code_login_new_device_instruction_2"
app:qrCodeLoginInstruction3="@string/qr_code_login_new_device_instruction_3" />
<im.vector.app.core.ui.views.QrCodeImageView
android:id="@+id/qrCodeLoginSHowQrCodeImageView"
android:layout_width="240dp"
android:layout_height="240dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/qrCodeLoginShowQrCodeView"
app:layout_constraintBottom_toTopOf="@id/qrCodeLoginShowQrCodeCancelButton"/>
<Button
android:id="@+id/qrCodeLoginShowQrCodeCancelButton"
style="@style/Widget.Vector.Button.Outlined.Login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="@string/action_cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>