Skip to content

Instantly share code, notes, and snippets.

@afollestad
Created November 30, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afollestad/2c8bef0c700cfa2e71cc76795fd78464 to your computer and use it in GitHub Desktop.
Save afollestad/2c8bef0c700cfa2e71cc76795fd78464 to your computer and use it in GitHub Desktop.
import android.content.ContentValues
/**
* Returns a [ContentValues] instance which contains only values that have changed between
* the receiver (original) and parameter (new) instances.
*/
fun ContentValues.diffFrom(contentValues: ContentValues): ContentValues {
val diff = ContentValues()
for ((name, oldValue) in this.valueSet()) {
val newValue = contentValues.get(name)
if (newValue != oldValue) {
diff.putAny(name, newValue)
}
}
return diff
}
/**
* Auto casts an [Any] value and uses the appropriate `put` method to store it
* in the [ContentValues] instance.
*/
fun ContentValues.putAny(
name: String,
value: Any?
) {
if (value == null) {
putNull(name)
return
}
when (value) {
is String -> put(name, value)
is Byte -> put(name, value)
is Short -> put(name, value)
is Int -> put(name, value)
is Long -> put(name, value)
is Float -> put(name, value)
is Double -> put(name, value)
is Boolean -> put(name, value)
is ByteArray -> put(name, value)
else -> throw IllegalArgumentException("ContentValues can't hold $value")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment