MVVM and selection of elements in the adapter - LiveData

In my previous article, I talked about the first attempt to write a library for a simple and convenient selection of items from a list in Android, taking into account the MVVM approach. Last time, the solution was not tied to the platform, so I did not reach the final goal.


A few months later, when I thought enough, procrastinated and worked, I already got a solution that is more suitable for Android, as it is based on LiveData. I ask everyone interested to read.


Changes made


Before talking about the new library, to begin with, as soon as possible, I will briefly describe the changes in what I spoke about in the previous article.


Interceptor Inheritance


The first thing that seemed strange to me in my own solution was a common interface, which obliges us to implement the logic of interceptors - a method addSelectionInterceptor- so I essentially did not let my standard implementation of this method be inherited for new classes. Therefore, I put the specified method into a separate interface - InterceptableSelectionManagerwhich inherits the common interface. As a result, users can choose an option in which they are not forced to work with interceptors, since this functionality, as it seems to me, is still not very often in demand.


Well, speaking of the inheritance of the logic I wrote, a basic abstract class BaseInterceptableSelectionManageris made in which only interceptors are described. Like - use on health. In any case, you can always use the interface and implement everything from scratch in your own way.


More precise naming


Yes, I changed the name of the interface method in the project, which I declared ready and shared. Yes, I understand that burning in hell for such antics. In my defense, I can only say that the name actually did not accurately describe the action taking place in it. It's about the method, which in the previous version was called selectPosition, and now it clickPosition.


, , , . , . , ( , ).


, deselectPosition, , . .



. , , .



, , , - . , β€” SelectableDataSource.


class SelectableDataSource<T>(private var dataSource: ArrayList<T>,
                              private val selectionManager: SelectionManager)
    : SelectionManager by selectionManager

:


  1. SelectionManager, . , . .


  2. SelectionManager, , .


  3. ArrayList, . ( ), , β€” ArrayList.


    constructor(selectionManager: SelectionManager) : this(arrayListOf(), selectionManager)

  4. setDataSource.


    fun setDataSource(dataSource: ArrayList<T>, changeMode: ChangeDataSourceMode)

    , . :


    • ChangeDataSourceMode.ClearAllSelection β€” , . , , ;
    • ChangeDataSourceMode.HoldSelectedPositions β€” . , 2 , 2 . , , 3 ;
    • ChangeDataSourceMode.HoldSelectedItems β€” ( ). , . , , Equals.

  5. , ArrayIndexOutOfBoundsException. clickPosition , setDataSource , . , , SelectionManager', , β€” .


    val selectionManager: SelectionManager
    val dataSource = SelectableDataSource<User>(selectionManager)
    selectionManager.registerSelectionChangeListener { position: Int, isSelected: Boolean -> ...} //
    dataSource.registerSelectionChangeListener { position: Int, isSelected: Boolean -> ...} //

  6. , SelectionManager', . , InterceptableSelectableDataSource. , , , .


    class InterceptableSelectableDataSource<T>(dataSource: ArrayList<T>,
                         private val selectionManager: InterceptableSelectionManager)
    : SelectableDataSource<T>(dataSource, selectionManager),
        InterceptableSelectionManager


LiveDataSource


β€” . - , LiveData. InterceptableSelectionManager, .


val users = LiveDataSource<User>(MultipleSelection())

setDataSource, SelectableDataSource.


val newValues: ArrayList<User>
users.setDataSource(newValues)
//
users.setDataSource(newValues, ChangeDataSourceMode.HoldSelectedItems)

. allItems, LiveData.


viewModel.users.allItems.observe(this, Observer { items: ArrayList<User> -> ... })
//this -    Activity    LifecycleOwner

selectedItems.


viewModel.users.selectedItems.observe(this, Observer { selectedItems: ArrayList<User> -> ... })

, , , observeSelectionChange observeItemSelectionChange. , , β€” .


viewModel.users.observeSelectionChange(this) { position: Int, isSelected: Boolean -> ... }
viewModel.users.observeItemSelectionChange(this) { user: User, isSelected: Boolean -> ... }


, , .


  1. , RecyclerView.Adapter, LiveData . , - , .
  2. . , .
  3. LiveDataSource SelectionManager', . , , . , - .


:



Links in Gradle:
implementation 'ru.ircover.selectionmanager:core:1.1.0'
implementation 'ru.ircover.selectionmanager:livesource:1.0.0'


All Articles