-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from asrovnov/develop
Develop
- Loading branch information
Showing
32 changed files
with
486 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,21 @@ | ||
incredible-pets | ||
Incredible Pets | ||
====================== | ||
|
||
[![Build Status](https://travis-ci.org/asrovnov/incredible-pets-android.svg?branch=master)](https://travis-ci.org/asrovnov/incredible-pets-android) | ||
[![Build Status](https://travis-ci.org/asrovnov/incredible-pets-android.svg?branch=master)](https://travis-ci.org/asrovnov/incredible-pets-android) | ||
|
||
--- | ||
![incredible-pets-start-screen](https://user-images.githubusercontent.com/28740150/75627008-6f370c00-5bdd-11ea-9cd0-7c625017fdbe.gif) | ||
--- | ||
|
||
***Stack:*** | ||
* Kotlin | ||
* Clean Architecture | ||
* Single-Activity | ||
* RxPM | ||
* RxJava | ||
* Koin | ||
* Retrofit | ||
* Moshi | ||
|
||
|
||
|
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
114 changes: 114 additions & 0 deletions
114
app/src/main/java/ru/app/incredible/pets/data/gateway/DownloadImageGateway.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,114 @@ | ||
package ru.app.incredible.pets.data.gateway | ||
|
||
import android.content.Context | ||
import com.jakewharton.rxrelay2.BehaviorRelay | ||
import com.liulishuo.filedownloader.BaseDownloadTask | ||
import com.liulishuo.filedownloader.FileDownloadSampleListener | ||
import com.liulishuo.filedownloader.FileDownloader | ||
import com.liulishuo.filedownloader.model.FileDownloadStatus | ||
import io.reactivex.Completable | ||
import io.reactivex.Observable | ||
import io.reactivex.Single | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.schedulers.Schedulers | ||
import ru.app.incredible.pets.domain.ImageDownloadState | ||
import ru.app.incredible.pets.domain.Pet | ||
import ru.app.incredible.pets.domain.exceptions.InternetUnavailableException | ||
import java.io.File | ||
|
||
class DownloadImageGateway( | ||
private val context: Context, | ||
private val networkStateGateway: NetworkStateGateway | ||
) { | ||
|
||
companion object { | ||
private const val DIR_IMAGE = "images" | ||
private const val BOTTOM_SPACE = "_" | ||
} | ||
|
||
private val downloadImageIds = mutableMapOf<Pet, Int>() | ||
private val fileDownloader = FileDownloader.getImpl() | ||
|
||
private val imageDownloadStates: BehaviorRelay<MutableMap<Pet, ImageDownloadState>> = | ||
BehaviorRelay.createDefault( | ||
mutableMapOf() | ||
) | ||
|
||
fun getDownloadState(pet: Pet, imageUrl: String): Observable<ImageDownloadState> { | ||
return imageDownloadStates.hide() | ||
.map { it[pet] ?: ImageDownloadState.IDLE } | ||
.doOnSubscribe { checkLoadImage(pet, imageUrl) } | ||
} | ||
|
||
fun startDownload(pet: Pet, imageUrl: String): Completable { | ||
return networkStateGateway | ||
.isNetworkEnabled() | ||
.firstOrError() | ||
.flatMapCompletable { | ||
if (it) { | ||
Completable.fromAction { | ||
imageDownload(pet, imageUrl) | ||
} | ||
} else { | ||
Completable.error(InternetUnavailableException()) | ||
} | ||
} | ||
} | ||
|
||
fun removeImage(imageUrl: String): Single<Boolean> { | ||
return Single | ||
.fromCallable { getImageFile(imageUrl).delete() } | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
} | ||
|
||
private fun imageDownload(pet: Pet, imageUrl: String) { | ||
val downloadStart = fileDownloader.create(imageUrl) | ||
.setPath(getImageFile(imageUrl).toString()) | ||
.setListener(object : FileDownloadSampleListener() { | ||
override fun started(task: BaseDownloadTask?) { | ||
updateDownloadState(pet, ImageDownloadState.PROGRESS) | ||
} | ||
|
||
override fun completed(task: BaseDownloadTask?) { | ||
updateDownloadState(pet, ImageDownloadState.FINISHED) | ||
} | ||
|
||
override fun error(task: BaseDownloadTask?, e: Throwable?) { | ||
updateDownloadState(pet, ImageDownloadState.ERROR) | ||
} | ||
}) | ||
|
||
downloadImageIds[pet] = downloadStart.start() | ||
} | ||
|
||
private fun getImageFile(imageUrl: String): File { | ||
val imageName = | ||
"${imageUrl.substringBeforeLast("/").substringAfterLast("/")}$BOTTOM_SPACE${imageUrl.substringAfterLast("/")}" | ||
|
||
return File(context.getExternalFilesDir(DIR_IMAGE), imageName) | ||
} | ||
|
||
private fun checkLoadImage(pet: Pet, imageUrl: String) { | ||
val downloadStatus = downloadImageIds[pet]?.let { | ||
fileDownloader.getStatusIgnoreCompleted(it) | ||
} | ||
|
||
when { | ||
getImageFile(imageUrl).exists() -> updateDownloadState(pet, ImageDownloadState.FINISHED) | ||
downloadStatus == FileDownloadStatus.progress -> updateDownloadState( | ||
pet, | ||
ImageDownloadState.PROGRESS | ||
) | ||
else -> updateDownloadState(pet, ImageDownloadState.IDLE) | ||
} | ||
} | ||
|
||
private fun updateDownloadState(pet: Pet, state: ImageDownloadState) { | ||
imageDownloadStates.accept( | ||
imageDownloadStates.value.apply { | ||
this!![pet] = state | ||
}!! | ||
) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/ru/app/incredible/pets/data/gateway/PetGateway.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,22 @@ | ||
package ru.app.incredible.pets.data.gateway | ||
|
||
import com.jakewharton.rxrelay2.BehaviorRelay | ||
import io.reactivex.Observable | ||
|
||
class PetGateway { | ||
|
||
companion object { | ||
const val PET_DOG = 0 | ||
const val PET_CAT = 1 | ||
} | ||
|
||
private val selectedPet: BehaviorRelay<Int> = BehaviorRelay.createDefault(PET_DOG) | ||
|
||
fun getSelectedPet(): Observable<Int> = selectedPet.hide() | ||
|
||
fun setSelectedPet(petType: Int) { | ||
if (selectedPet.hasValue().not() || selectedPet.value != petType) { | ||
selectedPet.accept(petType) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package ru.app.incredible.pets.di | ||
|
||
import org.koin.dsl.module | ||
import ru.app.incredible.pets.data.gateway.NetworkStateGateway | ||
import ru.app.incredible.pets.data.gateway.RandomCatGateway | ||
import ru.app.incredible.pets.data.gateway.RandomDogGateway | ||
import ru.app.incredible.pets.data.gateway.* | ||
|
||
object GatewayModule { | ||
|
||
fun create() = module { | ||
single { RandomDogGateway(get()) } | ||
single { RandomCatGateway(get()) } | ||
single { NetworkStateGateway(get()) } | ||
single { DownloadImageGateway(get(), get()) } | ||
single { PetGateway() } | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
app/src/main/java/ru/app/incredible/pets/di/InteractorModule.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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package ru.app.incredible.pets.di | ||
|
||
import org.koin.dsl.module | ||
import ru.app.incredible.pets.domain.RandomCatInteractor | ||
import ru.app.incredible.pets.domain.RandomDogInteractor | ||
import ru.app.incredible.pets.domain.* | ||
|
||
object InteractorModule { | ||
|
||
fun create() = module { | ||
factory { RandomDogInteractor(get(), get()) } | ||
factory { RandomCatInteractor(get(), get()) } | ||
factory { DownloadImageInteractor(get()) } | ||
factory { GetDownloadStateInteractor(get()) } | ||
factory { RemoveImageInteractor(get()) } | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ru/app/incredible/pets/domain/DownloadImageInteractor.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,13 @@ | ||
package ru.app.incredible.pets.domain | ||
|
||
import io.reactivex.Completable | ||
import ru.app.incredible.pets.data.gateway.DownloadImageGateway | ||
|
||
class DownloadImageInteractor( | ||
private val downloadImageGateway: DownloadImageGateway | ||
) { | ||
|
||
fun execute(pet: Pet, imageUrl: String): Completable { | ||
return downloadImageGateway.startDownload(pet, imageUrl) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ru/app/incredible/pets/domain/GetDownloadStateInteractor.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,13 @@ | ||
package ru.app.incredible.pets.domain | ||
|
||
import io.reactivex.Observable | ||
import ru.app.incredible.pets.data.gateway.DownloadImageGateway | ||
|
||
class GetDownloadStateInteractor( | ||
private val downloadImageGateway: DownloadImageGateway | ||
) { | ||
|
||
fun execute(pet: Pet, imageUrl: String): Observable<ImageDownloadState> { | ||
return downloadImageGateway.getDownloadState(pet, imageUrl) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/ru/app/incredible/pets/domain/ImageDownloadState.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,5 @@ | ||
package ru.app.incredible.pets.domain | ||
|
||
enum class ImageDownloadState { | ||
IDLE, FINISHED, ERROR, PROGRESS | ||
} |
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,3 @@ | ||
package ru.app.incredible.pets.domain | ||
|
||
data class Pet(val petId: Int) |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ru/app/incredible/pets/domain/RemoveImageInteractor.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,13 @@ | ||
package ru.app.incredible.pets.domain | ||
|
||
import io.reactivex.Single | ||
import ru.app.incredible.pets.data.gateway.DownloadImageGateway | ||
|
||
class RemoveImageInteractor( | ||
private val downloadImageGateway: DownloadImageGateway | ||
) { | ||
|
||
fun execute(imageUrl: String): Single<Boolean> { | ||
return downloadImageGateway.removeImage(imageUrl) | ||
} | ||
} |
Oops, something went wrong.