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) |
View ImageIntent.kt
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) | |
} |
View FileProvider.xml
<?xml version="1.0" encoding="utf-8"?> | |
<paths> | |
<files-path | |
name="images" | |
path="." /> | |
</paths> |
NewerOlder