Created
November 30, 2018 22:21
-
-
Save afollestad/2c8bef0c700cfa2e71cc76795fd78464 to your computer and use it in GitHub Desktop.
This file contains 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
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