Skip to content

Instantly share code, notes, and snippets.

import java.util.Scanner
fun staircase(n: Int): Unit {
for (y in 1..n) {
var line = ""
for (x in 1..n) {
if (x <= n-y)
line += " "
else
line += "#"
import java.util.Scanner
fun main(args: Array<String>) {
val inputReader = Scanner(System.`in`)
val input = inputReader.nextLine()
val sizeOfStairs = input.toInt()
StaircaseBuilder()
.build(sizeOfStairs)
class TransactionListViewModel @Inject constructor(
private val repository: TransactionListRepository
) : ViewModel() {
val transactions = MediatorLiveData<Resource<List<Transaction>>>()
private val retryTransactionsTrigger = MutableLiveData<Nothing>()
private val transactionListFilter = MutableLiveData<TransactionListFilter>()
init {
transactionListFilter.value = TransactionListFilter()
@Benjiko99
Benjiko99 / GsonGenericAdapter.kt
Created November 17, 2018 15:40
Serialization and deserialization of a generic type in gson
data class UniqueIdentifier<out T>(val value: T)
typealias TransactionId = UniqueIdentifier<Int>
class UniqueIdentifierAdapter : JsonSerializer<UniqueIdentifier<*>>, JsonDeserializer<UniqueIdentifier<*>> {
override fun serialize(
src: UniqueIdentifier<*>,
typeOfSrc: Type,
context: JsonSerializationContext
@Benjiko99
Benjiko99 / gradle.properties
Created October 7, 2019 18:44
Gradle Properties for faster build times.
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.caching=true
android.enableBuildCache=true
@Benjiko99
Benjiko99 / DbMigrationsHelper.kt
Last active February 2, 2022 18:16
Android Room Database migration helper for ALTER TABLE
object Example {
fun alterTableUsage(database: SupportSQLiteDatabase) {
DbMigrationsHelper.alterTable(
db = database,
tableName = "Reservations",
columns = mapOf(
"id INTEGER".toExisting(), // Retains without changes
"title TEXT".toExisting("name"), // Renames column "name" to "title"
"description TEXT".toNothing(), // Adds a new column
@Benjiko99
Benjiko99 / LiveDataZipExtensions.kt
Last active June 15, 2020 12:38
LiveData Zip Extensions, for awaiting data from multiple sources before doing an operation on them.
data class Quadruple<out A, out B, out C, out D>(val first: A, val second: B, val third: C, val forth: D)
data class Quintuple<out A, out B, out C, out D, out E>(val first: A, val second: B, val third: C, val fourth: D, val fifth: E)
/** A shorthand for a null LiveData object */
val NULL_LD = null as LiveData<Nothing>?
fun <X, Y> LiveData<X>.map(mapFunction: (X) -> Y): LiveData<Y> {
return Transformations.map(this, mapFunction)
}
@Benjiko99
Benjiko99 / JsonNullableAdapter.kt
Created December 6, 2019 09:59
Moshi Adapter. Fields annotated with @JsonNullable will be serialized as `null` instead of being ignored.
@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class JsonNullable
/**
* Fields annotated with @JsonNullable will be serialized as `null` instead of being ignored.
*/
class JsonNullableAdapter : JsonAdapter.Factory {
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<Any>? {
@Benjiko99
Benjiko99 / CredentialsValidator.kt
Last active November 22, 2020 14:16
Credentials Validator
/**
* Validates login credentials.
*
* To use it, call [validate] with your [Credentials] and check the returned [ValidationResult].
*
* To add new validations, define the rules which need to be satisfied
* and the errors that can be thrown in response to dissatisfied rules.
*
* For each field in your login form define a mapping of [Rule]s to [Error]s
* which apply to the given field.
@Benjiko99
Benjiko99 / AndroidManifest.xml
Last active December 24, 2021 10:49
Intent Chooser for Email apps
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<!-- Let's us query installed apps so we can open them (required on Android 11) -->
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />