Skip to content

Commit

Permalink
feat(shared): complete docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gaebel committed Jul 5, 2021
1 parent 4ccf0d4 commit 948ff46
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
65 changes: 41 additions & 24 deletions kvault/src/androidMain/kotlin/com/liftric/kvault/KVault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey

actual open class KVault(private val context: Context) {
actual open class KVault(context: Context) {
private val encSharedPrefs: SharedPreferences

init {
Expand All @@ -23,6 +23,7 @@ actual open class KVault(private val context: Context) {
* Saves a string value in the SharedPreferences.
* @param key The key to store
* @param stringValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, stringValue: String): Boolean {
return encSharedPrefs
Expand All @@ -35,6 +36,7 @@ actual open class KVault(private val context: Context) {
* Saves an int value in the SharedPreferences.
* @param key The key to store
* @param intValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, intValue: Int): Boolean {
return encSharedPrefs
Expand All @@ -47,6 +49,7 @@ actual open class KVault(private val context: Context) {
* Saves a long value in the SharedPreferences.
* @param key The key to store
* @param longValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, longValue: Long): Boolean {
return encSharedPrefs
Expand All @@ -59,6 +62,7 @@ actual open class KVault(private val context: Context) {
* Saves a float value in the SharedPreferences.
* @param key The key to store
* @param floatValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, floatValue: Float): Boolean {
return encSharedPrefs
Expand All @@ -71,6 +75,7 @@ actual open class KVault(private val context: Context) {
* Saves a double value in the SharedPreferences.
* @param key The key to store
* @param doubleValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, doubleValue: Double): Boolean {
return encSharedPrefs
Expand All @@ -83,6 +88,7 @@ actual open class KVault(private val context: Context) {
* Saves a boolean value in the SharedPreferences.
* @param key The key to store
* @param boolValue The value to store
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun set(key: String, boolValue: Boolean): Boolean {
return encSharedPrefs
Expand All @@ -94,7 +100,7 @@ actual open class KVault(private val context: Context) {
/**
* Checks if object with key exists in the SharedPreferences.
* @param forKey The key to query
* @return True or false, depending on wether it is in the shared preferences or not
* @return True or false, depending on whether the value has been stored in the SharedPreferences
*/
actual fun existsObject(forKey: String): Boolean {
return encSharedPrefs.contains(forKey)
Expand All @@ -112,61 +118,66 @@ actual open class KVault(private val context: Context) {
/**
* Returns the int value of an object in the SharedPreferences.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored int value, or null if it is missing
*/
actual fun int(forKey: String): Int? {
if (existsObject(forKey)) {
return encSharedPrefs.getInt(forKey, Int.MIN_VALUE)
return if (existsObject(forKey)) {
encSharedPrefs.getInt(forKey, Int.MIN_VALUE)
} else {
null
}
return null
}

/**
* Returns the long value of an object in the SharedPreferences.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored long value, or null if it is missing
*/
actual fun long(forKey: String): Long? {
if (existsObject(forKey)) {
return encSharedPrefs.getLong(forKey, Long.MIN_VALUE)
return if (existsObject(forKey)) {
encSharedPrefs.getLong(forKey, Long.MIN_VALUE)
} else {
null
}
return null
}

/**
* Returns the float value of an object in the SharedPreferences.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored float value, or null if it is missing
*/
actual fun float(forKey: String): Float? {
if (existsObject(forKey)) {
return encSharedPrefs.getFloat(forKey, Float.MIN_VALUE)
return if (existsObject(forKey)) {
encSharedPrefs.getFloat(forKey, Float.MIN_VALUE)
} else {
null
}
return null
}

/**
* Returns the double value of an object in the SharedPreferences.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored double value, or null if it is missing
*/
actual fun double(forKey: String): Double? {
if (existsObject(forKey)) {
return Double.fromBits(encSharedPrefs.getLong(forKey, Double.MIN_VALUE.toRawBits()))
return if (existsObject(forKey)) {
Double.fromBits(encSharedPrefs.getLong(forKey, Double.MIN_VALUE.toRawBits()))
} else {
null
}
return null
}

/**
* Returns the boolean value of an object in the SharedPreferences.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored boolean value, or null if it is missing
*/
actual fun bool(forKey: String): Boolean? {
if (existsObject(forKey)) {
return encSharedPrefs.getBoolean(forKey, false)
return if (existsObject(forKey)) {
encSharedPrefs.getBoolean(forKey, false)
} else {
null
}
return null
}

/**
Expand All @@ -175,14 +186,20 @@ actual open class KVault(private val context: Context) {
* @return True or false, depending on whether the object has been deleted
*/
actual fun deleteObject(forKey: String): Boolean {
return encSharedPrefs.edit().remove(forKey).commit()
return encSharedPrefs
.edit()
.remove(forKey)
.commit()
}

/**
* Deletes all objects from the SharedPreferences.
* @return True or false, depending on whether the objects have been deleted
*/
actual fun clear(): Boolean {
return encSharedPrefs.edit().clear().commit()
return encSharedPrefs
.edit()
.clear()
.commit()
}
}
22 changes: 14 additions & 8 deletions kvault/src/iosMain/kotlin/com/liftric/kvault/KVault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ actual open class KVault(
* Saves a string value in the Keychain.
* @param key The key to store
* @param stringValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, stringValue: String): Boolean {
return addOrUpdate(key, stringValue.toNSData())
Expand All @@ -48,6 +49,7 @@ actual open class KVault(
* Saves an int value in the Keychain.
* @param key The key to store
* @param intValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, intValue: Int): Boolean {
return addOrUpdate(key, NSNumber(int = intValue).toNSData())
Expand All @@ -57,6 +59,7 @@ actual open class KVault(
* Saves a long value in the Keychain.
* @param key The key to store
* @param longValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, longValue: Long): Boolean {
return addOrUpdate(key, NSNumber(long = longValue).toNSData())
Expand All @@ -66,6 +69,7 @@ actual open class KVault(
* Saves a float value in the Keychain.
* @param key The key to store
* @param floatValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, floatValue: Float): Boolean {
return addOrUpdate(key, NSNumber(float = floatValue).toNSData())
Expand All @@ -75,6 +79,7 @@ actual open class KVault(
* Saves a double value in the Keychain.
* @param key The key to store
* @param doubleValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, doubleValue: Double): Boolean {
return addOrUpdate(key, NSNumber(double = doubleValue).toNSData())
Expand All @@ -84,6 +89,7 @@ actual open class KVault(
* Saves a boolean value in the Keychain.
* @param key The key to store
* @param boolValue The value to store
* @return True or false, depending on whether the value has been stored in the Keychain
*/
actual fun set(key: String, boolValue: Boolean): Boolean {
return addOrUpdate(key, NSNumber(bool = boolValue).toNSData())
Expand All @@ -101,7 +107,7 @@ actual open class KVault(
/**
* Returns the int value of an object in the Keychain.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored int value, or null if it is missing
*/
actual fun int(forKey: String): Int? {
return value(forKey)?.toNSNumber()?.intValue
Expand All @@ -110,7 +116,7 @@ actual open class KVault(
/**
* Returns the long value of an object in the Keychain.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored long value, or null if it is missing
*/
actual fun long(forKey: String): Long? {
return value(forKey)?.toNSNumber()?.longValue
Expand All @@ -119,7 +125,7 @@ actual open class KVault(
/**
* Returns the float value of an object in the Keychain.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored float value, or null if it is missing
*/
actual fun float(forKey: String): Float? {
return value(forKey)?.toNSNumber()?.floatValue
Expand All @@ -128,7 +134,7 @@ actual open class KVault(
/**
* Returns the double value of an object in the Keychain.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored double value, or null if it is missing
*/
actual fun double(forKey: String): Double? {
return value(forKey)?.toNSNumber()?.doubleValue
Expand All @@ -137,7 +143,7 @@ actual open class KVault(
/**
* Returns the boolean value of an object in the Keychain.
* @param forKey The key to query
* @return The stored string value, or null if it is missing
* @return The stored boolean value, or null if it is missing
*/
actual fun bool(forKey: String): Boolean? {
return value(forKey)?.toNSNumber()?.boolValue
Expand Down Expand Up @@ -176,7 +182,7 @@ actual open class KVault(

/**
* Deletes all objects.
* If the service name and/or the access group are null, all items in the apps
* If the service name and/or the access group are null, all items in the apps'
* Keychain will be deleted.
* @return True or false, depending on whether the objects have been deleted
*/
Expand All @@ -195,7 +201,7 @@ actual open class KVault(

private fun addOrUpdate(key: String, value: NSData?): Boolean {
return if(existsObject(key)) {
update(value, key)
update(key, value)
} else {
add(key, value)
}
Expand All @@ -212,7 +218,7 @@ actual open class KVault(
.validate()
}

private fun update(value: Any?, forKey: String): Boolean = context(forKey, value) { (account, data) ->
private fun update(key: String, value: Any?): Boolean = context(key, value) { (account, data) ->
val query = query(
kSecClass to kSecClassGenericPassword,
kSecAttrAccount to account,
Expand Down

0 comments on commit 948ff46

Please sign in to comment.