Skip to content

Commit

Permalink
Fix document ordering with many elements
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger committed Apr 18, 2024
1 parent 52a2972 commit a10ea67
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class CertificateFragment : Fragment(R.layout.fragment_certificate) {
is ViewEvent.ShowChangeDocumentOrderDialog -> {
certificateDialogs.showChangeDocumentOrder(
context = requireContext(),
scope = lifecycleScope,
originalOrder = it.originalOrder,
onOrderChanged = vm::onOrderChangeConfirmed
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import com.michaeltroger.gruenerpass.R
import com.michaeltroger.gruenerpass.db.Certificate
import com.michaeltroger.gruenerpass.certificate.documentorder.DocumentOrderItem
import com.xwray.groupie.GroupieAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

interface CertificateDialogs {
Expand All @@ -30,6 +33,7 @@ interface CertificateDialogs {
)
fun showChangeDocumentOrder(
context: Context,
scope: CoroutineScope,
originalOrder: List<Certificate>,
onOrderChanged: (List<String>) -> Unit
)
Expand Down Expand Up @@ -134,63 +138,61 @@ class CertificateDialogsImpl @Inject constructor() : CertificateDialogs {

override fun showChangeDocumentOrder(
context: Context,
scope: CoroutineScope,
originalOrder: List<Certificate>,
onOrderChanged: (List<String>) -> Unit
) {
val customAlertDialogView = LayoutInflater.from(context)
.inflate(R.layout.layout_document_order_dialog, null, false)

val myAdapter = GroupieAdapter()
val list = originalOrder.toMutableList()
val listFlow: MutableStateFlow<List<Certificate>> = MutableStateFlow(originalOrder)

fun onUpClicked(id: String) {
val index = list.indexOfFirst {
val index = listFlow.value.indexOfFirst {
it.id == id
}
val original = list.getOrNull(index)
val new = list.getOrNull(index - 1)
if (new != null) {
list[index - 1] = original!!
list[index] = new
myAdapter.notifyItemMoved(index, index - 1)
}
val newState = listFlow.value.toMutableList()
newState[index] = listFlow.value.getOrNull(index - 1) ?: return
newState[index - 1] = listFlow.value[index]
listFlow.value = newState
}

fun onDownClicked(id: String) {
val index = list.indexOfFirst {
val index = listFlow.value.indexOfFirst {
it.id == id
}
val original = list.getOrNull(index)
val new = list.getOrNull(index + 1)
if (new != null) {
list[index + 1] = original!!
list[index] = new
myAdapter.notifyItemMoved(index, index + 1)
}
val newState = listFlow.value.toMutableList()
newState[index] = listFlow.value.getOrNull(index + 1) ?: return
newState[index + 1] = listFlow.value[index]
listFlow.value = newState
}

myAdapter.update(
originalOrder.map { certificate ->
DocumentOrderItem(
fileName = certificate.id,
documentName = certificate.name,
onDownClicked = {
onDownClicked(certificate.id)
},
onUpClicked = {
onUpClicked(certificate.id)
}
)
}
)
scope.launch {
listFlow.collect { list ->
val items = list.map { certificate ->
DocumentOrderItem(
fileName = certificate.id,
documentName = certificate.name,
onDownClicked = {
onDownClicked(it)
},
onUpClicked = {
onUpClicked(it)
}
)
}
myAdapter.update(items)
}
}

customAlertDialogView.findViewById<RecyclerView>(R.id.document_order).adapter = myAdapter

val dialog = MaterialAlertDialogBuilder(context)
.setTitle(R.string.dialog_document_order_title)
.setView(customAlertDialogView)
.setPositiveButton(R.string.ok) { _, _ ->
onOrderChanged(list.map { it.id })
onOrderChanged(listFlow.value.map { it.id })
}
.setNegativeButton(R.string.cancel, null)
.setOnDismissListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.xwray.groupie.viewbinding.GroupieViewHolder
class DocumentOrderItem(
private val fileName: String,
private val documentName: String,
private val onUpClicked: () -> Unit,
private val onDownClicked: () -> Unit,
private val onUpClicked: (String) -> Unit,
private val onDownClicked: (String) -> Unit,
) : BindableItem<ItemDocumentOrderBinding>() {

override fun initializeViewBinding(view: View): ItemDocumentOrderBinding = ItemDocumentOrderBinding.bind(view)
Expand All @@ -29,10 +29,10 @@ class DocumentOrderItem(
viewHolder.binding.apply {
documentNameTextField.text = documentName
up.setOnClickListener {
onUpClicked()
onUpClicked(fileName)
}
down.setOnClickListener {
onDownClicked()
onDownClicked(fileName)
}
}

Expand Down

0 comments on commit a10ea67

Please sign in to comment.