add logic of searchView

master
Julien THEME 2 years ago
parent 9e7e320a77
commit 5c31039fd4

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/pixel_5_-_api_33.avd" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-02-02T22:43:38.241003Z" />
</component>
</project>

@ -0,0 +1 @@
TMDB_API_KEY="e53e59cf1e29b9afff93d9ca1208f0cf"

@ -3,11 +3,16 @@ plugins {
id 'org.jetbrains.kotlin.android'
}
def apikeyPropertiesFile = rootProject.file("app/apikey.properties")
def apikeyProperties = new Properties()
apikeyProperties.load(new FileInputStream(apikeyPropertiesFile))
android {
namespace 'com.example.cinapp'
compileSdk 32
defaultConfig {
buildConfigField("String", "TMDB_API_KEY", apikeyProperties['TMDB_API_KEY'])
applicationId "com.example.cinapp"
minSdk 21
targetSdk 32

@ -2,6 +2,7 @@ package com.example.cinapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.example.cinapp.databinding.ActivityMainBinding
import com.example.cinapp.fragments.HomeFragment

@ -17,8 +17,8 @@ class MediaApi {
private val service = retrofit.create(MediaService::class.java)
val mediaMapper = MediaMapper()
fun search(apiKey: String, language: String, query: String, page: Int, includeAdult: Boolean, callback: (List<Media>) -> Unit) {
val response: Call<Demmy> = service.searchMedia(apiKey, language, query, page, includeAdult)
fun search(query: String, page: Int, callback: (List<Media>) -> Unit) {
val response: Call<Demmy> = service.searchMedia(query, page)
val listMedia: MutableList<Media> = mutableListOf()
Log.d("TAG", "search: " + response.request().url())
@ -42,8 +42,8 @@ class MediaApi {
Log.d("Liste2", listMedia.size.toString())
}
fun getPopularMovies(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularMovies(apiKey)
fun getPopularMovies(callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularMovies()
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
@ -63,8 +63,8 @@ class MediaApi {
})
}
fun getPopularSeries(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularSeries(apiKey)
fun getPopularSeries(callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularSeries()
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
@ -84,8 +84,8 @@ class MediaApi {
})
}
fun getTopRatedMovie(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedMovie(apiKey)
fun getTopRatedMovie(callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedMovie()
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
@ -105,8 +105,8 @@ class MediaApi {
})
}
fun getTopRatedSerie(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedSerie(apiKey)
fun getTopRatedSerie(callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedSerie()
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {

@ -1,5 +1,6 @@
package com.example.cinapp.Request
import com.example.cinapp.BuildConfig
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
@ -7,22 +8,22 @@ import retrofit2.http.Query
interface MediaService {
@GET("search/multi")
fun searchMedia(
@Query("api_key") apiKey: String,
@Query("language") language: String,
@Query("query") query: String,
@Query("page") page: Int,
@Query("include_adult") includeAdult: Boolean
@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY,
@Query("language") language: String = "fr",
@Query("include_adult") includeAdult: Boolean = true
): Call<Demmy>
@GET("movie/popular")
fun getPopularMovies(@Query("api_key") apiKey: String): Call<Demmy>
fun getPopularMovies(@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY): Call<Demmy>
@GET("tv/popular")
fun getPopularSeries(@Query("api_key") apiKey: String): Call<Demmy>
fun getPopularSeries(@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY): Call<Demmy>
@GET("movie/top_rated")
fun getTopRatedMovie(@Query("api_key") apiKey: String): Call<Demmy>
fun getTopRatedMovie(@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY): Call<Demmy>
@GET("tv/top_rated")
fun getTopRatedSerie(@Query("api_key") apiKey: String): Call<Demmy>
fun getTopRatedSerie(@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY): Call<Demmy>
}

@ -42,24 +42,23 @@ class HomeFragment : Fragment() {
//getPopularMovies
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_home, container, false)
val apiKey = "e53e59cf1e29b9afff93d9ca1208f0cf"
MediaApi().getPopularMovies(apiKey) { listMedia ->
MediaApi().getPopularMovies() { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_popular_movies)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getPopularSeries(apiKey) { listMedia ->
MediaApi().getPopularSeries() { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_popular_series)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getTopRatedMovie(apiKey) { listMedia ->
MediaApi().getTopRatedMovie() { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_top_rated_movies)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getTopRatedSerie(apiKey) { listMedia ->
MediaApi().getTopRatedSerie() { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_top_rated_series)
addMediaToRecyclerView(listMedia, recyclerView)
}

@ -41,38 +41,8 @@ class MovieFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Récupération de la vue racine
val rootView = inflater.inflate(R.layout.fragment_movie, container, false)
// Récupération de la vue souhaitée
val myView = rootView.findViewById<TextView>(R.id.name)
//var listMedias : Unit = MediaApi().search("e53e59cf1e29b9afff93d9ca1208f0cf", "en-US", "One Piece", 1, false) { listMedia -> listMedia }
//var listMedia : List<Media> = MediaApi().getPopularMovies("e53e59cf1e29b9afff93d9ca1208f0cf")
//var listMedia : List<Media> = MediaApi().getForrestGump()
val apiKey = "e53e59cf1e29b9afff93d9ca1208f0cf"
val language = "en-US"
val query = "One Piece"
val page = 1
val includeAdult = false
MediaApi().search(apiKey, language, query, page, includeAdult) { listMedia ->
//Do something with listMedia
Log.d("TAG", "onCreateView: " + listMedia)
Log.d("Image", "onCreateView: " + listMedia[0].posterPath.toString())
myView.text = listMedia[0].title.toString() + " " + listMedia[0].id.toString() + " " + listMedia[0].posterPath.toString()
//ajouter le recyclerView à la vue
val recyclerView = rootView.findViewById<RecyclerView>(R.id.recycler_view_movies)
//recyclerView.layoutManager = LinearLayoutManager(context)
val layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)
recyclerView.layoutManager = layoutManager
val adapter = MediaAdapter(listMedia)
recyclerView.adapter = adapter
}
return rootView
}

@ -8,6 +8,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.example.cinapp.R
import com.example.cinapp.viewModel.SearchViewModel
@ -25,11 +26,11 @@ class SearchFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var searchView: SearchView
private lateinit var searchVM: SearchViewModel
private lateinit var searchViewModel: SearchViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("InfoSearch", "onCreateView: ")
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
@ -42,16 +43,16 @@ class SearchFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_search, container, false)
searchView = view.findViewById(R.id.searchView)
Log.d("SearchViewModel", "setSearchView: $searchView")
return view
Log.d("InfoSearch", "onCreateView: ")
val rootView = inflater.inflate(R.layout.fragment_search, container, false)
val viewModelProvider = ViewModelProvider(this)
searchViewModel = viewModelProvider.get(SearchViewModel::class.java)
searchViewModel.setSearchView(rootView)
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
searchVM = ViewModelProvider(this)[SearchViewModel::class.java]
searchVM.setSearchView(searchView)
}
companion object {

@ -2,32 +2,51 @@ package com.example.cinapp.viewModel
import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.cinapp.BuildConfig
import com.example.cinapp.MainActivity
import com.example.cinapp.R
import com.example.cinapp.Request.MediaAdapter
import com.example.cinapp.Request.MediaApi
@SuppressLint("StaticFieldLeak")
class SearchViewModel : ViewModel() {
var searchView: SearchView? = null
val context : MainActivity = MainActivity()
@JvmName("setSearchView1")
fun setSearchView(searchView: SearchView) {
this.searchView = searchView
fun setSearchView(rootView: View) {
searchView = rootView.findViewById(R.id.searchView)
Log.d("SearchViewModel", "setSearchView: $searchView")
}
var sv = searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
// Traitement lors de la soumission de la requête
Log.d("SearchViewModel", "onQueryTextSubmit: $query")
MediaApi().search(query.toString(), 1)
{ listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.searchRecyclerView)
recyclerView.layoutManager = GridLayoutManager(context, 2)
recyclerView.adapter = MediaAdapter(listMedia)
}
Log.d("SearchViewModel -----", "onQueryTextSubmit: $query")
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
// Traitement lorsque le texte de la requête change
Log.d("SearchViewModel", "onQueryTextSubmit: $newText")
Log.d("SearchViewModel *******", "onQueryTextChange: $newText")
return false
}
})
}
}

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.cinapp.viewModel.SearchViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout 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"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/searchcl"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -26,6 +26,16 @@
app:iconifiedByDefault="false"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/searchRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@+id/searchView"
tools:layout_editor_absoluteX="-16dp"
tools:listitem="@layout/item_search" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="@color/first_grey"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardview">
<ImageView
android:id="@+id/poster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>
Loading…
Cancel
Save