From 89730eae9cda3ebd93cec849f231031bd060ed23 Mon Sep 17 00:00:00 2001 From: Aurelien Hubert Date: Thu, 16 May 2019 12:47:00 +0200 Subject: [PATCH] Improve custom field management --- .../ui/adapter/RowRepresentableAdapter.kt | 3 + .../fragment/data/CustomFieldDataFragment.kt | 86 ++++++++++++------- .../ui/fragment/data/EditableDataFragment.kt | 1 + .../android/ui/view/RowViewType.kt | 2 + app/src/main/res/drawable/ic_list.xml | 9 ++ .../main/res/layout/fragment_custom_view.xml | 31 +++++++ 6 files changed, 102 insertions(+), 30 deletions(-) create mode 100644 app/src/main/res/drawable/ic_list.xml diff --git a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableAdapter.kt index f712f987..29237a74 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/adapter/RowRepresentableAdapter.kt @@ -55,10 +55,13 @@ class RowRepresentableAdapter( return } + /* val index = this.dataSource.indexForRow(row) if (index >= 0) { notifyItemChanged(index) } + */ + notifyDataSetChanged() } /** diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/CustomFieldDataFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/CustomFieldDataFragment.kt index 424f8431..e1b1eae6 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/CustomFieldDataFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/CustomFieldDataFragment.kt @@ -7,12 +7,14 @@ import android.view.ViewGroup import androidx.interpolator.view.animation.FastOutSlowInInterpolator import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.chip.ChipGroup import kotlinx.android.synthetic.main.fragment_custom_view.* import net.pokeranalytics.android.R import net.pokeranalytics.android.model.realm.CustomField import net.pokeranalytics.android.model.realm.CustomFieldEntry import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource +import net.pokeranalytics.android.ui.extensions.ChipGroupExtension import net.pokeranalytics.android.ui.extensions.px import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment import net.pokeranalytics.android.ui.view.RowRepresentable @@ -22,6 +24,7 @@ import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow import net.pokeranalytics.android.util.NULL_TEXT import timber.log.Timber import java.util.* +import kotlin.collections.ArrayList /** * Custom EditableDataFragment to manage the Transaction data @@ -35,7 +38,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa } private val oldRows: ArrayList = ArrayList() - + private val currentEntriesOrder: ArrayList = ArrayList() private val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() { @@ -54,6 +57,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa } override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { + if (target.adapterPosition <= 2) { return false } @@ -147,7 +151,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa is CustomFieldEntry -> { val data = customField.editDescriptors(row) Timber.d("data: $data") - BottomSheetFragment.create(fragmentManager, row, this, data, false) + BottomSheetFragment.create(fragmentManager, row, this, data, isClearable = false, isDeletable = true) } else -> super.onRowSelected(position, row, fromAction) } @@ -158,16 +162,26 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa when (row) { is CustomFieldEntry -> { row.value = value as String? ?: "" - rowRepresentableAdapter.refreshRow(row) + customField.updateRowRepresentation() + rowRepresentableAdapter.notifyDataSetChanged() } CustomFieldRow.TYPE -> { customField.updateValue(value, row) updateUI() - updateAdapterUI() + rowRepresentableAdapter.notifyDataSetChanged() } else -> super.onRowValueChanged(value, row) } + } + override fun onRowDeleted(row: RowRepresentable) { + super.onRowDeleted(row) + when (row) { + is CustomFieldEntry -> { + customField.deleteEntry(row) + rowRepresentableAdapter.notifyDataSetChanged() + } + } } /** @@ -178,15 +192,50 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa bottomBar.translationY = 72f.px bottomBar.visibility = View.VISIBLE - itemTouchHelper.attachToRecyclerView(recyclerView) + if (customField.sortCondition == CustomField.Sort.DEFAULT.uniqueIdentifier) { + itemTouchHelper.attachToRecyclerView(recyclerView) + } else { + itemTouchHelper.attachToRecyclerView(null) + } + + when(customField.sortCondition) { + CustomField.Sort.DEFAULT.uniqueIdentifier -> sortDefault.isChecked = true + CustomField.Sort.ASCENDING.uniqueIdentifier -> sortAscending.isChecked = true + CustomField.Sort.DESCENDING.uniqueIdentifier -> sortDescending.isChecked = true + } addItem.setOnClickListener { customField.addEntry() - updateAdapterUI() + rowRepresentableAdapter.notifyDataSetChanged() } + sortChoices.setOnCheckedChangeListener(object : ChipGroupExtension.SingleSelectionOnCheckedListener() { + override fun onCheckedChanged(group: ChipGroup, checkedId: Int) { + super.onCheckedChanged(group, checkedId) + + @SuppressWarnings + if (checkedId < 0) { // when unchecked, checkedId returns -1, causing a crash + return + } + + when(checkedId) { + R.id.sortDefault -> customField.sortCondition = CustomField.Sort.DEFAULT.uniqueIdentifier + R.id.sortAscending -> customField.sortCondition = CustomField.Sort.ASCENDING.uniqueIdentifier + R.id.sortDescending -> customField.sortCondition = CustomField.Sort.DESCENDING.uniqueIdentifier + } + + if (customField.sortCondition == CustomField.Sort.DEFAULT.uniqueIdentifier) { + itemTouchHelper.attachToRecyclerView(recyclerView) + } else { + itemTouchHelper.attachToRecyclerView(null) + } + rowRepresentableAdapter.notifyDataSetChanged() + } + }) + updateUI() - updateAdapterUI() + rowRepresentableAdapter.notifyDataSetChanged() + } /** @@ -204,27 +253,4 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa } } - /** - * Update adapter UI - */ - private fun updateAdapterUI() { - rowRepresentableAdapter.notifyDataSetChanged() - /* - //TODO: Code to animate changes - - // Test: - // not really, i agree with your part about performance but notifyDataSetChanged() does not kill animations, - // to animate using notifyDataSetChanged(), a) call setHasStableIds(true) on your RecyclerView.Adapter object and b) - // override getItemId inside your Adapter to return a unique long value for each row and check it out, the animations do work - - customField.adapterRows()?.let { - val diffResult = DiffUtil.calculateDiff(RowRepresentableDiffCallback(it, oldRows)) - rowRepresentableAdapter.updateRows(diffResult) - - oldRows.clear() - oldRows.addAll(it) - } - */ - } - } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/EditableDataFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/EditableDataFragment.kt index 5d3d6148..d9a131a8 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/EditableDataFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/data/EditableDataFragment.kt @@ -123,6 +123,7 @@ open class EditableDataFragment : RealmFragment(), RowRepresentableDelegate { val dataSource = getDataSource() this.rowRepresentableAdapter = RowRepresentableAdapter(getDataSource(), this) + this.rowRepresentableAdapter.setHasStableIds(true) this.recyclerView.adapter = rowRepresentableAdapter // When creating an object, open automatically the keyboard for the first row diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt index 2f857f0f..efa3a5c2 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt @@ -188,6 +188,7 @@ enum class RowViewType(private var layoutRes: Int) { // Icon itemView.findViewById(R.id.icon)?.let { imageView -> + imageView.setImageDrawable(null) row.imageRes?.let { imageRes -> imageView.setImageResource(imageRes) } @@ -195,6 +196,7 @@ enum class RowViewType(private var layoutRes: Int) { // Action itemView.findViewById(R.id.action)?.let { imageView -> + imageView.setImageDrawable(null) row.imageRes?.let { imageRes -> imageView.visibility = View.VISIBLE imageView.setImageResource(imageRes) diff --git a/app/src/main/res/drawable/ic_list.xml b/app/src/main/res/drawable/ic_list.xml new file mode 100644 index 00000000..4c2fb883 --- /dev/null +++ b/app/src/main/res/drawable/ic_list.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_custom_view.xml b/app/src/main/res/layout/fragment_custom_view.xml index 1ccb0e2f..e06f0ab9 100644 --- a/app/src/main/res/layout/fragment_custom_view.xml +++ b/app/src/main/res/layout/fragment_custom_view.xml @@ -65,6 +65,7 @@ android:layout_gravity="bottom" android:visibility="gone" app:contentInsetStart="8dp" + app:contentInsetEnd="8dp" tools:visibility="visible"> + + + + + + + + + +