Skip to content

Commit a9c4357

Browse files
Merge pull request #1 from SimformSolutionsPvtLtd/feature/added_swiptodelete
✨ Added Swipe to Delete Feature
2 parents e96e491 + f86e144 commit a9c4357

File tree

43 files changed

+513
-97
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+513
-97
lines changed

SSJetpackComposeSwipeToDelete/app/src/androidTest/java/com/example/ssjetpackcomposeswipetodelete/ExampleInstrumentedTest.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

SSJetpackComposeSwipeToDelete/app/src/main/java/com/example/ssjetpackcomposeswipetodelete/MainActivity.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

SSJetpackComposeSwipeToDelete/app/src/test/java/com/example/ssjetpackcomposeswipetodelete/ExampleUnitTest.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
1-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
buildscript {
3-
ext {
4-
compose_version = '1.0.1'
5-
}
6-
repositories {
7-
google()
8-
mavenCentral()
1+
plugins {
2+
id 'com.android.library'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdk 31
8+
9+
defaultConfig {
10+
minSdk 21
11+
targetSdk 31
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
consumerProguardFiles "consumer-rules.pro"
917
}
10-
dependencies {
11-
classpath "com.android.tools.build:gradle:7.0.3"
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
1318

14-
// NOTE: Do not place your application dependencies here; they belong
15-
// in the individual module build.gradle files
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
buildFeatures {
26+
compose true
27+
}
28+
composeOptions {
29+
kotlinCompilerExtensionVersion '1.1.0-rc01'
30+
kotlinCompilerVersion '1.5.31'
31+
}
32+
compileOptions {
33+
sourceCompatibility JavaVersion.VERSION_1_8
34+
targetCompatibility JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = '1.8'
1638
}
1739
}
1840

19-
task clean(type: Delete) {
20-
delete rootProject.buildDir
41+
dependencies {
42+
43+
implementation 'androidx.core:core-ktx:1.7.0'
44+
implementation 'androidx.appcompat:appcompat:1.4.0'
45+
implementation 'com.google.android.material:material:1.4.0'
46+
implementation "androidx.compose.ui:ui:$compose_version"
47+
implementation "androidx.compose.material:material:$compose_version"
48+
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
49+
testImplementation 'junit:junit:4.+'
50+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
51+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
2152
}

SSJetpackComposeSwipeToDelete/consumer-rules.pro

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.ssjetpackcomposeswipetodelete">
4+
5+
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.ssjetpackcomposeswipetodelete.swipetodelete
2+
3+
/**
4+
* Swipe Directions :-
5+
* Left :- If you want to swipe left
6+
* Right :- If you want to swipe right
7+
* Both :- If you want to swipe left + right both
8+
**/
9+
enum class SwipeDirection {
10+
LEFT, RIGHT, BOTH
11+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.example.ssjetpackcomposeswipetodelete.swipetodelete
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.gestures.Orientation
5+
import androidx.compose.foundation.layout.Arrangement
6+
import androidx.compose.foundation.layout.Box
7+
import androidx.compose.foundation.layout.Column
8+
import androidx.compose.foundation.layout.Row
9+
import androidx.compose.foundation.layout.Spacer
10+
import androidx.compose.foundation.layout.fillMaxWidth
11+
import androidx.compose.foundation.layout.height
12+
import androidx.compose.foundation.layout.offset
13+
import androidx.compose.foundation.layout.width
14+
import androidx.compose.material.ExperimentalMaterialApi
15+
import androidx.compose.material.FractionalThreshold
16+
import androidx.compose.material.Icon
17+
import androidx.compose.material.IconButton
18+
import androidx.compose.material.rememberSwipeableState
19+
import androidx.compose.material.swipeable
20+
import androidx.compose.runtime.Composable
21+
import androidx.compose.ui.Alignment
22+
import androidx.compose.ui.Modifier
23+
import androidx.compose.ui.graphics.Color
24+
import androidx.compose.ui.graphics.vector.ImageVector
25+
import androidx.compose.ui.platform.LocalDensity
26+
import androidx.compose.ui.unit.Dp
27+
import androidx.compose.ui.unit.IntOffset
28+
import androidx.compose.ui.unit.dp
29+
import kotlin.math.roundToInt
30+
31+
@ExperimentalMaterialApi
32+
@Composable
33+
34+
/**
35+
* @param leftViewIcons Icons which needs to be shown on the left side of the screen requires a Pair of Icon and Id of the view Pair(Icon, Id).
36+
* @param rightViewIcons Icons which needs to be shown on the right side of the screen requires a Pair of Icon and Id of the view Pair(Icon, Id).
37+
* @param position Position of the cell.
38+
* @param onClick OnClick event of the swipeable view returns a Pair of Position(Int) and Id(String) of the view Pair(Position, Id).
39+
* @param height Height of the swipeable view.
40+
* @param backgroundColor Background color of swipeable view.
41+
* @param fractionalThreshold The fraction (between 0 and 1) that the threshold will be at.
42+
* @param content Pass the content of your cell.
43+
**/
44+
fun SwipeAbleItemCell(
45+
leftViewIcons: ArrayList<Pair<ImageVector, String>>,
46+
rightViewIcons: ArrayList<Pair<ImageVector, String>>,
47+
position: Int,
48+
onClick: (Pair<Int, String>) -> (Unit),
49+
swipeDirection: SwipeDirection,
50+
leftViewWidth: Dp,
51+
rightViewWidth: Dp,
52+
height: Dp,
53+
backgroundColor: Color,
54+
fractionalThreshold: Float,
55+
content: @Composable () -> Unit
56+
) {
57+
58+
val swipeAbleState = rememberSwipeableState(initialValue = 0)
59+
val anchors = when (swipeDirection) {
60+
SwipeDirection.LEFT -> mapOf(
61+
0f to 0,
62+
-(with(LocalDensity.current) { rightViewWidth.toPx() }) to 1
63+
)
64+
SwipeDirection.RIGHT -> mapOf(
65+
0f to 0,
66+
(with(LocalDensity.current) { leftViewWidth.toPx() }) to 1
67+
)
68+
else -> mapOf(
69+
0f to 0,
70+
((with(LocalDensity.current) { leftViewWidth.toPx() })) to 1,
71+
-(with(LocalDensity.current) { rightViewWidth.toPx() }) to 2
72+
)
73+
}
74+
75+
Column(
76+
modifier = Modifier
77+
.fillMaxWidth()
78+
.height(height)
79+
) {
80+
Column(
81+
verticalArrangement = Arrangement.Center,
82+
horizontalAlignment = Alignment.CenterHorizontally
83+
) {
84+
Box(
85+
modifier = Modifier
86+
.fillMaxWidth()
87+
.background(backgroundColor)
88+
.swipeable(
89+
state = swipeAbleState,
90+
anchors = anchors,
91+
thresholds = { _, _ ->
92+
FractionalThreshold(fractionalThreshold)
93+
},
94+
orientation = Orientation.Horizontal
95+
),
96+
) {
97+
Row(
98+
modifier = Modifier
99+
.fillMaxWidth()
100+
.height(height),
101+
horizontalArrangement = when (swipeDirection) {
102+
SwipeDirection.BOTH -> Arrangement.SpaceBetween
103+
SwipeDirection.LEFT -> Arrangement.End
104+
else -> Arrangement.Start
105+
},
106+
verticalAlignment = Alignment.CenterVertically
107+
) {
108+
// When user Swipes to right the left side of the view which is visible
109+
Row {
110+
if (swipeDirection == SwipeDirection.RIGHT || swipeDirection == SwipeDirection.BOTH) {
111+
leftViewIcons.forEachIndexed { index, pair ->
112+
IconButton(
113+
onClick = {
114+
// Pair(Position, Id)
115+
onClick(Pair(position, pair.second))
116+
}
117+
) {
118+
// Pair(Icon, Id)
119+
Icon(imageVector = pair.first, contentDescription = pair.second)
120+
}
121+
if (index != (leftViewIcons.size - 1)) {
122+
Spacer(modifier = Modifier.width(10.dp))
123+
}
124+
}
125+
}
126+
}
127+
// When user Swipes to left the right side of the view which is visible
128+
Row {
129+
if (swipeDirection == SwipeDirection.LEFT || swipeDirection == SwipeDirection.BOTH) {
130+
rightViewIcons.forEachIndexed { index, pair ->
131+
IconButton(
132+
onClick = {
133+
// Pair(Position, Id)
134+
onClick(Pair(position, pair.second))
135+
}
136+
) {
137+
// Pair(Icon, Id)
138+
Icon(imageVector = pair.first, contentDescription = pair.second)
139+
}
140+
if (index != (rightViewIcons.size - 1)) {
141+
Spacer(modifier = Modifier.width(10.dp))
142+
}
143+
}
144+
}
145+
}
146+
}
147+
// Main content view of the cell
148+
Box(
149+
modifier = Modifier
150+
.offset {
151+
IntOffset(
152+
swipeAbleState.offset.value.roundToInt(), 0
153+
)
154+
}
155+
.fillMaxWidth()
156+
) {
157+
content()
158+
}
159+
}
160+
}
161+
}
162+
}

SSJetpackComposeSwipeToDelete/app/build.gradle renamed to app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ android {
3737
compose true
3838
}
3939
composeOptions {
40-
kotlinCompilerExtensionVersion compose_version
41-
kotlinCompilerVersion '1.5.21'
40+
kotlinCompilerExtensionVersion '1.1.0-rc01'
41+
kotlinCompilerVersion '1.5.31'
4242
}
4343
packagingOptions {
4444
resources {
@@ -57,6 +57,7 @@ dependencies {
5757
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
5858
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
5959
implementation 'androidx.activity:activity-compose:1.4.0'
60+
implementation project(path: ':ssjetpackcomposeswipetodelete')
6061
testImplementation 'junit:junit:4.+'
6162
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
6263
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

0 commit comments

Comments
 (0)