privatefun<T>findPreference(name: String, default: T):T = with(prefs) { val res: Any = when(default) { isInt -> getInt(name, default) isLong -> getLong(name, default) isFloat -> getFloat(name, default) is String -> getString(name, default) isBoolean -> getBoolean(name, default) else -> throw IllegalAccessException("this type can not be gotten from preference") } return@with res as T }
privatefun<U>putPreference(name: String, value: U) = with(prefs.edit()) { when(value) { isInt -> putInt(name, value) isLong -> putLong(name, value) isFloat -> putFloat(name, value) is String -> putString(name, value) isBoolean -> putBoolean(name, value) else -> throw IllegalAccessException("this type can not be saved into preference") }.apply() } }