adding support for the boolean features and adding a new flag for the already have an account

This commit is contained in:
Adam Brown 2021-12-09 16:17:15 +00:00
parent 768419b11a
commit e3c0b34651
7 changed files with 150 additions and 14 deletions

View file

@ -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.default.toEmoji()}",
"",
""
)
arrayAdapter.addAll(options)
adapter = arrayAdapter
feature.override?.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")
}

View file

@ -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())

View file

@ -28,16 +28,25 @@ class DebugFeaturesStateFactory @Inject constructor(
return FeaturesState(listOf(
createEnumFeature(
label = "Onboarding variant",
selection = debugFeatures.onboardingVariant(),
override = debugFeatures.onboardingVariant(),
default = defaultFeatures.onboardingVariant()
),
Feature.BooleanFeature(
label = "Splash - already has an account",
override = debugFeatures.isAlreadyHaveAccountSplashEnabled().takeIf {
debugFeatures.hasOverride(DebugFeatureKeys.alreadyHaveAnAccount)
},
default = 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, override: T, default: T): Feature {
return Feature.EnumFeature(
label = label,
selection = selection.takeIf { debugFeatures.hasEnumOverride(T::class) },
override = override.takeIf { debugFeatures.hasEnumOverride(T::class) },
default = default,
options = enumValues<T>().toList(),
type = T::class

View file

@ -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
@ -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)
}
}
@ -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")
}

View file

@ -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)
}
}
@ -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() {
@ -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>)
}
}

View file

@ -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
@ -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 override: Boolean?,
val default: 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
@ -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
}

View file

@ -22,6 +22,8 @@ interface VectorFeatures {
fun onboardingVariant(): OnboardingVariant
fun isAlreadyHaveAccountSplashEnabled(): Boolean
enum class OnboardingVariant {
LEGACY,
LOGIN_2,
@ -36,4 +38,5 @@ interface VectorFeatures {
class DefaultVectorFeatures : VectorFeatures {
override fun onboardingVariant(): VectorFeatures.OnboardingVariant = BuildConfig.ONBOARDING_VARIANT
override fun isAlreadyHaveAccountSplashEnabled() = true
}