Restored initial login form on assistant landing page

This commit is contained in:
Sylvain Berfini 2024-03-18 14:05:49 +01:00
parent 5ab0b32705
commit 2ca3d152d1
5 changed files with 123 additions and 39 deletions

View file

@ -27,16 +27,21 @@ import android.view.View
import android.view.ViewGroup
import androidx.annotation.UiThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.core.tools.Log
import org.linphone.databinding.AssistantLandingFragmentBinding
import org.linphone.ui.assistant.AssistantActivity
import org.linphone.ui.assistant.model.AcceptConditionsAndPolicyDialogModel
import org.linphone.ui.assistant.viewmodel.LandingViewModel
import org.linphone.utils.DialogUtils
import org.linphone.utils.PhoneNumberUtils
@UiThread
class LandingFragment : Fragment() {
@ -65,6 +70,14 @@ class LandingFragment : Fragment() {
binding.lifecycleOwner = viewLifecycleOwner
binding.viewModel = viewModel
coreContext.postOnCoreThread {
val dialPlan = PhoneNumberUtils.getDeviceDialPlan(requireContext())
if (dialPlan != null) {
viewModel.internationalPrefix.postValue(dialPlan.countryCallingCode)
viewModel.internationalPrefixIsoCountryCode.postValue(dialPlan.isoCountryCode)
}
}
binding.setBackClickListener {
requireActivity().finish()
}
@ -90,6 +103,42 @@ class LandingFragment : Fragment() {
}
}
binding.setForgottenPasswordClickListener {
val url = getString(R.string.web_platform_forgotten_password_url)
try {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(browserIntent)
} catch (ise: IllegalStateException) {
Log.e(
"$TAG Can't start ACTION_VIEW intent for URL [$url], IllegalStateException: $ise"
)
}
}
viewModel.showPassword.observe(viewLifecycleOwner) {
lifecycleScope.launch {
delay(50)
binding.password.setSelection(binding.password.text?.length ?: 0)
}
}
viewModel.accountLoggedInEvent.observe(viewLifecycleOwner) {
it.consume {
Log.i("$TAG Account successfully logged-in, go to profile mode fragment")
val action = LandingFragmentDirections.actionLandingFragmentToProfileModeFragment()
findNavController().navigate(action)
}
}
viewModel.accountLoginErrorEvent.observe(viewLifecycleOwner) {
it.consume { message ->
(requireActivity() as AssistantActivity).showRedToast(
message,
R.drawable.warning_circle
)
}
}
viewModel.redirectToDigestAuthEvent.observe(viewLifecycleOwner) {
it.consume { address ->
goToLoginFragment(address)

View file

@ -38,7 +38,7 @@ import org.linphone.core.tools.Log
import org.linphone.utils.AppUtils
import org.linphone.utils.Event
class AccountLoginViewModel @UiThread constructor() : ViewModel() {
open class AccountLoginViewModel @UiThread constructor() : ViewModel() {
companion object {
private const val TAG = "[Account Login ViewModel]"
}

View file

@ -21,22 +21,17 @@ package org.linphone.ui.assistant.viewmodel
import androidx.annotation.UiThread
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.core.Factory
import org.linphone.core.tools.Log
import org.linphone.utils.Event
class LandingViewModel @UiThread constructor() : ViewModel() {
class LandingViewModel @UiThread constructor() : AccountLoginViewModel() {
companion object {
private const val TAG = "[Account Login ViewModel]"
}
val showBackButton = MutableLiveData<Boolean>()
val sipIdentity = MutableLiveData<String>()
val hideCreateAccount = MutableLiveData<Boolean>()
val hideScanQrCode = MutableLiveData<Boolean>()
@ -63,33 +58,4 @@ class LandingViewModel @UiThread constructor() : ViewModel() {
conditionsAndPrivacyPolicyAccepted = corePreferences.conditionsAndPrivacyPolicyAccepted
}
}
@UiThread
fun login() {
coreContext.postOnCoreThread {
var identity = sipIdentity.value.orEmpty().trim()
if (!identity.startsWith("sip:")) {
identity = "sip:$identity"
}
if (!identity.contains("@")) {
identity = "$identity@${corePreferences.defaultDomain}"
}
val identityAddress = Factory.instance().createAddress(identity)
if (identityAddress == null) {
// TODO: FIXME: show error
Log.e("$TAG Can't parse [$identity] as Address!")
return@postOnCoreThread
}
// TODO: SSO or password auth?
if (identityAddress.domain == corePreferences.defaultDomain) {
Log.i("$TAG Address matches default domain, using digest authentication")
redirectToDigestAuthEvent.postValue(Event(identityAddress.asStringUriOnly()))
} else {
Log.i("$TAG Address doesn't match default domain, using Single Sign On")
// TODO FIXME: use username or full SIP address as login?
redirectToSingleSignOnEvent.postValue(Event(identityAddress.username.orEmpty()))
}
}
}
}

View file

@ -8,6 +8,9 @@
<variable
name="backClickListener"
type="View.OnClickListener" />
<variable
name="forgottenPasswordClickListener"
type="View.OnClickListener" />
<variable
name="registerClickListener"
type="View.OnClickListener" />
@ -102,9 +105,54 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/header_style"
android:id="@+id/password_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:text="@{@string/password + `*`, default=@string/password}"
app:layout_constraintWidth_max="@dimen/text_input_max_width"
app:layout_constraintTop_toBottomOf="@id/sip_identity"
app:layout_constraintStart_toStartOf="@id/password"/>
<androidx.appcompat.widget.AppCompatEditText
style="@style/default_text_style"
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@={viewModel.password, default=`johndoe`}"
android:textSize="14sp"
android:textColor="?attr/color_main2_600"
android:background="@drawable/edit_text_background"
android:hint="@string/password"
android:inputType="@{viewModel.showPassword ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD, default=textPassword}"
app:layout_constraintWidth_max="@dimen/text_input_max_width"
app:layout_constraintTop_toBottomOf="@id/password_label"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:onClick="@{() -> viewModel.toggleShowPassword()}"
android:id="@+id/eye"
android:layout_width="@dimen/icon_size"
android:layout_height="0dp"
android:padding="4dp"
android:layout_marginEnd="20dp"
android:src="@{viewModel.showPassword ? @drawable/eye_slash : @drawable/eye, default=@drawable/eye}"
app:tint="?attr/color_main2_500"
app:layout_constraintEnd_toEndOf="@id/password"
app:layout_constraintTop_toTopOf="@id/password"
app:layout_constraintBottom_toBottomOf="@id/password" />
<androidx.appcompat.widget.AppCompatTextView
android:onClick="@{() -> viewModel.login()}"
android:enabled="@{viewModel.sipIdentity.length() > 0, default=false}"
android:enabled="@{viewModel.loginEnabled &amp;&amp; !viewModel.registrationInProgress, default=false}"
style="@style/primary_button_label_style"
android:id="@+id/login"
android:layout_width="0dp"
@ -116,10 +164,28 @@
android:paddingEnd="20dp"
android:text="@string/assistant_account_login"
app:layout_constraintWidth_max="@dimen/button_max_width"
app:layout_constraintTop_toBottomOf="@id/sip_identity"
app:layout_constraintTop_toBottomOf="@id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:onClick="@{forgottenPasswordClickListener}"
style="@style/default_text_style_600"
android:id="@+id/forgotten_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/assistant_forgotten_password"
android:textSize="13sp"
android:textColor="?attr/color_main2_500"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/login"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/or"
@ -133,7 +199,7 @@
android:textColor="?attr/color_main2_500"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/login"/>
app:layout_constraintTop_toBottomOf="@id/forgotten_password"/>
<View
android:layout_width="0dp"

View file

@ -180,6 +180,9 @@
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:launchSingleTop="true" />
<action
android:id="@+id/action_landingFragment_to_profileModeFragment"
app:destination="@id/profileModeFragment" />
</fragment>