Fix double click bug

This commit is contained in:
Lucas Lima 2020-06-30 11:39:48 -03:00
parent 6e45de68af
commit ea2c5036f2
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A
2 changed files with 27 additions and 8 deletions

View file

@ -14,10 +14,13 @@ import dev.lucasnlm.antimine.common.R
import dev.lucasnlm.antimine.common.level.models.Area
import dev.lucasnlm.antimine.common.level.models.AreaPaintSettings
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.core.control.ControlStyle
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
class AreaAdapter(
context: Context,
private val viewModel: GameViewModel
private val viewModel: GameViewModel,
private val preferencesRepository: IPreferencesRepository
) : RecyclerView.Adapter<AreaViewHolder>() {
private var field = listOf<Area>()
@ -74,7 +77,18 @@ class AreaAdapter(
}
}
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean = false
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
if (preferencesRepository.controlStyle() == ControlStyle.DoubleClick) {
val position = adapterPosition
if (position == RecyclerView.NO_POSITION) {
Log.d(TAG, "Item no longer exists.")
} else if (clickEnabled) {
viewModel.onSingleClick(position)
return true
}
}
return false
}
})
itemView.setOnLongClickListener { target ->
@ -91,11 +105,13 @@ class AreaAdapter(
}
itemView.setOnClickListener {
val position = adapterPosition
if (position == RecyclerView.NO_POSITION) {
Log.d(TAG, "Item no longer exists.")
} else if (clickEnabled) {
viewModel.onSingleClick(position)
if (preferencesRepository.controlStyle() != ControlStyle.DoubleClick) {
val position = adapterPosition
if (position == RecyclerView.NO_POSITION) {
Log.d(TAG, "Item no longer exists.")
} else if (clickEnabled) {
viewModel.onSingleClick(position)
}
}
}

View file

@ -13,14 +13,17 @@ import androidx.recyclerview.widget.RecyclerView
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.common.level.widget.FixedGridLayoutManager
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import javax.inject.Inject
abstract class CommonLevelFragment : Fragment() {
@Inject
lateinit var dimensionRepository: IDimensionRepository
@Inject
lateinit var preferencesRepository: IPreferencesRepository
protected val viewModel: GameViewModel by activityViewModels()
protected val areaAdapter by lazy { AreaAdapter(requireContext(), viewModel) }
protected val areaAdapter by lazy { AreaAdapter(requireContext(), viewModel, preferencesRepository) }
protected lateinit var recyclerGrid: RecyclerView
abstract val levelFragmentResId: Int