Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sub4ikGG committed Sep 30, 2023
0 parents commit e869953
Show file tree
Hide file tree
Showing 29 changed files with 898 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
/.idea/
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
68 changes: 68 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("maven-publish")
}

android {
namespace = "ru.chatan.swipebutton"
compileSdk = 33

defaultConfig {
minSdk = 26

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

buildFeatures {
viewBinding = true
}
}

afterEvaluate {
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "ru.chatan.swipebutton"
artifactId = "swipe-button"
version = "1.0.0"

from(components["release"])
}
}
}
}

dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.guava:guava:27.0.1-android")

// Shimmer
implementation("com.facebook.shimmer:shimmer:0.5.0")

// JitPack
implementation("com.github.jitpack:gradle-simple:1.0")
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.chatan.swipebutton

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ru.chatan.swipebutton", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>

</manifest>
230 changes: 230 additions & 0 deletions app/src/main/java/ru/chatan/swipebutton/SwipeButton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package ru.chatan.swipebutton

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.content.res.TypedArray
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import ru.chatan.swipebutton.databinding.SwipeButtonLayoutBinding

@SuppressLint("ClickableViewAccessibility")
class SwipeButton(context: Context, attrs: AttributeSet): RelativeLayout(context, attrs) {

private var _binding: SwipeButtonLayoutBinding? = null
private val binding get() = _binding!!

private var animation: ObjectAnimator? = null
private var onSwipeButtonListener: OnSwipeButtonListener? = null

private var defaultText: String = ""

init {
_binding = SwipeButtonLayoutBinding.inflate(LayoutInflater.from(context))
addView(binding.root)

setThumbTouchListener()
binding.shimmerBackground.showShimmer(true)

applyAttrs(context, attrs)
}

private fun applyAttrs(context: Context, attrs: AttributeSet) {
val typedArray = context.obtainStyledAttributes(
attrs,
R.styleable.SwipeButton, 0, 0
)

applyTextAttrs(typedArray, context)
applyDrawableColorAttrs(typedArray, context)

typedArray.recycle()
}

private fun applyDrawableColorAttrs(
typedArray: TypedArray,
context: Context
) {
val drawable =
typedArray.getResourceId(R.styleable.SwipeButton_swipeDrawable, R.drawable.swipe_arrow)
val thumbColor = typedArray.getColor(
R.styleable.SwipeButton_swipeThumbColor,
ContextCompat.getColor(context, R.color.light_blue)
)
val progressColor = typedArray.getColor(
R.styleable.SwipeButton_swipeProgressColor,
ContextCompat.getColor(context, R.color.light_blue)
)

binding.thumb.setImageResource(drawable)
binding.thumb.backgroundTintList = ColorStateList.valueOf(thumbColor)
binding.backgroundProgress.setBackgroundColor(progressColor)
}

private fun applyTextAttrs(
typedArray: TypedArray,
context: Context
) {
defaultText = typedArray.getString(R.styleable.SwipeButton_swipeText).orEmpty()
val font =
typedArray.getResourceId(R.styleable.SwipeButton_swipeFontFamily, R.font.inter_medium)
val color = typedArray.getColor(
R.styleable.SwipeButton_swipeTextColor,
ContextCompat.getColor(context, R.color.light_blue)
)

val textSize = typedArray.getDimensionPixelSize(R.styleable.SwipeButton_swipeTextSize, 16)
binding.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
binding.textView.typeface = ResourcesCompat.getFont(context, font)
binding.textView.setTextColor(color)
binding.textView.text = defaultText
}

private fun setThumbTouchListener() {
var offsetX = 0F
var endX = -1
var layoutParams: ViewGroup.LayoutParams?

binding.thumb.setOnTouchListener { view, event ->
val textStartX = binding.textView.left
val textEndX = binding.textView.right
if (endX == -1) endX = binding.shimmerBackground.right

when (event.action) {
MotionEvent.ACTION_DOWN -> {
offsetX = event.rawX - view.x
animation?.cancel()
}

MotionEvent.ACTION_UP -> {
if (view.x + view.right < endX) {
animation = ObjectAnimator.ofFloat(view, "translationX", 0f)
animation?.duration = 200
animation?.interpolator = LinearInterpolator()

animation?.addUpdateListener {
layoutParams = binding.backgroundProgress.layoutParams
layoutParams?.width = (it.animatedValue as Float + (view.right / 2) + 15).toInt()
binding.backgroundProgress.layoutParams = layoutParams
}

animation?.addListener(object : AnimatorListener {
override fun onAnimationStart(animation: Animator) {
}

override fun onAnimationEnd(animation: Animator) {
setTextVisibility(1f)
}

override fun onAnimationCancel(animation: Animator) {
}

override fun onAnimationRepeat(animation: Animator) {
}
})

animation?.start()
}
}

MotionEvent.ACTION_MOVE -> {
val newX = event.rawX - offsetX
view.x = newX

if (view.x + view.right >= endX) {
view.x = endX.toFloat() - view.right

binding.thumb.isEnabled = false
binding.thumb.isClickable = false

onSwipeButtonListener?.onSwiped()
}
if (view.x <= 0) view.x = 0F

var rawAlpha = (textStartX - (view.x + view.right))
if (rawAlpha > 100) setTextVisibility(1f)
else if (rawAlpha in 0.0..100.0) setTextVisibility(rawAlpha / 100)
else if (view.x + view.right > textStartX && view.x + view.right < textEndX) setTextVisibility(0f)
else {
rawAlpha = (view.x + view.left) - textEndX
if (rawAlpha > 100) setTextVisibility(1f)
else if (rawAlpha in 0.0..100.0) setTextVisibility(rawAlpha / 100)
}
}
}

layoutParams = binding.backgroundProgress.layoutParams
layoutParams?.width = (view.x + (view.right / 2) + 15).toInt()
binding.backgroundProgress.layoutParams = layoutParams

true
}
}

private fun setTextVisibility(alpha: Float) {
binding.textView.alpha = alpha
}

/**
* Set listener for SwipeButton
*/
fun setListener(onSwipeButtonListener: OnSwipeButtonListener) {
this.onSwipeButtonListener = onSwipeButtonListener
}

/**
* Set SwipeButton text
*/
fun setText(text: String) {
binding.textView.text = text
}

/**
* Set SwipeButton default text
*/
fun setDefaultText(defaultText: String) {
this.defaultText = defaultText
}

/**
* Reload SwipeButton, set 'setDefaultText = true' if you want return defaultText
*/
fun reload(setDefaultText: Boolean = false) {
binding.thumb.isEnabled = true
binding.thumb.isClickable = true

binding.thumb.x = 0F
val layoutParams = binding.backgroundProgress.layoutParams
layoutParams.width = 0
binding.backgroundProgress.layoutParams = layoutParams

if (setDefaultText)
binding.textView.text = defaultText
}

interface OnSwipeButtonListener {
fun onSwiped()
}

override fun onViewRemoved(child: View?) {
super.onViewRemoved(child)

_binding = null
onSwipeButtonListener = null

animation?.cancel()
animation = null
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/swipe_arrow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#000000" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/swipe_button_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#a0dcee"/>
</shape>
Loading

0 comments on commit e869953

Please sign in to comment.