This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
fun submitPost(applicationContext: Context, duration: Int, successPercent:Int, tagName: String): UUID { | |
val constraints = Constraints.Builder() | |
.setRequiredNetworkType(NetworkType.CONNECTED) | |
.build() | |
val restWorkRequest = OneTimeWorkRequestBuilder<PostToServerWorker>() | |
.setInputData(workDataOf(JOB_TIME to duration, | |
JOB_SUCCESS_CHANCE to successPercent, | |
ATTEMPT_RETRIES to 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostToServerWorker(appContext: Context, workerParams: WorkerParameters) | |
: CoroutineWorker(appContext, workerParams) { | |
private val random by lazy { Random(Date().time)} | |
override suspend fun doWork(): Result { | |
// Read input data | |
val jobDuration = inputData.getInt(JOB_TIME, 100) | |
val jobSuccessRate = inputData.getInt(JOB_SUCCESS_CHANCE, 0) | |
val maxAttempts = inputData.getInt(ATTEMPT_RETRIES, 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun performCheck(context: Context, callback: Callback, defaultEntryPoint: Boolean = false) { | |
compositeDisposable.add(job(1).subscribeOn(Schedulers.io()) | |
.mergeWith(job(2).subscribeOn(Schedulers.io())) | |
.mergeWith(job(3).subscribeOn(Schedulers.io())) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe( | |
{ callback.pass() | |
Timber.d("Job OnComplete") | |
}, | |
{ callback.fail(Callback.FailReason.LoggedOut) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test { | |
private fun returnSomething(str: String):String { | |
println("evaluating $str") | |
return str | |
} | |
val testEquals = returnSomething("testEquals") | |
val testLazy by lazy { returnSomething("testLazy") } | |
val testGetBrackets: String | |
get() { return returnSomething("testGetBrackets") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Silly simple way of making new array | |
fun catTwoIntArraysSimple(array1 :IntArray, array2 :IntArray) = array1 + array2 | |
inline fun <reified T> catTwoArraysSimple(array1 :Array<T>, array2 :Array<T>) = array1 + array2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a class that mimics an immutable Array<> | |
class CatTwoArrays<T>(internal val array1 : Array<T>, internal val array2 : Array<T>) { | |
public operator fun get(index: Int): T { | |
return if (index < array1.size) array1[index] else array2[index - array1.size] | |
} | |
public val size: Int = array1.size + array2.size | |
internal inner class CatTwoArraysIterator() : Iterator<T> { | |
internal var position = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copy using the lamda in the constructor | |
inline fun <reified T> catTwoArrays3(array1 :Array<T>, array2 :Array<T>) : Array<T> { | |
return Array<T>(array1.size + array2.size, | |
{ if (it<array1.size) array1[it] else array2[it-array1.size]}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copy using the lamda in the constructor | |
fun catTwoIntArrays3(array1 :IntArray, array2 :IntArray) : IntArray { | |
return IntArray(array1.size + array2.size, | |
{ if (it<array1.size) array1[it] else array2[it-array1.size]}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copy using for loops | |
fun catTwoIntArrays2(array1 :IntArray, array2 :IntArray) : IntArray { | |
val newArray = IntArray(array1.size + array2.size ) | |
for ((index, value) in array1.withIndex()) { | |
newArray[index] = value | |
} | |
for ((index, value) in array2.withIndex()) { | |
newArray[index + array1.size] = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copy using arraycopy | |
fun catTwoIntArrays1(array1 :IntArray, array2 :IntArray) : IntArray { | |
val newArray = IntArray(array1.size + array2.size) | |
System.arraycopy(array1, 0, newArray, 0 , array1.size) | |
System.arraycopy(array2, 0, newArray, array1.size , array2.size) | |
return newArray | |
} |
NewerOlder