@@ -8,10 +8,13 @@ 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.DocumentReference
12
+ import com.google.firebase.firestore.FieldValue
11
13
import com.google.firebase.firestore.ktx.firestore
12
14
import com.google.firebase.ktx.Firebase
13
15
import java.io.InputStream
14
16
import java.net.URL
17
+ import java.util.concurrent.TimeUnit
15
18
16
19
17
20
class ProfileViewModel : ViewModel () {
@@ -27,11 +30,17 @@ class ProfileViewModel : ViewModel() {
27
30
)
28
31
29
32
private lateinit var curAppUser: AppUser
33
+ private var timeHMSString: MutableLiveData <String > = MutableLiveData (DEFAULT_TIME )
34
+ private lateinit var runUserDocRef : DocumentReference
30
35
31
36
private fun getUid () : String {
32
37
return curAppUser.uid
33
38
}
34
39
40
+ private fun getTotalTimeMillisecond () : Long? {
41
+ return curAppUser.totalTimeMillisecond.value
42
+ }
43
+
35
44
private fun setTotalDistanceMeters (totalDistanceMeters : Long ) {
36
45
curAppUser.totalDistanceMeters.value = totalDistanceMeters
37
46
}
@@ -48,6 +57,40 @@ class ProfileViewModel : ViewModel() {
48
57
curAppUser.singleRunIDList.value = singleRunIDList
49
58
}
50
59
60
+ private fun convertMStoStringHMS (millionSeconds : Long ) : String {
61
+ return " %02d:%02d:%02d" .format(
62
+ TimeUnit .MILLISECONDS .toHours(millionSeconds),
63
+ TimeUnit .MILLISECONDS .toMinutes(millionSeconds) % TimeUnit .HOURS .toMinutes(1 ),
64
+ TimeUnit .MILLISECONDS .toSeconds(millionSeconds) % TimeUnit .MINUTES .toSeconds(1 )
65
+ )
66
+ }
67
+
68
+ private fun setTimeHMSString () {
69
+ val ms = getTotalTimeMillisecond()
70
+ val hms = ms ?.let {convertMStoStringHMS(ms)} ? : run { DEFAULT_TIME }
71
+ timeHMSString.value = hms
72
+ Log .d(PROFILE_VM_TAG , " setTimeHMSString hms: $hms " )
73
+ }
74
+
75
+ private fun getNewSingleRunningRecordRef (singleRunningTimeMillionSeconds : Long , time : String ) {
76
+
77
+ }
78
+
79
+ private fun syncAppUserStatistic () {
80
+ runUserDocRef.get()
81
+ .addOnSuccessListener {document ->
82
+ Log .d(PROFILE_VM_TAG , " Get doc successfully" )
83
+ setTotalDistanceMeters(document.data!! [KEY_TOTAL_DIS_M ] as Long )
84
+ setTotalEnergyCalories(document.data!! [KEY_TOTAL_EN_CAL ] as Long )
85
+ setTotalTimeMillisecond(document.data!! [KEY_TOTAL_TIME_MS ] as Long )
86
+ setSingleRunIDList(document.data!! [KEY_SINGLE_RUN_ID_LIST ] as ArrayList <String >)
87
+ setTimeHMSString()
88
+ }
89
+ .addOnFailureListener {
90
+ Log .w(PROFILE_VM_TAG , " Get doc Failed" )
91
+ }
92
+ }
93
+
51
94
fun getUserName () : String {
52
95
return curAppUser.userName
53
96
}
@@ -60,28 +103,45 @@ class ProfileViewModel : ViewModel() {
60
103
return curAppUser.googleAccountProfileUrl
61
104
}
62
105
106
+ fun getTimeHMSStringMutableLiveData () : MutableLiveData <String > {
107
+ return timeHMSString
108
+ }
109
+
63
110
fun initAppUser (curFirebaseUser : FirebaseUser ) {
64
111
Log .d(PROFILE_VM_TAG , " initAppUser" )
65
112
curAppUser = AppUser (curFirebaseUser.displayName ? : " " , curFirebaseUser.email ? : " " ,
66
113
curFirebaseUser.uid, curFirebaseUser.photoUrl.toString())
114
+ runUserDocRef = Firebase .firestore.collection(USER_COLLECTION_NAME ).document(getUid())
115
+ syncAppUserStatistic()
67
116
}
68
117
69
- fun initAppUserStatistic () {
70
- val uid = getUid()
71
- val ref = Firebase .firestore.collection(USER_COLLECTION_NAME ).document(uid)
72
- ref.get()
73
- .addOnSuccessListener {document ->
74
- Log .d(PROFILE_VM_TAG , " Get doc successfully" )
75
- setTotalDistanceMeters(document.data!! [KEY_TOTAL_DIS_M ] as Long )
76
- setTotalEnergyCalories(document.data!! [KEY_TOTAL_EN_CAL ] as Long )
77
- setTotalTimeMillisecond(document.data!! [KEY_TOTAL_TIME_MS ] as Long )
78
- setSingleRunIDList(document.data!! [KEY_SINGLE_RUN_ID_LIST ] as ArrayList <String >)
79
- }
80
- .addOnFailureListener {
81
- Log .w(PROFILE_VM_TAG , " Get doc Failed" )
118
+ fun uploadNewRecord (singleRunningTimeMillionSeconds : Long , time : String ) {
119
+
120
+ val newTotalTimeMillisecond = getTotalTimeMillisecond()?.plus(
121
+ singleRunningTimeMillionSeconds
122
+ )
123
+ Log .d(PROFILE_VM_TAG , " newTotalTimeMillisecond $newTotalTimeMillisecond " )
124
+
125
+ // val singleRunDocRef = Firebase.firestore.collection(RUN_COLLECTION_NAME).document()
126
+
127
+ Firebase .firestore.runBatch { batch ->
128
+ // TODO update single run list
129
+
130
+ // update total time
131
+ val updateRunUserData = hashMapOf(
132
+ KEY_TOTAL_TIME_MS to newTotalTimeMillisecond,
133
+ KEY_SINGLE_RUN_ID_LIST to FieldValue .arrayUnion(" This is test" )
134
+ )
135
+ batch.update(runUserDocRef, updateRunUserData)
136
+
137
+ }
138
+ .addOnSuccessListener {
139
+ Log .d(PROFILE_VM_TAG , " " )
140
+ syncAppUserStatistic()
82
141
}
83
142
}
84
143
144
+
85
145
companion object {
86
146
const val PROFILE_VM_TAG = " ProfileVM"
87
147
const val USER_COLLECTION_NAME = " RunUser"
@@ -92,6 +152,7 @@ class ProfileViewModel : ViewModel() {
92
152
const val KEY_TOTAL_EN_CAL = " TotalEnergyCalories"
93
153
const val KEY_TOTAL_TIME_MS = " TotalTimeMillisecond"
94
154
const val KEY_SINGLE_RUN_ID_LIST = " SingleRunIDList"
155
+ const val DEFAULT_TIME = " 00:00:00"
95
156
}
96
157
}
97
158
0 commit comments