Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / WorkManagerExample.kt
Created September 15, 2019 16:10
Unique work chain
WorkManager.getInstance()
.beginUniqueWork(Constants.UNIQUE_NAME, ExistingWorkPolicy.REPLACE, task1)
.then(task2)
.then(task3)
.enqueue()
UUID syncWorkId = syncOnlyOnceWork.getId();
@magdamiu
magdamiu / WorkManagerExample.kt
Created September 15, 2019 16:29
BackoffPolicy example
// add initial delay only for OneTimeWorkRequest
val syncOnlyOnce = OneTimeWorkRequestBuilder<SyncWorker>()
.setInitialDelay(15, TimeUnit.MINUTES)
.build()
// backoff delay and policy
val syncOnlyOnce = OneTimeWorkRequestBuilder<SyncWorker>()
.setBackoffCriteria(BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MICROSECONDS)
@magdamiu
magdamiu / WorkManagerExample.kt
Last active September 15, 2019 16:36
Chained tasks and parallel chains
val leftChain = WorkManager.getInstance()
.beginWith(task1)
.then(task2)
val rightChain = WorkManager.getInstance()
.beginWith(task3)
.then(task4)
val resultChain = WorkContinuation
.combine(listOf(leftChain, rightChain))
.then(task5)
resultChain.enqueue()
@magdamiu
magdamiu / KotlinBasics.kt
Created November 17, 2019 20:59
val vs var
// immediate assignment
val countOfEvenNumbers: Int = 10
// `Int` type is inferred
var sumOfNumbers = 0
// type required when no initializer is provided
var countOfPrimeNumbers: Int
// deferred assignment
@magdamiu
magdamiu / KotlinBasics.kt
Created November 17, 2019 21:04
Types in Kotlin
// Kotlin Numeric Types Examples
val myByte: Byte = 10
val myShort: Short = 125
val myInt = 3000
val myLong = 3000L // The suffix 'L' is used to specify a long value
val myFloat = 123.45f // The suffix 'f' or 'F' represents a Float
val myDouble = 321.05
@magdamiu
magdamiu / KotlinBasics.kt
Last active November 17, 2019 21:25
Array in Kotlin
var numbers = arrayOf(1, 2, 3, 4, 5)
var colors = arrayOf("red", "blue", "pink", "yellow")
// primitive arrays
val myCharArray = charArrayOf('K', 'O', 'T', 'L', 'I', 'N') // CharArray (corresponds to Java 'char[]')
val myIntArray = intArrayOf(1, 3, 5, 7, 9, 11) // IntArray (corresponds to Java 'int[]')
// Array constructor
val numbersArray = Array(8, { i -> i * 2 }) // 0 2 4 6 8 10 12 14
@magdamiu
magdamiu / KotlinBasics.kt
Created November 17, 2019 21:30
String interpolation
val firstWord = "Learn "
val secondWord = "Kotlin"
var bothWords = "$firstWord $secondWord"
println("$bothWords has ${bothWords.length}")
println(""""$bothWords" has ${bothWords.length}""")
@magdamiu
magdamiu / KotlinBasics.kt
Created November 17, 2019 21:35
if/else
val number = 0
val result = if (number > 0) {
"Positive number"
} else if (number < 0) {
"Negative number"
} else {
"Zero"
}
println(result) // => Zero
@magdamiu
magdamiu / KotlinBasics.kt
Last active November 17, 2019 21:36
when
val firstValue = 6
val secondValue = 3
val operator = "/"
val resultOfOperation = when (operator) {
"+" -> firstValue + secondValue
"-" -> firstValue - secondValue
"*" -> firstValue * secondValue
"/" -> firstValue / secondValue
else -> "$operator operator is invalid."
}