From faca25a6638be37e1e8339a919089c0280a7f5c3 Mon Sep 17 00:00:00 2001 From: marysoloman Date: Wed, 9 Oct 2019 19:21:34 +0530 Subject: [PATCH 01/62] NumberInputInteractionView dynamically created on state interaction id NumericInput from dummy data welcome.json ,welcome.json file "init_state_name"modified to "Numeric input", --- .idea/codeStyles/Project.xml | 16 ++++---- .idea/codeStyles/codeStyleConfig.xml | 3 +- .../NumberInputInteractionView.kt | 40 +++++++++++++++++++ .../player/state/StateFragmentPresenter.kt | 37 ++++++++++++++++- app/src/main/res/layout/state_fragment.xml | 35 ++++++++++++---- domain/src/main/assets/welcome.json | 11 ++++- 6 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/org/oppia/app/customview/inputInteractionView/NumberInputInteractionView.kt diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index e828e2898e6..157deee79d3 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,5 +1,8 @@ + + \ No newline at end of file diff --git a/app/src/main/java/org/oppia/app/customview/inputInteractionView/NumberInputInteractionView.kt b/app/src/main/java/org/oppia/app/customview/inputInteractionView/NumberInputInteractionView.kt new file mode 100644 index 00000000000..95e0f05b2b3 --- /dev/null +++ b/app/src/main/java/org/oppia/app/customview/inputInteractionView/NumberInputInteractionView.kt @@ -0,0 +1,40 @@ +package org.oppia.app.customview.inputInteractionView + +import android.content.Context +import android.text.InputFilter +import android.text.method.DigitsKeyListener +import android.widget.EditText +import org.oppia.app.R + +/** The customclass for [NumberInputInteractionView]. */ +class NumberInputInteractionView : EditText { + + private var mContext: Context + private var placeholder: String + private var rows: Int + + constructor(context: Context, placeholder: String, rows: Int) : super(context) { + this.mContext = context + this.placeholder = placeholder + this.rows = rows + attributes() + } + + /** This function contains default attributes of [NumberInputInteractionView]. */ + fun attributes() { + setBackgroundResource(R.drawable.edit_text_background) + val paddingPixel = mContext.resources.getDimension(R.dimen.padding_8) + val density = resources.displayMetrics.density + val paddingDp = (paddingPixel * density).toInt() + setPadding(paddingDp, paddingDp, paddingDp, paddingDp) + setHint(placeholder) + setKeyListener(DigitsKeyListener.getInstance("0123456789.")) + setEditTextMaxLength(200) + } + + fun setEditTextMaxLength(length: Int) { + val filterArray = arrayOfNulls(1) + filterArray[0] = InputFilter.LengthFilter(length) + setFilters(filterArray) + } +} diff --git a/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt b/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt index 45a30923e1a..f8bf0bb38b3 100755 --- a/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt +++ b/app/src/main/java/org/oppia/app/player/state/StateFragmentPresenter.kt @@ -1,12 +1,16 @@ package org.oppia.app.player.state +import android.content.res.Resources import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.LinearLayout import androidx.fragment.app.Fragment import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.lifecycle.Transformations +import org.oppia.app.R +import org.oppia.app.customview.inputInteractionView.NumberInputInteractionView import org.oppia.app.databinding.StateFragmentBinding import org.oppia.app.fragment.FragmentScope import org.oppia.app.model.CellularDataPreference @@ -17,6 +21,7 @@ import org.oppia.domain.audio.CellularDialogController import org.oppia.domain.exploration.ExplorationProgressController import org.oppia.util.data.AsyncResult import org.oppia.util.logging.Logger +import java.lang.Exception import javax.inject.Inject private const val TAG_CELLULAR_DATA_DIALOG = "CELLULAR_DATA_DIALOG" @@ -33,7 +38,7 @@ class StateFragmentPresenter @Inject constructor( private var showCellularDataDialog = true private var useCellularData = false - + private var llRoot: LinearLayout? = null fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? { cellularDialogController.getCellularDataPreference() .observe(fragment, Observer> { @@ -51,7 +56,7 @@ class StateFragmentPresenter @Inject constructor( } subscribeToCurrentState() - + llRoot = binding.root.findViewById(R.id.llRoot) return binding.root } @@ -94,10 +99,34 @@ class StateFragmentPresenter @Inject constructor( private fun subscribeToCurrentState() { ephemeralStateLiveData.observe(fragment, Observer { result -> + try { + if (result.state.interaction.id.equals("NumericInput")) addNumberInputContentCard( + result.state.interaction.customizationArgsMap.get( + "placeholder" + )!!.normalizedString + ) + } catch (e: Exception) { + } logger.d("StateFragment", "getCurrentState: ${result.state.name}") }) } + /** The function for adding [NumberInputInteractionView]. */ + fun addNumberInputContentCard(placeholder: String) { + var contentComponent = NumberInputInteractionView( + fragment.context!!, + placeholder, + 1 + ) + val params = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ) + params.setMargins(dpToPx(8), dpToPx(8), dpToPx(8), dpToPx(8)) + llRoot!!.addView(contentComponent, params) + + } + private val ephemeralStateLiveData: LiveData by lazy { getEphemeralState() } @@ -112,4 +141,8 @@ class StateFragmentPresenter @Inject constructor( } return ephemeralStateResult.getOrDefault(EphemeralState.getDefaultInstance()) } + + fun dpToPx(dp: Int): Int { + return (dp * Resources.getSystem().getDisplayMetrics().density).toInt() + } } diff --git a/app/src/main/res/layout/state_fragment.xml b/app/src/main/res/layout/state_fragment.xml index d1cff627ed7..ad133121e4f 100755 --- a/app/src/main/res/layout/state_fragment.xml +++ b/app/src/main/res/layout/state_fragment.xml @@ -1,34 +1,53 @@ - + - + + + + type="org.oppia.app.player.state.StateFragment" /> + + type="org.oppia.app.player.state.StateViewModel" /> + + + + android:layout_height="wrap_content" /> + + + +