@@ -4,10 +4,10 @@ import android.app.Application
4
4
import android.graphics.Bitmap
5
5
import android.graphics.BitmapFactory
6
6
import android.graphics.drawable.Drawable
7
+ import androidx.compose.runtime.getValue
8
+ import androidx.compose.runtime.mutableStateOf
9
+ import androidx.compose.runtime.setValue
7
10
import androidx.core.graphics.drawable.toBitmap
8
- import androidx.lifecycle.LiveData
9
- import androidx.lifecycle.MutableLiveData
10
- import androidx.lifecycle.Observer
11
11
import androidx.lifecycle.AndroidViewModel
12
12
import androidx.lifecycle.viewModelScope
13
13
import com.couchbase.lite.Blob
@@ -28,112 +28,105 @@ class UserProfileViewModel(
28
28
) : AndroidViewModel(application) {
29
29
30
30
// track our fields in our composable
31
- private val _givenName = MutableLiveData (" " )
32
- val givenName : LiveData < String > = _givenName
31
+ var givenName by mutableStateOf (" " )
32
+ private set
33
33
34
- private val _surname = MutableLiveData (" " )
35
- val surname : LiveData < String > = _surname
34
+ var surname by mutableStateOf (" " )
35
+ private set
36
36
37
- private val _jobTitle = MutableLiveData (" " )
38
- val jobTitle : LiveData < String > = _jobTitle
37
+ var jobTitle by mutableStateOf (" " )
38
+ private set
39
39
40
- private val _emailAddress = MutableLiveData (" " )
41
- val emailAddress : LiveData < String > = _emailAddress
40
+ var emailAddress by mutableStateOf (" " )
41
+ private set
42
42
43
- private val _team = MutableLiveData (" " )
44
- val team : LiveData < String > = _team
43
+ var team by mutableStateOf (" " )
44
+ private set
45
45
46
- private val _toastMessage = MutableLiveData (" " )
47
- val toastMessage : LiveData < String > = _toastMessage
46
+ var toastMessage by mutableStateOf (" " )
47
+ private set
48
48
49
- private val _profilePic = MutableLiveData <Bitmap ?>(null )
50
- val profilePic: LiveData <Bitmap ?> = _profilePic
49
+ var profilePic by mutableStateOf(defaultProfilePic())
50
+ private set
51
+
52
+ private fun defaultProfilePic (): Bitmap {
53
+ return BitmapFactory .decodeResource(getApplication<Application >().resources, R .drawable.profile_placeholder)
54
+ }
51
55
52
56
private val userObserver: (User ? ) -> Unit = { currentUser ->
53
57
currentUser?.let { authenticatedUser ->
54
- _emailAddress .value = authenticatedUser.username
55
- _team .value = authenticatedUser.team
58
+ emailAddress = authenticatedUser.username
59
+ team = authenticatedUser.team
56
60
// when getting information from the database need to make sure
57
61
// to use Dispatchers.IO so that Disk I/O work isn't done on the main thread
58
62
viewModelScope.launch(Dispatchers .IO ) {
59
63
val userProfile = repository.get(authenticatedUser.username)
60
64
// make sure when we update the UI we update on the Main Thread
61
65
withContext(Dispatchers .Main ) {
62
- userProfile[" givenName" ]?.let {
63
- _givenName .value = userProfile[" givenName" ] as String
64
- }
65
- userProfile[" surname" ]?.let {
66
- _surname .value = userProfile[" surname" ] as String
67
- }
68
- userProfile[" jobTitle" ]?.let {
69
- _jobTitle .value = userProfile[" jobTitle" ] as String
70
- }
71
- userProfile[" imageData" ]?.let {
72
- val blob = userProfile[" imageData" ] as Blob
66
+ givenName = userProfile[" givenName" ] as ? String ? : " "
67
+ surname = userProfile[" surname" ] as ? String ? : " "
68
+ jobTitle = userProfile[" jobTitle" ] as ? String ? : " "
69
+ profilePic = (userProfile[" imageData" ] as ? Blob )?.let { blob ->
73
70
val d = Drawable .createFromStream(blob.contentStream, " res" )
74
- _profilePic .value = d?.toBitmap()
75
- }
71
+ d?.toBitmap()
72
+ } ? : defaultProfilePic()
76
73
}
77
74
}
78
75
}
79
76
}
80
77
81
78
init {
82
79
authService.currentUser.observeForever(userObserver)
83
- _profilePic .value = BitmapFactory .decodeResource(getApplication<Application >().resources, R .drawable.profile_placeholder)
84
80
}
85
81
86
82
override fun onCleared () {
87
83
authService.currentUser.removeObserver(userObserver)
88
84
}
89
85
90
86
val onGivenNameChanged: (String ) -> Unit = { newValue ->
91
- _givenName .value = newValue
87
+ givenName = newValue
92
88
}
93
89
94
90
val onSurnameChanged: (String ) -> Unit = { newValue ->
95
- _surname .value = newValue
91
+ surname = newValue
96
92
}
97
93
98
94
val onJobTitleChanged: (String ) -> Unit = { newValue ->
99
- _jobTitle .value = newValue
95
+ jobTitle = newValue
100
96
}
101
97
102
98
val onProfilePicChanged: (Bitmap ) -> Unit = { newValue ->
103
99
viewModelScope.launch(Dispatchers .Main ) {
104
- _profilePic .value = newValue
100
+ profilePic = newValue
105
101
}
106
102
}
107
103
108
104
val clearToastMessage: () -> Unit = {
109
- _toastMessage .value = " "
105
+ toastMessage = " "
110
106
}
111
107
112
108
val onSave: () -> Unit = {
113
109
// when saving information to the database need to make sure
114
110
// to use Dispatchers.IO so that Disk I/O work isn't done on the main thread
115
111
viewModelScope.launch(Dispatchers .IO ) {
116
112
val profile = HashMap <String , Any >()
117
- profile[" givenName" ] = givenName.value as Any
118
- profile[" surname" ] = surname.value as Any
119
- profile[" jobTitle" ] = jobTitle.value as Any
120
- profile[" email" ] = emailAddress.value as Any
121
- profile[" team" ] = team.value as Any
122
- profile[" documentType" ] = " user" as Any
123
- profilePic.value?.let {
124
- val outputStream = ByteArrayOutputStream ()
125
- it.compress(Bitmap .CompressFormat .JPEG , 100 , outputStream)
126
- profile[" imageData" ] =
127
- Blob (" image/jpeg" , outputStream.toByteArray()) as Any
128
- }
113
+ profile[" givenName" ] = givenName
114
+ profile[" surname" ] = surname
115
+ profile[" jobTitle" ] = jobTitle
116
+ profile[" email" ] = emailAddress
117
+ profile[" team" ] = team
118
+ profile[" documentType" ] = " user"
119
+ val outputStream = ByteArrayOutputStream ()
120
+ profilePic.compress(Bitmap .CompressFormat .JPEG , 100 , outputStream)
121
+ profile[" imageData" ] = Blob (" image/jpeg" , outputStream.toByteArray())
129
122
val didSave = repository.save(profile)
130
123
131
124
// make sure when we update the UI we update on the Main Thread
132
125
withContext(Dispatchers .Main ) {
133
- if (didSave) {
134
- _toastMessage .value = " Successfully updated profile"
126
+ toastMessage = if (didSave) {
127
+ " Successfully updated profile"
135
128
} else {
136
- _toastMessage .value = " Error saving, try again later."
129
+ " Error saving, try again later."
137
130
}
138
131
}
139
132
}
0 commit comments