Skip to content

Commit

Permalink
Merge pull request #4 from aclassen/desktop
Browse files Browse the repository at this point in the history
Desktop support
  • Loading branch information
aclassen authored Aug 14, 2021
2 parents b7962d0 + a3b2e87 commit 3ce2aff
Show file tree
Hide file tree
Showing 65 changed files with 1,018 additions and 767 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.idea/artifacts/
.idea/deploymentTargetDropDown.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
local.properties
.idea/jarRepositories.xml
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 48 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
# Compose LazyList reorder

[![](https://jitpack.io/v/aclassen/ComposeReorderable.svg)](https://jitpack.io/#aclassen/ComposeReorderable)

A Jetpack Compose modifier enabling reordering in a LazyList.
A Jetpack Compose (Desktop) modifier enabling reordering in a LazyList.

![Sample](readme/sample.gif)

## Download

```
repositories {
maven { setUrl("https://jitpack.io") }
// maven { url 'https://jitpack.io' }
}
dependencies {
implementation("com.github.aclassen:ComposeReorderable:<latest_version>")
implementation("org.burnoutcrew.composereorderable:reorderable:<latest_version>")
}
```

## How to use

Add a `Reorderable` to your composition:
Create `reorderState` and add the `reorderable` Modifier to the LazyList:

```
val state: ReorderableState = rememberReorderState()
val state = rememberReorderState()
Reorderable(state, { from, to -> data.move(from, to) })
LazyColumn(state = state.listState) {
LazyColumn(
state = state.listState,
modifier = Modifier.reorderable(state, { from, to -> data.move(from, to) })) {
...
}
```

To apply the dragged item offset:
To make an item reorderable/draggable add at least one drag modifier to the item:

```
Modifier.detectReorder(state)
or
Modifier.detectReorderAfterLongPress(state)
```

> Adding one of the detect modifiers to the LazyList instead of an item , will make all items reordable.
At least apply the dragged item offset:

```
items(items, { it.key }) {item ->
Expand All @@ -43,33 +47,44 @@ items(items, { it.key }) {item ->
...
}
}
```
Make an item reorderable by adding at least one drag modifier to the item:
or without keyed items:
```
Modifier.detectReorder(state, { item.key })
or
Modifier.detectReorderAfterLongPress(state, { item.key })
itemsIndexed(items) { idx, item ->
Column(
modifier = Modifier.draggedItem(state.offsetOf(idx))
) {
...
}
}
```

If you want to use a non keyed item list `detectReorder` and `detectReorderAfterLongPress` will not work , use the `detectListReorder`
modifier instead.

Add this modifier to your LazyList , this will make the items reorderable after long press.
> You can use `draggedItem` for a default dragged effect or create your own.
Complete example:
```
Reorderable(state, { from, to -> data.move(from, to) })
LazyRow(
state = state.listState,
modifier = Modifier
.detectListReorder(state),
)
@Composable
fun ReorderableList(){
val state = rememberReorderState()
val data = List(100) { "item $it" }.toMutableStateList()
LazyColumn(
state = state.listState,
modifier = Modifier.reorderable(state, { a, b -> data.move(a, b) })
) {
items(data, { it }) { item ->
Box(
modifier = Modifier
.fillMaxWidth()
.draggedItem(state.offsetOf(item))
.detectReorderAfterLongPress(state)
) {
Text(text = item)
}
}
}
}
```

Use the item index, instead of the key , to find the offset `state.offsetOf(idx)`

You can use `draggedItem` for a default dragged effect or create your own.

## Notes

Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.jetbrains.compose.compose

plugins {
id("org.jetbrains.compose")
id("com.android.application")
kotlin("android")
}

dependencies {
implementation(project(":reorderable"))
implementation(compose.runtime)
implementation(compose.material)
implementation("androidx.activity:activity-compose:1.3.1")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07")
}

android {
compileSdk = rootProject.extra.get("compileSdk") as Int
defaultConfig {
minSdk = rootProject.extra.get("minVersion") as Int
targetSdk = rootProject.extra.get("targetSdk") as Int
versionCode = 1
versionName = "1.0"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}
}
16 changes: 16 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.burnoutcrew.android">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LazyReorderList">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.burnoutcrew.lazyreorderlist.ui
package org.burnoutcrew.android.ui

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.burnoutcrew.lazyreorderlist.ui
package org.burnoutcrew.android.ui

import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import io.burnoutcrew.lazyreorderlist.R
import io.burnoutcrew.lazyreorderlist.ui.reorderlist.ReorderList
import io.burnoutcrew.lazyreorderlist.ui.theme.ReorderListTheme
import org.burnoutcrew.android.R
import org.burnoutcrew.android.ui.reorderlist.ReorderList
import org.burnoutcrew.android.ui.theme.ReorderListTheme

@Composable
fun Root() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.burnoutcrew.lazyreorderlist.ui.reorderlist
package org.burnoutcrew.android.ui.reorderlist

import androidx.compose.runtime.Stable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.burnoutcrew.lazyreorderlist.ui.reorderlist
package org.burnoutcrew.android.ui.reorderlist


import androidx.compose.foundation.Image
Expand All @@ -38,7 +38,8 @@ import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import io.burnoutcrew.reorderable.*
import org.burnoutcrew.reorderable.*


@Composable
fun ReorderList(vm: ReorderListViewModel = viewModel()) {
Expand All @@ -63,13 +64,11 @@ fun HorizontalReorderList(
state: ReorderableState = rememberReorderState(),
onMove: (fromPos: Int, toPos: Int) -> (Unit),
) {
Reorderable(state, onMove)
LazyRow(
state = state.listState,
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier
.detectListReorder(state, Orientation.Horizontal)
.then(modifier),
modifier = modifier
.then(Modifier.reorderable(state, onMove = onMove, orientation = Orientation.Horizontal)),
) {
itemsIndexed(items) { idx, item ->
Box(
Expand All @@ -83,6 +82,7 @@ fun HorizontalReorderList(
.scale(if (state.draggedIndex == null || state.draggedIndex == idx) 1f else .9f)
.clip(RoundedCornerShape(8.dp))
.background(MaterialTheme.colors.primary)
.detectReorderAfterLongPress(state)
) {
Text(item.title)
}
Expand All @@ -98,10 +98,10 @@ fun VerticalReorderList(
onMove: (fromPos: Int, toPos: Int) -> (Unit),
canDragOver: ((index: Int) -> Boolean),
) {
Reorderable(state, onMove, canDragOver)
LazyColumn(
state = state.listState,
modifier = modifier
.then(Modifier.reorderable(state, onMove = onMove, canDragOver = canDragOver))
) {
items(items, { it.key }) { item ->
if (item.isLocked) {
Expand All @@ -121,21 +121,12 @@ fun VerticalReorderList(
.fillMaxWidth()
.draggedItem(state.offsetOf(item.key))
.background(MaterialTheme.colors.surface)
.detectReorderAfterLongPress(state)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
Text(
text = item.title,
modifier = Modifier.padding(16.dp)
) {
Image(
imageVector = Icons.Filled.Menu,
contentDescription = "",
modifier = Modifier.detectReorder(state, { item.key })
)
Text(
text = item.title,
modifier = Modifier.padding(start = 8.dp)
)
}
)
Divider()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.burnoutcrew.lazyreorderlist.ui.reorderlist
package org.burnoutcrew.android.ui.reorderlist

import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.ViewModel
import io.burnoutcrew.reorderable.move
import org.burnoutcrew.reorderable.move


class ReorderListViewModel : ViewModel() {
val cats = List(500) { ItemData("Cat $it", "") }.toMutableStateList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.burnoutcrew.lazyreorderlist.ui.theme
package org.burnoutcrew.android.ui.theme

import androidx.compose.ui.graphics.Color

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.burnoutcrew.lazyreorderlist.ui.theme
package org.burnoutcrew.android.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
Expand Down
Loading

0 comments on commit 3ce2aff

Please sign in to comment.