Skip to content

Instantly share code, notes, and snippets.

@smichel17
smichel17 / Logger.kt
Created March 5, 2017 08:08
Best of Anko and kotlin-logging
/*
* Copyright (c) 2017 Stephen Michel <s@smichel.me>
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
@wispborne
wispborne / Preferences.kt
Last active July 21, 2022 03:47
Android SharedPreferences helper class for Kotlin. Easy-to-use delegated properties, automatic database creation, and listening for property changes.
import android.content.Context
import android.content.SharedPreferences
import kotlin.reflect.KProperty
/**
* Represents a single [SharedPreferences] file.
*
* Usage:
*
* ```kotlin
@cbeyls
cbeyls / KotlinFunctions.md
Last active June 20, 2022 14:59
Comparison of Kotlin functions: also, apply, let, run, with
Function Function type Target passed as Returns
also Extension it Target
apply Extension this Target
let Extension it Block return value
run Extension this Block return value
with Regular this Block return value
/*
* Copyright (C) 2017 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@magneticflux-
magneticflux- / LiveDataUtils.kt
Last active February 3, 2023 19:08
Helpful Android-Kotlin LiveData utilities
//--------------------------------
// CHECK THE COMMENTS FOR UPDATES!
//--------------------------------
/*
* Copyright (C) 2017 Mitchell Skaggs, Keturah Gadson, Ethan Holtgrieve, Nathan Skelton, Pattonville School District
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@saschpe
saschpe / Enum.kt
Last active May 24, 2022 21:50
Kotlin enum class extension for Android (SharedPreference, Parcelable, Bundle, Intent)
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Parcel
inline fun <reified T : Enum<T>> Bundle.getEnum(key: String, default: T) =
getInt(key).let { if (it >= 0) enumValues<T>()[it] else default }
fun <T : Enum<T>> Bundle.putEnum(key: String, value: T?) =
putInt(key, value?.ordinal ?: -1)
fun withDrawable(@DrawableRes id: Int, @ColorRes tint: Int? = null, tintMode: PorterDuff.Mode = SRC_IN) = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("ImageView with drawable same as drawable with id $id")
tint?.let { description.appendText(", tint color id: $tint, mode: $tintMode") }
}
override fun matchesSafely(view: View): Boolean {
val context = view.context
val tintColor = tint?.toColor(context)
val expectedBitmap = context.getDrawable(id).tinted(tintColor, tintMode).toBitmap()
@bleeding182
bleeding182 / App.kt
Created March 30, 2018 17:52
Dagger 2 `@PerScreen` scope surviving configuration change
class App : Application() {
@Inject
lateinit var applicationInjector: ApplicationInjector
override fun onCreate() {
super.onCreate()
DaggerAppComponent.create().inject(this)
registerActivityLifecycleCallbacks(applicationInjector)
@NiPfi
NiPfi / FirestoreRepository.java
Created April 18, 2018 07:36
Basic implementation of a repository pattern for Firebase Firestore
package ch.jojoni.jamplan.model.repository;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
@JoseAlcerreca
JoseAlcerreca / Event.kt
Created April 26, 2018 10:25
An event wrapper for data that is exposed via a LiveData that represents an event.
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.