@@ -8,6 +8,7 @@ import android.widget.ImageView
8
8
import androidx.lifecycle.MutableLiveData
9
9
import androidx.lifecycle.ViewModel
10
10
import com.google.firebase.auth.FirebaseUser
11
+ import com.google.firebase.firestore.CollectionReference
11
12
import com.google.firebase.firestore.DocumentReference
12
13
import com.google.firebase.firestore.FieldValue
13
14
import com.google.firebase.firestore.ktx.firestore
@@ -26,12 +27,19 @@ class ProfileViewModel : ViewModel() {
26
27
var totalDistanceMeters : MutableLiveData <Long > = MutableLiveData (0),
27
28
var totalEnergyCalories : MutableLiveData <Long > = MutableLiveData (0),
28
29
var totalTimeMillisecond : MutableLiveData <Long > = MutableLiveData (0),
29
- var singleRunIDList : MutableLiveData < ArrayList <String >> = MutableLiveData ( ArrayList () )
30
+ var runHistoryList : ArrayList <HashMap < String , Any >> =ArrayList ()
30
31
)
31
32
33
+ data class SingleRun (
34
+ var time : String ,
35
+ var dateTime : String
36
+ )
37
+
38
+ private lateinit var runUserDocRef : DocumentReference
39
+ private lateinit var runCollectionRef : CollectionReference
32
40
private lateinit var curAppUser: AppUser
41
+ private var runHistoryListForView: ArrayList <SingleRun > = ArrayList ()
33
42
private var timeHMSString: MutableLiveData <String > = MutableLiveData (DEFAULT_TIME )
34
- private lateinit var runUserDocRef : DocumentReference
35
43
36
44
private fun getUid () : String {
37
45
return curAppUser.uid
@@ -41,6 +49,12 @@ class ProfileViewModel : ViewModel() {
41
49
return curAppUser.totalTimeMillisecond.value
42
50
}
43
51
52
+ private fun getRunHistoryList () : ArrayList <HashMap <String , Any >> {
53
+ val reversedRunHistory = curAppUser.runHistoryList
54
+ reversedRunHistory.reverse()
55
+ return reversedRunHistory
56
+ }
57
+
44
58
private fun setTotalDistanceMeters (totalDistanceMeters : Long ) {
45
59
curAppUser.totalDistanceMeters.value = totalDistanceMeters
46
60
}
@@ -53,8 +67,18 @@ class ProfileViewModel : ViewModel() {
53
67
curAppUser.totalTimeMillisecond.value = totalTimeMillisecond
54
68
}
55
69
56
- private fun setSingleRunIDList (singleRunIDList : ArrayList <String >) {
57
- curAppUser.singleRunIDList.value = singleRunIDList
70
+ private fun setRunHistoryList (runHistoryList : ArrayList <HashMap <String , Any >>) {
71
+ curAppUser.runHistoryList = runHistoryList
72
+ }
73
+
74
+ private fun setRunHistoryListForView () {
75
+ runHistoryListForView.clear()
76
+ val runHistoryListOfHashMap = getRunHistoryList()
77
+ for (singleRun in runHistoryListOfHashMap) {
78
+ val singleRunTime : String = convertMStoStringHMS(singleRun[KEY_SINGLE_RUN_TIME ] as Long )
79
+ val singleRunTimestamp : String = singleRun[KEY_SINGLE_RUN_TIMESTAMP ] as String
80
+ runHistoryListForView.add(SingleRun (singleRunTime, singleRunTimestamp))
81
+ }
58
82
}
59
83
60
84
private fun convertMStoStringHMS (millionSeconds : Long ) : String {
@@ -72,9 +96,6 @@ class ProfileViewModel : ViewModel() {
72
96
Log .d(PROFILE_VM_TAG , " setTimeHMSString hms: $hms " )
73
97
}
74
98
75
- private fun getNewSingleRunningRecordRef (singleRunningTimeMillionSeconds : Long , time : String ) {
76
-
77
- }
78
99
79
100
private fun syncAppUserStatistic () {
80
101
runUserDocRef.get()
@@ -83,7 +104,8 @@ class ProfileViewModel : ViewModel() {
83
104
setTotalDistanceMeters(document.data!! [KEY_TOTAL_DIS_M ] as Long )
84
105
setTotalEnergyCalories(document.data!! [KEY_TOTAL_EN_CAL ] as Long )
85
106
setTotalTimeMillisecond(document.data!! [KEY_TOTAL_TIME_MS ] as Long )
86
- setSingleRunIDList(document.data!! [KEY_SINGLE_RUN_ID_LIST ] as ArrayList <String >)
107
+ setRunHistoryList(document.data!! [KEY_RUN_HISTORY ] as ArrayList <HashMap <String , Any >>)
108
+ setRunHistoryListForView()
87
109
setTimeHMSString()
88
110
}
89
111
.addOnFailureListener {
@@ -107,11 +129,20 @@ class ProfileViewModel : ViewModel() {
107
129
return timeHMSString
108
130
}
109
131
132
+ fun getTotalEnergyCaloriesMutableLiveData () : MutableLiveData <Long > {
133
+ return curAppUser.totalEnergyCalories
134
+ }
135
+
136
+ fun getRunHistoryListForView (): ArrayList <SingleRun > {
137
+ return runHistoryListForView
138
+ }
139
+
110
140
fun initAppUser (curFirebaseUser : FirebaseUser ) {
111
141
Log .d(PROFILE_VM_TAG , " initAppUser" )
112
142
curAppUser = AppUser (curFirebaseUser.displayName ? : " " , curFirebaseUser.email ? : " " ,
113
143
curFirebaseUser.uid, curFirebaseUser.photoUrl.toString())
114
144
runUserDocRef = Firebase .firestore.collection(USER_COLLECTION_NAME ).document(getUid())
145
+ runCollectionRef = Firebase .firestore.collection(RUN_COLLECTION_NAME )
115
146
syncAppUserStatistic()
116
147
}
117
148
@@ -122,41 +153,42 @@ class ProfileViewModel : ViewModel() {
122
153
)
123
154
Log .d(PROFILE_VM_TAG , " newTotalTimeMillisecond $newTotalTimeMillisecond " )
124
155
125
- val singleRunDocRef = Firebase .firestore.collection(RUN_COLLECTION_NAME ).document()
126
156
val singleRunData = hashMapOf(
127
157
KEY_SINGLE_RUN_TIME to singleRunningTimeMillionSeconds,
128
158
KEY_SINGLE_RUN_TIMESTAMP to timestamp
129
159
)
160
+
130
161
val updateRunUserData = hashMapOf(
131
162
KEY_TOTAL_TIME_MS to newTotalTimeMillisecond,
132
- KEY_SINGLE_RUN_ID_LIST to FieldValue .arrayUnion(singleRunDocRef.id)
163
+ KEY_TOTAL_EN_CAL to (newTotalTimeMillisecond?.div(10000 ) ? : 0 ),
164
+ KEY_RUN_HISTORY to FieldValue .arrayUnion(singleRunData)
133
165
)
134
166
135
- Firebase .firestore.runBatch { batch ->
136
- // Create single run record
137
- batch.set(singleRunDocRef, singleRunData)
138
167
139
- // update user statistics
168
+ Firebase .firestore.runBatch { batch ->
140
169
batch.update(runUserDocRef, updateRunUserData)
141
170
142
171
}
143
172
.addOnSuccessListener {
144
173
Log .d(PROFILE_VM_TAG , " Upload record successfully" )
145
174
syncAppUserStatistic()
175
+ // No need to call Adapter.notifyDataSetChanged()
176
+ // Each time we access profile page, just create a new one.
146
177
}
147
178
}
148
179
149
180
150
181
companion object {
151
182
const val PROFILE_VM_TAG = " ProfileVM"
183
+
152
184
const val USER_COLLECTION_NAME = " RunUser"
153
185
const val RUN_COLLECTION_NAME = " SingleRun"
154
186
const val KEY_USR_NAME = " UserName"
155
187
const val KEY_USR_EMAIL = " Email"
156
188
const val KEY_TOTAL_DIS_M = " TotalDistanceMeters"
157
189
const val KEY_TOTAL_EN_CAL = " TotalEnergyCalories"
158
190
const val KEY_TOTAL_TIME_MS = " TotalTimeMillisecond"
159
- const val KEY_SINGLE_RUN_ID_LIST = " SingleRunIDList "
191
+ const val KEY_RUN_HISTORY = " RunHistory "
160
192
161
193
const val KEY_SINGLE_RUN_TIME = " Time"
162
194
const val KEY_SINGLE_RUN_TIMESTAMP = " Timestamp"
0 commit comments