Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cache detail #58

Merged
merged 8 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/database/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
13 changes: 13 additions & 0 deletions data/database/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("io.filmtime.gradle.android.data")
}

android {
namespace = "io.filmtime.data.database"
}

dependencies {
implementation(libs.room.runtime)
implementation(libs.room.ktx)
kapt(libs.room.compiler)
}
Empty file.
21 changes: 21 additions & 0 deletions data/database/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions data/database/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.filmtime.data.database

import android.content.Context
import androidx.room.Room
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
moallemi marked this conversation as resolved.
Show resolved Hide resolved

@Singleton
@Provides
fun providesDatabase(@ApplicationContext context: Context): FilmTimeDatabase {
return Room.databaseBuilder(context, FilmTimeDatabase::class.java, "film-time-db")
.build()
}

@Provides
@Singleton
fun providesMovieDetailDao(db: FilmTimeDatabase): MovieDetailDao = db.movieDao()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.filmtime.data.database

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [MovieDetailEntity::class], version = 1)
abstract class FilmTimeDatabase : RoomDatabase() {
moallemi marked this conversation as resolved.
Show resolved Hide resolved

abstract fun movieDao(): MovieDetailDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.filmtime.data.database

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface MovieDetailDao {

@Query("SELECT * FROM MovieDetailEntity WHERE tmdbId = :tmdbId")
suspend fun getMovieByTmdbId(tmdbId: Int): MovieDetailEntity?

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun storeMovie(movie: MovieDetailEntity)

@Query("DELETE FROM MovieDetailEntity")
suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.filmtime.data.database

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class MovieDetailEntity(
moallemi marked this conversation as resolved.
Show resolved Hide resolved
@PrimaryKey
val tmdbId: Int,
val name: String,
val posterUrl: String,
val coverUrl: String,
val year: Int,
val description: String,
val releaseDate: String,
val runtime: String?,
val originalLanguage: String?,
)
1 change: 1 addition & 0 deletions data/tmdb-movies/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dependencies {
implementation(project(":data:api:trakt"))

api(libs.paging.runtime)
implementation(project(":data:database"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.fimltime.data.tmdb.movies

import io.filmtime.data.database.MovieDetailEntity
import io.filmtime.data.model.VideoDetail
import io.filmtime.data.model.VideoId

fun MovieDetailEntity.toMovie(): VideoDetail {
return VideoDetail(
ids = VideoId(tmdbId = tmdbId, traktId = null),
title = name,
posterUrl = posterUrl,
coverUrl = coverUrl,
year = year,
genres = listOf(),
moallemi marked this conversation as resolved.
Show resolved Hide resolved
description = description,
releaseDate = releaseDate,
isWatched = null,
runtime = runtime,
voteAverage = 0F,
voteColor = 0,
originalLanguage = originalLanguage,
spokenLanguages = listOf(),
)
}

fun VideoDetail.toEntity(): MovieDetailEntity {
return MovieDetailEntity(
tmdbId = ids.tmdbId!!,
name = title,
coverUrl = coverUrl,
description = description,
posterUrl = posterUrl,
releaseDate = releaseDate,
year = year,
originalLanguage = originalLanguage,
runtime = runtime,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow

interface TmdbMovieRepository {

suspend fun getMovieDetails(movieId: Int): Result<VideoDetail, GeneralError>
suspend fun getMovieDetails(movieId: Int): Flow<Result<VideoDetail, GeneralError>>

suspend fun getTrendingMovies(): Result<List<VideoThumbnail>, GeneralError>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,35 @@ import androidx.paging.PagingData
import io.filmtime.data.api.tmdb.TmdbMoviesRemoteSource
import io.filmtime.data.api.trakt.TraktSearchRemoteSource
import io.filmtime.data.api.trakt.TraktSyncRemoteSource
import io.filmtime.data.database.MovieDetailDao
import io.filmtime.data.model.CreditItem
import io.filmtime.data.model.GeneralError
import io.filmtime.data.model.Result
import io.filmtime.data.model.VideoDetail
import io.filmtime.data.model.VideoThumbnail
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

internal class TmdbMovieRepositoryImpl @Inject constructor(
class TmdbMovieRepositoryImpl @Inject constructor(
moallemi marked this conversation as resolved.
Show resolved Hide resolved
private val tmdbMoviesRemoteSource: TmdbMoviesRemoteSource,
private val traktMovieSearchRemoteSource: TraktSearchRemoteSource,
private val traktSyncRemoteSource: TraktSyncRemoteSource,
private val movieDao: MovieDetailDao,
) : TmdbMovieRepository {

override suspend fun getMovieDetails(movieId: Int): Result<VideoDetail, GeneralError> {
return when (val result = tmdbMoviesRemoteSource.movieDetails(movieId)) {
override suspend fun getMovieDetails(movieId: Int): Flow<Result<VideoDetail, GeneralError>> = flow {
val localMovie = movieDao.getMovieByTmdbId(movieId)
if (localMovie != null) emit(Result.Success(localMovie.toMovie()))
val result = when (val result = tmdbMoviesRemoteSource.movieDetails(movieId)) {
is Result.Failure -> result
is Result.Success -> {
return when (val traktIdResult = traktMovieSearchRemoteSource.getByTmdbId(result.data.ids.tmdbId.toString())) {
movieDao.storeMovie(result.data.toEntity())
when (val traktIdResult = traktMovieSearchRemoteSource.getByTmdbId(result.data.ids.tmdbId.toString())) {
is Result.Failure -> traktIdResult
is Result.Success -> {
val traktId = traktIdResult.data.toInt()
return when (val watched = traktSyncRemoteSource.getHistoryById(traktId.toString())) {
when (val watched = traktSyncRemoteSource.getHistoryById(traktId.toString())) {
is Result.Failure -> result.run {
copy(
data = data.copy(
Expand All @@ -39,21 +45,25 @@ internal class TmdbMovieRepositoryImpl @Inject constructor(
)
}

is Result.Success -> result.run {
copy(
data = data.copy(
ids = data.ids.copy(
traktId = traktId,
is Result.Success -> {
result.run {
copy(
data = data.copy(
ids = data.ids.copy(
traktId = traktId,
),
isWatched = watched.data,
),
isWatched = watched.data,
),
)
)
}
}
}
}
}
}
}

emit(result)
moallemi marked this conversation as resolved.
Show resolved Hide resolved
}

override suspend fun getTrendingMovies(): Result<List<VideoThumbnail>, GeneralError> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.filmtime.domain.tmdb.movies
import io.filmtime.data.model.GeneralError
import io.filmtime.data.model.Result
import io.filmtime.data.model.VideoDetail
import kotlinx.coroutines.flow.Flow

interface GetMovieDetailsUseCase {
suspend operator fun invoke(movieId: Int): Result<VideoDetail, GeneralError>
suspend operator fun invoke(movieId: Int): Flow<Result<VideoDetail, GeneralError>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import io.filmtime.data.model.Result
import io.filmtime.data.model.VideoDetail
import io.filmtime.domain.tmdb.movies.GetMovieDetailsUseCase
import io.fimltime.data.tmdb.movies.TmdbMovieRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

internal class GetMovieDetailsUseCaseImpl @Inject constructor(
private val tmdbMovieRepository: TmdbMovieRepository,
) : GetMovieDetailsUseCase {

override suspend fun invoke(movieId: Int): Result<VideoDetail, GeneralError> =
override suspend fun invoke(movieId: Int): Flow<Result<VideoDetail, GeneralError>> =
tmdbMovieRepository.getMovieDetails(movieId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,46 @@ class MovieDetailViewModel @Inject constructor(
}

fun load() = viewModelScope.launch {
_state.value = _state.value.copy(isLoading = true)
_state.update { it.copy(isLoading = true) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a known bug here that we should reset error as well. otherwise our offline implementation does not work well.

Suggested change
_state.update { it.copy(isLoading = true) }
_state.update { it.copy(isLoading = true, error = null) }


when (val result = getMovieDetail(videoId)) {
is Success -> {
_state.update { state ->
state.copy(videoDetail = result.data, isLoading = false)
getMovieDetail(videoId).collect {
when (val result = it) {
is Success -> {
_state.update { state ->
state.copy(videoDetail = result.data, isLoading = false)
}
}
}

is Failure -> {
when (result.error) {
is GeneralError.ApiError -> {
_state.update { state ->
state.copy(
error = result.error,
message = (result.error as GeneralError.ApiError).message,
isLoading = false,
)
is Failure -> {
when (result.error) {
is GeneralError.ApiError -> {
_state.update { state ->
state.copy(
error = result.error,
message = (result.error as GeneralError.ApiError).message,
isLoading = false,
)
}
}
}

GeneralError.NetworkError -> {
_state.update { state ->
GeneralError.NetworkError -> {
_state.update { state ->
state.copy(
error = result.error,
message = "No internet connection. Please check your network settings.",
isLoading = false,
)
}
}

is GeneralError.UnknownError -> _state.update { state ->
state.copy(
error = result.error,
message = "No internet connection. Please check your network settings.",
message = (result.error as GeneralError.UnknownError).error.message,
isLoading = false,
)
}
}

is GeneralError.UnknownError -> _state.update { state ->
state.copy(
error = result.error,
message = (result.error as GeneralError.UnknownError).error.message,
isLoading = false,
)
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ material = "1.11.0"
media3-exoplayer = "1.2.1"
datastore = "1.0.0"
paging = "3.2.1"
room = "2.6.1"
ksp = "1.9.23-1.0.20"

[libraries]
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3-exoplayer" }
Expand Down Expand Up @@ -57,6 +59,10 @@ lottie = "com.airbnb.android:lottie-compose:6.3.0"
datastore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore" }
androidx-ui-graphics-android = { group = "androidx.compose.ui", name = "ui-graphics-android", version = "1.6.4" }

room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }


# Dependencies of the included build-logic
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
Expand All @@ -70,6 +76,7 @@ com-android-library = { id = "com.android.library", version.ref = "agp" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt-android" }
spotless = "com.diffplug.spotless:6.20.0"
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
moallemi marked this conversation as resolved.
Show resolved Hide resolved

[bundles]

1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ include(":core:design-system")
include(":feature:movies")
include(":feature:shows")
include(":feature:settings")
include(":data:database")