/** Returns the signed in users ID as a `String` or `null` if no ID in shared preferences found (or `0`). */
fun SharedPreferencesManager.userId(): String? {
val userIdLong: Long = this.loadCurrentAdminPsid()
return if (userIdLong == 0L) null else userIdLong.toString()
}
e
ephemient
07/28/2021, 8:57 AM
implementation looks reasonable if that's the specified interface. if you want to abbreviate it, you could
Copy code
fun SharedPreferencesManager.userId() =
loadCurrentAdminPsid().takeIf { it != 0L }?.toString()
👍 1
k
KV
07/28/2021, 9:03 AM
Thank you let me try it
e
Eugen Martynov
07/28/2021, 9:56 AM
IMO original is easier to read and understand for me. I would probably drop type in declaration and that would be it.
k
KV
07/28/2021, 10:12 AM
Yeah 👍
m
Michael Böiers
08/02/2021, 7:10 AM
Copy code
fun SharedPreferencesManager.userId() = loadCurrentAdminPsid().let {
if (it == 0L) null : it.toString()
}
Of course it would be helpful if you could refactor
loadCurrentAdminPsid()
to return null if the id is invalid. And ideally you would use a value type for the id.