Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / AndroidNotes.md
Created February 3, 2019 09:22
Android things I just can't seem to keep straight.

Android things I just can't seem to keep straight.

Theme Variants

  • Light means your environment is light - daytime.
  • Dark means your environment is dark - night time.

Colors

  • colorPrimary is the app bar
  • colorPrimaryDark is the status bar
@alwarren
alwarren / colors_android_api_28.xml
Last active November 13, 2021 17:19
Android Colors
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/colors.xml
**
** Copyright 2006, The Android Open Source Project
**
** 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
**
@alwarren
alwarren / build.gradle
Last active February 6, 2019 01:43
Kotlin Build System
import java.text.DateFormat
import java.text.SimpleDateFormat
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
apply from: 'buildsystem/dependencies.gradle'
addRepos(repositories)
dependencies {
classpath deps.android_gradle_plugin
classpath deps.kotlin.kotlin_gradle_plugin