Skip to content

Instantly share code, notes, and snippets.

@alwarren
alwarren / AbstractListRecycler.kt
Last active January 31, 2019 22:58
Kotlin Generic RecyclerView.Adapter with Listener
abstract class AbstractListRecycler<T> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
abstract fun layoutId(): Int
var collection: List<T> by Delegates.observable(emptyList()) {
_, _, _ -> notifyDataSetChanged()
}
internal var clickListener: (T) -> Unit = { }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
@alwarren
alwarren / KotlinRecycler.kt
Last active April 15, 2019 10:32
Kotlin RecyclerView.Adapter - credits to Fernando Cejas
class MyAdapter : RecyclerView.Adapter<ViewHolder>() {
var data: List<MyClass> by Delegates.observable(emptyList()) {
_, _, _ -> notifyDataSetChanged()
}
internal var clickListener: (MyClass) -> Unit = { }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ViewHolder(parent.inflate(R.layout.data_row))
@alwarren
alwarren / Mock.kt
Last active January 11, 2019 14:23
Kotlin static mocks because I'm too lazy to use frameworks.
object Mock {
val currentWeatherEntry by lazy { currentWeatherEntryMock() }
val condition by lazy { conditionMock() }
val weatherLocation by lazy { weatherLocationMock() }
val currentWeatherResponse by lazy { currentWeatherResponseMock(currentWeatherEntry, weatherLocation) }
const val string: String = "string"
const val int:Int = 1
const val long: Long = 1L
const val double:Double = 1.0
@alwarren
alwarren / LoggingActivity.kt
Last active January 2, 2019 20:30
Base classes for logging lifecycle method calls
package com.ntxdroid.forecastv3.ui.base
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
@SuppressLint("Registered")
open class LoggingActivity : AppCompatActivity() {
@alwarren
alwarren / Kotlin2D.md
Created November 28, 2018 16:40
Represent a square matrix using only List. Access to the matrix must use one-based indices.

Problem statement:

Represent a square matrix using only List. Access to the matrix must use one-based indices.

Given:

interface Cell {
    val i: Int
 val j: Int
@alwarren
alwarren / Either.kt
Created October 19, 2018 07:24
A Kotlin Monad
/**
* Copyright (C) 2018 Fernando Cejas Open Source Project
* Modifications Copyright (C) 2018 Al Warren
*
* 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
*
@alwarren
alwarren / AndroidCoroutineExample.kt
Created October 10, 2018 05:45
From RxJava async to Coroutines in two lines of code.
val job = GlobalScope.async(Dispatchers.Default, CoroutineStart.DEFAULT, { MyApi.execute() })
GlobalScope.launch(Dispatchers.Main, CoroutineStart.DEFAULT, { updateUi(job.await()) })
@alwarren
alwarren / TimberDebugTree.kt
Last active October 3, 2018 16:25
A Custom Timber Debug Tree with Prepended Tags
import android.os.Build
import timber.log.Timber
const val TIMBER_TAG = "--->"
const val MAX_TAG_LENGTH = 23
class TimberDebugTree : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String? {
val tag = TIMBER_TAG + super.createStackElementTag(element)
@alwarren
alwarren / TimberLoggingActivity.kt
Last active October 3, 2018 17:50
Android Timber Logging Activity/Fragment
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import timber.log.Timber
open class TimberLoggingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
Timber.d("onCreate is called")
super.onCreate(savedInstanceState, persistentState)
}
@alwarren
alwarren / ColorInterpolator.java
Last active February 14, 2019 13:29
Calculates a color value using a range pointer that lies somewhere between two colors. Originaly used an Android ProgressBar as the pointer.
/*
* Copyright (c) 2016. Al Warren.
*
* 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