Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class PopularLocalDataSourceImpl @Inject constructor(

private fun deleteExpiredData() {
CoroutineScope(Dispatchers.IO).launch {
popularSectionDao.getAll().forEach { popularLocal ->
if (popularLocal.date.isDayExpired())
popularSectionDao.getAll().forEach { _ ->
if (isDayExpired())
popularSectionDao.deleteAll()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TopRatedDataSourceImpl @Inject constructor(
private fun deleteExpiredData() {
CoroutineScope(Dispatchers.IO).launch {
topRatedDao.getAll().forEach { popularLocal ->
if (popularLocal.date.isDayExpired())
if (isDayExpired())
topRatedDao.deleteAll()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UpComingLocalDataSourceImpl @Inject constructor(
private fun deleteExpiredData() {
CoroutineScope(Dispatchers.IO).launch {
upcomingSectionDao.getAll().forEach { upcomingSectionLocal ->
if (upcomingSectionLocal.date.isDayExpired())
if (isDayExpired())
upcomingSectionDao.deleteAll()
}
}
Expand Down
7 changes: 0 additions & 7 deletions data/src/main/java/com/london/data/local/utils/jsonList.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.london.data.remote.model

object ApiConstants {

const val SEARCH_PATH_MOVIES = "search/movie"
const val SEARCH_PATH_TVS = "search/tv"
const val SEARCH_PATH_ACTORS = "search/person"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.london.data.remote.model.toprated


import com.london.data.mapper.genre.GenreMapper
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.london.data.remote.model.toprated


import com.london.data.mapper.genre.GenreMapper
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import retrofit2.http.Query
interface AccountApiService {

@GET(ApiConstants.GET_ACCOUNT_DETAILS)
suspend fun getAccountDetails(
suspend fun getAccountInfo(
@Query("session_id") sessionId: String
): Response<AccountInfoResponse>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.london.data.remote.source.account
import com.london.data.remote.model.account.AccountInfoResponse

interface AccountRemoteDataSource {
suspend fun getAccountDetails(sessionId: String): Result<AccountInfoResponse>
suspend fun getAccountInfo(sessionId: String): Result<AccountInfoResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import javax.inject.Inject
class AccountRemoteDataSourceImpl @Inject constructor(
private val accountApiService: AccountApiService
) : AccountRemoteDataSource, BaseRemoteDatasource {
override suspend fun getAccountDetails(sessionId: String): Result<AccountInfoResponse> = callApi(
apiCall = { accountApiService.getAccountDetails(sessionId) },
override suspend fun getAccountInfo(sessionId: String): Result<AccountInfoResponse> = callApi(
apiCall = { accountApiService.getAccountInfo(sessionId) },
mapper = { it }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AccountRepositoryImpl @Inject constructor(

override suspend fun getAccountInfo(): AccountInfo =
authenticationPreferences.getSessionId()?.let { sessionId ->
accountRemoteDataSource.getAccountDetails(sessionId)
accountRemoteDataSource.getAccountInfo(sessionId)
.getOrThrow()
.toEntity()
} ?: AccountInfo(id = 0, userName = "", avatarPath = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AuthenticationRepositoryImpl @Inject constructor(
}

private suspend fun getUserAccount(session: SessionResponse) {
val accountResult = accountRemoteDataSource.getAccountDetails(session.sessionId.orEmpty())
val accountResult = accountRemoteDataSource.getAccountInfo(session.sessionId.orEmpty())
val accountInfo = accountResult.getOrThrow().toEntity()
val accountId = accountInfo.id
saveUserAccount(id = accountId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.london.data.mapper.search.toReviewEntity
import com.london.data.remote.source.movie.MovieRemoteDataSource
import com.london.data.utils.CrashReporter
import com.london.data.utils.asYoutubeUrlOrEmpty
import com.london.data.utils.fetchAndSync
import com.london.data.utils.loadFromCacheOrFetch
import com.london.domain.entity.actor.ActorMediaDetails
import com.london.domain.entity.genre.MovieGenre
import com.london.domain.entity.movie.Movie
Expand Down Expand Up @@ -71,7 +71,7 @@ class MovieRepositoryImpl @Inject constructor(

override suspend fun getMovieReviews(
movieId: Int, pageNumber: Int
): PagedFetchResponse<Review> = fetchAndSync(
): PagedFetchResponse<Review> = loadFromCacheOrFetch(
networkBlock = { getMovieReviewsFromRemote(movieId = movieId, pageNumber = pageNumber) }
).run {
PagedFetchResponse(
Expand All @@ -95,7 +95,7 @@ class MovieRepositoryImpl @Inject constructor(
override suspend fun getUpcomingMoviesByGenre(
genre: MovieGenre,
pageNumber: Int
): PagedFetchResponse<UpComingMovie> = fetchAndSync(
): PagedFetchResponse<UpComingMovie> = loadFromCacheOrFetch(
cacheBlock = { getUpComingCachedMovies(pageNumber = pageNumber, genre = genre) },
crashReporter = crashReporter,
syncBlock = { upComingLocalDataSource.insert(it) },
Expand All @@ -110,7 +110,7 @@ class MovieRepositoryImpl @Inject constructor(
}

override suspend fun getTopRatedMovies(pageNumber: Int): PagedFetchResponse<TopRatedMedia> =
fetchAndSync(
loadFromCacheOrFetch(
cacheBlock = { getTopRatedCashedMovies() },
networkBlock = { getTopRatedRemoteMovies(pageNumber) },
syncBlock = { topRatedMovies ->
Expand All @@ -119,7 +119,7 @@ class MovieRepositoryImpl @Inject constructor(
crashReporter = crashReporter
).run { getTopRatedPages(pageNumber) }

override suspend fun getFirstPageTopRatedMovies() = fetchAndSync(
override suspend fun getFirstPageTopRatedMovies() = loadFromCacheOrFetch(
cacheBlock = { getTopRatedCashedMovies() },
networkBlock = { fetchFirstTopRatedPageMovies() },
syncBlock = { topRatedMovies ->
Expand Down Expand Up @@ -156,7 +156,7 @@ class MovieRepositoryImpl @Inject constructor(
).isSuccess
}

override suspend fun getPopularMovies(): List<PopularMedia> = fetchAndSync(
override suspend fun getPopularMovies(): List<PopularMedia> = loadFromCacheOrFetch(
cacheBlock = { getCachedPopularMovies() },
networkBlock = { movieRemoteDataSource.getPopularMovies().getOrThrow().toPopularMovies() },
syncBlock = { popularList ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.london.data.mapper.search.toReviewEntity
import com.london.data.remote.source.tvshow.TvShowRemoteDataSource
import com.london.data.utils.CrashReporter
import com.london.data.utils.asYoutubeUrlOrEmpty
import com.london.data.utils.fetchAndSync
import com.london.data.utils.loadFromCacheOrFetch
import com.london.domain.entity.actor.ActorMediaDetails
import com.london.domain.entity.genre.TvShowGenre
import com.london.domain.entity.popular.PopularMedia
Expand Down Expand Up @@ -70,7 +70,7 @@ class TvShowRepositoryImpl @Inject constructor(

override suspend fun getTopRatedTvShows(
pageNumber: Int
): PagedFetchResponse<TopRatedMedia> = fetchAndSync(
): PagedFetchResponse<TopRatedMedia> = loadFromCacheOrFetch(
cacheBlock = { getTopRatedCachedTvShows() },
networkBlock = { getTopRatedRemoteTvShows(pageNumber) },
syncBlock = { topRatedTvShows ->
Expand All @@ -79,7 +79,7 @@ class TvShowRepositoryImpl @Inject constructor(
crashReporter = crashReporter
).run { getTopRatedPages(pageNumber) }

override suspend fun getFirstPageTopRatedTvShows() = fetchAndSync(
override suspend fun getFirstPageTopRatedTvShows() = loadFromCacheOrFetch(
cacheBlock = { getTopRatedCachedTvShows() },
networkBlock = { fetchFirstTopRatedPageTvShows() },
syncBlock = { topRatedTvShows ->
Expand Down Expand Up @@ -114,7 +114,7 @@ class TvShowRepositoryImpl @Inject constructor(
sessionId = authenticationPreferences.getSessionId()
).isSuccess

override suspend fun getPopularTvShows(): List<PopularMedia> = fetchAndSync(
override suspend fun getPopularTvShows(): List<PopularMedia> = loadFromCacheOrFetch(
cacheBlock = { getCachedPopularTvShows() },
networkBlock = { tvShowRemoteDataSource.getPopularTvShows().getOrThrow().toPopularTvShows() },
syncBlock = { popularList ->
Expand Down Expand Up @@ -181,7 +181,7 @@ class TvShowRepositoryImpl @Inject constructor(
override suspend fun getTvShowReviews(
tvShowId: Int,
pageNumber: Int
): PagedFetchResponse<Review> = fetchAndSync(
): PagedFetchResponse<Review> = loadFromCacheOrFetch(
networkBlock = { getTvShowReviewsFromRemote(tvShowId = tvShowId, pageNumber = pageNumber) }
).run {
PagedFetchResponse(
Expand Down
2 changes: 1 addition & 1 deletion data/src/main/java/com/london/data/utils/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun String.extractYear() =

fun Int.orDefault(default: Int = 1): Int = if (this != 0) this else default

fun Long.isDayExpired(): Boolean {
fun isDayExpired(): Boolean {
val oneDayInMillis = 24 * 60 * 60 * 1000L
val oneDayAgo = System.currentTimeMillis() - oneDayInMillis
return System.currentTimeMillis() < oneDayAgo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.london.data.utils

suspend fun <T> Result<T?>.getNotNullOrElse(elseBlock: suspend () -> T): Result<T> =
runCatching { getOrElse { elseBlock() } ?: elseBlock() }

suspend fun <T> fetchAndSync(
suspend fun <T> loadFromCacheOrFetch(
cacheBlock: (suspend () -> T?)? = null,
networkBlock: suspend () -> T,
syncBlock: (suspend (T) -> Unit)? = null,
Expand All @@ -13,3 +10,6 @@ suspend fun <T> fetchAndSync(
syncBlock?.invoke(it)
}
}.onFailure { crashReporter?.logException(it) }.getOrThrow()

private suspend fun <T> Result<T?>.getNotNullOrElse(elseBlock: suspend () -> T): Result<T> =
runCatching { getOrElse { elseBlock() } ?: elseBlock() }
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AuthenticationRepositoryImplTest {
coEvery { authRemoteDataSource.createSession(REQUEST_TOKEN) } returns Result.success(
SessionResponse(true, SESSION_ID)
)
coEvery { accountRemoteDataSource.getAccountDetails(SESSION_ID) } returns Result.success(
coEvery { accountRemoteDataSource.getAccountInfo(SESSION_ID) } returns Result.success(
AccountInfoResponse(
id = ACCOUNT_ID,
userName = USERNAME,
Expand All @@ -78,7 +78,7 @@ class AuthenticationRepositoryImplTest {
authenticationPreferences.setGuestMode(false)
authenticationPreferences.saveAccountId(ACCOUNT_ID)
}
coVerify { accountRemoteDataSource.getAccountDetails(SESSION_ID) }
coVerify { accountRemoteDataSource.getAccountInfo(SESSION_ID) }
}

@Test
Expand All @@ -100,7 +100,7 @@ class AuthenticationRepositoryImplTest {

assertFalse(result)
coVerify(exactly = 0) { authenticationPreferences.saveSessionId(any()) }
coVerify(exactly = 0) { accountRemoteDataSource.getAccountDetails(any()) }
coVerify(exactly = 0) { accountRemoteDataSource.getAccountInfo(any()) }
}

@Test
Expand Down Expand Up @@ -154,7 +154,7 @@ class AuthenticationRepositoryImplTest {
coEvery { authRemoteDataSource.createSession(REQUEST_TOKEN) } returns Result.success(
SessionResponse(true, SESSION_ID)
)
coEvery { accountRemoteDataSource.getAccountDetails(SESSION_ID) } returns Result.failure(
coEvery { accountRemoteDataSource.getAccountInfo(SESSION_ID) } returns Result.failure(
accountError
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.london.data.remote.exception.ResponseException
import com.london.data.remote.source.search.SearchRemoteDataSource
import com.london.data.repository.search.SearchRepositoryImpl
import com.london.data.utils.CrashReporter
import com.london.data.utils.fetchAndSync
import com.london.data.utils.loadFromCacheOrFetch
import com.london.domain.entity.genre.MovieGenre
import com.london.domain.entity.genre.TvShowGenre
import io.mockk.coEvery
Expand Down Expand Up @@ -52,7 +52,7 @@ class SearchRepositoryImplTest {
val actualException = assertThrows<RuntimeException>(
expectedException::class.java.simpleName
) {
fetchAndSync(
loadFromCacheOrFetch(
cacheBlockAction,
networkBlockAction,
syncBlockAction,
Expand Down Expand Up @@ -129,7 +129,7 @@ class SearchRepositoryImplTest {


val actualException = assertThrows<RuntimeException> {
fetchAndSync(
loadFromCacheOrFetch(
cacheBlock = { throw cacheException },
networkBlock = { throw networkException },
syncBlock = { /* Do nothing */ },
Expand Down
Loading
Loading