Skip to content

Instantly share code, notes, and snippets.

import java.text.DateFormat
import java.text.SimpleDateFormat
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// your other gradle code here
static def getDateTime() {
DateFormat df = new SimpleDateFormat("YYYYMMddHHmm");
return df.format(new Date());

Task: Manually add a Java module in Android Studio

Steps

  1. Switch to Project view.
  2. In the project root folder, create a folder with a unique identifiable name for the module. This folder becomes the root folder for the module.
  3. Within the module's root folder, create a src/main/java folder.
  4. Within the module's root folder, create a build.gradle file.
  5. Within the module's build.gradle, insert the following two lines:
@alwarren
alwarren / Extensions.kt
Last active April 9, 2018 16:21
Using DiffUtil in Android RecyclerView
// See https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00
fun <T> RecyclerView.Adapter<*>.autoNotify(oldList: List<T>, newList: List<T>, compare: (T, T) -> Boolean) {
val diff = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return compare(oldList[oldItemPosition], newList[newItemPosition])
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
@alwarren
alwarren / SSLUtils.kt
Last active March 24, 2018 17:58
Handle TLS in OkHttp3 client on pre-Lollipop versions of Android
import android.os.Build
import android.util.Log
import okhttp3.ConnectionSpec
import okhttp3.OkHttpClient
import okhttp3.TlsVersion
import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import java.security.KeyStore
import okhttp3.*
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
import java.io.File
import java.io.IOException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
@alwarren
alwarren / android.library.build.gradle
Last active March 17, 2018 01:20
Kotlin build.gradle (Android Module)
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
@alwarren
alwarren / Endpoint.kt
Created March 16, 2018 10:45
A helper class for creating REST endpoints.
/**
* A helper class for building REST API endpoints.
*/
class Endpoint(
val path: String,
val query: String,
val value: String) {
// Returns a list of objects containing a subset of object properties
class Filter {
@alwarren
alwarren / States-v3.md
Created March 10, 2018 09:34 — forked from andymatuschak/States-v3.md
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@alwarren
alwarren / Connectivity.java
Created March 8, 2018 15:45 — forked from emil2k/Connectivity.java
Android utility class for checking device's network connectivity and speed.
/*
* Copyright (c) 2017 Emil Davtyan
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
@alwarren
alwarren / ExtensionsSnackbar.kt
Last active March 8, 2018 05:22 — forked from antoniolg/HomeActivity.kt
Snackbar extensions on Kotlin, to create a useful small DSL.
fun Context.colorInt(color: Int) = ContextCompat.getColor(this, color)
inline fun View.snack(@IntegerRes messageRes: Int, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
snack(resources.getString(messageRes), length, f)
}
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
val snack = Snackbar.make(this, message, length)
snack.f()
snack.show()