Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
steadymoka committed Nov 2, 2019
1 parent 3426114 commit e9a9a55
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
1 change: 1 addition & 0 deletions res/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.appcompat:appcompat:1.1.0"
}
5 changes: 0 additions & 5 deletions res/src/main/java/land/moka/res/MyClass.kt

This file was deleted.

145 changes: 145 additions & 0 deletions res/src/main/java/land/moka/res/ViewExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package land.moka.res

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.EditText

inline fun View.visible() {
this.visibility = View.VISIBLE
}

inline fun View.gone() {
this.visibility = View.GONE
}

inline fun View.invisible() {
this.visibility = View.INVISIBLE
}

fun View.visibleFadeIn(duration: Long = 0L, onFinish: (() -> Unit)? = null) {
this.visible()
val fadeInAnimation = AlphaAnimation(0f, 1f)
fadeInAnimation.interpolator = AccelerateInterpolator()
fadeInAnimation.duration = duration

fadeInAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
}

override fun onAnimationEnd(p0: Animation?) {
onFinish?.invoke()
}

override fun onAnimationStart(p0: Animation?) {
}
})
postDelayed({ this.clearAnimation() }, fadeInAnimation.duration)
this.startAnimation(fadeInAnimation)
}

fun View.invisibleFadeOut(duration: Long = 0L, onFinish: (() -> Unit)? = null) {
this.invisible()

val fadeOutAnimation = AlphaAnimation(1f, 0f)
fadeOutAnimation.interpolator = AccelerateInterpolator()
fadeOutAnimation.duration = duration

fadeOutAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
}

override fun onAnimationEnd(p0: Animation?) {
onFinish?.invoke()
}

override fun onAnimationStart(p0: Animation?) {
}
})
postDelayed({ this.clearAnimation() }, fadeOutAnimation.duration)
this.startAnimation(fadeOutAnimation)
}

fun View.goneFadeOut(duration: Long = 0L, onFinish: (() -> Unit)? = null) {
this.gone()

val fadeOutAnimation = AlphaAnimation(1f, 0f)
fadeOutAnimation.interpolator = AccelerateInterpolator()
fadeOutAnimation.duration = duration

fadeOutAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
}

override fun onAnimationEnd(p0: Animation?) {
onFinish?.invoke()
}

override fun onAnimationStart(p0: Animation?) {
}
})
postDelayed({ this.clearAnimation() }, fadeOutAnimation.duration)
this.startAnimation(fadeOutAnimation)
}

fun toGone(vararg views: View) {
views.forEach { it.gone() }
}

fun toInvisible(vararg views: View) {
views.forEach { it.invisible() }
}

fun toInVisibleFadeOut(vararg views: View) {
views.forEach {
if (it.visibility == View.VISIBLE)
it.invisibleFadeOut()
}
}

fun toVisible(vararg views: View) {
views.forEach { it.visible() }
}

fun toVisibleFadeIn(vararg views: View) {
views.forEach { it.visibleFadeIn() }
}

fun View.visibleOrGone(isVisible: Boolean) {
if (isVisible)
this.visible()
else
this.gone()
}

// -

fun EditText.toCursorLast() {
this.setSelection(this.text.length)
}

fun EditText.countValid(cnt: Int, callback: (() -> Unit)? = null) {
this.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val text = this@countValid.text.toString()
val textCount = text.length

if (cnt < textCount) {
this@countValid.setText(text.substring(0, cnt))
this@countValid.setSelection(cnt)
if (null != callback) callback()
}
}

})
}

0 comments on commit e9a9a55

Please sign in to comment.