First, I am using Kotlin not Java for this app, so I'd like solutions to be in Kotlin, please.
I am trying to figure out the best practices for loading a user's profile information from Firebase when the fragment is first loaded, but on subsequent loads, don't query Firebase and re-download the information (Data persistence?). I was initially thinking about using saveSharedPreferences, but not sure if that is the correct route to go. I was thinking I could store this info in saveSharedPrefs as well as Firebase and then query if saveSharedPrefs is null
or not. But also, I'd need to check for data changes. Logically, if the user updated their profile, I'd update saveSharedPrefs as well as Firebase, so only checking for null would be necessary. I'm looking at storing username, photo, email, bio, interests, city, website and maybe 1 or 2 more items so there wouldn't be a lot of stored local data. Plus, I'd only be storing non-sensitive information.
Is this the best and most efficient way of handling data? Is there another method I am not thinking about? I'd also like to apply this to other parts of the app such that, unless the data from Firebase differs from the local data, don't reload from Firebase, get the data from saveSharedPrefs.
Please help with suggestions. Also, I've not done much with saveSharedPrefs yet, so I'm still learning how to do that. Thanks!
*****Update*****
Get photo from Firebase:
Here's my code for loading the user profile info (Just the Name and Photo uri):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
doAsync {
//get user profile information
if (user != null) {
uiThread {
//update UI thread after completing task
view.profileUsername.setText(user?.displayName)
view.profileProgressBar.visibility = View.GONE
if (user?.photoUrl != null) {
view.profileUserPhoto.setImageURI(user?.photoUrl)
}
}
}
}
}
set onClickListener
for the floatingActionButton
and the call the chooseProfileImage
method to get the image from local storage:
view.fab_profile_photo_edit.setOnClickListener {
chooseProfileImage()
}
private fun chooseProfileImage() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), profileImageRequest)
}
These actions are all performed inside a fragment, just fyi.