-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'PEC-CSS:main' into main
- Loading branch information
Showing
3 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.pec_acm.moviedroid.mainpage.profile | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.firebase.database.ktx.database | ||
import com.google.firebase.ktx.Firebase | ||
import com.pec_acm.moviedroid.firebase.User | ||
import kotlinx.coroutines.launch | ||
|
||
class ProfileViewModel: ViewModel() { | ||
private var databaseReference = Firebase.database.reference | ||
private var userReference = databaseReference.child("Users") | ||
val user : MutableLiveData<User> = MutableLiveData() | ||
|
||
val overallRating: MutableLiveData<String> = MutableLiveData("Rate something to view your average rating!") | ||
val movieRating: MutableLiveData<String> = MutableLiveData("") | ||
val tvRating: MutableLiveData<String> = MutableLiveData("") | ||
|
||
fun setUserRatingValues(uid : String) | ||
{ | ||
viewModelScope.launch { | ||
userReference.child(uid).get().addOnCompleteListener { | ||
val user = it.result.getValue(User::class.java) | ||
var movieRate = 0 | ||
var tvRate = 0 | ||
var movieCount = 0 | ||
var tvCount = 0 | ||
|
||
for (item in user!!.userList) | ||
{ | ||
if (item.personalScore != 0) | ||
{ | ||
if (item.category == "tv") | ||
{ | ||
tvCount += 1 | ||
tvRate += item.personalScore | ||
} | ||
else | ||
{ | ||
movieCount += 1 | ||
movieRate += item.personalScore | ||
} | ||
} | ||
} | ||
|
||
if (movieCount + tvCount != 0) | ||
{ | ||
overallRating.value = buildString { | ||
append("Overall Rating:\n") | ||
append(String.format("%.2f", (movieRate + tvRate).toFloat() / (movieCount + tvCount))) | ||
append("/10") | ||
} | ||
} | ||
if (tvCount != 0) | ||
{ | ||
tvRating.value = buildString { | ||
append("TV Shows Rating:\n") | ||
append(String.format("%.2f", tvRate.toFloat() / tvCount)) | ||
append("/10") | ||
} | ||
} | ||
if (movieCount != 0) | ||
{ | ||
movieRating.value = buildString { | ||
append("Movie Rating:\n") | ||
append(String.format("%.2f", movieRate.toFloat() / movieCount)) | ||
append("/10") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters