Skip to content

Instantly share code, notes, and snippets.

@ericksli
ericksli / DemoActivity.kt
Last active September 3, 2018 15:00
Simplify the creation of ViewModel #android #kotlin
class DemoActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = viewModel(MyViewModel.Factory(application))
setContentView(R.layout.activity_demo)
}
}
@ericksli
ericksli / ReactNativeExtensions.kt
Last active March 20, 2024 19:51
React Native Kotlin extension functions for creating WritableMap and WritableArray #android #react-native #kotlin
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
fun writableMapOf(vararg values: Pair<String, *>): WritableMap {
val map = Arguments.createMap()
for ((key, value) in values) {
when (value) {
null -> map.putNull(key)
is Boolean -> map.putBoolean(key, value)
@ericksli
ericksli / DividerItemDecoration.kt
Last active September 3, 2018 14:59
Draw bottom divider in RecyclerView with customisable show/hide divider and paddings for each item #android
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.support.annotation.ColorRes
import android.support.v4.content.res.ResourcesCompat
import android.support.v4.view.ViewCompat
import android.support.v7.widget.RecyclerView
import org.jetbrains.anko.dip
@ericksli
ericksli / lr-station-platform.json
Last active September 2, 2017 14:36
Hong Kong MTR Light Rail stations and platforms
[
{
"code": "001",
"initial": "FEP",
"titleZh": "屯門碼頭",
"titleEn": "Tuen Mun Ferry Pier",
"latitude": 22.37284525,
"longitude": 113.9661371,
"fareZone": "1",
"platforms": [
@ericksli
ericksli / SemVer.kt
Last active June 18, 2017 13:29
Kotlin data object for Semantic Versioning (SemVer) 2.0.0 specification
/**
* Version number in [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html) specification (SemVer).
*
* @property major major version, increment it when you make incompatible API changes.
* @property minor minor version, increment it when you add functionality in a backwards-compatible manner.
* @property patch patch version, increment it when you make backwards-compatible bug fixes.
* @property preRelease pre-release version.
* @property buildMetadata build metadata.
*/
data class SemVer(
@ericksli
ericksli / SharedPreferencesDelegates.kt
Last active September 3, 2018 14:58
Kotlin delegates for Android shared preferences #android #kotlin
import android.annotation.SuppressLint
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class BooleanSharedPreferencesDelegate(val preferences: SharedPreferences, val key: String, val defValue: Boolean, val writeSynchronously: Boolean = false) : ReadWriteProperty<Any, Boolean> {
override fun getValue(thisRef: Any, property: KProperty<*>): Boolean = preferences.getBoolean(key, defValue)
@SuppressLint("CommitPrefEdits")
@ericksli
ericksli / TimberJava.xml
Last active February 11, 2021 18:19
Timber Android Studio live template for Java and Kotlin #kotlin #android
<templateSet group="TimberJava">
<template name="timd" value="timber.log.Timber.d(&quot;$METHOD_NAME$: $content$&quot;);" description="Timber.d(String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="methodName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_STATEMENT" value="true" />
</context>
</template>
<template name="time" value="timber.log.Timber.e($exception$, &quot;$METHOD_NAME$: $content$&quot;);" description="Timber.e(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="exception" expression="" defaultValue="e" alwaysStopAt="true" />
@ericksli
ericksli / doze.sh
Created April 12, 2017 08:25
Check device idle state
#!/bin/bash
while true
do
adb shell dumpsys deviceidle step
sleep 1
done
@ericksli
ericksli / build.gradle
Created August 29, 2016 04:09
Android app show Git commit hash as version name
import java.util.regex.Pattern
apply plugin: 'com.android.application'
/**
* Extract Git commit hash of HEAD with number of changed files
*/
def getGitHashVersionName = {
try {
def hashOutput = new ByteArrayOutputStream()
@ericksli
ericksli / diff.sh
Created April 1, 2016 02:13
Diff PNG files between two folders and list out the images that are different using ImageMagick
#!/bin/bash
files=`find $1/*.png`
while read -r srcfile; do
name=`basename $srcfile`
targetfile="${2}/${name}"
result=`compare -metric RMSE $srcfile $targetfile /dev/null 2>&1;`
if [[ $result != 0* ]]; then
echo "${name} = ${result}"