Skip to content

Instantly share code, notes, and snippets.

@umpteenthdev
umpteenthdev / MainActivity.kt
Created May 16, 2019 10:00
How to get height of status bar
class MainActivity : ActivityCompat {
private lateinit var bordersListener: ViewTreeObserver.OnGlobalLayoutListener
private var statusBarHeight = 0
override fun onCreate(savedInstanceState: Bundle?) {
val decorView = window.decorView
bordersListener = ViewTreeObserver.OnGlobalLayoutListener {
val r = Rect()
@umpteenthdev
umpteenthdev / GeoDecimalToDegreesConverter.kt
Created May 6, 2019 10:22
Convert decimal coordinates to degrees
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
private const val NORTH = "N"
private const val SOUTH = "S"
private const val EAST = "E"
private const val WEST = "W"
fun convertDecimalToDegrees(lat: Double, lng: Double): String = "${convertLat(lat)} ${convertLng(lng)}"
import android.annotation.SuppressLint
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.ViewTreeObserver
import androidx.core.view.updatePaddingRelative
class UnderKeyboardViewElevator(private val decorView: View, private val contentView: View) {
private var initialPaddingBottom: Int = contentView.paddingBottom
@umpteenthdev
umpteenthdev / Coordinate.kt
Created March 11, 2019 10:04
Base class that describes a coordinate (in decimal degrees)
import kotlin.math.*
const val EARTH_RADIUS_METERS = 6378137
data class Coordinate(
val latitude: Double,
val longitude: Double
) {
/**
@umpteenthdev
umpteenthdev / BigDecimalUtils.kt
Created February 22, 2019 10:18
Useful extension for `BigDecimal`
import java.math.BigDecimal
import java.math.RoundingMode
val BigDecimal.fractionLength: Int
get() =
stripTrailingZeros().toPlainString().substringAfterLast(".", "").length
infix fun BigDecimal.isValueEquals(another: BigDecimal?): Boolean {
if (another == null) return false
return compareTo(another) == 0
@umpteenthdev
umpteenthdev / Additional info.md
Last active March 14, 2019 05:18
File uploader, image compressor (Android). Kotlin. Coroutines.

Dependencies

implementation "androidx.exifinterface:exifinterface:1.0.0"

@umpteenthdev
umpteenthdev / RegexInputFilter.kt
Last active November 25, 2019 12:59
Android input filter implemented using regular expressions
import android.text.InputFilter
import android.text.Spanned
abstract class SuggestedInputFilter : InputFilter {
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
val suggestedResult = dest.replaceRange(dstart, dend, source)
val cancelResult = dest.slice(dstart until dend)
return if (accept(suggestedResult)) null else cancelResult
}
@umpteenthdev
umpteenthdev / VectorToBitmap.kt
Created February 16, 2019 06:40
How to get bitmap from vector
fun Context.vectorToBitmap(@DrawableRes id: Int): Bitmap {
val drawable = ContextCompat.getDrawable(this, id)!!
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
@umpteenthdev
umpteenthdev / Iso8601SimpleDateFormatParser.kt
Created January 25, 2019 07:32
How to parse ISO 8601 String to Date. SimpleDateFormat
import java.text.SimpleDateFormat
import java.util.*
fun toDate(text: String): Date {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ")
val modifiedText = text.substring(0..25) + "GMT" + text.substring(26)
return dateFormat.parse(modifiedText)
}
@umpteenthdev
umpteenthdev / SocketConnectionChecker.kt
Created January 22, 2019 04:32
How to check internet connection fastly
import java.net.InetSocketAddress
import java.net.Socket
class SocketConnectionChecker(
private val timeout: Int = 1000,
private val hostname: String = "8.8.8.8",
private val port: Port = Port.DNS
) {
fun isConnected(): Boolean =