Skip to content

Instantly share code, notes, and snippets.

View sczerwinski's full-sized avatar
👨‍💻
Feeding my inner nerd

Sławomir Czerwiński sczerwinski

👨‍💻
Feeding my inner nerd
View GitHub Profile
@sczerwinski
sczerwinski / SquareMatrix.kt
Last active July 4, 2017 18:14
Functional square matrix
package pl.info.czerwinski.geom
class SquareMatrix(val size: Int, private val elements: (Int, Int) -> Float) {
operator fun get(row: Int, col: Int): Float {
require(row in 0..size - 1) { "Row ${row} out of bounds: 0..${size - 1}" }
require(col in 0..size - 1) { "Column ${col} out of bounds: 0..${size - 1}" }
return elements(row, col)
}
@sczerwinski
sczerwinski / MaterialColor.kt
Created July 22, 2016 14:04
Material Design colors palette in Kotlin
package pl.info.czerwinski
enum class MaterialColor(val tones: Map<Int, Int> = emptyMap(), val accentTones: Map<Int, Int> = emptyMap(), val singleColor: Int? = null) {
RED(
tones = mapOf(
50 to 0xFFEBEE,
100 to 0xFFCDD2,
200 to 0xEF9A9A,
300 to 0xE57373,
400 to 0xEF5350,
@sczerwinski
sczerwinski / SSL.kt
Last active January 25, 2017 13:32
Kotlin port of the solution proposed by @naderghanabri: http://stackoverflow.com/a/28787883/4568679
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.*
object SSL {
fun trustAll() {
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, arrayOf(TrustAll), SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)
@sczerwinski
sczerwinski / AndroidTestRules.kt
Last active June 12, 2017 11:24
Kotlin functions providing ActivityTestRule and IntentsTestRule with a simplified syntax.
import android.app.Activity
import android.support.test.espresso.intent.rule.IntentsTestRule
import android.support.test.rule.ActivityTestRule
inline fun <reified T : Activity> activityTestRule(initialTouchMode: Boolean = false, launchActivity: Boolean = true) =
ActivityTestRule(T::class.java, initialTouchMode, launchActivity)
inline fun <reified T : Activity> intentsTestRule(initialTouchMode: Boolean = false, launchActivity: Boolean = true) =
IntentsTestRule(T::class.java, initialTouchMode, launchActivity)
@sczerwinski
sczerwinski / AndroidMatchers.kt
Created June 15, 2017 19:31
Kotlin functions creating custom Hamcrest matchers for Android
import android.view.View
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
inline fun <reified T : View> withType(): Matcher<View> = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description?) {
description?.appendText("with type: ${T::class.java.name}")
}
@sczerwinski
sczerwinski / WeakReferenceProperty.kt
Created September 14, 2017 09:10
Weak reference property delegate in Kotlin
class WeakReferenceProperty<T>(private val creator: () -> T) {
private var value: WeakReference<T> =
WeakReference(creator())
operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
value.get() ?: creator().also { value = WeakReference(it) }
}
private fun <T> weak(creator: () -> T) =
@sczerwinski
sczerwinski / NotNullAssertionOperator.kt
Created March 10, 2019 00:18
Comparison of `!!` operator, `requireNotNull()`, `checkNotNull()` and null-safe operators: `?.`, `?:`
import org.junit.Test
class NotNullAssertionOperator {
@Test
@Throws(Exception::class)
fun exclamation() {
fun square(x: Double?): Double = x!!.times(x)
square(null)
@sczerwinski
sczerwinski / AndroidString.kt
Created July 23, 2019 09:26
Representation of Android String, which is either a string or a string resource
import android.content.Context
import androidx.annotation.StringRes
import it.czerwinski.kotlin.util.Either
import it.czerwinski.kotlin.util.Left
import it.czerwinski.kotlin.util.Right
import it.czerwinski.kotlin.util.merge
typealias AndroidString = Either<Int, String>
fun AndroidString(string: String): AndroidString = Right(string)
@sczerwinski
sczerwinski / SIGNING.md
Created August 20, 2020 23:19
Creating signing key for Maven Central upload

Generate and publish key:

gpg --gen-key
gpg --list-keys --keyid-format 0xSHORT
gpg --list-secret-keys --keyid-format 0xSHORT
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys [KEY_ID]
gpg --keyserver http://keyserver.ubuntu.com:11371/ --send-keys [KEY_ID]

Create and encrypt Secure Ring File:

@sczerwinski
sczerwinski / LiveDataTransformations.kt
Last active December 18, 2020 22:58
Additional LiveData transformations. Now released as a library: https://github.com/sczerwinski/android-lifecycle
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Observer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext