Skip to content

Instantly share code, notes, and snippets.

View terrakok's full-sized avatar
🔧
KMM

Konstantin terrakok

🔧
KMM
View GitHub Profile
@terrakok
terrakok / NanoRedux.kt
Created September 13, 2020 19:10
Very lighweight and simple redux implementation for android apps.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import timber.log.Timber
interface State
interface Action
interface Effect
interface Screen<T : State> {
@terrakok
terrakok / AndroidBaseFragment.kt
Last active September 14, 2020 11:49
Android fragment with real lifecycle
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
open class BaseFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLayoutId) {
protected lateinit var runId: String
private set
private var instanceStateSaved: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
import io.ktor.utils.io.core.String
private const val BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
private val BASE64_INVERSE_ALPHABET = IntArray(256) { BASE64_ALPHABET.indexOf(it.toChar()) }
fun String.encodeBase64(): String = String(encodeBase64ToByteArray())
fun String.encodeBase64ToByteArray(): ByteArray = encodeToByteArray().encodeBase64()
fun ByteArray.encodeBase64ToString(): String = String(encodeBase64())
fun String.decodeBase64(): String = String(decodeBase64ToByteArray())
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 18.09.17.
*/
class SmartField<T: Any>(
private val editText: EditText,
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.08.17.
*/
class SimpleDividerDecorator(
private val dividerSizePx: Int,
dividerColor: Int
) : RecyclerView.ItemDecoration() {
private val paint = Paint().apply { color = dividerColor }
fun Resources.color(colorRes: Int) =
if (Build.VERSION.SDK_INT >= 23) {
this.getColor(colorRes, null)
} else {
this.getColor(colorRes)
}
fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.arellomobile.mvp.MvpDelegate
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.OnMapReadyCallback
/*
* Copyright (C) 2016 Jeff Gilfelt.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software