Skip to content

Commit

Permalink
Merge pull request #577 from willowtreeapps/bugfix/edit_phrase_should…
Browse files Browse the repository at this point in the history
…_not_vertical_scroll

#566: Replaces RecyclerView with GridView on edit phrases screen
  • Loading branch information
kaffn8ed27 authored Jul 30, 2024
2 parents 487ac50 + 79517d5 commit 53c6671
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.willowtree.vocable.settings.customcategories

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.willowtree.vocable.BaseFragment
import com.willowtree.vocable.BindingInflater
import com.willowtree.vocable.R
import com.willowtree.vocable.databinding.FragmentCustomCategoryPhraseListBinding
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.Phrase
import com.willowtree.vocable.settings.EditCategoryPhrasesFragmentDirections
import com.willowtree.vocable.settings.customcategories.adapter.CustomCategoryPhraseAdapter
import com.willowtree.vocable.utils.ItemOffsetDecoration
import com.willowtree.vocable.settings.customcategories.adapter.CustomCategoryPhraseGridAdapter
import org.koin.androidx.viewmodel.ext.android.viewModel

class CustomCategoryPhraseListFragment : BaseFragment<FragmentCustomCategoryPhraseListBinding>() {
Expand All @@ -37,7 +36,10 @@ class CustomCategoryPhraseListFragment : BaseFragment<FragmentCustomCategoryPhra
private lateinit var category: Category

private val onPhraseEdit = { phrase: Phrase ->
val action = EditCategoryPhrasesFragmentDirections.actionEditCategoryPhrasesFragmentToEditPhrasesKeyboardFragment(phrase)
val action =
EditCategoryPhrasesFragmentDirections.actionEditCategoryPhrasesFragmentToEditPhrasesKeyboardFragment(
phrase
)
if (findNavController().currentDestination?.id == R.id.editCategoryPhrasesFragment) {
findNavController().navigate(action)
}
Expand All @@ -63,16 +65,13 @@ class CustomCategoryPhraseListFragment : BaseFragment<FragmentCustomCategoryPhra

phrases?.let {
with(binding.customCategoryPhraseHolder) {
layoutManager = GridLayoutManager(requireContext(), numColumns)
addItemDecoration(
ItemOffsetDecoration(
requireContext(),
R.dimen.edit_category_phrase_button_margin,
it.size
)
setNumColumns(numColumns)
adapter = CustomCategoryPhraseGridAdapter(
context = requireContext(),
phrases = it,
onPhraseEdit = onPhraseEdit,
onPhraseDelete = onPhraseDelete,
)
setHasFixedSize(true)
adapter = CustomCategoryPhraseAdapter(it, onPhraseEdit, onPhraseDelete)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.willowtree.vocable.settings.customcategories.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.core.view.isInvisible
import com.willowtree.vocable.R
import com.willowtree.vocable.databinding.EditCustomCategoryPhraseItemBinding
import com.willowtree.vocable.presets.Phrase
import org.koin.core.component.KoinComponent

class CustomCategoryPhraseGridAdapter(
context: Context,
private var phrases: List<Phrase>,
private val onPhraseEdit: (Phrase) -> Unit,
private val onPhraseDelete: (Phrase) -> Unit
) :
ArrayAdapter<Phrase>(
context,
R.layout.edit_custom_category_phrase_item
),
KoinComponent {

private lateinit var binding: EditCustomCategoryPhraseItemBinding

init {
addAll(phrases)
}

// Linter warns about using ViewHolder for smoother scrolling, but we shouldn't be scrolling here anyway
@SuppressLint("ViewHolder")
override fun getView(position: Int, itemView: View?, parent: ViewGroup): View {
binding =
EditCustomCategoryPhraseItemBinding.inflate(LayoutInflater.from(context), parent, false)

val listItemView: View = binding.root

listItemView.isInvisible = true
binding.removeCategoryButton.action = {
onPhraseDelete(phrases[position])
}
binding.phraseTextButton.action = {
onPhraseEdit(phrases[position])
}
binding.root.setPaddingRelative(
0,
context.resources.getDimensionPixelSize(R.dimen.edit_category_phrase_button_margin),
0,
0
)
binding.phraseTextButton.text = phrases[position].text(context)
parent.post {
with(listItemView) {
isInvisible = false
}
}
return listItemView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
<GridView
android:id="@+id/custom_category_phrase_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Expand Down

0 comments on commit 53c6671

Please sign in to comment.