Skip to content

Instantly share code, notes, and snippets.

View Syex's full-sized avatar

Tom Seifert Syex

View GitHub Profile
version: "3.9"
services:
api_service:
build: .
restart: always
ports:
- 8080:8080
depends_on:
- db
abstract class UseCase<out Type, in Params> where Type : Any {
abstract suspend fun run(params: Params): Either<Exception, Type>
suspend operator fun invoke(params: Params, onSuccess: (Type) -> Unit, onFailure: (Exception) -> Unit) {
val result = run(params)
coroutineScope {
launch(uiDispatcher) {
result.fold(
failed = { onFailure(it) },
class PopularMoviesPresenter(
private val getPopularMovies: GetPopularMovies,
coroutineContext: CoroutineContext = defaultDispatcher
) : BasePresenter<PopularMoviesView>(coroutineContext) {
override fun onViewAttached(view: PopularMoviesView) {
view.setLoadingVisible(true)
getPopularMovies()
}
abstract class BasePresenter<T>(private val coroutineContext: CoroutineContext) {
protected var view: T? = null
protected lateinit var scope: PresenterCoroutineScope
fun attachView(view: T) {
this.view = view
scope = PresenterCoroutineScope(coroutineContext)
onViewAttached(view)
}
class GetPopularMovies(private val moviesApi: MoviesApi)
: UseCase<PopularMovies, UseCase.None>() {
override suspend fun run(params: None): Either<Exception, PopularMovies> {
return try {
val movies = moviesApi.getPopularMovies().toModel()
Success(movies)
} catch (e: Exception) {
Failure(e)
}
actual val httpClientEngine: HttpClientEngine by lazy {
OkHttp.create {
val networkInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
addNetworkInterceptor(networkInterceptor)
}
}
private val client = HttpClient(clientEngine) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
suspend fun getPopularMovies(): PopularMoviesEntity {
val response = client.get<HttpResponse> {
url {
protocol = URLProtocol.HTTPS
@Syex
Syex / DownloadProgressBody.kt
Created January 7, 2019 09:55
An OkHttp ResponseBody to observe download progress. It's the sample migrated to Kotlin: https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java
private const val EXHAUSTED_SOURCE = -1L
/**
* A [ResponseBody] that informs a [ProgressListener] about the download progress.
*/
class DownloadProgressBody(
private val responseBody: ResponseBody,
private val progressListener: ProgressListener
) : ResponseBody() {
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
val matrix = Matrix().apply { postRotate(90f) }
val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
val outputFile = File(filesDir, "test.jpeg")
val imageUri = FileProvider.getUriForFile(
this@MainActivity,
context.packageName.plus("provider"),
outputFile)
val takePhotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
}