-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Items added to watchlist are now shown in the watchlist fragment
- Loading branch information
Showing
6 changed files
with
182 additions
and
47 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/example/moviemate/adapter/WatchlistMovieAdapter.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,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) | ||
} | ||
} |
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
63 changes: 62 additions & 1 deletion
63
app/src/main/java/com/example/moviemate/fragments/WatchlistFragment.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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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
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
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