parent
c9ca71b7a0
commit
a987d817b5
@ -0,0 +1,11 @@
|
||||
package fr.yobreuil.allomovies
|
||||
|
||||
import android.app.Application
|
||||
import fr.yobreuil.allomovies.data.persistance.AlloMoviesDatabase
|
||||
|
||||
class AlloMoviesApplication : Application() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
AlloMoviesDatabase.initialize(this)
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package fr.yobreuil.allomovies.api
|
||||
|
||||
import fr.yobreuil.allomovies.data.entities.Genre
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class GenreManager {
|
||||
private val URL = "https://api.themoviedb.org/3/genre/movie/list?api_key=3fda2cd4981d6b7ee72244a2cdd869a3&language=fr-FR"
|
||||
|
||||
fun getAllGenres() : Call<List<Genre>> {
|
||||
val retrofit = Retrofit.Builder()
|
||||
.baseUrl(URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
||||
val service = retrofit.create(GenreService::class.java)
|
||||
|
||||
return service.getGenres()
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package fr.yobreuil.allomovies.api
|
||||
|
||||
import fr.yobreuil.allomovies.data.entities.Genre
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
|
||||
interface GenreService {
|
||||
@GET("genre")
|
||||
fun getGenres() : Call<List<Genre>>
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package fr.yobreuil.allomovies.data.entities
|
||||
|
||||
import androidx.room.Entity
|
||||
|
||||
@Entity(primaryKeys = ["genreId", "movieId"])
|
||||
data class GenreMovieCrossRef(
|
||||
val genreId: Long,
|
||||
val movieId: Long
|
||||
)
|
@ -0,0 +1,15 @@
|
||||
package fr.yobreuil.allomovies.data.entities
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Junction
|
||||
import androidx.room.Relation
|
||||
|
||||
data class GenreWithMovies(
|
||||
@Embedded val genre: Genre,
|
||||
@Relation(
|
||||
parentColumn = "genreId",
|
||||
entityColumn = "movieId",
|
||||
associateBy = Junction(GenreMovieCrossRef::class)
|
||||
)
|
||||
val movies : List<Movie>
|
||||
)
|
@ -0,0 +1,15 @@
|
||||
package fr.yobreuil.allomovies.data.entities
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Junction
|
||||
import androidx.room.Relation
|
||||
|
||||
data class MovieWithGenres(
|
||||
@Embedded val movie: Movie,
|
||||
@Relation(
|
||||
parentColumn = "movieId",
|
||||
entityColumn = "genreId",
|
||||
associateBy = Junction(GenreMovieCrossRef::class)
|
||||
)
|
||||
val genres : List<Genre>
|
||||
)
|
@ -1,11 +0,0 @@
|
||||
package fr.yobreuil.allomovies.data.persistance
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import fr.yobreuil.allomovies.data.Genre
|
||||
|
||||
@Dao
|
||||
interface GenreDao {
|
||||
@Query("SELECT * FROM genres")
|
||||
fun getAll(): List<Genre>
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package fr.yobreuil.allomovies.data.persistance
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
|
||||
@Dao
|
||||
interface MovieDao {
|
||||
@Query("")
|
||||
fun getAll();
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package fr.yobreuil.allomovies.data.persistance.dao
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.*
|
||||
import fr.yobreuil.allomovies.data.entities.Genre
|
||||
import fr.yobreuil.allomovies.data.entities.GenreWithMovies
|
||||
|
||||
@Dao
|
||||
interface GenreDao {
|
||||
@Query("SELECT * FROM genres")
|
||||
fun getAll() : LiveData<List<Genre>>
|
||||
|
||||
@Query("SELECT * FROM genres WHERE id = :id")
|
||||
fun getById(id : Long) : LiveData<Genre>
|
||||
|
||||
@Query("SELECT")
|
||||
fun getGenreWithMovies(idGenre : Long)
|
||||
|
||||
@Query("SELECT")
|
||||
fun getMoviesByGenre(genreId: Long): LiveData<GenreWithMovies>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(genre: Genre)
|
||||
|
||||
@Insert
|
||||
suspend fun insertAll(vararg genres: Genre)
|
||||
|
||||
@Update(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun update(genre: Genre)
|
||||
|
||||
@Delete
|
||||
suspend fun delete(genre: Genre)
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package fr.yobreuil.allomovies.data.persistance.dao
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import fr.yobreuil.allomovies.data.entities.GenreWithMovies
|
||||
import fr.yobreuil.allomovies.data.entities.Movie
|
||||
|
||||
@Dao
|
||||
interface MovieDao {
|
||||
@Query("SELECT * FROM movies")
|
||||
fun getAll() : LiveData<List<Movie>>
|
||||
|
||||
@Query("SELECT * FROM movies ORDER BY movieName ASC")
|
||||
fun getAllAlphabetical() : LiveData<List<Movie>>
|
||||
|
||||
@Query("SELECT * FROM movies ORDER BY movieName ASC")
|
||||
fun getMoviesByGenre(genreId: Long) : LiveData<GenreWithMovies>;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package fr.yobreuil.allomovies.data.repositories
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import fr.yobreuil.allomovies.data.entities.Genre
|
||||
import fr.yobreuil.allomovies.data.persistance.dao.GenreDao
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class GenreRepository(private val genreDao: GenreDao) {
|
||||
suspend fun insert(genre: Genre) = withContext(Dispatchers.IO) { Log.i("GenreRepo", "INSERT"); genreDao.insert(genre) }
|
||||
suspend fun delete(genre: Genre) = withContext(Dispatchers.IO) { Log.i("GenreRepo", "DELETE"); genreDao.delete(genre) }
|
||||
suspend fun update(genre: Genre) = withContext(Dispatchers.IO) { Log.i("GenreRepo", "UPDATE"); genreDao.update(genre) }
|
||||
|
||||
fun findById(genreId: Long): LiveData<Genre> {
|
||||
Log.i("GenreRepo", "FIND GENRE")
|
||||
return genreDao.getById(genreId)
|
||||
}
|
||||
|
||||
fun getAll(): LiveData<List<Genre>> {
|
||||
Log.i("GenreRepo", "GET ALL GENRES")
|
||||
return genreDao.getAll()
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package fr.yobreuil.allomovies.data.repositories
|
||||
|
||||
class MovieRepository {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package fr.yobreuil.allomovies.ui.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import fr.yobreuil.allomovies.data.persistance.AlloMoviesDatabase
|
||||
import fr.yobreuil.allomovies.data.repositories.GenreRepository
|
||||
|
||||
class GenreViewModel : ViewModel() {
|
||||
private val genreRepo = GenreRepository(AlloMoviesDatabase.getInstance().genreDao())
|
||||
|
||||
val genreList = genreRepo.getAll()
|
||||
}
|
@ -1,23 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<layout 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"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="genreListVM"
|
||||
type="fr.yobreuil.allomovies.ui.viewmodel.GenreViewModel" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.fragment.GenreListFragment">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
<view class="androidx.appcompat.app.AlertController$RecycleListView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||
app:spanCount="2"
|
||||
tools:listitem="@layout/card_genre">
|
||||
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
<androidx.constraintlayout.widget.Group
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</view>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id 'com.android.application' version '7.4.0' apply false
|
||||
id 'com.android.library' version '7.4.0' apply false
|
||||
id 'com.android.application' version '7.4.1' apply false
|
||||
id 'com.android.library' version '7.4.1' apply false
|
||||
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
|
||||
}
|
Loading…
Reference in new issue