diff options
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/preference/SharedPreferenceHelper.kt')
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/preference/SharedPreferenceHelper.kt | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/preference/SharedPreferenceHelper.kt b/app/src/main/java/net/lacolaco/smileessence/preference/SharedPreferenceHelper.kt new file mode 100644 index 00000000..6702f1c3 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/preference/SharedPreferenceHelper.kt @@ -0,0 +1,127 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.preference + +import android.content.SharedPreferences +import net.lacolaco.smileessence.Application + +abstract class SharedPreferenceHelper { + // --------------------- GETTER / SETTER METHODS --------------------- + + protected abstract val preferences: SharedPreferences + + // -------------------------- OTHER METHODS -------------------------- + + operator fun get(key: Int, defaultValue: String): String { + return preferences.getString(getString(key), defaultValue) + } + + // int, long, float value may be stored in String format (old versions) + operator fun get(key: Int, defaultValue: Int): Int { + try { + return preferences.getInt(getString(key), defaultValue) + } catch (ex: ClassCastException) { + val ret = Integer.parseInt(get(key, defaultValue.toString())) + set(key, ret) + return ret + } + + } + + operator fun get(key: Int, defaultValue: Long): Long { + try { + return preferences.getLong(getString(key), defaultValue) + } catch (ex: ClassCastException) { + val ret = java.lang.Long.parseLong(get(key, defaultValue.toString())) + set(key, ret) + return ret + } + + } + + operator fun get(key: Int, defaultValue: Float): Float { + try { + return preferences.getFloat(getString(key), defaultValue) + } catch (ex: ClassCastException) { + val ret = java.lang.Float.parseFloat(get(key, defaultValue.toString())) + set(key, ret) + return ret + } + + } + + operator fun get(key: Int, defaultValue: Boolean): Boolean { + return preferences.getBoolean(getString(key), defaultValue) + } + + operator fun get(key: Int, defaultValue: Set<String>): Set<String> { + return preferences.getStringSet(getString(key), defaultValue) + } + + operator fun set(key: Int, value: String): Boolean { + return preferences.edit() + .putString(getString(key), value) + .commit() + } + + operator fun set(key: Int, value: Int): Boolean { + return preferences.edit() + .putInt(getString(key), value) + .commit() + } + + operator fun set(key: Int, value: Long): Boolean { + return preferences.edit() + .putLong(getString(key), value) + .commit() + } + + operator fun set(key: Int, value: Float): Boolean { + return preferences.edit() + .putFloat(getString(key), value) + .commit() + } + + operator fun set(key: Int, value: Boolean): Boolean { + return preferences.edit() + .putBoolean(getString(key), value) + .commit() + } + + operator fun set(key: Int, value: Set<String>): Boolean { + return preferences.edit() + .putStringSet(getString(key), value) + .commit() + } + + private fun getString(resID: Int): String? { + try { + return Application.getInstance().getString(resID) + } catch (e: Exception) { + return null + } + + } +} |