Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#566: Replaces RecyclerView with GridView on edit phrases screen #577

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading