Skip to content

Instantly share code, notes, and snippets.

@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 / 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
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 / 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 {

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:
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());
@alwarren
alwarren / SingletonClass.java
Created May 23, 2018 17:33
Java thread-safe singleton
public class SingletonClass {
// dummy object for synchronization
private static final Object sLock = new Object();
// single instance of the class
private static SingletonClass INSTANCE;
public static SingletonClass getInstance() {
// force threads to wait until synchronized block completes
synchronized (sLock) {
@alwarren
alwarren / ResponseObject.kt
Last active June 11, 2018 19:29
A generic Kotlin wrapper class for determining the success or failure of some method
/**
* A generic wrapper class for determining the success or failure of some method
*
* In the business logic, examine the instance class to determine success or failure
*
* Example -
* <pre>
* <code>
* val result: ResponseObject = SomeRepository().getData(true)
* if (result is ResponseObject.Success) {
@alwarren
alwarren / SwitchMapViewModel.kt
Created June 13, 2018 16:25
Android ViewModel Using SwitchMap
class SwitchMapViewModel @Inject constructor(private val dataSource: Data) : ViewModel() {
// observed by ViewModel as a trigger to retrieve data from the source
private var liveTrigger: MutableLiveData<String> = MutableLiveData()
// observed by Activity/Fragment or some other class as a trigger to do something with the data
private var liveData: LiveData<String>
init {
// default observable trigger