|
|
|
|
@ -5,15 +5,17 @@ import android.view.LayoutInflater |
|
|
|
|
import android.view.View |
|
|
|
|
import android.view.ViewGroup |
|
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator |
|
|
|
|
import androidx.recyclerview.widget.DiffUtil |
|
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper |
|
|
|
|
import androidx.recyclerview.widget.RecyclerView |
|
|
|
|
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.px |
|
|
|
|
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomFieldRow |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow |
|
|
|
|
@ -34,6 +36,71 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
|
|
|
|
|
private val oldRows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() { |
|
|
|
|
|
|
|
|
|
var dragFrom = -1 |
|
|
|
|
var dragTo = -1 |
|
|
|
|
|
|
|
|
|
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { |
|
|
|
|
return if (viewHolder.adapterPosition <= 2) { |
|
|
|
|
return 0 |
|
|
|
|
} else { |
|
|
|
|
makeFlag( |
|
|
|
|
ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.DOWN |
|
|
|
|
or ItemTouchHelper.UP or ItemTouchHelper.START or ItemTouchHelper.END |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { |
|
|
|
|
if (target.adapterPosition <= 2) { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val fromPosition = viewHolder.adapterPosition |
|
|
|
|
val toPosition = target.adapterPosition |
|
|
|
|
|
|
|
|
|
if (dragFrom == -1) { |
|
|
|
|
dragFrom = viewHolder.adapterPosition - 1 // Header |
|
|
|
|
} |
|
|
|
|
dragTo = target.adapterPosition - 1 // Header |
|
|
|
|
|
|
|
|
|
rowRepresentableAdapter.notifyItemMoved(fromPosition, toPosition) |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
adapterRows()?.filter { it is CustomFieldEntry }?.forEachIndexed { index, rowRepresentable -> |
|
|
|
|
val entry = rowRepresentable as CustomFieldEntry |
|
|
|
|
Timber.d("$index ${entry.value} ${entry.order}") |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onMoved(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, fromPos: Int, target: RecyclerView.ViewHolder, toPos: Int, x: Int, y: Int) { |
|
|
|
|
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y) |
|
|
|
|
|
|
|
|
|
Collections.swap(customField.entries, fromPos - (CustomFieldRow.values().size + 1), toPos - (CustomFieldRow.values().size + 1)) |
|
|
|
|
customField.entries.forEachIndexed { index, rowRepresentable -> |
|
|
|
|
val entry = rowRepresentable as CustomFieldEntry |
|
|
|
|
entry.order = index |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { |
|
|
|
|
super.clearView(recyclerView, viewHolder) |
|
|
|
|
dragFrom = -1 |
|
|
|
|
dragTo = -1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
return inflater.inflate(R.layout.fragment_custom_view, container, false) |
|
|
|
|
@ -69,7 +136,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun intForRow(row: RowRepresentable): Int { |
|
|
|
|
return when(row) { |
|
|
|
|
return when (row) { |
|
|
|
|
CustomFieldRow.TYPE -> customField.type |
|
|
|
|
else -> super.intForRow(row) |
|
|
|
|
} |
|
|
|
|
@ -78,23 +145,37 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor>? { |
|
|
|
|
return when (row) { |
|
|
|
|
SimpleRow.NAME -> row.editingDescriptors(mapOf("defaultValue" to this.customField.name)) |
|
|
|
|
is CustomFieldEntry -> row.editingDescriptors(mapOf("defaultValue" to row.value)) |
|
|
|
|
else -> null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
|
when (row) { |
|
|
|
|
is CustomFieldEntry -> { |
|
|
|
|
val data = customField.editDescriptors(row) |
|
|
|
|
Timber.d("data: $data") |
|
|
|
|
BottomSheetFragment.create(fragmentManager, row, this, data, false) |
|
|
|
|
} |
|
|
|
|
else -> super.onRowSelected(position, row, fromAction) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
|
|
|
|
//super.onRowValueChanged(value, row) |
|
|
|
|
Timber.d("onRowValueChanged: $row => $value") |
|
|
|
|
when (row) { |
|
|
|
|
is CustomFieldEntry -> { |
|
|
|
|
row.value = value as String? ?: "" |
|
|
|
|
rowRepresentableAdapter.refreshRow(row) |
|
|
|
|
} |
|
|
|
|
CustomFieldRow.TYPE -> { |
|
|
|
|
customField.updateValue(value, row) |
|
|
|
|
updateUI() |
|
|
|
|
updateAdapterUI() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Init UI |
|
|
|
|
@ -104,8 +185,11 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
bottomBar.translationY = 72f.px |
|
|
|
|
bottomBar.visibility = View.VISIBLE |
|
|
|
|
|
|
|
|
|
itemTouchHelper.attachToRecyclerView(recyclerView) |
|
|
|
|
|
|
|
|
|
addItem.setOnClickListener { |
|
|
|
|
Timber.d("Click on add item") |
|
|
|
|
customField.addEntry() |
|
|
|
|
updateAdapterUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateUI() |
|
|
|
|
@ -123,7 +207,6 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
} else { |
|
|
|
|
bottomBar.animate().translationY(72f.px) |
|
|
|
|
.setInterpolator(FastOutSlowInInterpolator()) |
|
|
|
|
.withEndAction { } |
|
|
|
|
.start() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -132,6 +215,15 @@ 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) |
|
|
|
|
@ -139,6 +231,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa |
|
|
|
|
oldRows.clear() |
|
|
|
|
oldRows.addAll(it) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |