So I've written a class for storing user/device specific settings, but I've written the Kotlin pretty much like I would in Java:
import android.content.Context
import android.content.SharedPreferences
import java.util.*
class UserSettings {
private lateinit var _prefs :SharedPreferences
fun UserSettings(ctx : Context) {
_prefs = ctx.getSharedPreferences("UserSettings", Context.MODE_PRIVATE)
}
private val KEY_DEVICEID = "deviceId"
var deviceId :String
get() {
var value = _prefs.getString(KEY_DEVICEID, null)
if (null == value) {
value = UUID.randomUUID().toString()
this.deviceId = value
}
return value
}
set(it) {
_prefs.edit()
.putString(KEY_DEVICEID, it)
.apply()
}
}
This feels really verbose. I've considered writing a helper library to simply store all values from a class in SharedPreferences
via reflection, but I'm not sure if that's overkill. The above code obviously only has one value at the moment, but I'll be adding a couple more that will look very similar to the above.