add home page

Add home page with different series and movie proposals such as the most popular and the best rated
master
Julien THEME 2 years ago
parent 3e10b69996
commit f3c8ca074f

@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.cinapp.databinding.ActivityMainBinding
import com.example.cinapp.model.navigation.HomeFragment
import com.example.cinapp.model.navigation.MovieFragment
import com.example.cinapp.model.navigation.SearchFragment
import com.example.cinapp.model.navigation.SerieFragment
@ -17,10 +18,11 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
replaceFragment(MovieFragment())
replaceFragment(HomeFragment())
binding.bottomNavigation.setOnItemSelectedListener {
when(it.itemId){
R.id.home -> replaceFragment(HomeFragment())
R.id.movie -> replaceFragment(MovieFragment())
R.id.serie -> replaceFragment(SerieFragment())
R.id.search -> replaceFragment(SearchFragment())

@ -42,7 +42,7 @@ class MediaApi {
Log.d("Liste2", listMedia.size.toString())
}
fun getPopularMovies(apiKey: String): List<Media> {
fun getPopularMovies(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularMovies(apiKey)
val listMedia: MutableList<Media> = mutableListOf()
@ -52,20 +52,40 @@ class MediaApi {
val allMedia = response.body()!!
val listMediaResponse = allMedia.results
listMediaResponse.forEach { mediaResponse ->
listMedia.add(mediaMapper.searchMapToMedia(mediaResponse))
listMedia.add(mediaMapper.mapToMovie(mediaResponse))
}
callback(listMedia)
}
override fun onFailure(call: Call<Demmy>, t: Throwable) {
Log.d("ErrorRetrofit", t.message.toString())
}
})
}
fun getPopularSeries(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getPopularSeries(apiKey)
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
override fun onResponse(call: Call<Demmy>, response: Response<Demmy>) {
Log.d("Liste1", response.body().toString())
val allMedia = response.body()!!
val listMediaResponse = allMedia.results
listMediaResponse.forEach { mediaResponse ->
listMedia.add(mediaMapper.mapToSerie(mediaResponse))
}
callback(listMedia)
}
return listMedia
override fun onFailure(call: Call<Demmy>, t: Throwable) {
Log.d("ErrorRetrofit", t.message.toString())
}
})
}
fun getForrestGump(): List<Media> {
val response: Call<Demmy> = service.getForrestGump()
fun getTopRatedMovie(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedMovie(apiKey)
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
@ -74,15 +94,35 @@ class MediaApi {
val allMedia = response.body()!!
val listMediaResponse = allMedia.results
listMediaResponse.forEach { mediaResponse ->
listMedia.add(mediaMapper.searchMapToMedia(mediaResponse))
listMedia.add(mediaMapper.mapToMovie(mediaResponse))
}
callback(listMedia)
}
override fun onFailure(call: Call<Demmy>, t: Throwable) {
Log.d("ErrorRetrofit", t.message.toString())
}
})
}
fun getTopRatedSerie(apiKey: String, callback: (List<Media>) -> Unit ) {
val response: Call<Demmy> = service.getTopRatedSerie(apiKey)
val listMedia: MutableList<Media> = mutableListOf()
response.enqueue(object : Callback<Demmy> {
override fun onResponse(call: Call<Demmy>, response: Response<Demmy>) {
Log.d("Liste1", response.body().toString())
val allMedia = response.body()!!
val listMediaResponse = allMedia.results
listMediaResponse.forEach { mediaResponse ->
listMedia.add(mediaMapper.mapToSerie(mediaResponse))
}
callback(listMedia)
}
return listMedia
override fun onFailure(call: Call<Demmy>, t: Throwable) {
Log.d("ErrorRetrofit", t.message.toString())
}
})
}
}

@ -7,13 +7,13 @@ import com.example.cinapp.model.Serie
class MediaMapper {
fun searchMapToMedia(mediaResponse: MediaResponse): Media {
return when (mediaResponse.media_type) {
"movie" -> searchMapToMovie(mediaResponse)
"tv" -> searchMapToSerie(mediaResponse)
"movie" -> mapToMovie(mediaResponse)
"tv" -> mapToSerie(mediaResponse)
else -> throw IllegalArgumentException("Invalid media type")
}
}
private fun searchMapToMovie(mediaResponse: MediaResponse): Movie {
fun mapToMovie(mediaResponse: MediaResponse): Movie {
return Movie(
id = mediaResponse.id,
title = mediaResponse.title,
@ -32,7 +32,7 @@ class MediaMapper {
)
}
private fun searchMapToSerie(mediaResponse: MediaResponse): Serie {
fun mapToSerie(mediaResponse: MediaResponse): Serie {
return Serie(
id = mediaResponse.id,
title = mediaResponse.name,

@ -5,9 +5,6 @@ import retrofit2.http.GET
import retrofit2.http.Query
interface MediaService {
@GET("search/movie?api_key=e53e59cf1e29b9afff93d9ca1208f0cf&query=Forrest%20Gump&language=fr")
fun getForrestGump(): Call<Demmy>
@GET("search/multi")
fun searchMedia(
@Query("api_key") apiKey: String,
@ -19,4 +16,13 @@ interface MediaService {
@GET("movie/popular")
fun getPopularMovies(@Query("api_key") apiKey: String): Call<Demmy>
@GET("tv/popular")
fun getPopularSeries(@Query("api_key") apiKey: String): Call<Demmy>
@GET("movie/top_rated")
fun getTopRatedMovie(@Query("api_key") apiKey: String): Call<Demmy>
@GET("tv/top_rated")
fun getTopRatedSerie(@Query("api_key") apiKey: String): Call<Demmy>
}

@ -0,0 +1,97 @@
package com.example.cinapp.model.navigation
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.cinapp.R
import com.example.cinapp.Request.MediaAdapter
import com.example.cinapp.Request.MediaApi
import com.example.cinapp.model.Media
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [HomeFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class HomeFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//getPopularMovies
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_home, container, false)
val apiKey = "e53e59cf1e29b9afff93d9ca1208f0cf"
MediaApi().getPopularMovies(apiKey) { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_popular_movies)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getPopularSeries(apiKey) { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_popular_series)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getTopRatedMovie(apiKey) { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_top_rated_movies)
addMediaToRecyclerView(listMedia, recyclerView)
}
MediaApi().getTopRatedSerie(apiKey) { listMedia ->
val recyclerView = rootView.findViewById<RecyclerView>(R.id.rc_top_rated_series)
addMediaToRecyclerView(listMedia, recyclerView)
}
return rootView
}
fun addMediaToRecyclerView(listMedia: List<Media>, recyclerView: RecyclerView) {
val layoutManager = StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)
recyclerView.layoutManager = layoutManager
val adapter = MediaAdapter(listMedia)
recyclerView.adapter = adapter
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"/>
</vector>

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".model.navigation.HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView3"
android:layout_width="414dp"
android:layout_height="24dp"
android:text="@string/current_popular_movies"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="-3dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rc_popular_movies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView4"
android:layout_width="414dp"
android:layout_height="24dp"
android:text="@string/current_popular_serie"
app:layout_constraintTop_toBottomOf="@+id/rc_popular_movies"
tools:layout_editor_absoluteX="0dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rc_popular_series"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textView6"
android:layout_width="414dp"
android:layout_height="24dp"
android:text="@string/movies_top_rated"
app:layout_constraintTop_toBottomOf="@+id/rc_popular_series"
tools:layout_editor_absoluteX="-3dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rc_top_rated_movies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
<TextView
android:id="@+id/textView5"
android:layout_width="414dp"
android:layout_height="24dp"
android:text="@string/movies_top_rated"
app:layout_constraintTop_toBottomOf="@+id/rc_top_rated_movies"
tools:layout_editor_absoluteX="0dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rc_top_rated_series"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Home"
android:id="@+id/home"
android:icon="@drawable/ic_baseline_home_24"/>/>
<item android:title="Movie"
android:id="@+id/movie"
android:icon="@drawable/ic_baseline_movie_24" />

@ -1,5 +1,12 @@
<resources>
<string name="app_name">Cin\'app</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="current_popular_movies">Current popular movies</string>
<string name="current_popular_serie">Current popular serie</string>
<string name="movies_in_theaters">Movies in theaters</string>
<string name="series_on_the_air">Series on the air</string>
<string name="movies_upcoming">Upcoming movies</string>
<string name="series_airing_today">Series airing today</string>
<string name="movies_top_rated">Top rated movies</string>
<string name="series_top_rated">Top rated series</string>
</resources>
Loading…
Cancel
Save