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

Support for selected item in canDragOver #184

Merged
merged 1 commit into from
Nov 11, 2022
Merged

Support for selected item in canDragOver #184

merged 1 commit into from
Nov 11, 2022

Conversation

VeselyJan92
Copy link
Contributor

@VeselyJan92 VeselyJan92 commented Oct 21, 2022

Motivation:

I use LazyGrid for two types of items: categories and items. I am allow to reorder categories and items separetly but those two should never mixed up. My goal was is use canDragOver with return false if item A and B have different type (category, item). I hope it justifies the pull request.

Possible implementation:

  private fun Any?.type() = (this as String).split("_").first()

  val state = rememberReorderableLazyGridState(
      onDragEnd = { 
              from, to -> vm.onDragEnd()
      },
      canDragOver = { 
              draggedOver, dragging -> draggedOver.key.type() == dragging.key.type() 
      },
      onMove = { from, to ->
          val itemsBefore = data.activities.size + 1

          when {
              from.key.type() == "activity" && to.key.type() == "activity" -> vm.onMoveActivity(
                  from.index - 1,
                  to.index - 1
              )
              from.key.type() == "group" && to.key.type() == "group" -> vm.onMoveGroup(
                  from.index - itemsBefore,
                  to.index - itemsBefore
              )
              else -> throw IllegalArgumentException()
          }
      }
  )

Layout for reference:

Screenshot_20221021-195612

@VeselyJan92
Copy link
Contributor Author

@aclassen

@aclassen aclassen merged commit 28ed78f into aclassen:main Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants