Skip to content

Commit

Permalink
Merge pull request #4668 from vector-im/feature/adm/i-already-have-an…
Browse files Browse the repository at this point in the history
…-account

FTUE Auth - I already have an account
  • Loading branch information
ouchadam authored Jan 6, 2022
2 parents b40324a + 41f931e commit f24c962
Show file tree
Hide file tree
Showing 23 changed files with 474 additions and 70 deletions.
1 change: 1 addition & 0 deletions changelog.d/4382.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updates onboarding splash screen to have a dedicated sign in button and removes the dual purpose sign in/up stage
2 changes: 1 addition & 1 deletion tools/check/forbidden_strings_in_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Formatter\.formatShortFileSize===1
# android\.text\.TextUtils

### This is not a rule, but a warning: the number of "enum class" has changed. For Json classes, it is mandatory that they have `@JsonClass(generateAdapter = false)`. If the enum is not used as a Json class, change the value in file forbidden_strings_in_code.txt
enum class===117
enum class===118

### Do not import temporary legacy classes
import org.matrix.android.sdk.internal.legacy.riot===3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2019 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.debug.features

import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel

@EpoxyModelClass(layout = im.vector.app.R.layout.item_feature)
abstract class BooleanFeatureItem : VectorEpoxyModel<BooleanFeatureItem.Holder>() {

@EpoxyAttribute
lateinit var feature: Feature.BooleanFeature

@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var listener: Listener? = null

override fun bind(holder: Holder) {
super.bind(holder)
holder.label.text = feature.label

holder.optionsSpinner.apply {
val arrayAdapter = ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item)
val options = listOf(
"DEFAULT - ${feature.featureDefault.toEmoji()}",
"",
""
)
arrayAdapter.addAll(options)
adapter = arrayAdapter

feature.featureOverride?.let {
setSelection(options.indexOf(it.toEmoji()), false)
}

onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (position) {
0 -> listener?.onBooleanOptionSelected(option = null, feature)
else -> listener?.onBooleanOptionSelected(options[position].fromEmoji(), feature)
}
}

override fun onNothingSelected(parent: AdapterView<*>?) {
// do nothing
}
}
}
}

class Holder : VectorEpoxyHolder() {
val label by bind<TextView>(im.vector.app.R.id.feature_label)
val optionsSpinner by bind<Spinner>(im.vector.app.R.id.feature_options)
}

interface Listener {
fun onBooleanOptionSelected(option: Boolean?, feature: Feature.BooleanFeature)
}
}

private fun Boolean.toEmoji() = if (this) "" else ""
private fun String.fromEmoji() = when (this) {
"" -> true
"" -> false
else -> error("unexpected input $this")
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ class DebugFeaturesSettingsActivity : VectorBaseActivity<FragmentGenericRecycler

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
controller.listener = object : EnumFeatureItem.Listener {
override fun <T : Enum<T>> onOptionSelected(option: T?, feature: Feature.EnumFeature<T>) {
controller.listener = object : FeaturesController.Listener {
override fun <T : Enum<T>> onEnumOptionSelected(option: T?, feature: Feature.EnumFeature<T>) {
debugFeatures.overrideEnum(option, feature.type)
}

override fun onBooleanOptionSelected(option: Boolean?, feature: Feature.BooleanFeature) {
debugFeatures.override(option, feature.key)
}
}
views.genericRecyclerView.configureWith(controller)
controller.setData(debugFeaturesStateFactory.create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ class DebugFeaturesStateFactory @Inject constructor(
return FeaturesState(listOf(
createEnumFeature(
label = "Onboarding variant",
selection = debugFeatures.onboardingVariant(),
default = defaultFeatures.onboardingVariant()
featureOverride = debugFeatures.onboardingVariant(),
featureDefault = defaultFeatures.onboardingVariant()
),

Feature.BooleanFeature(
label = "FTUE Splash - I already have an account",
featureOverride = debugFeatures.isAlreadyHaveAccountSplashEnabled().takeIf {
debugFeatures.hasOverride(DebugFeatureKeys.alreadyHaveAnAccount)
},
featureDefault = defaultFeatures.isAlreadyHaveAccountSplashEnabled(),
key = DebugFeatureKeys.alreadyHaveAnAccount
)
))
}

private inline fun <reified T : Enum<T>> createEnumFeature(label: String, selection: T, default: T): Feature {
private inline fun <reified T : Enum<T>> createEnumFeature(label: String, featureOverride: T, featureDefault: T): Feature {
return Feature.EnumFeature(
label = label,
selection = selection.takeIf { debugFeatures.hasEnumOverride(T::class) },
default = default,
override = featureOverride.takeIf { debugFeatures.hasEnumOverride(T::class) },
default = featureDefault,
options = enumValues<T>().toList(),
type = T::class
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.MutablePreferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
Expand All @@ -42,13 +43,26 @@ class DebugVectorFeatures(
return readPreferences().getEnum<VectorFeatures.OnboardingVariant>() ?: vectorFeatures.onboardingVariant()
}

override fun isAlreadyHaveAccountSplashEnabled(): Boolean = readPreferences()[DebugFeatureKeys.alreadyHaveAnAccount]
?: vectorFeatures.isAlreadyHaveAccountSplashEnabled()

fun <T> override(value: T?, key: Preferences.Key<T>) = updatePreferences {
if (value == null) {
it.remove(key)
} else {
it[key] = value
}
}

fun <T> hasOverride(key: Preferences.Key<T>) = readPreferences().contains(key)

fun <T : Enum<T>> hasEnumOverride(type: KClass<T>) = readPreferences().containsEnum(type)

fun <T : Enum<T>> overrideEnum(value: T?, type: KClass<T>) {
fun <T : Enum<T>> overrideEnum(value: T?, type: KClass<T>) = updatePreferences {
if (value == null) {
updatePreferences { it.removeEnum(type) }
it.removeEnum(type)
} else {
updatePreferences { it.putEnum(value, type) }
it.putEnum(value, type)
}
}

Expand Down Expand Up @@ -76,3 +90,8 @@ private inline fun <reified T : Enum<T>> Preferences.getEnum(): T? {
private inline fun <reified T : Enum<T>> enumPreferencesKey() = enumPreferencesKey(T::class)

private fun <T : Enum<T>> enumPreferencesKey(type: KClass<T>) = stringPreferencesKey("enum-${type.simpleName}")

object DebugFeatureKeys {

val alreadyHaveAnAccount = booleanPreferencesKey("already-have-an-account")
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ abstract class EnumFeatureItem : VectorEpoxyModel<EnumFeatureItem.Holder>() {
arrayAdapter.addAll(feature.options.map { it.name })
adapter = arrayAdapter

feature.selection?.let {
feature.override?.let {
setSelection(feature.options.indexOf(it) + 1, false)
}

onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (position) {
0 -> listener?.onOptionSelected(option = null, feature)
0 -> listener?.onEnumOptionSelected(option = null, feature)
else -> feature.onOptionSelected(position - 1)
}
}
Expand All @@ -65,7 +65,7 @@ abstract class EnumFeatureItem : VectorEpoxyModel<EnumFeatureItem.Holder>() {
}

private fun <T : Enum<T>> Feature.EnumFeature<T>.onOptionSelected(selection: Int) {
listener?.onOptionSelected(options[selection], this)
listener?.onEnumOptionSelected(options[selection], this)
}

class Holder : VectorEpoxyHolder() {
Expand All @@ -74,6 +74,6 @@ abstract class EnumFeatureItem : VectorEpoxyModel<EnumFeatureItem.Holder>() {
}

interface Listener {
fun <T : Enum<T>> onOptionSelected(option: T?, feature: Feature.EnumFeature<T>)
fun <T : Enum<T>> onEnumOptionSelected(option: T?, feature: Feature.EnumFeature<T>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package im.vector.app.features.debug.features

import androidx.datastore.preferences.core.Preferences
import com.airbnb.epoxy.TypedEpoxyController
import javax.inject.Inject
import kotlin.reflect.KClass
Expand All @@ -28,16 +29,23 @@ sealed interface Feature {

data class EnumFeature<T : Enum<T>>(
val label: String,
val selection: T?,
val override: T?,
val default: T,
val options: List<T>,
val type: KClass<T>
) : Feature

data class BooleanFeature(
val label: String,
val featureOverride: Boolean?,
val featureDefault: Boolean,
val key: Preferences.Key<Boolean>
) : Feature
}

class FeaturesController @Inject constructor() : TypedEpoxyController<FeaturesState>() {

var listener: EnumFeatureItem.Listener? = null
var listener: Listener? = null

override fun buildModels(data: FeaturesState?) {
if (data == null) return
Expand All @@ -49,7 +57,14 @@ class FeaturesController @Inject constructor() : TypedEpoxyController<FeaturesSt
feature(feature)
listener(this@FeaturesController.listener)
}
is Feature.BooleanFeature -> booleanFeatureItem {
id(index)
feature(feature)
listener(this@FeaturesController.listener)
}
}
}
}

interface Listener : EnumFeatureItem.Listener, BooleanFeatureItem.Listener
}
3 changes: 3 additions & 0 deletions vector/src/main/java/im/vector/app/features/VectorFeatures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface VectorFeatures {

fun onboardingVariant(): OnboardingVariant

fun isAlreadyHaveAccountSplashEnabled(): Boolean

enum class OnboardingVariant {
LEGACY,
LOGIN_2,
Expand All @@ -36,4 +38,5 @@ interface VectorFeatures {

class DefaultVectorFeatures : VectorFeatures {
override fun onboardingVariant(): VectorFeatures.OnboardingVariant = BuildConfig.ONBOARDING_VARIANT
override fun isAlreadyHaveAccountSplashEnabled() = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class LoginSplashFragment @Inject constructor(
views.loginSplashVersion.text = "Version : ${BuildConfig.VERSION_NAME}\n" +
"Branch: ${BuildConfig.GIT_BRANCH_NAME}\n" +
"Build: ${BuildConfig.BUILD_NUMBER}"
views.loginSplashVersion.debouncedClicks { navigator.openDebug(requireContext()) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LoginSplashSignUpSignInSelectionFragment2 @Inject constructor(
views.loginSplashVersion.text = "Version : ${BuildConfig.VERSION_NAME}\n" +
"Branch: ${BuildConfig.GIT_BRANCH_NAME}\n" +
"Build: ${BuildConfig.BUILD_NUMBER}"
views.loginSplashVersion.debouncedClicks { navigator.openDebug(requireContext()) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
import org.matrix.android.sdk.internal.network.ssl.Fingerprint

sealed class OnboardingAction : VectorViewModelAction {
data class OnGetStarted(val resetLoginConfig: Boolean) : OnboardingAction()
data class OnGetStarted(val resetLoginConfig: Boolean, val onboardingFlow: OnboardingFlow) : OnboardingAction()
data class OnIAlreadyHaveAnAccount(val resetLoginConfig: Boolean, val onboardingFlow: OnboardingFlow) : OnboardingAction()

data class UpdateServerType(val serverType: ServerType) : OnboardingAction()
data class UpdateHomeServer(val homeServerUrl: String) : OnboardingAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import javax.inject.Inject
class OnboardingActivity : VectorBaseActivity<ActivityLoginBinding>(), ToolbarConfigurable, UnlockedActivity {

private val onboardingVariant by lifecycleAwareLazy {
onboardingVariantFactory.create(this, onboardingViewModel = lazyViewModel(), loginViewModel2 = lazyViewModel())
onboardingVariantFactory.create(this, views = views, onboardingViewModel = lazyViewModel(), loginViewModel2 = lazyViewModel())
}

@Inject lateinit var onboardingVariantFactory: OnboardingVariantFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@

package im.vector.app.features.onboarding

import im.vector.app.databinding.ActivityLoginBinding
import im.vector.app.features.VectorFeatures
import im.vector.app.features.login2.LoginViewModel2
import im.vector.app.features.onboarding.ftueauth.FtueAuthVariant
import javax.inject.Inject

class OnboardingVariantFactory @Inject constructor(
private val vectorFeatures: VectorFeatures,
) {

fun create(activity: OnboardingActivity,
views: ActivityLoginBinding,
onboardingViewModel: Lazy<OnboardingViewModel>,
loginViewModel2: Lazy<LoginViewModel2>
) = when (vectorFeatures.onboardingVariant()) {
VectorFeatures.OnboardingVariant.LEGACY -> error("Legacy is not supported by the FTUE")
VectorFeatures.OnboardingVariant.FTUE_AUTH -> OnboardingAuthVariant(
views = activity.getBinding(),
VectorFeatures.OnboardingVariant.LEGACY -> error("Legacy is not supported by the FTUE")
VectorFeatures.OnboardingVariant.FTUE_AUTH -> FtueAuthVariant(
views = views,
onboardingViewModel = onboardingViewModel.value,
activity = activity,
supportFragmentManager = activity.supportFragmentManager
)
VectorFeatures.OnboardingVariant.LOGIN_2 -> Login2Variant(
views = activity.getBinding(),
VectorFeatures.OnboardingVariant.LOGIN_2 -> Login2Variant(
views = views,
loginViewModel = loginViewModel2.value,
activity = activity,
supportFragmentManager = activity.supportFragmentManager
Expand Down
Loading

0 comments on commit f24c962

Please sign in to comment.