Skip to content

Commit

Permalink
Items added to watchlist are now shown in the watchlist fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mys7erio committed Apr 15, 2024
1 parent 8cd2b93 commit 557e887
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.example.moviemate.adapter

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageRequest
import com.example.moviemate.utils.IMAGE_BASE_API
import com.example.moviemate.fragments.MovieInfoFragment
import com.example.moviemate.R
import com.example.moviemate.api.createErrorListener
import com.example.moviemate.utils.POSTER_SIZE

class WatchlistMovieAdapter(
private val requestQueue: RequestQueue,
private val movieList: ArrayList<MovieModel>,
private val fragmentManager: FragmentManager

) :
RecyclerView.Adapter<MovieViewHolder>() {
override fun getItemCount(): Int {
return movieList.size
}

override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val currentItem = movieList[position]
setCardPoster(holder, "$IMAGE_BASE_API/$POSTER_SIZE/${currentItem.posterPath}")
holder.id = currentItem.id
holder.posterPath = currentItem.posterPath
holder.backdropPath = currentItem.backdropPath
holder.itemView.setOnClickListener { navigateToMovieInfo(holder) }
}

private fun setCardPoster(holder: MovieViewHolder, url: String) {
val request = ImageRequest(
url, { posterBitmap ->
holder.cardPoster.setImageBitmap(posterBitmap)
}, 0, 0, null, null,
createErrorListener() // Handle Errors
)
requestQueue.add(request)
Log.i("MOVIEMATE", "FETCHED POSTER: $url")
}

private fun navigateToMovieInfo(holder: MovieViewHolder) {
Log.i("MOVIEMATE", "CLICKED: ${holder.title}")
val movieInfoFragment = MovieInfoFragment()
movieInfoFragment.arguments = Bundle().apply {
putString("MovieID", holder.id.toString())
}
fragmentManager
.beginTransaction()
.replace(R.id.frame_container, movieInfoFragment)
.addToBackStack(null)
.commit()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.card_movie, parent, false)
return MovieViewHolder(view)
}
}
55 changes: 26 additions & 29 deletions app/src/main/java/com/example/moviemate/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
import com.example.moviemate.BuildConfig
import com.example.moviemate.MainActivity
import com.example.moviemate.R
import com.example.moviemate.adapter.MovieAdapter
import com.example.moviemate.adapter.WatchlistMovieAdapter
import com.example.moviemate.api.getRecommendedMovies
import com.example.moviemate.api.getTrendingMovies
import com.example.moviemate.api.setImage
Expand Down Expand Up @@ -78,33 +79,29 @@ class HomeFragment : Fragment() {
// TRENDING MOVIES
getTrendingMovies(requestQueue, apiKey) { movieList ->
val rvTrending = view.findViewById<RecyclerView>(R.id.rvTrendingMovies)
rvTrending.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
rvTrending.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)

// RECOMMENDED MOVIES
getRecommendedMovies(requestQueue, apiKey, 693134) { movieList ->
val rvRecommends = view.findViewById<RecyclerView>(R.id.rvRecommendedMovies)
rvRecommends.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
rvRecommends.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)
}

// TRENDING SHOWS
getTrendingMovies(requestQueue, apiKey) { movieList ->
val rvTrending = view.findViewById<RecyclerView>(R.id.rvTrendingShows)
rvTrending.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
rvTrending.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)
}

// RECOMMENDED SHOWS
getRecommendedMovies(requestQueue, apiKey, 13183) { movieList ->
val rvRecommends = view.findViewById<RecyclerView>(R.id.rvRecommendedShows)
rvRecommends.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
rvRecommends.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)
}
rvTrending.layoutManager = LinearLayoutManager(requireContext(), HORIZONTAL, false)
rvTrending.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
}

// RECOMMENDED MOVIES
getRecommendedMovies(requestQueue, apiKey, 693134) { movieList ->
val rvRecommends = view.findViewById<RecyclerView>(R.id.rvRecommendedMovies)
rvRecommends.layoutManager = LinearLayoutManager(requireContext(), HORIZONTAL, false)
rvRecommends.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
}

// TRENDING SHOWS
getTrendingMovies(requestQueue, apiKey) { movieList ->
val rvTrending = view.findViewById<RecyclerView>(R.id.rvTrendingShows)
rvTrending.layoutManager = LinearLayoutManager(requireContext(), HORIZONTAL, false)
rvTrending.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
}

// RECOMMENDED SHOWS
getRecommendedMovies(requestQueue, apiKey, 13183) { movieList ->
val rvRecommends = view.findViewById<RecyclerView>(R.id.rvRecommendedShows)
rvRecommends.layoutManager = LinearLayoutManager(requireContext(), HORIZONTAL, false)
rvRecommends.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
}
}

Expand All @@ -116,4 +113,4 @@ class HomeFragment : Fragment() {
.addToBackStack(null)
.commit()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,80 @@
package com.example.moviemate.fragments

import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager
import android.util.Log
import android.view.Gravity
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.toolbox.Volley
import com.example.moviemate.MainActivity
import com.example.moviemate.R
import com.example.moviemate.adapter.MovieAdapter
import com.example.moviemate.adapter.MovieModel
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore

class WatchlistFragment : Fragment() {

private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
private lateinit var fragmentManager: FragmentManager
private var movieList: ArrayList<MovieModel> = arrayListOf()

private lateinit var watchlistRootLayout: LinearLayout
private lateinit var cpbWatchlist: CircularProgressIndicator
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_watchlist, container, false)
val view = inflater.inflate(R.layout.fragment_watchlist, container, false)

auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
fragmentManager = (activity as MainActivity).supportFragmentManager
cpbWatchlist = view.findViewById(R.id.cpbWatchlist)
watchlistRootLayout = view.findViewById(R.id.watchlistRootLayout)

val requestQueue = Volley.newRequestQueue(requireContext())

val userEmail = auth.currentUser!!.email.toString()
db.collection("users")
.document(userEmail)
.collection("watchlist")
.get()
.addOnSuccessListener { snapshot ->
// TODO: INVESTIGATE WHY THE NUMBER OF ITEMS INCREASE EVERYTIME THE FRAGMENT IS VISIBLE
movieList.clear()
for (doc in snapshot) {
val movie = MovieModel(
id = doc.id.toInt(),
posterPath = doc.getString("poster_path").toString(),
backdropPath = doc.getString("backdrop_path").toString()
)
Log.e("MOVIEMATE", doc.getString("posterPath").toString())
movieList.add(movie)
}

Log.e("WATCHLIST", movieList.toString())
val rvWatchlist = view.findViewById<RecyclerView>(R.id.rvWatchlist)
var layoutManager = GridLayoutManager(requireContext(), 2)
rvWatchlist.layoutManager = layoutManager
rvWatchlist.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)


// Hide progressbar and show Root layout for Watchlist Fragment
cpbWatchlist.visibility = View.GONE
watchlistRootLayout.visibility = View.VISIBLE
}

return view
}
}
11 changes: 1 addition & 10 deletions app/src/main/res/layout/card_movie.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="144dp"
android:layout_height="192dp"
android:layout_gravity="center"
android:layout_marginEnd="16dp"
android:layout_margin="8dp"
android:clickable="true"
app:cardBackgroundColor="@color/fg_dark"
app:cardCornerRadius="16dp"
Expand All @@ -15,15 +15,6 @@
android:layout_height="match_parent"
android:orientation="vertical">

<!-- <TextView-->
<!-- android:id="@+id/cmMovieName"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:gravity="center"-->
<!-- android:text="MOVIE NAME"-->
<!-- android:textColor="@color/white"-->
<!-- android:textSize="24sp" />-->

<ImageView
android:id="@+id/cardPoster"
android:layout_width="match_parent"
Expand Down
34 changes: 27 additions & 7 deletions app/src/main/res/layout/fragment_watchlist.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -10,34 +11,53 @@
android:paddingTop="32dp"
android:paddingBottom="16dp">

<FrameLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".fragments.WatchlistFragment"
tools:ignore="ScrollViewSize">

<LinearLayout
android:id="@+id/watchlistRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
android:paddingBottom="64dp"
android:visibility="gone">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:fontFamily="@font/montserrat_bold"
android:gravity="center"
android:text="Your Watchlist"
android:text="@string/your_watchlist"
android:textColor="@color/white"
android:textSize="24sp" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvWatchlist"
android:layout_width="match_parent"
android:layout_marginVertical="16dp"
android:layout_height="match_parent" />
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
tools:listitem="@layout/card_movie" />

</LinearLayout>


</FrameLayout>
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/cpbWatchlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
app:indicatorColor="@color/primary"
app:trackColor="@color/white" />
</RelativeLayout>
</ScrollView>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
<string name="already_watched">Already Watched</string>
<string name="done_watching">Done Watching</string>
<string name="save_to_watch_later">Save to Watch Later</string>
<string name="your_watchlist">Your Watchlist</string>

</resources>

0 comments on commit 557e887

Please sign in to comment.