Skip to content

Instantly share code, notes, and snippets.

@hector6872
hector6872 / AspectRatioCardView.kt
Created May 9, 2018 16:21
AspectRatioCardView Android - Kotlin
class AspectRatioCardView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : CardView(context, attrs, defStyleAttr) {
private var ratio = 1.0f
init {
attrs?.let {
@hector6872
hector6872 / MultiState.kt
Last active April 24, 2018 14:53
MultiStateFrameLayout Kotlin Android
enum class MultiState {
CONTENT,
EMPTY,
LOADING,
ERROR
}
@hector6872
hector6872 / NoAnimationItemAnimator.kt
Created April 20, 2018 14:26
NoAnimationItemAnimator Kotlin Android RecyclerView
class NoAnimationItemAnimator : SimpleItemAnimator() {
override fun animateRemove(holder: RecyclerView.ViewHolder): Boolean {
dispatchRemoveFinished(holder)
return false
}
override fun animateAdd(holder: RecyclerView.ViewHolder): Boolean {
dispatchAddFinished(holder)
return false
}
@hector6872
hector6872 / StickySectionItemDecoration.kt
Last active March 22, 2019 09:54
StickySectionItemDecoration Kotlin
class StickySectionItemDecoration(
private val recyclerView: RecyclerView,
private val callback: Callback
) : RecyclerView.ItemDecoration() {
private var headerViewCache: View? = null
private var isInLayout = false
interface Callback {
fun isHeader(position: Int): Boolean
@hector6872
hector6872 / Base64.kt
Last active May 31, 2021 12:41
Base64.kt for Kotlin
fun String.encodeBase64ToString(): String = String(this.toByteArray().encodeBase64())
fun String.encodeBase64ToByteArray(): ByteArray = this.toByteArray().encodeBase64()
fun ByteArray.encodeBase64ToString(): String = String(this.encodeBase64())
fun String.decodeBase64(): String = String(this.toByteArray().decodeBase64())
fun String.decodeBase64ToByteArray(): ByteArray = this.toByteArray().decodeBase64()
fun ByteArray.decodeBase64ToString(): String = String(this.decodeBase64())
fun ByteArray.encodeBase64(): ByteArray {
val table = (CharRange('A', 'Z') + CharRange('a', 'z') + CharRange('0', '9') + '+' + '/').toCharArray()
@hector6872
hector6872 / Bus.kt
Created January 18, 2018 15:13
Simple Event Bus Kotlin
object Bus {
// val event = Event<Any>()
}
class Event<TYPE> {
private val handlers = arrayListOf<((TYPE) -> Unit)>()
operator fun plusAssign(handler: (TYPE) -> Unit) {
handlers.add(handler)
}
@hector6872
hector6872 / EditTextExtensions.kt
Created January 16, 2018 10:57
EditTextExtensions Kotlin
inline fun TextView.textWatcher(init: CustomTextWatcher.() -> Unit) = addTextChangedListener(CustomTextWatcher().apply(init))
@Suppress("unused")
class CustomTextWatcher : TextWatcher {
private var _beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
private var _afterTextChanged: ((Editable?) -> Unit)? = null
private var _beforeTextChangedShout: (() -> Unit)? = null
private var _onTextChangedShout: (() -> Unit)? = null
@hector6872
hector6872 / ClearableAutoCompleteTextView.kt
Last active November 29, 2024 12:09
ClearableAutoCompleteTextView Kotlin
class ClearableAutoCompleteTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.editTextStyle)
: AutoCompleteTextView(context, attrs, defStyleAttr) {
private val clearDrawable: Drawable? = ContextCompat.getDrawable(context, R.drawable.ic_clear)
init {
clearDrawable?.setBounds(0, 0, clearDrawable.intrinsicWidth, clearDrawable.intrinsicHeight)
setOnTouchListener { _, event ->
if (isClearDrawableVisible() && event.action == MotionEvent.ACTION_UP) {
@hector6872
hector6872 / linux.sh
Created January 7, 2018 14:32
MP4 to GIF
# change resolution
ffmpeg -i video_1920.mp4 -vf scale=300:534 video_300.mp4
# convert
ffmpeg -i video_300.mp4 final.gif
@hector6872
hector6872 / BaseActivity.kt
Last active November 16, 2017 09:31
MVP Kotlin
abstract class BaseActivity<MODEL : BaseModel<Bundle>, VIEW : BaseView, out PRESENTER : BasePresenter<MODEL, VIEW>> : KoinActivity() {
protected abstract val presenter: PRESENTER
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val arguments = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
val model = Class.forName(arguments.first().javaClass.toString().split(" ").last()).newInstance()
presenter.bind(model as MODEL, this as VIEW)
}