Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Feeding my inner nerd

Sławomir Czerwiński sczerwinski

👨‍💻
Feeding my inner nerd
View GitHub Profile
@sczerwinski
sczerwinski / AndroidString.kt
Created Jul 23, 2019
Representation of Android String, which is either a string or a string resource
View AndroidString.kt
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 / NotNullAssertionOperator.kt
Created Mar 10, 2019
Comparison of `!!` operator, `requireNotNull()`, `checkNotNull()` and null-safe operators: `?.`, `?:`
View NotNullAssertionOperator.kt
import org.junit.Test
class NotNullAssertionOperator {
@Test
@Throws(Exception::class)
fun exclamation() {
fun square(x: Double?): Double = x!!.times(x)
square(null)
@sczerwinski
sczerwinski / WeakReferenceProperty.kt
Created Sep 14, 2017
Weak reference property delegate in Kotlin
View WeakReferenceProperty.kt
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 / AndroidMatchers.kt
Created Jun 15, 2017
Kotlin functions creating custom Hamcrest matchers for Android
View AndroidMatchers.kt
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 / AndroidTestRules.kt
Last active Jun 12, 2017
Kotlin functions providing ActivityTestRule and IntentsTestRule with a simplified syntax.
View AndroidTestRules.kt
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 / SSL.kt
Last active Jan 25, 2017
Kotlin port of the solution proposed by @naderghanabri: http://stackoverflow.com/a/28787883/4568679
View SSL.kt
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 / MaterialColor.kt
Created Jul 22, 2016
Material Design colors palette in Kotlin
View MaterialColor.kt
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 / SquareMatrix.kt
Last active Jul 4, 2017
Functional square matrix
View SquareMatrix.kt
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)
}
You can’t perform that action at this time.