Skip to content

Commit

Permalink
Fix #156 & #157: Continue/End Exploration player buttons- low-fi (#251)
Browse files Browse the repository at this point in the history
* Button test implementation

* Added test cases

* Nit changes

* Added 1 more test case

* Optimised test cases

* Nit changes and KDocs

* Changes to StateFragmentPresenter

* Nit changes

* Optimised test-cases
  • Loading branch information
rt4914 authored Oct 22, 2019
1 parent cbeb8de commit 9169616
Show file tree
Hide file tree
Showing 7 changed files with 755 additions and 16 deletions.
92 changes: 92 additions & 0 deletions app/src/main/java/org/oppia/app/player/state/StateAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.oppia.app.player.state

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.databinding.library.baseAdapters.BR
import kotlinx.android.synthetic.main.state_button_item.view.*
import org.oppia.app.R
import org.oppia.app.player.state.itemviewmodel.StateButtonViewModel
import org.oppia.app.player.state.listener.ButtonInteractionListener
import org.oppia.app.databinding.StateButtonItemBinding

@Suppress("unused")
private const val VIEW_TYPE_CONTENT = 1
@Suppress("unused")
private const val VIEW_TYPE_INTERACTION_READ_ONLY = 2
@Suppress("unused")
private const val VIEW_TYPE_NUMERIC_INPUT_INTERACTION = 3
@Suppress("unused")
private const val VIEW_TYPE_TEXT_INPUT_INTERACTION = 4
private const val VIEW_TYPE_STATE_BUTTON = 5

/** Adapter to inflate different items/views inside [RecyclerView]. The itemList consists of various ViewModels. */
class StateAdapter(
private val itemList: MutableList<Any>,
private val buttonInteractionListener: ButtonInteractionListener
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

lateinit var stateButtonViewModel: StateButtonViewModel

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
// TODO(#249): Generalize this binding to make adding future interactions easier.
VIEW_TYPE_STATE_BUTTON -> {
val inflater = LayoutInflater.from(parent.context)
val binding =
DataBindingUtil.inflate<StateButtonItemBinding>(
inflater,
R.layout.state_button_item,
parent,
/* attachToParent= */false
)
StateButtonViewHolder(binding, buttonInteractionListener)
}
else -> throw IllegalArgumentException("Invalid view type") as Throwable
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
VIEW_TYPE_STATE_BUTTON -> {
(holder as StateButtonViewHolder).bind(itemList[position] as StateButtonViewModel)
}
}
}

override fun getItemViewType(position: Int): Int {
return when (itemList[position]) {
is StateButtonViewModel -> {
stateButtonViewModel = itemList[position] as StateButtonViewModel
VIEW_TYPE_STATE_BUTTON
}
else -> throw IllegalArgumentException("Invalid type of data $position")
}
}

override fun getItemCount(): Int {
return itemList.size
}

private class StateButtonViewHolder(
val binding: ViewDataBinding,
private val buttonInteractionListener: ButtonInteractionListener
) : RecyclerView.ViewHolder(binding.root) {
internal fun bind(stateButtonViewModel: StateButtonViewModel) {
binding.setVariable(BR.buttonViewModel, stateButtonViewModel)
binding.root.interaction_button.setOnClickListener {
buttonInteractionListener.onInteractionButtonClicked()
}
binding.root.next_state_image_view.setOnClickListener {
buttonInteractionListener.onNextButtonClicked()
}
binding.root.previous_state_image_view.setOnClickListener {
buttonInteractionListener.onPreviousButtonClicked()
}
binding.executePendingBindings()
}
}
}
Loading

0 comments on commit 9169616

Please sign in to comment.