Skip to content

Commit

Permalink
Implemented search feature in search fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mys7erio committed Apr 17, 2024
1 parent 33dbb87 commit 85913e6
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.moviemate.adapter

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -16,7 +17,7 @@ import com.example.moviemate.utils.POSTER_SIZE

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

) :
Expand All @@ -39,6 +40,12 @@ class MovieAdapter(
holder.itemView.setOnClickListener { navigateToMovieInfo(holder) }
}

@SuppressLint("NotifyDataSetChanged")
fun setMovieList(newMovieList: ArrayList<MovieModel>){
movieList = newMovieList
notifyDataSetChanged()
}

private fun setCardPoster(holder: MovieViewHolder, url: String) {
val request = ImageRequest(
url, { posterBitmap ->
Expand Down

This file was deleted.

43 changes: 43 additions & 0 deletions app/src/main/java/com/example/moviemate/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ fun getRecommendedMovies(
}


// https://api.themoviedb.org/3/search/movie
fun searchMovies(
queue: RequestQueue,
apiKey: String,
searchQuery: String,
callback: (ArrayList<MovieModel>) -> Unit // Callback parameter
): ArrayList<MovieModel> {

val url = "https://api.themoviedb.org/3/search/movie?query=$searchQuery"
Log.i("MOVIEMATE", "SEARCH $url")
val moviesList = ArrayList<MovieModel>()

val request = object :
JsonObjectRequest(
Method.GET, url, null,

// RUN CODE ON SUCCESS
Response.Listener { response ->
val resultsArray = response.getJSONArray("results")
for (i in 0 until resultsArray.length()) {
val movieObject = resultsArray.getJSONObject(i)
val movie = getParsedMovieModel(movieObject)
moviesList.add(movie)
}
// Invoke the callback with the fetched data
callback(moviesList)
},

// RUN CODE ON FAILURE
createErrorListener()
) {

// Set AUTH Headers
override fun getHeaders(): MutableMap<String, String> {
return createHeaders(apiKey) // Reusing the headers function
}
}

queue.add(request)
return moviesList
}


// MOVIE DETAILS
fun getMovieDetails(
queue: RequestQueue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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.WatchlistMovieAdapter
import com.example.moviemate.adapter.MovieAdapter
import com.example.moviemate.api.getRecommendedMovies
import com.example.moviemate.api.getTrendingMovies
import com.example.moviemate.api.setImage
Expand Down Expand Up @@ -80,28 +80,28 @@ class HomeFragment : Fragment() {
getTrendingMovies(requestQueue, apiKey) { movieList ->
val rvTrending = view.findViewById<RecyclerView>(R.id.rvTrendingMovies)
rvTrending.layoutManager = LinearLayoutManager(requireContext(), HORIZONTAL, false)
rvTrending.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
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(), HORIZONTAL, false)
rvRecommends.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
rvRecommends.adapter = MovieAdapter(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)
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(), HORIZONTAL, false)
rvRecommends.adapter = WatchlistMovieAdapter(requestQueue, movieList, fragmentManager)
rvRecommends.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,70 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.example.moviemate.R
import android.widget.SearchView
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager
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.adapter.MovieAdapter
import com.example.moviemate.adapter.MovieModel
import com.example.moviemate.api.searchMovies
import com.google.android.material.progressindicator.CircularProgressIndicator

class SearchFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
private val apiKey = BuildConfig.API_KEY
private var movieList = arrayListOf<MovieModel>()
private lateinit var searchBar: SearchView
private lateinit var requestQueue: RequestQueue
private lateinit var rvSearchFragment: RecyclerView
private lateinit var fragmentManager: FragmentManager
private lateinit var searchFragRootLayout: LinearLayout
private lateinit var cpiSearch: CircularProgressIndicator

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {


// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_search, container, false)

searchBar = view.findViewById(R.id.searchBarNew)
requestQueue = Volley.newRequestQueue(requireContext())
fragmentManager = (activity as MainActivity).supportFragmentManager

cpiSearch = view.findViewById(R.id.cpiSearch)
searchFragRootLayout = view.findViewById(R.id.searchFragRootLayout)

rvSearchFragment = view.findViewById(R.id.rvSearchFragment)
rvSearchFragment.layoutManager = GridLayoutManager(requireContext(), 3)
rvSearchFragment.adapter = MovieAdapter(requestQueue, movieList, fragmentManager)

searchBar.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
// handle query text changed
override fun onQueryTextChange(newText: String?) = true

override fun onQueryTextSubmit(query: String?): Boolean {
// Hide layout and show progress indicator
searchFragRootLayout.visibility = View.GONE
cpiSearch.visibility = View.VISIBLE

// Make http request
searchMovies(requestQueue, apiKey, query.toString()) { movieList ->
(rvSearchFragment.adapter as? MovieAdapter)?.setMovieList(movieList)

// Show search fragment root layout again and hide progress bar
searchFragRootLayout.visibility = View.VISIBLE
cpiSearch.visibility = View.GONE
}
return true
}

})
return view
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class WatchlistFragment : Fragment() {

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

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/rounded_corner_search_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="64dp" /> <!-- Adjust the radius to your preference -->
<solid android:color="@color/fg_dark" /> <!-- Background color -->
</shape>
71 changes: 64 additions & 7 deletions app/src/main/res/layout/fragment_search.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"


<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"
tools:context=".fragments.SearchFragment">
android:background="@color/black"
android:fillViewport="true">

<!-- TODO: Update blank fragment layout -->
<TextView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
android:layout_height="wrap_content"
android:paddingBottom="64dp"
tools:context=".fragments.SearchFragment">

<LinearLayout
android:id="@+id/searchFragRootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingBottom="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="16dp"
android:fontFamily="@font/montserrat_bold"
android:text="@string/search"
android:textColor="@color/white"
android:textSize="24sp" />


<android.widget.SearchView
android:id="@+id/searchBarNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="8dp"
android:background="@drawable/rounded_corner_search_view"
android:iconifiedByDefault="false"
android:queryBackground="@android:color/transparent"
android:queryHint="Search for a movie"
android:textColorHint="@color/white" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvSearchFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3"
tools:listitem="@layout/card_movie" />
</LinearLayout>

</FrameLayout>
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/cpiSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:visibility="gone"
app:indicatorColor="@color/primary"
app:trackColor="@color/white" />
</RelativeLayout>
</ScrollView>

0 comments on commit 85913e6

Please sign in to comment.