-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from reactivedroid/feature/clean-arch
More towards clean code architecture
- Loading branch information
Showing
22 changed files
with
242 additions
and
119 deletions.
There are no files selected for viewing
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
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
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
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/android/tvflix/domain/AddToFavoritesUseCase.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,17 @@ | ||
package com.android.tvflix.domain | ||
|
||
import com.android.tvflix.db.favouriteshow.FavoriteShow | ||
import com.android.tvflix.di.IoDispatcher | ||
import com.android.tvflix.favorite.FavoriteShowsRepository | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import javax.inject.Inject | ||
|
||
class AddToFavoritesUseCase @Inject | ||
constructor( | ||
private val favoriteShowsRepository: FavoriteShowsRepository, | ||
@IoDispatcher ioDispatcher: CoroutineDispatcher | ||
) : SuspendUseCase<FavoriteShow, Unit>(ioDispatcher) { | ||
override suspend fun execute(parameters: FavoriteShow) { | ||
favoriteShowsRepository.insertIntoFavorites(parameters) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/android/tvflix/domain/GetFavoriteShowsUseCase.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,21 @@ | ||
package com.android.tvflix.domain | ||
|
||
import com.android.tvflix.db.favouriteshow.FavoriteShow | ||
import com.android.tvflix.di.IoDispatcher | ||
import com.android.tvflix.favorite.FavoriteShowsRepository | ||
import com.android.tvflix.home.HomeViewModel | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
class GetFavoriteShowsUseCase | ||
@Inject | ||
constructor( | ||
private val favoriteShowsRepository: FavoriteShowsRepository, | ||
@IoDispatcher ioDispatcher: CoroutineDispatcher | ||
) : SuspendUseCase<Unit, List<Long>>(ioDispatcher) { | ||
override suspend fun execute(parameters: Unit): List<Long> { | ||
return favoriteShowsRepository.allFavoriteShowIds() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/android/tvflix/domain/GetSchedulesUseCase.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,34 @@ | ||
package com.android.tvflix.domain | ||
|
||
import com.android.tvflix.di.IoDispatcher | ||
import com.android.tvflix.model.respositores.SchedulesRepository | ||
import com.android.tvflix.network.home.Episode | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
|
||
class GetSchedulesUseCase | ||
@Inject | ||
constructor( | ||
private val schedulesRepository: SchedulesRepository, | ||
@IoDispatcher ioDispatcher: CoroutineDispatcher | ||
) : | ||
SuspendUseCase<Unit, List<Episode>>(ioDispatcher) { | ||
override suspend fun execute(parameters: Unit): List<Episode> { | ||
return schedulesRepository.getSchedule(country = COUNTRY, currentDate = currentDate) | ||
} | ||
|
||
companion object { | ||
private const val QUERY_DATE_FORMAT = "yyyy-MM-dd" | ||
const val COUNTRY = "US" | ||
private val currentDate: String | ||
get() { | ||
val simpleDateFormat = SimpleDateFormat(QUERY_DATE_FORMAT, Locale.US) | ||
val calendar = Calendar.getInstance() | ||
return simpleDateFormat.format(calendar.time) | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/android/tvflix/domain/RemoveFromFavoritesUseCase.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,19 @@ | ||
package com.android.tvflix.domain | ||
|
||
import com.android.tvflix.db.favouriteshow.FavoriteShow | ||
import com.android.tvflix.di.IoDispatcher | ||
import com.android.tvflix.favorite.FavoriteShowsRepository | ||
import com.android.tvflix.network.home.Show | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import javax.inject.Inject | ||
|
||
class RemoveFromFavoritesUseCase | ||
@Inject | ||
constructor( | ||
private val favoriteShowsRepository: FavoriteShowsRepository, | ||
@IoDispatcher ioDispatcher: CoroutineDispatcher | ||
) : SuspendUseCase<FavoriteShow, Unit>(ioDispatcher) { | ||
override suspend fun execute(parameters: FavoriteShow) { | ||
favoriteShowsRepository.removeFromFavorites(parameters) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/android/tvflix/domain/SuspendUseCase.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,35 @@ | ||
package com.android.tvflix.domain | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* Executes business logic synchronously or asynchronously using Coroutines. | ||
* | ||
* The [execute] method of [SuspendUseCase] is a suspend function | ||
*/ | ||
abstract class SuspendUseCase<in P, R>(private val coroutineDispatcher: CoroutineDispatcher) { | ||
|
||
/** Executes the use case asynchronously and returns a [Result]. | ||
* | ||
* @return a [Result]. | ||
* | ||
* @param parameters the input parameters to run the use case with | ||
*/ | ||
val tag: String = this.javaClass.simpleName | ||
|
||
suspend operator fun invoke(parameters: P): R { | ||
// Moving all use case's executions to the injected dispatcher | ||
// In production code, this is usually the Default dispatcher (background thread) | ||
// In tests, this becomes a TestCoroutineDispatcher | ||
return withContext(coroutineDispatcher) { | ||
execute(parameters) | ||
} | ||
} | ||
|
||
/** | ||
* Override this to set the code to be executed. | ||
*/ | ||
@Throws(RuntimeException::class) | ||
protected abstract suspend fun execute(parameters: P): R | ||
} |
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
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
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
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
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
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
Oops, something went wrong.