forked from mutualmobile/Praxis
-
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.
- Loading branch information
1 parent
8e27df1
commit 8737d41
Showing
19 changed files
with
412 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mutualmobile.praxis.di | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) class AppModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAppContext(@ApplicationContext application: Context): Context { | ||
return application | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="https://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center_horizontal|center_vertical" | ||
android:orientation="vertical"> | ||
|
||
<androidx.appcompat.widget.AppCompatImageView | ||
android:id="@+id/photoView" | ||
android:layout_width="@dimen/random_photo_size" | ||
android:layout_height="@dimen/random_photo_size" | ||
android:background="@android:color/black" | ||
android:padding="@dimen/padding_small" /> | ||
|
||
<androidx.appcompat.widget.AppCompatTextView | ||
android:id="@+id/progressText" | ||
android:visibility="gone" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" /> | ||
|
||
|
||
<androidx.appcompat.widget.AppCompatButton | ||
android:id="@+id/randomPhotoButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:padding="@dimen/padding_small" | ||
android:text="@string/random_photo"> | ||
|
||
</androidx.appcompat.widget.AppCompatButton> | ||
</LinearLayout> |
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 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<dimen name="random_photo_size">160dp</dimen> | ||
<dimen name="padding_small">8dp</dimen> | ||
</resources> |
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,3 +1,9 @@ | ||
<resources> | ||
<string name="title_activity_main">MainActivity</string> | ||
<string name="random_photo">Random Photo</string> | ||
<string name="except_file_not_found">Could not save the image, FileNotFound!</string> | ||
<string name="downloaded_glue">Downloaded Size %d bytes</string> | ||
<string name="downloading_glue">Downloading %d bytes</string> | ||
<string name="except_no_network">No network available!</string> | ||
<string name="except_generic">Some error occurred</string> | ||
</resources> |
1 change: 1 addition & 0 deletions
1
data/src/main/java/com/mutualmobile/praxis/data/AppConstants.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,4 +1,5 @@ | ||
package com.mutualmobile.praxis.data | ||
|
||
object AppConstants { | ||
const val PIC_SUM_URL = "https://picsum.photos/800" | ||
} |
20 changes: 20 additions & 0 deletions
20
data/src/main/java/com/mutualmobile/praxis/data/injection/RandomPhotosModule.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,20 @@ | ||
package com.mutualmobile.praxis.data.injection | ||
|
||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchRepository | ||
import com.mutualmobile.praxis.domain.usecases.FetchRandomPhotoUseCase | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class RandomPhotosModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideFetchRandomPhotoUseCase(photoFetchRepository: PhotoFetchRepository) = | ||
FetchRandomPhotoUseCase(photoFetchRepository) | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
data/src/main/java/com/mutualmobile/praxis/data/injection/RandomPhotosRepositoryModule.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.mutualmobile.praxis.data.injection | ||
|
||
import com.mutualmobile.praxis.data.repositories.PicSumPhotoFetchRepositoryImpl | ||
import com.mutualmobile.praxis.data.repositories.RandomFileServiceImpl | ||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchRepository | ||
import com.mutualmobile.praxis.domain.repositories.RandomFileService | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class RandomPhotosRepositoryModule { | ||
|
||
@Binds | ||
abstract fun bindRandomFileService(randomFileServiceImpl: RandomFileServiceImpl): RandomFileService | ||
|
||
@Binds | ||
abstract fun bindPhotoFetchRepository(photoFetchRepositoryImpl: PicSumPhotoFetchRepositoryImpl): PhotoFetchRepository | ||
} |
82 changes: 82 additions & 0 deletions
82
...src/main/java/com/mutualmobile/praxis/data/repositories/PicSumPhotoFetchRepositoryImpl.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,82 @@ | ||
package com.mutualmobile.praxis.data.repositories | ||
|
||
import com.mutualmobile.praxis.domain.model.StreamingFile | ||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchListener | ||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchRepository | ||
import com.mutualmobile.praxis.domain.repositories.RandomFileService | ||
import com.mutualmobile.praxis.injection.dispatcher.CoroutineDispatcherProvider | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.features.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import kotlinx.coroutines.withContext | ||
import timber.log.Timber | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
class PicSumPhotoFetchRepositoryImpl @Inject constructor( | ||
private val coroutineContext: CoroutineDispatcherProvider, | ||
private val networkClient: HttpClient, | ||
private val fileCreationService: RandomFileService | ||
) : | ||
PhotoFetchRepository { | ||
companion object { | ||
private const val PIC_SUM_URL = "https://picsum.photos/800" | ||
} | ||
|
||
private var fileDownloadListener: PhotoFetchListener? = null | ||
|
||
override fun setListener(listener: PhotoFetchListener) { | ||
fileDownloadListener = listener | ||
} | ||
|
||
override fun removeListener(listener: PhotoFetchListener) { | ||
fileDownloadListener = null | ||
} | ||
|
||
override suspend fun fetchPhoto(): Unit = | ||
withContext(coroutineContext.io) { | ||
try { | ||
val file = fileCreationService.getTempFile() | ||
val response: HttpResponse = responseWithListener(PIC_SUM_URL, file) | ||
val bytes = response.receive<ByteArray>() | ||
file.writeBytes(bytes) | ||
notifyFileDownloaded(file) | ||
} catch (ex: Exception) { | ||
fileDownloadListener?.onFailed(ex) | ||
} | ||
} | ||
|
||
private fun notifyFileDownloaded(file: File) { | ||
fileDownloadListener?.onReceive( | ||
StreamingFile( | ||
file.length(), | ||
file, isComplete = true | ||
) | ||
) | ||
fileDownloadListener?.onComplete() | ||
} | ||
|
||
private suspend fun responseWithListener( | ||
url: String, | ||
file: File | ||
): HttpResponse = networkClient.get(url) { | ||
onDownload { bytesSentTotal, contentLength -> | ||
Timber.d("${bytesSentTotal}/${contentLength}") | ||
prepareCallback(bytesSentTotal, file) | ||
} | ||
} | ||
|
||
private fun prepareCallback( | ||
bytesSentTotal: Long, | ||
file: File | ||
) { | ||
fileDownloadListener?.onReceive( | ||
StreamingFile( | ||
bytesSentTotal, | ||
file, isComplete = false | ||
) | ||
) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
data/src/main/java/com/mutualmobile/praxis/data/repositories/RandomFileServiceImpl.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,12 @@ | ||
package com.mutualmobile.praxis.data.repositories | ||
|
||
import android.content.Context | ||
import com.mutualmobile.praxis.domain.repositories.RandomFileService | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
class RandomFileServiceImpl @Inject constructor(private val context: Context) : RandomFileService { | ||
override fun getTempFile(): File { | ||
return File(context.filesDir, "random") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/mutualmobile/praxis/domain/model/StreamingFile.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,9 @@ | ||
package com.mutualmobile.praxis.domain.model | ||
|
||
import java.io.File | ||
|
||
data class StreamingFile( | ||
var progress: Long, | ||
var file: File, | ||
var isComplete: Boolean, | ||
) |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/mutualmobile/praxis/domain/repositories/PhotoFetchListener.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,9 @@ | ||
package com.mutualmobile.praxis.domain.repositories | ||
|
||
import com.mutualmobile.praxis.domain.model.StreamingFile | ||
|
||
interface PhotoFetchListener { | ||
fun onReceive(streamingFile: StreamingFile) | ||
fun onFailed(throwable: Throwable) | ||
fun onComplete() | ||
} |
8 changes: 8 additions & 0 deletions
8
domain/src/main/java/com/mutualmobile/praxis/domain/repositories/PhotoFetchRepository.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,8 @@ | ||
package com.mutualmobile.praxis.domain.repositories | ||
|
||
interface PhotoFetchRepository { | ||
suspend fun fetchPhoto() | ||
|
||
fun setListener(listener: PhotoFetchListener) | ||
fun removeListener(listener: PhotoFetchListener) | ||
} |
7 changes: 7 additions & 0 deletions
7
domain/src/main/java/com/mutualmobile/praxis/domain/repositories/RandomFileService.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,7 @@ | ||
package com.mutualmobile.praxis.domain.repositories | ||
|
||
import java.io.File | ||
|
||
interface RandomFileService { | ||
fun getTempFile(): File | ||
} |
44 changes: 44 additions & 0 deletions
44
domain/src/main/java/com/mutualmobile/praxis/domain/usecases/FetchRandomPhotoUseCase.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,44 @@ | ||
package com.mutualmobile.praxis.domain.usecases | ||
|
||
import com.mutualmobile.praxis.domain.model.StreamingFile | ||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchListener | ||
import com.mutualmobile.praxis.domain.repositories.PhotoFetchRepository | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.channels.ProducerScope | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.onFailure | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
class FetchRandomPhotoUseCase(private val photoFetchRepository: PhotoFetchRepository) : | ||
BaseUseCase< StreamingFile,String> { | ||
|
||
override fun performStreaming(input: String?): Flow<StreamingFile> { | ||
return callbackFlow { | ||
val listener = photoFetchListener() | ||
photoFetchRepository.setListener(listener) | ||
photoFetchRepository.fetchPhoto() | ||
awaitClose { | ||
photoFetchRepository.removeListener(listener) | ||
} | ||
} | ||
} | ||
|
||
private fun ProducerScope<StreamingFile>.photoFetchListener() = | ||
object : PhotoFetchListener { | ||
override fun onReceive(streamingFile: StreamingFile) { | ||
trySend(streamingFile).onFailure { | ||
cancel(CancellationException("Download Error", it)) | ||
} | ||
} | ||
|
||
override fun onFailed(throwable: Throwable) { | ||
cancel(CancellationException("Download Error", throwable)) | ||
} | ||
|
||
override fun onComplete(){ | ||
channel.close() | ||
} | ||
} | ||
} |
Oops, something went wrong.