-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
54 deletions.
There are no files selected for viewing
19 changes: 9 additions & 10 deletions
19
app/src/main/java/com/salt/apps/moov/data/source/local/LocalDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
package com.salt.apps.moov.data.source.local | ||
|
||
import com.salt.apps.moov.data.source.local.entity.MoovEntity | ||
import com.salt.apps.moov.data.source.local.room.MoovDao | ||
import com.salt.apps.moov.data.source.local.entity.MovieEntity | ||
import com.salt.apps.moov.data.source.local.room.MovieDao | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class LocalDataSource @Inject constructor( | ||
private val moovDao: MoovDao | ||
private val movieDao: MovieDao | ||
) { | ||
suspend fun insertMoovs(moovs: List<MoovEntity>) = moovDao.insertAllMoov(moovs) | ||
suspend fun insertMovies(movies: List<MovieEntity>) = movieDao.insertMovies(movies) | ||
|
||
// suspend fun insertMovie(moovs: MovieEntity) = movieDao.insertMovie(moovs) | ||
fun getMovies(type: String) = movieDao.getMovies(type) | ||
|
||
fun getMoovs(type: String) = moovDao.getAllMoov(type) | ||
fun getFavoriteMovies() = movieDao.getFavoriteMovies() | ||
|
||
// fun getFavoriteMoovs() = moovDao.getFavoriteMoov() | ||
fun getMovieById(moovId: Int) = movieDao.getMovieById(moovId) | ||
|
||
// fun isFavoriteMoov(movieId: Int) = moovDao.isFavoriteMovie(movieId) | ||
|
||
// suspend fun updateMoov(movieId: Int, isFavorite: Boolean) = moovDao.updateMoovById(movieId, isFavorite) | ||
suspend fun updateMovieById(movieId: Int, isFavorite: Boolean) = | ||
movieDao.updateMovieById(movieId, isFavorite) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
app/src/main/java/com/salt/apps/moov/data/source/local/room/MoovDao.kt
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
app/src/main/java/com/salt/apps/moov/data/source/local/room/MoovDatabase.kt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/salt/apps/moov/data/source/local/room/MovieDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.salt.apps.moov.data.source.local.room | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.salt.apps.moov.data.source.local.entity.MovieEntity | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface MovieDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertMovies(movies: List<MovieEntity>) | ||
|
||
@Query("SELECT * FROM moov WHERE movieType = :movieType") | ||
fun getMovies(movieType: String): Flow<List<MovieEntity>> | ||
|
||
@Query("SELECT * FROM moov WHERE id=:movieId ORDER BY id") | ||
fun getMovieById(movieId: Int): Flow<MovieEntity> | ||
|
||
@Query("UPDATE moov SET isFavorite = :isFavorite WHERE id = :movieId") | ||
suspend fun updateMovieById(movieId: Int, isFavorite: Boolean) | ||
|
||
@Query("SELECT * FROM moov WHERE isFavorite = 1") | ||
fun getFavoriteMovies(): Flow<List<MovieEntity>> | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/salt/apps/moov/data/source/local/room/MovieDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.salt.apps.moov.data.source.local.room | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverter | ||
import androidx.room.TypeConverters | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import com.salt.apps.moov.data.source.local.entity.MovieEntity | ||
import com.salt.apps.moov.utilities.Constants.DB_VERSION | ||
|
||
@Database( | ||
entities = [MovieEntity::class], | ||
version = DB_VERSION, | ||
exportSchema = false | ||
) | ||
|
||
@TypeConverters(Converters::class) | ||
abstract class MovieDatabase : RoomDatabase() { | ||
abstract fun movieDao(): MovieDao | ||
} | ||
|
||
class Converters { | ||
|
||
@TypeConverter | ||
fun fromString(value: String?): List<Int>? { | ||
if (value == null) { | ||
return emptyList() | ||
} | ||
val listType = object : TypeToken<List<Int>>() {}.type | ||
return Gson().fromJson(value, listType) | ||
} | ||
|
||
@TypeConverter | ||
fun toString(list: List<Int>?): String { | ||
return Gson().toJson(list) | ||
} | ||
} |