From c6c9303fd40e011c8439da803e9a22ce9f194e52 Mon Sep 17 00:00:00 2001 From: armaan Date: Sat, 18 Feb 2023 21:49:40 +0530 Subject: [PATCH 1/5] added user rating xml things --- app/src/main/res/layout/activity_profile.xml | 47 ++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index e766f05..34f58a2 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -29,7 +29,8 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent"> + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.35000002"> @@ -61,11 +62,51 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_10sdp" - android:fontFamily="@font/roboto" + android:fontFamily="@font/nunito" android:text="@string/email_id" android:textSize="@dimen/_10ssp" /> + + + + + + + + + + + + \ No newline at end of file From f6385d2191a9c56ab299434a245f4f90f10b6aa2 Mon Sep 17 00:00:00 2001 From: armaan Date: Sun, 19 Feb 2023 00:11:44 +0530 Subject: [PATCH 2/5] rating display complete --- .../com/pec_acm/moviedroid/ProfileActivity.kt | 46 +++++++++++++++++++ app/src/main/res/layout/activity_profile.xml | 16 +++---- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt index b824441..0b97e2b 100644 --- a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt +++ b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt @@ -4,7 +4,10 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.bumptech.glide.Glide import com.google.firebase.auth.FirebaseAuth +import com.google.firebase.database.ktx.database +import com.google.firebase.ktx.Firebase import com.pec_acm.moviedroid.databinding.ActivityProfileBinding +import com.pec_acm.moviedroid.firebase.User import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -19,6 +22,9 @@ class ProfileActivity : AppCompatActivity() { val user = FirebaseAuth.getInstance().currentUser + val databaseReference = Firebase.database.reference + val userReference = databaseReference.child("Users") + //update the info if (user != null) { @@ -29,6 +35,46 @@ class ProfileActivity : AppCompatActivity() { .load(user.photoUrl) .placeholder(R.drawable.ic_baseline_account_circle_24) .into(binding.imgProfile) + + var movieRate = 0 + var tvRate = 0 + var movieCount = 0 + var tvCount = 0 + + userReference.child(user.uid).get().addOnCompleteListener { + val userDbRef = it.result.getValue(User::class.java) + for (item in userDbRef!!.userList) + { + if (item.personalScore != 0) + { + if (item.category == "tv") + { + tvCount += 1 + tvRate += item.personalScore + } + else + { + movieCount += 1 + movieRate += item.personalScore + } + } + } + binding.OverallRateText.text = buildString { + append("Overall Rating: ") + append((movieRate + tvRate) / (movieCount + tvCount)) + append("/10") + } + binding.MovieRateText.text = buildString { + append("Overall Rating: ") + append(movieRate / movieCount) + append("/10") + } + binding.TVRateText.text = buildString { + append("Overall Rating: ") + append((tvRate / tvCount)) + append("/10") + } + } } binding.backBtnProfile.setOnClickListener { diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index 34f58a2..b9a7965 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -30,7 +30,7 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintVertical_bias="0.35000002"> + app:layout_constraintVertical_bias="0.35"> + + android:text="" /> + android:gravity="center" /> + android:gravity="center" /> From 96d3f559abf57b465361333b94b701a3571f58fd Mon Sep 17 00:00:00 2001 From: armaan Date: Mon, 20 Feb 2023 14:36:31 +0530 Subject: [PATCH 3/5] div by zero error gone --- .../com/pec_acm/moviedroid/ProfileActivity.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt index 0b97e2b..199769b 100644 --- a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt +++ b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt @@ -59,21 +59,21 @@ class ProfileActivity : AppCompatActivity() { } } } - binding.OverallRateText.text = buildString { - append("Overall Rating: ") + binding.OverallRateText.text = if (movieCount + tvCount != 0) buildString { + append("Overall Rating:\n") append((movieRate + tvRate) / (movieCount + tvCount)) append("/10") - } - binding.MovieRateText.text = buildString { - append("Overall Rating: ") + } else "Rate something to view your average rating!" + binding.MovieRateText.text = if (movieCount != 0) buildString { + append("Movie Rating:\n") append(movieRate / movieCount) append("/10") - } - binding.TVRateText.text = buildString { - append("Overall Rating: ") + } else "" + binding.TVRateText.text = if (tvCount != 0) buildString { + append("TV Shows Rating:\n") append((tvRate / tvCount)) append("/10") - } + } else "" } } From 48d910cf22446c36bf98820ee591d7c0bc9ae0be Mon Sep 17 00:00:00 2001 From: armaan Date: Mon, 20 Feb 2023 20:50:31 +0530 Subject: [PATCH 4/5] create ProfileViewModel.kt --- .../com/pec_acm/moviedroid/ProfileActivity.kt | 58 ++++---------- .../mainpage/profile/ProfileViewModel.kt | 76 +++++++++++++++++++ 2 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt diff --git a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt index 199769b..f893aa9 100644 --- a/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt +++ b/app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt @@ -2,29 +2,27 @@ package com.pec_acm.moviedroid import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.lifecycle.ViewModelProvider import com.bumptech.glide.Glide import com.google.firebase.auth.FirebaseAuth -import com.google.firebase.database.ktx.database -import com.google.firebase.ktx.Firebase import com.pec_acm.moviedroid.databinding.ActivityProfileBinding -import com.pec_acm.moviedroid.firebase.User +import com.pec_acm.moviedroid.mainpage.profile.ProfileViewModel import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class ProfileActivity : AppCompatActivity() { + private lateinit var profileViewModel: ProfileViewModel private lateinit var binding: ActivityProfileBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityProfileBinding.inflate(layoutInflater) + profileViewModel = ViewModelProvider(this)[ProfileViewModel::class.java] setContentView(binding.root) val user = FirebaseAuth.getInstance().currentUser - - val databaseReference = Firebase.database.reference - val userReference = databaseReference.child("Users") - + profileViewModel.setUserRatingValues(user!!.uid) //update the info if (user != null) { @@ -36,44 +34,14 @@ class ProfileActivity : AppCompatActivity() { .placeholder(R.drawable.ic_baseline_account_circle_24) .into(binding.imgProfile) - var movieRate = 0 - var tvRate = 0 - var movieCount = 0 - var tvCount = 0 - - userReference.child(user.uid).get().addOnCompleteListener { - val userDbRef = it.result.getValue(User::class.java) - for (item in userDbRef!!.userList) - { - if (item.personalScore != 0) - { - if (item.category == "tv") - { - tvCount += 1 - tvRate += item.personalScore - } - else - { - movieCount += 1 - movieRate += item.personalScore - } - } - } - binding.OverallRateText.text = if (movieCount + tvCount != 0) buildString { - append("Overall Rating:\n") - append((movieRate + tvRate) / (movieCount + tvCount)) - append("/10") - } else "Rate something to view your average rating!" - binding.MovieRateText.text = if (movieCount != 0) buildString { - append("Movie Rating:\n") - append(movieRate / movieCount) - append("/10") - } else "" - binding.TVRateText.text = if (tvCount != 0) buildString { - append("TV Shows Rating:\n") - append((tvRate / tvCount)) - append("/10") - } else "" + profileViewModel.overallRating.observe(this) {rate -> + binding.OverallRateText.text = rate + } + profileViewModel.tvRating.observe(this) {rate -> + binding.TVRateText.text = rate + } + profileViewModel.movieRating.observe(this) {rate -> + binding.MovieRateText.text = rate } } diff --git a/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt b/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt new file mode 100644 index 0000000..d67b9c5 --- /dev/null +++ b/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt @@ -0,0 +1,76 @@ +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 = MutableLiveData() + + val overallRating: MutableLiveData = MutableLiveData("Rate something to view your average rating!") + val movieRating: MutableLiveData = MutableLiveData("") + val tvRating: MutableLiveData = 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((movieRate + tvRate) / (movieCount + tvCount)) + append("/10") + } + } + if (tvCount != 0) + { + tvRating.value = buildString { + append("TV Shows Rating:\n") + append((tvRate / tvCount)) + append("/10") + } + } + if (movieCount != 0) + { + movieRating.value = buildString { + append("Movie Rating:\n") + append(movieRate / movieCount) + append("/10") + } + } + } + } + } + +} \ No newline at end of file From 19741adcbcb629cb2113748df7d1c5cabfe536ce Mon Sep 17 00:00:00 2001 From: armaan Date: Mon, 20 Feb 2023 21:12:44 +0530 Subject: [PATCH 5/5] user rating in float now --- .../moviedroid/mainpage/profile/ProfileViewModel.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt b/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt index d67b9c5..3a004f2 100644 --- a/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt +++ b/app/src/main/java/com/pec_acm/moviedroid/mainpage/profile/ProfileViewModel.kt @@ -8,7 +8,6 @@ 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") @@ -49,7 +48,7 @@ class ProfileViewModel: ViewModel() { { overallRating.value = buildString { append("Overall Rating:\n") - append((movieRate + tvRate) / (movieCount + tvCount)) + append(String.format("%.2f", (movieRate + tvRate).toFloat() / (movieCount + tvCount))) append("/10") } } @@ -57,7 +56,7 @@ class ProfileViewModel: ViewModel() { { tvRating.value = buildString { append("TV Shows Rating:\n") - append((tvRate / tvCount)) + append(String.format("%.2f", tvRate.toFloat() / tvCount)) append("/10") } } @@ -65,7 +64,7 @@ class ProfileViewModel: ViewModel() { { movieRating.value = buildString { append("Movie Rating:\n") - append(movieRate / movieCount) + append(String.format("%.2f", movieRate.toFloat() / movieCount)) append("/10") } }