@@ -4,8 +4,10 @@ import android.content.Context
44import android.graphics.Bitmap
55import android.graphics.BitmapFactory
66import android.graphics.drawable.Drawable
7- import androidx.compose.runtime.mutableStateOf
87import androidx.core.graphics.drawable.toBitmap
8+ import androidx.lifecycle.LiveData
9+ import androidx.lifecycle.MutableLiveData
10+ import androidx.lifecycle.Observer
911import androidx.lifecycle.ViewModel
1012import androidx.lifecycle.viewModelScope
1113import com.couchbase.lite.Blob
@@ -22,76 +24,90 @@ import com.couchbase.learningpath.models.User
2224
2325class UserProfileViewModel (
2426 private val repository : KeyValueRepository ,
25- authService : AuthenticationService ,
27+ private val authService : AuthenticationService ,
2628 context : WeakReference <Context >
2729) : ViewModel() {
2830
29- private var currentUser: User ? = null
30-
3131 // track our fields in our composable
32- var givenName = mutableStateOf(" " )
33- var surname = mutableStateOf(" " )
34- var jobTitle = mutableStateOf(" " )
35- var emailAddress = mutableStateOf(" " )
36- var team = mutableStateOf(" " )
37- var toastMessage = mutableStateOf(" " )
38- var profilePic = mutableStateOf<Bitmap ?>(null )
32+ private val _givenName = MutableLiveData (" " )
33+ val givenName: LiveData <String > = _givenName
3934
40- init {
41- currentUser = authService.getCurrentUser()
42- updateUserProfileInfo()
43- profilePic.value = BitmapFactory .decodeResource(context.get()?.resources, R .drawable.profile_placeholder)
44- }
35+ private val _surname = MutableLiveData (" " )
36+ val surname: LiveData <String > = _surname
37+
38+ private val _jobTitle = MutableLiveData (" " )
39+ val jobTitle: LiveData <String > = _jobTitle
40+
41+ private val _emailAddress = MutableLiveData (" " )
42+ val emailAddress: LiveData <String > = _emailAddress
43+
44+ private val _team = MutableLiveData (" " )
45+ val team: LiveData <String > = _team
46+
47+ private val _toastMessage = MutableLiveData (" " )
48+ val toastMessage: LiveData <String > = _toastMessage
4549
46- fun updateUserProfileInfo () {
50+ private val _profilePic = MutableLiveData <Bitmap ?>(null )
51+ val profilePic: LiveData <Bitmap ?> = _profilePic
52+
53+ private val userObserver: (User ? ) -> Unit = { currentUser ->
4754 currentUser?.let { authenticatedUser ->
48- emailAddress .value = authenticatedUser.username
49- team .value = authenticatedUser.team
55+ _emailAddress .value = authenticatedUser.username
56+ _team .value = authenticatedUser.team
5057 // when getting information from the database need to make sure
5158 // to use Dispatchers.IO so that Disk I/O work isn't done on the main thread
5259 viewModelScope.launch(Dispatchers .IO ) {
5360 val userProfile = repository.get(authenticatedUser.username)
5461 // make sure when we update the UI we update on the Main Thread
5562 withContext(Dispatchers .Main ) {
5663 userProfile[" givenName" ]?.let {
57- givenName .value = userProfile[" givenName" ] as String
64+ _givenName .value = userProfile[" givenName" ] as String
5865 }
5966 userProfile[" surname" ]?.let {
60- surname .value = userProfile[" surname" ] as String
67+ _surname .value = userProfile[" surname" ] as String
6168 }
6269 userProfile[" jobTitle" ]?.let {
63- jobTitle .value = userProfile[" jobTitle" ] as String
70+ _jobTitle .value = userProfile[" jobTitle" ] as String
6471 }
6572 userProfile[" imageData" ]?.let {
6673 val blob = userProfile[" imageData" ] as Blob
6774 val d = Drawable .createFromStream(blob.contentStream, " res" )
68- profilePic .value = d?.toBitmap()
75+ _profilePic .value = d?.toBitmap()
6976 }
7077 }
7178 }
7279 }
7380 }
7481
82+ init {
83+ authService.currentUser.observeForever(userObserver)
84+ _profilePic .value = BitmapFactory .decodeResource(context.get()?.resources, R .drawable.profile_placeholder)
85+ }
86+
87+ override fun onCleared () {
88+ authService.currentUser.removeObserver(userObserver)
89+ }
90+
7591 val onGivenNameChanged: (String ) -> Unit = { newValue ->
76- givenName .value = newValue
92+ _givenName .value = newValue
7793 }
7894
7995 val onSurnameChanged: (String ) -> Unit = { newValue ->
80- surname .value = newValue
96+ _surname .value = newValue
8197 }
8298
8399 val onJobTitleChanged: (String ) -> Unit = { newValue ->
84- jobTitle .value = newValue
100+ _jobTitle .value = newValue
85101 }
86102
87103 val onProfilePicChanged: (Bitmap ) -> Unit = { newValue ->
88104 viewModelScope.launch(Dispatchers .Main ) {
89- profilePic .value = newValue
105+ _profilePic .value = newValue
90106 }
91107 }
92108
93109 val clearToastMessage: () -> Unit = {
94- toastMessage .value = " "
110+ _toastMessage .value = " "
95111 }
96112
97113 val onSave: () -> Unit = {
@@ -116,9 +132,9 @@ class UserProfileViewModel(
116132 // make sure when we update the UI we update on the Main Thread
117133 withContext(Dispatchers .Main ) {
118134 if (didSave) {
119- toastMessage .value = " Successfully updated profile"
135+ _toastMessage .value = " Successfully updated profile"
120136 } else {
121- toastMessage .value = " Error saving, try again later."
137+ _toastMessage .value = " Error saving, try again later."
122138 }
123139 }
124140 }
0 commit comments