parent
745ecb427c
commit
07bdd4bac2
@ -0,0 +1,75 @@
|
|||||||
|
package fr.iut.cinecool
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
class MovieDetailFragment : Fragment() {
|
||||||
|
/*
|
||||||
|
private lateinit var binding: FragmentMovieDetailBinding
|
||||||
|
|
||||||
|
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?, savedInstanceState: Bundle?): View
|
||||||
|
{
|
||||||
|
binding = FragmentMovieDetailBinding.inflate(inflater, container, false)
|
||||||
|
return binding.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
|
||||||
|
{
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
|
val movieId = arguments?.getInt(ARG_MOVIE_ID)
|
||||||
|
|
||||||
|
// Appel de l'API pour récupérer les détails du film
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
val response = movieId?.let { MovieApiService().getMovieDetails(it) }
|
||||||
|
withContext(Dispatchers.Main)
|
||||||
|
{
|
||||||
|
if (response != null) {
|
||||||
|
if (response.isSuccessful)
|
||||||
|
{
|
||||||
|
val movie = response.body()
|
||||||
|
if (movie != null)
|
||||||
|
{
|
||||||
|
// Affichage des détails du film dans l'interface utilisateur
|
||||||
|
binding.titreFilm.text = movie.title
|
||||||
|
binding.description.text = movie.overview
|
||||||
|
binding.afficheFilm.load("https://image.tmdb.org/t/p/w500${movie.posterPath}") // Utilisation de la librairie Coil pour charger l'image
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Toast.makeText(
|
||||||
|
requireContext(),
|
||||||
|
getString(R.string.error_loading_movie),
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Ajout d'un écouteur de clic pour le bouton de retour
|
||||||
|
binding.backButton.setOnClickListener {
|
||||||
|
requireActivity().onBackPressed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val ARG_MOVIE_ID = "movie_id"
|
||||||
|
|
||||||
|
fun newInstance(movieId: Int): MovieDetailFragment {
|
||||||
|
val args = Bundle().apply {
|
||||||
|
putInt(ARG_MOVIE_ID, movieId)
|
||||||
|
}
|
||||||
|
return MovieDetailFragment().apply {
|
||||||
|
arguments = args
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package fr.iut.cinecool
|
||||||
|
|
||||||
|
import fr.iut.cinecool.services.TheMovieDbApiClient
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
object RetrofitClient {
|
||||||
|
private const val BASE_URL = "https://api.themoviedb.org/3/"
|
||||||
|
|
||||||
|
private val okHttpClient = OkHttpClient.Builder()
|
||||||
|
.connectTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
private val retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl(BASE_URL)
|
||||||
|
.client(okHttpClient)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val api: TheMovieDbApiClient = retrofit.create(TheMovieDbApiClient::class.java)
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
package fr.iut.cinecool.interfaces
|
|
||||||
|
|
||||||
import fr.iut.cinecool.model.Movie
|
|
||||||
import fr.iut.cinecool.model.MovieResponse
|
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.http.GET
|
|
||||||
import retrofit2.http.Query
|
|
||||||
|
|
||||||
interface ApiService {
|
|
||||||
|
|
||||||
@GET("movie/popular")
|
|
||||||
fun getPopularMovies(@Query("api_key") apiKey: String): Call<MovieResponse>
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut.cinecool.interfaces
|
||||||
|
|
||||||
|
interface ITheMovieDbApiCallback<T> {
|
||||||
|
fun onSuccess(result: T)
|
||||||
|
fun onError(error: Throwable)
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut.cinecool.interfaces
|
||||||
|
|
||||||
|
import fr.iut.cinecool.model.Movie
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Path
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
|
interface ITheMovieDbService {
|
||||||
|
@GET("movie/{movieId}")
|
||||||
|
fun getMovieDetails(@Path("movieId") movieId: Int, @Query("api_key") apiKey: String): Call<Movie>
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package fr.iut.cinecool.model
|
|
||||||
import com.google.gson.annotations.SerializedName
|
|
||||||
|
|
||||||
// MovieResponse pour stocker la réponse JSON de l'API :
|
|
||||||
data class MovieResponse(
|
|
||||||
@SerializedName("results") val movies: List<Movie>
|
|
||||||
)
|
|
@ -0,0 +1,57 @@
|
|||||||
|
package fr.iut.cinecool.services
|
||||||
|
|
||||||
|
import fr.iut.cinecool.interfaces.ITheMovieDbApiCallback
|
||||||
|
import fr.iut.cinecool.interfaces.ITheMovieDbService
|
||||||
|
import fr.iut.cinecool.model.MovieAPI
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.Callback
|
||||||
|
import retrofit2.Response
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
|
class TheMovieDbApiClient(apiKey: String) {
|
||||||
|
private val service: ITheMovieDbService = Retrofit.Builder()
|
||||||
|
.baseUrl("https://api.themoviedb.org/3/")
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
.create(ITheMovieDbService::class.java)
|
||||||
|
|
||||||
|
private val apiKeyInterceptor = Interceptor { chain ->
|
||||||
|
val url = chain.request().url.newBuilder()
|
||||||
|
.addQueryParameter("tmdb_api_key", apiKey)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val request = chain.request().newBuilder()
|
||||||
|
.url(url)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
chain.proceed(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
private val client: OkHttpClient = OkHttpClient.Builder()
|
||||||
|
.addInterceptor(apiKeyInterceptor)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
/*fun getMovieDetails(movieId: Int, callback: ITheMovieDbApiCallback<MovieAPI>) {
|
||||||
|
service.getMovieDetails(movieId).enqueue(object : Callback<MovieAPI> {
|
||||||
|
override fun onResponse(call: Call<MovieAPI>, response: Response<MovieAPI>) {
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
val result = response.body()
|
||||||
|
if (result != null) {
|
||||||
|
callback.onSuccess(result)
|
||||||
|
} else {
|
||||||
|
callback.onError(Throwable("Empty response body"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
callback.onError(Throwable("Error ${response.code()}: ${response.message()}"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(call: Call<MovieAPI>, t: Throwable) {
|
||||||
|
callback.onError(t)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}*/
|
||||||
|
}
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.1 KiB |
Loading…
Reference in new issue