Skip to content

Commit

Permalink
Merge branch 'PEC-CSS:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
armaanbadhan authored Feb 20, 2023
2 parents b17c202 + 2ded99b commit 0076a2a
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
16 changes: 15 additions & 1 deletion app/src/main/java/com/pec_acm/moviedroid/ProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +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.pec_acm.moviedroid.databinding.ActivityProfileBinding
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

profileViewModel.setUserRatingValues(user!!.uid)
//update the info

if (user != null) {
Expand All @@ -29,6 +33,16 @@ class ProfileActivity : AppCompatActivity() {
.load(user.photoUrl)
.placeholder(R.drawable.ic_baseline_account_circle_24)
.into(binding.imgProfile)

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
}
}

binding.backBtnProfile.setOnClickListener {
Expand Down
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")
}
}
}
}
}

}
45 changes: 42 additions & 3 deletions app/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.35">

<!-- google profile pic-->
<androidx.cardview.widget.CardView
Expand All @@ -51,7 +52,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_30sdp"
android:fontFamily="@font/roboto"
android:fontFamily="@font/nunito"
android:text="@string/name"
android:textSize="@dimen/_16ssp" />

Expand All @@ -61,11 +62,49 @@
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" />


</LinearLayout>

<!-- user ratings -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" >

<TextView
android:id="@+id/OverallRateText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/TVRateText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" />

<TextView
android:id="@+id/MovieRateText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 0076a2a

Please sign in to comment.