├── .gitignore ├── .travis.yml ├── Android ├── .gitignore ├── .idea │ ├── .gitignore │ ├── compiler.xml │ ├── encodings.xml │ ├── gradle.xml │ ├── jarRepositories.xml │ ├── misc.xml │ └── runConfigurations.xml ├── Core │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── apachat │ │ │ └── loadingbutton │ │ │ └── core │ │ │ ├── Extensions.kt │ │ │ ├── animatedDrawables │ │ │ ├── CircularProgressAnimatedDrawable.kt │ │ │ ├── CircularRevealAnimatedDrawable.kt │ │ │ └── ProgressType.kt │ │ │ ├── customViews │ │ │ ├── CircularProgressButton.kt │ │ │ ├── CircularProgressImageButton.kt │ │ │ ├── OnAnimationEndListener.java │ │ │ └── ProgressButton.kt │ │ │ ├── presentation │ │ │ └── ProgressButtonPresenter.kt │ │ │ └── utils │ │ │ └── Facilities.kt │ │ └── res │ │ ├── drawable │ │ ├── ic_done_white_48dp.png │ │ └── shape_default.xml │ │ └── values │ │ ├── attrs.xml │ │ └── colors.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── scripts │ ├── publish-module.gradle │ └── publish-root.gradle └── settings.gradle ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.aar 4 | *.ap_ 5 | *.aab 6 | 7 | # Files for the ART/Dalvik VM 8 | *.dex 9 | 10 | # Java class files 11 | *.class 12 | 13 | # Generated files 14 | bin/ 15 | gen/ 16 | out/ 17 | # Uncomment the following line in case you need and you don't have the release build type files in your app 18 | # release/ 19 | 20 | # Gradle files 21 | .gradle/ 22 | build/ 23 | 24 | # Local configuration file (sdk path, etc) 25 | local.properties 26 | 27 | # Proguard folder generated by Eclipse 28 | proguard/ 29 | 30 | # Log Files 31 | *.log 32 | 33 | # Android Studio Navigation editor temp files 34 | .navigation/ 35 | 36 | # Android Studio captures folder 37 | captures/ 38 | 39 | # IntelliJ 40 | *.iml 41 | .idea/workspace.xml 42 | .idea/tasks.xml 43 | .idea/gradle.xml 44 | .idea/assetWizardSettings.xml 45 | .idea/dictionaries 46 | .idea/libraries 47 | # Android Studio 3 in .gitignore file. 48 | .idea/caches 49 | .idea/modules.xml 50 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you 51 | .idea/navEditor.xml 52 | 53 | # Keystore files 54 | # Uncomment the following lines if you do not want to check your keystore files in. 55 | #*.jks 56 | #*.keystore 57 | 58 | # External native build folder generated in Android Studio 2.2 and later 59 | .externalNativeBuild 60 | .cxx/ 61 | 62 | # Google Services (e.g. APIs or Firebase) 63 | # google-services.json 64 | 65 | # Freeline 66 | freeline.py 67 | freeline/ 68 | freeline_project_description.json 69 | 70 | # fastlane 71 | fastlane/report.xml 72 | fastlane/Preview.html 73 | fastlane/screenshots 74 | fastlane/test_output 75 | fastlane/readme.md 76 | 77 | # Version control 78 | vcs.xml 79 | 80 | # lint 81 | lint/intermediates/ 82 | lint/generated/ 83 | lint/outputs/ 84 | lint/tmp/ 85 | # lint/reports/ 86 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: java 3 | jdk: oraclejdk8 4 | dist: trusty 5 | os: 6 | - linux 7 | git: 8 | depth: false 9 | submodules: false 10 | 11 | before_install: 12 | - "chmod +x ./Android/gradlew" 13 | 14 | script: 15 | - yes | ./Android/gradlew tasks --scan 16 | - yes | ./Android/gradlew tasks --all 17 | 18 | before_cache: 19 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 20 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 21 | 22 | cache: 23 | directories: 24 | - $HOME/.gradle/caches/ 25 | - $HOME/.gradle/wrapper/ 26 | - $HOME/.android/build-cache 27 | 28 | after_success: 29 | - bash <(curl -s https://codecov.io/bash) -t 9166c3b5-d71b-4714-95aa-3fd6811d59dc 30 | -------------------------------------------------------------------------------- /Android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /Android/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Android/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Android/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Android/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /Android/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /Android/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 22 | -------------------------------------------------------------------------------- /Android/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /Android/Core/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /Android/Core/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'kotlin-android' 4 | } 5 | 6 | android { 7 | compileSdkVersion 30 8 | buildToolsVersion "30.0.3" 9 | 10 | defaultConfig { 11 | minSdkVersion 21 12 | targetSdkVersion 30 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | consumerProguardFiles "consumer-rules.pro" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | compileOptions { 27 | sourceCompatibility JavaVersion.VERSION_1_8 28 | targetCompatibility JavaVersion.VERSION_1_8 29 | } 30 | 31 | kotlinOptions { 32 | jvmTarget = '1.8' 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 38 | implementation 'androidx.core:core-ktx:1.6.0' 39 | implementation 'androidx.appcompat:appcompat:1.3.0' 40 | implementation 'com.google.android.material:material:1.4.0' 41 | } 42 | 43 | ext { 44 | PUBLISH_GROUP_ID = 'com.apachat' 45 | PUBLISH_VERSION = '1.0.11' 46 | PUBLISH_ARTIFACT_ID = 'loadingbutton-android' 47 | } 48 | 49 | apply from: "${rootProject.projectDir}/scripts/publish-module.gradle" 50 | -------------------------------------------------------------------------------- /Android/Core/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FarhamHosseini/LoadingButton/19a6c69596169576452046701c6b43c6275b0439/Android/Core/consumer-rules.pro -------------------------------------------------------------------------------- /Android/Core/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /Android/Core/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/Extensions.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core 2 | 3 | import android.animation.Animator 4 | import android.view.View 5 | 6 | internal fun Animator.disposeAnimator() { 7 | end() 8 | removeAllListeners() 9 | cancel() 10 | } 11 | 12 | internal fun View.updateWidth(width: Int) { 13 | val layoutParams = this.layoutParams 14 | layoutParams.width = width 15 | this.layoutParams = layoutParams 16 | } 17 | 18 | internal fun View.updateHeight(height: Int) { 19 | val layoutParams = this.layoutParams 20 | layoutParams.height = height 21 | this.layoutParams = layoutParams 22 | } -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/animatedDrawables/CircularProgressAnimatedDrawable.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.animatedDrawables 2 | 3 | import android.animation.* 4 | import android.graphics.* 5 | import android.graphics.drawable.Animatable 6 | import android.graphics.drawable.Drawable 7 | import android.view.animation.AccelerateDecelerateInterpolator 8 | import android.view.animation.LinearInterpolator 9 | import com.apachat.loadingbutton.core.customViews.ProgressButton 10 | import com.apachat.loadingbutton.core.disposeAnimator 11 | 12 | const val MIN_PROGRESS = 0F 13 | const val MAX_PROGRESS = 100F 14 | private const val ANGLE_ANIMATOR_DURATION = 2000L 15 | private const val SWEEP_ANIMATOR_DURATION = 700L 16 | private const val MIN_SWEEP_ANGLE = 50F 17 | 18 | internal class CircularProgressAnimatedDrawable( 19 | private val progressButton: ProgressButton, 20 | private val borderWidth: Float, 21 | arcColor: Int, 22 | var progressType: ProgressType = ProgressType.INDETERMINATE 23 | ) : Drawable(), Animatable { 24 | 25 | private val fBounds: RectF by lazy { 26 | RectF().apply { 27 | left = bounds.left.toFloat() + borderWidth / 2F + .5F 28 | right = bounds.right.toFloat() - borderWidth / 2F - .5F 29 | top = bounds.top.toFloat() + borderWidth / 2F + .5F 30 | bottom = bounds.bottom.toFloat() - borderWidth / 2F - .5F 31 | } 32 | } 33 | 34 | private val paint = Paint().apply { 35 | isAntiAlias = true 36 | style = Paint.Style.STROKE 37 | strokeWidth = borderWidth 38 | color = arcColor 39 | } 40 | 41 | private var currentGlobalAngle: Float = 0F 42 | private var currentSweepAngle: Float = 0F 43 | private var currentGlobalAngleOffset: Float = 0F 44 | 45 | private var modeAppearing: Boolean = false 46 | 47 | private var shouldDraw: Boolean = true 48 | 49 | var progress: Float = 0F 50 | set(value) { 51 | if (progressType == ProgressType.INDETERMINATE) { 52 | stop() 53 | progressType = ProgressType.DETERMINATE 54 | } 55 | 56 | if (field == value) { 57 | return 58 | } 59 | 60 | field = when { 61 | value > MAX_PROGRESS -> MAX_PROGRESS 62 | value < MIN_PROGRESS -> MIN_PROGRESS 63 | else -> value 64 | } 65 | 66 | progressButton.invalidate() 67 | } 68 | 69 | private val indeterminateAnimator = AnimatorSet().apply { 70 | playTogether( 71 | angleValueAnimator(LinearInterpolator()), 72 | sweepValueAnimator(AccelerateDecelerateInterpolator()) 73 | ) 74 | } 75 | 76 | private fun toggleSweep() { 77 | modeAppearing = !modeAppearing 78 | 79 | if (modeAppearing) { 80 | currentGlobalAngleOffset = (currentGlobalAngleOffset + MIN_SWEEP_ANGLE * 2) % 360 81 | } 82 | } 83 | 84 | private fun angleValueAnimator(timeInterpolator: TimeInterpolator): ValueAnimator = 85 | ValueAnimator.ofFloat(0F, 360F).apply { 86 | interpolator = timeInterpolator 87 | duration = ANGLE_ANIMATOR_DURATION 88 | repeatCount = ValueAnimator.INFINITE 89 | 90 | addUpdateListener { animation -> currentGlobalAngle = animation.animatedValue as Float } 91 | } 92 | 93 | private fun sweepValueAnimator(timeInterpolator: TimeInterpolator): ValueAnimator = 94 | ValueAnimator.ofFloat(0F, 360F - 2 * MIN_SWEEP_ANGLE).apply { 95 | interpolator = timeInterpolator 96 | duration = SWEEP_ANIMATOR_DURATION 97 | repeatCount = ValueAnimator.INFINITE 98 | 99 | addUpdateListener { animation -> 100 | currentSweepAngle = animation.animatedValue as Float 101 | 102 | if (currentSweepAngle < 5) { 103 | shouldDraw = true 104 | } 105 | 106 | if (shouldDraw) { 107 | progressButton.invalidate() 108 | } 109 | } 110 | 111 | addListener(object : AnimatorListenerAdapter() { 112 | override fun onAnimationRepeat(animation: Animator) { 113 | toggleSweep() 114 | shouldDraw = false 115 | } 116 | }) 117 | } 118 | 119 | private fun getAngles(): Pair = 120 | when (progressType) { 121 | ProgressType.DETERMINATE -> { 122 | -90F to progress * 3.6F 123 | } 124 | ProgressType.INDETERMINATE -> { 125 | if (modeAppearing) { 126 | (currentGlobalAngle - currentGlobalAngleOffset) to currentSweepAngle + MIN_SWEEP_ANGLE 127 | } else { 128 | (currentGlobalAngle - currentGlobalAngleOffset + currentSweepAngle) to 129 | 360F - currentSweepAngle - MIN_SWEEP_ANGLE 130 | } 131 | } 132 | } 133 | 134 | fun setLoadingBarColor(color: Int) { 135 | paint.color = color 136 | } 137 | 138 | override fun isRunning(): Boolean = indeterminateAnimator.isRunning 139 | 140 | override fun start() { 141 | if (isRunning) { 142 | return 143 | } 144 | 145 | indeterminateAnimator.start() 146 | } 147 | 148 | override fun stop() { 149 | if (!isRunning) { 150 | return 151 | } 152 | 153 | indeterminateAnimator.end() 154 | } 155 | 156 | override fun draw(canvas: Canvas) { 157 | val (startAngle, sweepAngle) = getAngles() 158 | canvas.drawArc(fBounds, startAngle, sweepAngle, false, paint) 159 | } 160 | 161 | override fun setAlpha(alpha: Int) { 162 | paint.alpha = alpha 163 | } 164 | 165 | override fun getOpacity(): Int = PixelFormat.TRANSPARENT 166 | 167 | override fun setColorFilter(colorFilter: ColorFilter?) { 168 | paint.colorFilter = colorFilter 169 | } 170 | 171 | fun dispose() { 172 | indeterminateAnimator.disposeAnimator() 173 | } 174 | } -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/animatedDrawables/CircularRevealAnimatedDrawable.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.animatedDrawables 2 | 3 | import android.animation.* 4 | import android.graphics.* 5 | import android.graphics.drawable.Animatable 6 | import android.graphics.drawable.Drawable 7 | import android.view.animation.DecelerateInterpolator 8 | import com.apachat.loadingbutton.core.customViews.ProgressButton 9 | import com.apachat.loadingbutton.core.disposeAnimator 10 | 11 | private const val REVEAL_DURATION = 120L 12 | private const val ALPHA_ANIMATION_DURATION = 80L 13 | 14 | internal class CircularRevealAnimatedDrawable( 15 | private val progressButton: ProgressButton, 16 | fillColor: Int, 17 | image: Bitmap 18 | ) : Drawable(), Animatable { 19 | 20 | private var currentRadius = 0F 21 | private var isFilled = false 22 | private var imageReadyAlpha = 0 23 | 24 | private val finalRadius: Float by lazy { (bounds.right - bounds.left).toFloat() / 2 } 25 | private val centerWidth: Float by lazy { (bounds.right + bounds.left).toFloat() / 2 } 26 | private val centerHeight: Float by lazy { (bounds.bottom + bounds.top).toFloat() / 2 } 27 | 28 | private val readyImage: Bitmap by lazy { 29 | Bitmap.createScaledBitmap(image, bitMapWidth().toInt(), bitMapHeight().toInt(), false) 30 | } 31 | 32 | private val bitMapXOffset: Float by lazy { 33 | (centerWidth - bitMapWidth() / 2).toFloat() 34 | } 35 | 36 | private val bitMapYOffset: Float by lazy { 37 | ((centerHeight - bitMapHeight() / 2)).toFloat() 38 | } 39 | 40 | private val conclusionAnimation: AnimatorSet by lazy { 41 | AnimatorSet().apply { 42 | playSequentially( 43 | revealAnimator(finalRadius, DecelerateInterpolator()), 44 | alphaAnimator() 45 | ) 46 | } 47 | } 48 | 49 | private val paint = Paint().apply { 50 | isAntiAlias = true 51 | style = Paint.Style.FILL 52 | color = fillColor 53 | } 54 | 55 | private val imageReadyPaint = Paint().apply { 56 | isAntiAlias = true 57 | style = Paint.Style.FILL 58 | color = Color.TRANSPARENT 59 | } 60 | 61 | private fun bitMapWidth(): Double = ((bounds.right - bounds.left) * 0.6) 62 | 63 | private fun bitMapHeight(): Double = ((bounds.bottom - bounds.top) * 0.6) 64 | 65 | private fun revealAnimator(radius: Float, timeInterpolator: TimeInterpolator): Animator = 66 | ValueAnimator.ofFloat(0F, radius).apply { 67 | interpolator = timeInterpolator 68 | duration = REVEAL_DURATION 69 | 70 | addUpdateListener { animation -> 71 | currentRadius = animation.animatedValue as Float 72 | progressButton.invalidate() 73 | } 74 | 75 | addListener(object : AnimatorListenerAdapter() { 76 | override fun onAnimationEnd(animation: Animator?) { 77 | super.onAnimationEnd(animation) 78 | isFilled = true 79 | } 80 | }) 81 | } 82 | 83 | private fun alphaAnimator(): Animator = 84 | ValueAnimator.ofInt(0, 255).apply { 85 | duration = ALPHA_ANIMATION_DURATION 86 | addUpdateListener { animation -> 87 | imageReadyAlpha = animation.animatedValue as Int 88 | progressButton.invalidate() 89 | } 90 | } 91 | 92 | override fun draw(canvas: Canvas) { 93 | canvas.drawCircle(centerWidth, centerHeight, currentRadius, paint) 94 | 95 | if (isFilled) { 96 | imageReadyPaint.alpha = imageReadyAlpha 97 | canvas.drawBitmap(readyImage, bitMapXOffset, bitMapYOffset, imageReadyPaint) 98 | } 99 | } 100 | 101 | override fun setAlpha(alpha: Int) {} 102 | 103 | override fun getOpacity(): Int = PixelFormat.OPAQUE 104 | 105 | override fun setColorFilter(colorFilter: ColorFilter?) {} 106 | 107 | override fun isRunning(): Boolean = conclusionAnimation.isRunning 108 | 109 | override fun start() { 110 | conclusionAnimation.start() 111 | } 112 | 113 | override fun stop() { 114 | conclusionAnimation.end() 115 | } 116 | 117 | fun dispose() { 118 | conclusionAnimation.disposeAnimator() 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/animatedDrawables/ProgressType.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.animatedDrawables 2 | 3 | enum class ProgressType { 4 | DETERMINATE, INDETERMINATE 5 | } -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/customViews/CircularProgressButton.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.customViews 2 | 3 | import android.animation.AnimatorSet 4 | import android.content.Context 5 | import android.graphics.Bitmap 6 | import android.graphics.Canvas 7 | import android.graphics.Rect 8 | import android.graphics.drawable.Drawable 9 | import android.util.AttributeSet 10 | import androidx.appcompat.widget.AppCompatButton 11 | import androidx.core.content.ContextCompat 12 | import androidx.lifecycle.Lifecycle 13 | import androidx.lifecycle.OnLifecycleEvent 14 | import com.apachat.loadingbutton.core.animatedDrawables.CircularProgressAnimatedDrawable 15 | import com.apachat.loadingbutton.core.animatedDrawables.CircularRevealAnimatedDrawable 16 | import com.apachat.loadingbutton.core.animatedDrawables.ProgressType 17 | import com.apachat.loadingbutton.core.disposeAnimator 18 | import com.apachat.loadingbutton.core.presentation.ProgressButtonPresenter 19 | import com.apachat.loadingbutton.core.presentation.State 20 | 21 | open class CircularProgressButton : AppCompatButton, ProgressButton { 22 | 23 | constructor(context: Context) : super(context) { 24 | init() 25 | } 26 | 27 | constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { 28 | init(attrs) 29 | } 30 | 31 | constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( 32 | context, 33 | attrs, 34 | defStyleAttr 35 | ) { 36 | init(attrs, defStyleAttr) 37 | } 38 | 39 | override var paddingProgress = 0F 40 | 41 | override var spinningBarWidth = 10F 42 | override var spinningBarColor = ContextCompat.getColor(context, android.R.color.black) 43 | 44 | override var finalCorner = 0F 45 | override var initialCorner = 0F 46 | 47 | private lateinit var initialState: InitialState 48 | 49 | override val finalWidth: Int by lazy { 50 | val padding = Rect() 51 | drawableBackground.getPadding(padding) 52 | finalHeight - (Math.abs(padding.top - padding.left) * 2) 53 | } 54 | 55 | override val finalHeight: Int by lazy { height } 56 | private val initialHeight: Int by lazy { height } 57 | 58 | override var progressType: ProgressType 59 | get() = progressAnimatedDrawable.progressType 60 | set(value) { 61 | progressAnimatedDrawable.progressType = value 62 | } 63 | 64 | override lateinit var drawableBackground: Drawable 65 | 66 | private var savedAnimationEndListener: () -> Unit = {} 67 | 68 | private val presenter = ProgressButtonPresenter(this) 69 | 70 | private val morphAnimator by lazy { 71 | AnimatorSet().apply { 72 | playTogether( 73 | cornerAnimator(drawableBackground, initialCorner, finalCorner), 74 | widthAnimator(this@CircularProgressButton, initialState.initialWidth, finalWidth), 75 | heightAnimator(this@CircularProgressButton, initialHeight, finalHeight) 76 | ) 77 | 78 | addListener(morphListener(presenter::morphStart, presenter::morphEnd)) 79 | } 80 | } 81 | 82 | private val morphRevertAnimator by lazy { 83 | AnimatorSet().apply { 84 | playTogether( 85 | cornerAnimator(drawableBackground, finalCorner, initialCorner), 86 | widthAnimator(this@CircularProgressButton, finalWidth, initialState.initialWidth), 87 | heightAnimator(this@CircularProgressButton, finalHeight, initialHeight) 88 | ) 89 | 90 | addListener(morphListener(presenter::morphRevertStart, presenter::morphRevertEnd)) 91 | } 92 | } 93 | 94 | private val progressAnimatedDrawable: CircularProgressAnimatedDrawable by lazy { 95 | createProgressDrawable() 96 | } 97 | 98 | private lateinit var revealAnimatedDrawable: CircularRevealAnimatedDrawable 99 | 100 | override fun getState(): State = presenter.state 101 | 102 | override fun saveInitialState() { 103 | initialState = InitialState(width, text, compoundDrawables) 104 | } 105 | 106 | override fun recoverInitialState() { 107 | text = initialState.initialText 108 | setCompoundDrawables( 109 | initialState.compoundDrawables[0], 110 | initialState.compoundDrawables[1], 111 | initialState.compoundDrawables[2], 112 | initialState.compoundDrawables[3] 113 | ) 114 | } 115 | 116 | override fun hideInitialState() { 117 | text = null 118 | } 119 | 120 | override fun drawProgress(canvas: Canvas) { 121 | progressAnimatedDrawable.drawProgress(canvas) 122 | } 123 | 124 | override fun drawDoneAnimation(canvas: Canvas) { 125 | revealAnimatedDrawable.draw(canvas) 126 | } 127 | 128 | override fun startRevealAnimation() { 129 | revealAnimatedDrawable.start() 130 | } 131 | 132 | override fun startMorphAnimation() { 133 | applyAnimationEndListener(morphAnimator, savedAnimationEndListener) 134 | morphAnimator.start() 135 | } 136 | 137 | override fun startMorphRevertAnimation() { 138 | applyAnimationEndListener(morphRevertAnimator, savedAnimationEndListener) 139 | morphRevertAnimator.start() 140 | } 141 | 142 | override fun stopProgressAnimation() { 143 | progressAnimatedDrawable.stop() 144 | } 145 | 146 | override fun stopMorphAnimation() { 147 | morphAnimator.end() 148 | } 149 | 150 | override fun startAnimation(onAnimationEndListener: () -> Unit) { 151 | savedAnimationEndListener = onAnimationEndListener 152 | presenter.startAnimation() 153 | } 154 | 155 | override fun revertAnimation(onAnimationEndListener: () -> Unit) { 156 | savedAnimationEndListener = onAnimationEndListener 157 | presenter.revertAnimation() 158 | } 159 | 160 | override fun stopAnimation() { 161 | presenter.stopAnimation() 162 | } 163 | 164 | override fun doneLoadingAnimation(fillColor: Int, bitmap: Bitmap) { 165 | presenter.doneLoadingAnimation(fillColor, bitmap) 166 | } 167 | 168 | override fun initRevealAnimation(fillColor: Int, bitmap: Bitmap) { 169 | revealAnimatedDrawable = createRevealAnimatedDrawable(fillColor, bitmap) 170 | } 171 | 172 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 173 | fun dispose() { 174 | if (presenter.state != State.BEFORE_DRAW) { 175 | morphAnimator.disposeAnimator() 176 | morphRevertAnimator.disposeAnimator() 177 | } 178 | } 179 | 180 | override fun onDraw(canvas: Canvas) { 181 | super.onDraw(canvas) 182 | 183 | presenter.onDraw(canvas) 184 | } 185 | 186 | override fun setProgress(value: Float) { 187 | if (presenter.validateSetProgress()) { 188 | progressAnimatedDrawable.progress = value 189 | } else { 190 | throw IllegalStateException( 191 | "Set progress in being called in the wrong state: ${presenter.state}." + 192 | " Allowed states: ${State.PROGRESS}, ${State.MORPHING}, ${State.WAITING_PROGRESS}" 193 | ) 194 | } 195 | } 196 | 197 | data class InitialState( 198 | var initialWidth: Int, 199 | val initialText: CharSequence, 200 | val compoundDrawables: Array 201 | ) 202 | } 203 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/customViews/CircularProgressImageButton.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.customViews 2 | 3 | import android.animation.AnimatorSet 4 | import android.content.Context 5 | import android.graphics.Bitmap 6 | import android.graphics.Canvas 7 | import android.graphics.Rect 8 | import android.graphics.drawable.Drawable 9 | import android.util.AttributeSet 10 | import androidx.appcompat.widget.AppCompatImageButton 11 | import androidx.core.content.ContextCompat 12 | import androidx.lifecycle.Lifecycle 13 | import androidx.lifecycle.OnLifecycleEvent 14 | import com.apachat.loadingbutton.core.animatedDrawables.CircularProgressAnimatedDrawable 15 | import com.apachat.loadingbutton.core.animatedDrawables.CircularRevealAnimatedDrawable 16 | import com.apachat.loadingbutton.core.animatedDrawables.ProgressType 17 | import com.apachat.loadingbutton.core.disposeAnimator 18 | import com.apachat.loadingbutton.core.presentation.ProgressButtonPresenter 19 | import com.apachat.loadingbutton.core.presentation.State 20 | 21 | open class CircularProgressImageButton : AppCompatImageButton, ProgressButton { 22 | 23 | constructor(context: Context) : super(context) { 24 | init() 25 | } 26 | 27 | constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { 28 | init(attrs) 29 | } 30 | 31 | constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( 32 | context, 33 | attrs, 34 | defStyleAttr 35 | ) { 36 | init(attrs, defStyleAttr) 37 | } 38 | 39 | override var paddingProgress = 0F 40 | 41 | override var spinningBarWidth = 10F 42 | override var spinningBarColor = ContextCompat.getColor(context, android.R.color.black) 43 | 44 | override var finalCorner = 0F 45 | override var initialCorner = 0F 46 | 47 | private lateinit var initialState: InitialState 48 | 49 | override val finalHeight: Int by lazy { height } 50 | private val initialHeight: Int by lazy { height } 51 | override val finalWidth: Int by lazy { 52 | val padding = Rect() 53 | drawableBackground.getPadding(padding) 54 | finalHeight - (Math.abs(padding.top - padding.left) * 2) 55 | } 56 | 57 | override var progressType: ProgressType 58 | get() = progressAnimatedDrawable.progressType 59 | set(value) { 60 | progressAnimatedDrawable.progressType = value 61 | } 62 | 63 | override lateinit var drawableBackground: Drawable 64 | 65 | private var savedAnimationEndListener: () -> Unit = {} 66 | 67 | private val presenter = ProgressButtonPresenter(this) 68 | 69 | private val morphAnimator by lazy { 70 | AnimatorSet().apply { 71 | playTogether( 72 | cornerAnimator(drawableBackground, initialCorner, finalCorner), 73 | widthAnimator(this@CircularProgressImageButton, initialState.initialWidth, finalWidth), 74 | heightAnimator(this@CircularProgressImageButton, initialHeight, finalHeight) 75 | ) 76 | 77 | addListener(morphListener(presenter::morphStart, presenter::morphEnd)) 78 | } 79 | } 80 | 81 | private val morphRevertAnimator by lazy { 82 | AnimatorSet().apply { 83 | playTogether( 84 | cornerAnimator(drawableBackground, finalCorner, initialCorner), 85 | widthAnimator(this@CircularProgressImageButton, finalWidth, initialState.initialWidth), 86 | heightAnimator(this@CircularProgressImageButton, finalHeight, initialHeight) 87 | ) 88 | 89 | addListener(morphListener(presenter::morphRevertStart, presenter::morphRevertEnd)) 90 | } 91 | } 92 | 93 | private val progressAnimatedDrawable: CircularProgressAnimatedDrawable by lazy { 94 | createProgressDrawable() 95 | } 96 | 97 | private lateinit var revealAnimatedDrawable: CircularRevealAnimatedDrawable 98 | 99 | override fun getState(): State = presenter.state 100 | 101 | override fun saveInitialState() { 102 | initialState = InitialState(width) 103 | } 104 | 105 | override fun recoverInitialState() {} 106 | 107 | override fun hideInitialState() {} 108 | 109 | override fun drawProgress(canvas: Canvas) { 110 | progressAnimatedDrawable.drawProgress(canvas) 111 | } 112 | 113 | override fun drawDoneAnimation(canvas: Canvas) { 114 | revealAnimatedDrawable.draw(canvas) 115 | } 116 | 117 | override fun startRevealAnimation() { 118 | revealAnimatedDrawable.start() 119 | } 120 | 121 | override fun startMorphAnimation() { 122 | applyAnimationEndListener(morphAnimator, savedAnimationEndListener) 123 | morphAnimator.start() 124 | } 125 | 126 | override fun startMorphRevertAnimation() { 127 | applyAnimationEndListener(morphAnimator, savedAnimationEndListener) 128 | morphRevertAnimator.start() 129 | } 130 | 131 | override fun stopProgressAnimation() { 132 | progressAnimatedDrawable.stop() 133 | } 134 | 135 | override fun stopMorphAnimation() { 136 | morphAnimator.end() 137 | } 138 | 139 | override fun startAnimation(onAnimationEndListener: () -> Unit) { 140 | savedAnimationEndListener = onAnimationEndListener 141 | presenter.startAnimation() 142 | } 143 | 144 | override fun revertAnimation(onAnimationEndListener: () -> Unit) { 145 | savedAnimationEndListener = onAnimationEndListener 146 | presenter.revertAnimation() 147 | } 148 | 149 | override fun stopAnimation() { 150 | presenter.stopAnimation() 151 | } 152 | 153 | override fun doneLoadingAnimation(fillColor: Int, bitmap: Bitmap) { 154 | presenter.doneLoadingAnimation(fillColor, bitmap) 155 | } 156 | 157 | override fun initRevealAnimation(fillColor: Int, bitmap: Bitmap) { 158 | revealAnimatedDrawable = createRevealAnimatedDrawable(fillColor, bitmap) 159 | } 160 | 161 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 162 | fun dispose() { 163 | morphAnimator.disposeAnimator() 164 | morphRevertAnimator.disposeAnimator() 165 | } 166 | 167 | override fun onDraw(canvas: Canvas) { 168 | super.onDraw(canvas) 169 | 170 | presenter.onDraw(canvas) 171 | } 172 | 173 | override fun setProgress(value: Float) { 174 | if (presenter.validateSetProgress()) { 175 | progressAnimatedDrawable.progress = value 176 | } else { 177 | throw IllegalStateException( 178 | "Set progress in being called in the wrong state: ${presenter.state}." + 179 | " Allowed states: ${State.PROGRESS}, ${State.MORPHING}, ${State.WAITING_PROGRESS}" 180 | ) 181 | } 182 | } 183 | 184 | override fun setCompoundDrawables( 185 | left: Drawable?, 186 | top: Drawable?, 187 | right: Drawable?, 188 | bottom: Drawable? 189 | ) { 190 | } 191 | 192 | data class InitialState(var initialWidth: Int) 193 | } 194 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/customViews/OnAnimationEndListener.java: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.customViews; 2 | 3 | @FunctionalInterface 4 | public interface OnAnimationEndListener { 5 | void onAnimationEnd(); 6 | } -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/customViews/ProgressButton.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.customViews 2 | 3 | import android.animation.Animator 4 | import android.animation.AnimatorListenerAdapter 5 | import android.animation.ObjectAnimator 6 | import android.animation.ValueAnimator 7 | import android.annotation.SuppressLint 8 | import android.content.Context 9 | import android.content.res.TypedArray 10 | import android.graphics.Bitmap 11 | import android.graphics.Canvas 12 | import android.graphics.Rect 13 | import android.graphics.drawable.ColorDrawable 14 | import android.graphics.drawable.Drawable 15 | import android.graphics.drawable.GradientDrawable 16 | import android.util.AttributeSet 17 | import android.view.View 18 | import androidx.core.content.ContextCompat 19 | import androidx.lifecycle.LifecycleObserver 20 | import com.apachat.loadingbutton.core.R 21 | import com.apachat.loadingbutton.core.animatedDrawables.CircularProgressAnimatedDrawable 22 | import com.apachat.loadingbutton.core.animatedDrawables.CircularRevealAnimatedDrawable 23 | import com.apachat.loadingbutton.core.animatedDrawables.ProgressType 24 | import com.apachat.loadingbutton.core.presentation.State 25 | import com.apachat.loadingbutton.core.updateHeight 26 | import com.apachat.loadingbutton.core.updateWidth 27 | import com.apachat.loadingbutton.core.utils.addLifecycleObserver 28 | import com.apachat.loadingbutton.core.utils.parseGradientDrawable 29 | 30 | interface ProgressButton : Drawable.Callback, LifecycleObserver { 31 | var paddingProgress: Float 32 | var spinningBarWidth: Float 33 | var spinningBarColor: Int 34 | 35 | var initialCorner: Float 36 | var finalCorner: Float 37 | 38 | val finalWidth: Int 39 | val finalHeight: Int 40 | 41 | var drawableBackground: Drawable 42 | var progressType: ProgressType 43 | 44 | fun invalidate() 45 | 46 | fun getHeight(): Int 47 | fun getWidth(): Int 48 | fun getContext(): Context 49 | fun getState(): State 50 | 51 | fun setClickable(b: Boolean) 52 | fun setCompoundDrawables(left: Drawable?, top: Drawable?, right: Drawable?, bottom: Drawable?) 53 | fun setBackground(background: Drawable) 54 | 55 | fun saveInitialState() 56 | fun recoverInitialState() 57 | fun hideInitialState() 58 | 59 | fun startAnimation(onAnimationEndListener: () -> Unit) 60 | 61 | fun startAnimation(onAnimationEndListener: OnAnimationEndListener) { 62 | startAnimation(onAnimationEndListener as () -> Unit) 63 | } 64 | 65 | fun startAnimation() { 66 | startAnimation { } 67 | } 68 | 69 | fun startMorphAnimation() 70 | fun startMorphRevertAnimation() 71 | fun stopMorphAnimation() 72 | fun stopAnimation() 73 | fun stopProgressAnimation() 74 | 75 | fun revertAnimation(onAnimationEndListener: () -> Unit = {}) 76 | fun revertAnimation(onAnimationEndListener: OnAnimationEndListener) { 77 | this.revertAnimation(onAnimationEndListener as (() -> Unit)) 78 | } 79 | 80 | fun revertAnimation() { 81 | revertAnimation { } 82 | } 83 | 84 | fun doneLoadingAnimation(fillColor: Int, bitmap: Bitmap) 85 | 86 | fun startRevealAnimation() 87 | fun drawProgress(canvas: Canvas) 88 | fun drawDoneAnimation(canvas: Canvas) 89 | 90 | fun setProgress(value: Float) 91 | fun initRevealAnimation(fillColor: Int, bitmap: Bitmap) 92 | } 93 | 94 | internal fun ProgressButton.init(attrs: AttributeSet? = null, defStyleAttr: Int = 0) { 95 | val typedArray: TypedArray? = attrs?.run { 96 | getContext().obtainStyledAttributes(this, R.styleable.CircularProgressButton, defStyleAttr, 0) 97 | } 98 | 99 | val typedArrayBg: TypedArray? = attrs?.run { 100 | val attrsArray = intArrayOf(android.R.attr.background) 101 | getContext().obtainStyledAttributes(this, attrsArray, defStyleAttr, 0) 102 | } 103 | 104 | val tempDrawable = typedArrayBg?.getDrawable(0) 105 | ?: ContextCompat.getDrawable(getContext(), R.drawable.shape_default)!!.let { 106 | when (it) { 107 | is ColorDrawable -> GradientDrawable().apply { setColor(it.color) } 108 | else -> it 109 | } 110 | } 111 | drawableBackground = tempDrawable.let { 112 | it.constantState?.newDrawable()?.mutate() ?: it 113 | } 114 | 115 | setBackground(drawableBackground) 116 | 117 | typedArray?.let { tArray -> config(tArray) } 118 | 119 | typedArray?.recycle() 120 | typedArrayBg?.recycle() 121 | 122 | // all ProgressButton instances implement LifecycleObserver, so we can 123 | // auto-register each instance on initialization 124 | getContext().addLifecycleObserver(this) 125 | } 126 | 127 | internal fun ProgressButton.config(tArray: TypedArray) { 128 | initialCorner = tArray.getDimension(R.styleable.CircularProgressButton_initialCornerAngle, 0f) 129 | finalCorner = tArray.getDimension(R.styleable.CircularProgressButton_finalCornerAngle, 100f) 130 | 131 | spinningBarWidth = tArray.getDimension(R.styleable.CircularProgressButton_spinning_bar_width, 10f) 132 | spinningBarColor = 133 | tArray.getColor(R.styleable.CircularProgressButton_spinning_bar_color, spinningBarColor) 134 | 135 | paddingProgress = tArray.getDimension(R.styleable.CircularProgressButton_spinning_bar_padding, 0F) 136 | } 137 | 138 | internal fun ProgressButton.createProgressDrawable(): CircularProgressAnimatedDrawable = 139 | CircularProgressAnimatedDrawable(this, spinningBarWidth, spinningBarColor).apply { 140 | val offset = (finalWidth - finalHeight) / 2 141 | 142 | val padding = Rect() 143 | drawableBackground.getPadding(padding) 144 | 145 | val left = offset + paddingProgress.toInt() + padding.bottom 146 | val right = finalWidth - offset - paddingProgress.toInt() - padding.bottom 147 | val bottom = finalHeight - paddingProgress.toInt() - padding.bottom 148 | val top = paddingProgress.toInt() + padding.top 149 | 150 | setBounds(left, top, right, bottom) 151 | callback = this@createProgressDrawable 152 | } 153 | 154 | internal fun ProgressButton.createRevealAnimatedDrawable( 155 | fillColor: Int, 156 | bitmap: Bitmap 157 | ): CircularRevealAnimatedDrawable = 158 | CircularRevealAnimatedDrawable(this, fillColor, bitmap).apply { 159 | val padding = Rect() 160 | drawableBackground.getPadding(padding) 161 | val paddingSides = (Math.abs(padding.top - padding.left)) 162 | setBounds(paddingSides, padding.top, finalWidth - paddingSides, finalHeight - padding.bottom) 163 | callback = this@createRevealAnimatedDrawable 164 | } 165 | 166 | @SuppressLint("ObjectAnimatorBinding") 167 | internal fun cornerAnimator(drawable: Drawable, initial: Float, final: Float) = 168 | when (drawable) { 169 | is GradientDrawable -> ObjectAnimator.ofFloat(drawable, "cornerRadius", initial, final) 170 | else -> ObjectAnimator.ofFloat(parseGradientDrawable(drawable), "cornerRadius", initial, final) 171 | } 172 | 173 | internal fun widthAnimator(view: View, initial: Int, final: Int) = 174 | ValueAnimator.ofInt(initial, final).apply { 175 | addUpdateListener { animation -> 176 | view.updateWidth(animation.animatedValue as Int) 177 | } 178 | } 179 | 180 | internal fun heightAnimator(view: View, initial: Int, final: Int) = 181 | ValueAnimator.ofInt(initial, final).apply { 182 | addUpdateListener { animation -> 183 | view.updateHeight(animation.animatedValue as Int) 184 | } 185 | } 186 | 187 | internal fun morphListener(morphStartFn: () -> Unit, morphEndFn: () -> Unit) = 188 | object : AnimatorListenerAdapter() { 189 | override fun onAnimationEnd(animation: Animator?) { 190 | morphEndFn() 191 | } 192 | 193 | override fun onAnimationStart(animation: Animator?) { 194 | morphStartFn() 195 | } 196 | } 197 | 198 | internal fun CircularProgressAnimatedDrawable.drawProgress(canvas: Canvas) { 199 | if (isRunning) { 200 | draw(canvas) 201 | } else { 202 | start() 203 | } 204 | } 205 | 206 | internal fun applyAnimationEndListener(animator: Animator, onAnimationEndListener: () -> Unit) = 207 | animator.addListener(object : AnimatorListenerAdapter() { 208 | override fun onAnimationEnd(animation: Animator?) { 209 | onAnimationEndListener() 210 | animator.removeListener(this) 211 | } 212 | }) 213 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/presentation/ProgressButtonPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.presentation 2 | 3 | import android.graphics.Bitmap 4 | import android.graphics.Canvas 5 | import android.os.Handler 6 | import com.apachat.loadingbutton.core.customViews.ProgressButton 7 | 8 | enum class State { 9 | BEFORE_DRAW, 10 | IDLE, 11 | MORPHING, 12 | MORPHING_REVERT, 13 | WAITING_PROGRESS, 14 | PROGRESS, 15 | WAITING_DONE, 16 | DONE, 17 | WAITING_TO_STOP, 18 | STOPPED 19 | } 20 | 21 | internal class ProgressButtonPresenter(private val view: ProgressButton) { 22 | var state: State = State.BEFORE_DRAW 23 | 24 | fun morphStart() { 25 | view.run { 26 | hideInitialState() 27 | setClickable(false) 28 | setCompoundDrawables(null, null, null, null) 29 | } 30 | 31 | state = State.MORPHING 32 | } 33 | 34 | fun morphEnd() { 35 | state = when (state) { 36 | State.WAITING_DONE -> { 37 | Handler().postDelayed({ view.startRevealAnimation() }, 50) 38 | State.DONE 39 | } 40 | State.WAITING_TO_STOP -> State.STOPPED 41 | else -> State.PROGRESS 42 | } 43 | } 44 | 45 | fun morphRevertStart() { 46 | view.setClickable(false) 47 | state = State.MORPHING 48 | } 49 | 50 | fun morphRevertEnd() { 51 | view.setClickable(true) 52 | view.recoverInitialState() 53 | state = State.IDLE 54 | } 55 | 56 | fun onDraw(canvas: Canvas) { 57 | when (state) { 58 | State.BEFORE_DRAW -> { 59 | state = State.IDLE 60 | view.saveInitialState() 61 | } 62 | State.WAITING_PROGRESS -> { 63 | view.saveInitialState() 64 | view.startMorphAnimation() 65 | } 66 | State.PROGRESS -> view.drawProgress(canvas) 67 | State.DONE -> view.drawDoneAnimation(canvas) 68 | else -> return 69 | } 70 | } 71 | 72 | fun startAnimation() { 73 | if (state == State.BEFORE_DRAW) { 74 | state = State.WAITING_PROGRESS 75 | return 76 | } 77 | 78 | if (state != State.IDLE) { 79 | return 80 | } 81 | 82 | view.startMorphAnimation() 83 | } 84 | 85 | fun stopAnimation() { 86 | state = when (state) { 87 | State.PROGRESS -> { 88 | view.stopProgressAnimation() 89 | State.STOPPED 90 | } 91 | State.MORPHING, State.WAITING_PROGRESS -> State.WAITING_TO_STOP 92 | else -> State.STOPPED 93 | } 94 | } 95 | 96 | fun revertAnimation() { 97 | when (state) { 98 | State.MORPHING -> { 99 | view.stopMorphAnimation() 100 | view.startMorphRevertAnimation() 101 | } 102 | State.PROGRESS -> { 103 | view.stopProgressAnimation() 104 | view.startMorphRevertAnimation() 105 | } 106 | State.WAITING_DONE, State.STOPPED, State.DONE -> { 107 | view.startMorphRevertAnimation() 108 | } 109 | else -> return 110 | } 111 | } 112 | 113 | fun doneLoadingAnimation(fillColor: Int, bitmap: Bitmap) { 114 | view.initRevealAnimation(fillColor, bitmap) 115 | 116 | state = when (state) { 117 | State.PROGRESS -> { 118 | view.stopProgressAnimation() 119 | view.startRevealAnimation() 120 | State.DONE 121 | } 122 | State.MORPHING -> State.WAITING_DONE 123 | State.STOPPED -> { 124 | view.startRevealAnimation() 125 | State.DONE 126 | } 127 | else -> State.DONE 128 | } 129 | } 130 | 131 | internal fun validateSetProgress(): Boolean = 132 | state == State.PROGRESS || state == State.MORPHING || state == State.WAITING_PROGRESS 133 | } 134 | -------------------------------------------------------------------------------- /Android/Core/src/main/java/com/apachat/loadingbutton/core/utils/Facilities.kt: -------------------------------------------------------------------------------- 1 | package com.apachat.loadingbutton.core.utils 2 | 3 | import android.content.Context 4 | import android.graphics.drawable.* 5 | import android.os.Build 6 | import android.view.ContextThemeWrapper 7 | import androidx.lifecycle.LifecycleObserver 8 | import androidx.lifecycle.LifecycleOwner 9 | 10 | internal fun parseGradientDrawable(drawable: Drawable): GradientDrawable = 11 | when (drawable) { 12 | is GradientDrawable -> drawable 13 | is ColorDrawable -> GradientDrawable().apply { setColor(drawable.color) } 14 | is InsetDrawable -> { 15 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 16 | drawable.drawable?.let { innerDrawable -> 17 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 18 | when (innerDrawable) { 19 | is RippleDrawable -> { 20 | parseGradientDrawable(innerDrawable.getDrawable(0)) 21 | } 22 | else -> parseGradientDrawable(innerDrawable) 23 | } 24 | } else { 25 | parseGradientDrawable(innerDrawable) 26 | } 27 | } 28 | ?: throw RuntimeException("Error reading background... Use a shape or a color in xml!") 29 | } else { 30 | throw RuntimeException("Error reading background... Use a shape or a color in xml!") 31 | } 32 | } 33 | is StateListDrawable -> { 34 | if (drawable.current is GradientDrawable) { 35 | drawable.current as GradientDrawable 36 | } else { 37 | throw RuntimeException("Error reading background... Use a shape or a color in xml!") 38 | } 39 | } 40 | is LayerDrawable -> { 41 | parseGradientDrawable(drawable.getDrawable(0)) 42 | } 43 | else -> throw RuntimeException("Error reading background... Use a shape or a color in xml!") 44 | } 45 | 46 | internal fun Context.addLifecycleObserver(observer: LifecycleObserver) { 47 | when { 48 | this is LifecycleOwner -> this.lifecycle.addObserver(observer) 49 | this is ContextThemeWrapper -> this.baseContext.addLifecycleObserver(observer) 50 | this is androidx.appcompat.view.ContextThemeWrapper -> this.baseContext.addLifecycleObserver( 51 | observer 52 | ) 53 | } 54 | } -------------------------------------------------------------------------------- /Android/Core/src/main/res/drawable/ic_done_white_48dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FarhamHosseini/LoadingButton/19a6c69596169576452046701c6b43c6275b0439/Android/Core/src/main/res/drawable/ic_done_white_48dp.png -------------------------------------------------------------------------------- /Android/Core/src/main/res/drawable/shape_default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Android/Core/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Android/Core/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFA000 4 | #000 5 | #8BC34A 6 | 7 | -------------------------------------------------------------------------------- /Android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = "1.5.20" 3 | 4 | repositories { 5 | google() 6 | mavenCentral() 7 | maven { 8 | url "https://repo1.maven.org/maven2" 9 | } 10 | } 11 | 12 | dependencies { 13 | classpath "com.android.tools.build:gradle:4.2.2" 14 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 15 | classpath "io.github.gradle-nexus:publish-plugin:1.1.0" 16 | } 17 | } 18 | 19 | plugins { 20 | id("io.github.gradle-nexus.publish-plugin") version "1.1.0" 21 | } 22 | 23 | apply plugin: 'io.github.gradle-nexus.publish-plugin' 24 | 25 | apply from: "${rootDir}/scripts/publish-root.gradle" 26 | 27 | allprojects { 28 | repositories { 29 | google() 30 | mavenCentral() 31 | } 32 | } 33 | 34 | task clean(type: Delete) { 35 | delete rootProject.buildDir 36 | } 37 | -------------------------------------------------------------------------------- /Android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 2 | android.useAndroidX=true 3 | kotlin.code.style=official -------------------------------------------------------------------------------- /Android/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FarhamHosseini/LoadingButton/19a6c69596169576452046701c6b43c6275b0439/Android/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 21 19:55:03 IRDT 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /Android/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /Android/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Android/scripts/publish-module.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'maven-publish' 2 | apply plugin: 'signing' 3 | 4 | task androidSourcesJar(type: Jar) { 5 | archiveClassifier.set('sources') 6 | if (project.plugins.findPlugin("com.android.library")) { 7 | // For Android libraries 8 | from android.sourceSets.main.java.srcDirs 9 | // from android.sourceSets.main.kotlin.srcDirs 10 | } else { 11 | // For pure Kotlin libraries, in case you have them 12 | from sourceSets.main.java.srcDirs 13 | from sourceSets.main.kotlin.srcDirs 14 | } 15 | } 16 | 17 | artifacts { 18 | archives androidSourcesJar 19 | // archives javadocJar, androidSourcesJar 20 | } 21 | 22 | // group = PUBLISH_GROUP_ID 23 | version = PUBLISH_VERSION 24 | 25 | afterEvaluate { 26 | publishing { 27 | publications { 28 | release(MavenPublication) { 29 | // The coordinates of the library, being set from variables that 30 | // we'll set up later 31 | groupId PUBLISH_GROUP_ID 32 | artifactId PUBLISH_ARTIFACT_ID 33 | version PUBLISH_VERSION 34 | 35 | // Two artifacts, the `aar` (or `jar`) and the sources 36 | if (project.plugins.findPlugin("com.android.library")) { 37 | from components.release 38 | } else { 39 | from components.java 40 | } 41 | 42 | artifact androidSourcesJar 43 | // artifact javadocJar 44 | 45 | // Mostly self-explanatory metadata 46 | pom { 47 | name = PUBLISH_ARTIFACT_ID 48 | description = 'A button to substitute the ProgressDialog.' 49 | url = 'https://github.com/FarhamHosseini/LoadingButton' 50 | licenses { 51 | license { 52 | name = 'Apache License 2.0' 53 | url = 'https://github.com/FarhamHosseini/LoadingButton/blob/main/LICENSE' 54 | } 55 | } 56 | developers { 57 | developer { 58 | id = 'FarhamHosseini' 59 | name = 'Farham Hosseini' 60 | email = 'farham.hosseini@apachat.com' 61 | } 62 | developer { 63 | id = 'iRonBotxx' 64 | name = 'Farshad Hosseini' 65 | email = 'farshad.hosseini@apachat.com' 66 | } 67 | } 68 | 69 | scm { 70 | connection = 'scm:git:github.com/FarhamHosseini/LoadingButton.git' 71 | developerConnection = 'scm:git:ssh://github.com/FarhamHosseini/LoadingButton.git' 72 | url = 'https://github.com/FarhamHosseini/LoadingButton/tree/main' 73 | } 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | ext["signing.keyId"] = rootProject.ext["signing.keyId"] 81 | ext["signing.password"] = rootProject.ext["signing.password"] 82 | ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"] 83 | 84 | signing { 85 | sign publishing.publications 86 | } -------------------------------------------------------------------------------- /Android/scripts/publish-root.gradle: -------------------------------------------------------------------------------- 1 | ext["ossrhUsername"] = '' 2 | ext["ossrhPassword"] = '' 3 | ext["sonatypeStagingProfileId"] = '' 4 | ext["signing.keyId"] = '' 5 | ext["signing.password"] = '' 6 | ext["signing.secretKeyRingFile"] = '' 7 | 8 | File secretPropsFile = project.rootProject.file('local.properties') 9 | if (secretPropsFile.exists()) { 10 | Properties p = new Properties() 11 | new FileInputStream(secretPropsFile).withCloseable { is -> p.load(is) } 12 | p.each { name, value -> ext[name] = value } 13 | } else { 14 | ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME') 15 | ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD') 16 | ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID') 17 | ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID') 18 | ext["signing.password"] = System.getenv('SIGNING_PASSWORD') 19 | ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE') 20 | } 21 | 22 | nexusPublishing { 23 | repositories { 24 | sonatype { 25 | stagingProfileId = sonatypeStagingProfileId 26 | username = ossrhUsername 27 | password = ossrhPassword 28 | nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/")) 29 | snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")) 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Android/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "Android" 2 | include ':Core' 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Progress Button Android 2 | ![Preview](https://i.stack.imgur.com/8SHR1.gif) 3 | 4 | Android Button that morphs into a loading progress bar. 5 | - Fully customizable in the XML 6 | - Really simple to use. 7 | - Makes your app looks cooler ;) 8 | 9 | ## Contents 10 | - [Installation](#installation) 11 | - [How to use / Sample](#how-to-use) 12 | - [Animate and revert animation](#animate-and-revert-animation) 13 | - [Show done animation](#show-done-animation) 14 | - [Revert the loading animation with different text or image](#revert-the-loading-animation-with-different-text-or-image) 15 | - [Configure XML](#configure-xml) 16 | - [Bugs and feedback](#bugs-and-feedback) 17 | - [Credits](#credits) 18 | 19 | ## Installation 20 | #### Set up the dependency 21 | 1. Add the mavenCentral() repository to your root build.gradle at the end of repositories: 22 | ``` 23 | allprojects { 24 | repositories { 25 | ... 26 | mavenCentral() 27 | } 28 | } 29 | ``` 30 | 2. Add the LoadingButton dependency in the build.gradle: 31 | ``` 32 | implementation group: 'com.apachat', name: 'loadingbutton-android', version: '1.0.11' 33 | ``` 34 | 35 | Badge: 36 | ----- 37 | [![Maven Central](https://img.shields.io/maven-central/v/com.apachat/loadingbutton-android.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.apachat%22%20AND%20a:%22loadingbutton-android%22) 38 | 39 | ## How to use 40 | 41 | ### Animate and revert animation 42 | 43 | Add the button in your layout file and customize it the way you like it. 44 | 45 | ```xml 46 | 51 | app:spinning_bar_width="4dp" 52 | app:spinning_bar_color="#FFF" 53 | app:spinning_bar_padding="6dp" 54 | ``` 55 | 56 | Then, instanciate the button 57 | 58 | ```java 59 | CircularProgressButton btn = (CircularProgressButton) findViewById(R.id.btn_id) 60 | btn.startAnimation(); 61 | 62 | //[do some async task. When it finishes] 63 | //You can choose the color and the image after the loading is finished 64 | btn.doneLoadingAnimation(fillColor, bitmap); 65 | 66 | //[or just revert de animation] 67 | btn.revertAnimation(); 68 | ``` 69 | 70 | You can also add a callback to trigger an action after the startAnimation has finished resizing the button : 71 | 72 | ```kotlin 73 | btn.startAnimation { 74 | 75 | } 76 | ``` 77 | 78 | ### Switch to determinant progress 79 | You can switch between indeterminant and determinant progress: 80 | 81 | ```java 82 | circularProgressButton.setProgress(10) 83 | ... 84 | circularProgressButton.setProgress(50) 85 | ... 86 | circularProgressButton.setProgress(100) 87 | ``` 88 | 89 | ### - Show 'done' animation 90 | 91 | When the loading animation is running, call: 92 | 93 | ```java 94 | //Choose the color and the image that will be show 95 | circularProgressButton.doneLoadingAnimation(fillColor, bitmap); 96 | ``` 97 | 98 | ### - Revert the loading animation with different text or image 99 | 100 | ```kotlin 101 | progressButton.revertAnimation { 102 | progressButton.text = "Some new text" 103 | } 104 | ``` 105 | 106 | or 107 | 108 | ```kotlin 109 | progressImageButton.revertAnimation { 110 | progressImageButton.setImageResource(R.drawable.image) 111 | } 112 | ``` 113 | 114 | ### - Button State 115 | 116 | This button is a state machine and it changes its state during the animation process. The states are: 117 | 118 | #### Before Draw 119 | This state is the initial one, the button is in this state before the View is draw on the screen. This is the state when the button is accesed in the `onCreate()` of an Activity. 120 | 121 | #### Idle 122 | After the button is drawn in the screen, it gets in the `Idle` state. It is basically waiting for an animation. Call `startAnimation()` to start animations with this button. 123 | 124 | #### WAITING_PROGRESS 125 | If the `startAnimation()` is called before the `Idle` state, the button goes to this state. The button waits for the button to be drawn in the screen before start the morph animation. 126 | 127 | #### MORPHING 128 | The button stays in this state during the morphing animation. 129 | 130 | #### PROGRESS 131 | After the morph animation, the button start the progress animation. From this state the `done` and `revert` animations can happen. 132 | 133 | #### MORPHING_REVERT 134 | The button stays in this state during the morphing animation reversal. 135 | 136 | #### WAITING_DONE 137 | If the `doneLoadingAnimation(fillColor: Int, bitmap: Bitmap)` is called when the button is still morphing, it enter in this state. The button waits for the morph animation to complete and then start the done animation. 138 | 139 | #### DONE 140 | The button enters this state when the `doneLoadingAnimation` finishes. 141 | 142 | #### WAITING\_TO\_STOP 143 | The button enters this state when the `stopAnimation()` is called before the morph state is completed. The button waits for the morph animation to complete and the stops further animations. 144 | 145 | #### STOPPED 146 | The button enters this state after `stopAnimation()` when the button is not morphing. 147 | 148 | ## Configure XML 149 | 150 | - `app:spinning_bar_width` : Changes the width of the spinning bar inside the button 151 | - `app:spinning_bar_color`: Changes the color of the spinning bar inside the button 152 | - `app:spinning_bar_padding`: Changes the padding of the spinning bar in relation of the button bounds. 153 | - `app:initialCornerAngle`: The initial corner angle of the animation. Insert 0 if you have a square button. 154 | - `app:finalCornerAngle`: The final corner angle of the animation. 155 | 156 | ## Problems and troubleshooting 157 | 158 | ### Animation 159 | This library only works with selector as the background, but not with shape as the root tag. Please put your shape inside a selector, like this: 160 | 161 | ``` 162 | 163 | 164 | 165 | 166 | 167 | 168 | 170 | 171 | 172 | 173 | ``` 174 | *I still need to debug this problem.* 175 | 176 | 177 | ### Manifest merge 178 | 179 | This library only supports androidx since prior the version 2.0.0. So don't try to use it with the old Support Library. Use androidx instead. 180 | 181 | ## Bugs and Feedback 182 | 183 | For bugs, feature requests, and discussion please use [GitHub Issues](https://github.com/FarhamHosseini/LoadingButton/issues). 184 | 185 | ### And that's it! Enjoy! 186 | --------------------------------------------------------------------------------