Skip to content

Instantly share code, notes, and snippets.

View docker-compose2.yml
version: "3.9"
secrets:
POSTGRES_PASSWORD:
external: true
services:
api_service:
image: yourServerImage:latest
deploy:
View docker-compose1.yml
version: "3.9"
services:
api_service:
build: .
restart: always
ports:
- 8080:8080
depends_on:
- db
View BaseUseCase.kt
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) },
View PopularMoviesPresenter.kt
class PopularMoviesPresenter(
private val getPopularMovies: GetPopularMovies,
coroutineContext: CoroutineContext = defaultDispatcher
) : BasePresenter<PopularMoviesView>(coroutineContext) {
override fun onViewAttached(view: PopularMoviesView) {
view.setLoadingVisible(true)
getPopularMovies()
}
View BasePresenter.kt
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)
}
View GetPopularMovies.kt
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)
}
View AndroidClientEngine.kt
actual val httpClientEngine: HttpClientEngine by lazy {
OkHttp.create {
val networkInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
addNetworkInterceptor(networkInterceptor)
}
}
View MoviesApi.kt
private val client = HttpClient(clientEngine) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
suspend fun getPopularMovies(): PopularMoviesEntity {
val response = client.get<HttpResponse> {
url {
protocol = URLProtocol.HTTPS
View DownloadProgressBody.kt
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() {
View Rotate.kt
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)