Skip to content

Instantly share code, notes, and snippets.

View ashughes's full-sized avatar

Andrew Hughes ashughes

  • Steadfast Innovation, LLC
  • San Luis Obispo, CA
  • X @ashughes88
View GitHub Profile
@ashughes
ashughes / Data.kt
Created June 17, 2019 23:59
WorkManager data extensions to add require*() methods
inline fun Data.requireBoolean(key: String): Boolean = keyValueMap[key] as? Boolean ?: throw IllegalArgumentException("Missing boolean value for key: $key")
inline fun Data.requireBooleanArray(key: String): BooleanArray = getBooleanArray(key) ?: throw IllegalArgumentException("Missing boolean array value for key: $key")
inline fun Data.requireByte(key: String): Byte = keyValueMap[key] as? Byte ?: throw IllegalArgumentException("Missing byte value for key: $key")
inline fun Data.requireByteArray(key: String): ByteArray = getByteArray(key) ?: throw IllegalArgumentException("Missing byte array value for key: $key")
inline fun Data.requireInt(key: String): Int = keyValueMap[key] as? Int ?: throw IllegalArgumentException("Missing int value for key: $key")
inline fun Data.requireIntArray(key: String): IntArray = getIntArray(key) ?: throw IllegalArgumentException("Missing int array value for key: $key")
inline fun Data.requireLong(key: String): Long = keyValueMap[key] as? Long ?: throw IllegalArgumentException("M
@ashughes
ashughes / Playground.kt
Created June 17, 2019 21:39
WorkContinuation extension that maps output data to input data
fun test(context: Context) {
val backupWorker = OneTimeWorkRequestBuilder<BackupWorker>()
.setConstraints(
Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build()
)
.setInputData(workDataOf(BackupWorker.KEY_BACKUP_NAME to "backupName"))
.build()
@ashughes
ashughes / RecyclerActivity.java
Created May 20, 2015 21:37
RecyclerView Scroll on Insert Issue
package com.steadfastinnovation.recyclerissue;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;