-
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 #11 from wawandco/camera-feat
Camera feat 📷
- Loading branch information
Showing
70 changed files
with
2,373 additions
and
220 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
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
63 changes: 49 additions & 14 deletions
63
app/src/main/java/co/wawand/composetypesafenavigation/data/mapper/PhotoMapper.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,38 +1,73 @@ | ||
package co.wawand.composetypesafenavigation.data.mapper | ||
|
||
import co.wawand.composetypesafenavigation.data.local.database.entity.PhotoEntity | ||
import co.wawand.composetypesafenavigation.data.local.database.entity.PhotoType | ||
import co.wawand.composetypesafenavigation.data.local.database.entity.PhotoWithAlbum | ||
import co.wawand.composetypesafenavigation.data.remote.api.entity.PhotoAPIEntity | ||
import co.wawand.composetypesafenavigation.domain.model.Photo | ||
import co.wawand.composetypesafenavigation.domain.model.BasePhoto | ||
import co.wawand.composetypesafenavigation.domain.model.LocalPhoto | ||
import co.wawand.composetypesafenavigation.domain.model.RemotePhoto | ||
|
||
|
||
fun PhotoAPIEntity.toDBEntity(): PhotoEntity { | ||
val randomNumber = (0..100000).random() | ||
return PhotoEntity( | ||
id = id, | ||
title = title, | ||
type = PhotoType.REMOTE, | ||
thumbnailUrl = "https://picsum.photos/seed/$randomNumber/256/256", | ||
url = "https://picsum.photos/seed/$randomNumber/2400/1600", | ||
albumId = albumId | ||
) | ||
} | ||
|
||
fun PhotoEntity.toDomain(): Photo = Photo( | ||
id = id, | ||
title = title, | ||
thumbnailUrl = thumbnailUrl, | ||
url = url, | ||
album = albumId.toString() | ||
) | ||
fun PhotoEntity.toDomain(): BasePhoto { | ||
return when (type) { | ||
PhotoType.LOCAL -> LocalPhoto( | ||
id = id, | ||
title = title, | ||
path = path!!, | ||
size = size!!, | ||
lastModified = lastModified!! | ||
) | ||
|
||
fun PhotoWithAlbum.toDomain(): Photo { | ||
PhotoType.REMOTE -> RemotePhoto( | ||
id = id, | ||
title = title, | ||
thumbnailUrl = thumbnailUrl!!, | ||
url = url!!, | ||
album = albumId.toString() | ||
) | ||
} | ||
} | ||
|
||
fun PhotoWithAlbum.toDomain(): RemotePhoto { | ||
val photo = this.photoEntity | ||
val album = this.albumEntity | ||
|
||
return Photo( | ||
return RemotePhoto( | ||
id = photo.id, | ||
title = photo.title, | ||
thumbnailUrl = photo.thumbnailUrl, | ||
url = photo.url, | ||
album = album.title | ||
thumbnailUrl = photo.thumbnailUrl!!, | ||
url = photo.url!!, | ||
album = album?.title ?: "" | ||
) | ||
} | ||
} | ||
|
||
fun PhotoEntity.toPhotoDetailsEntity(): LocalPhoto = LocalPhoto( | ||
id = id, | ||
title = title, | ||
path = path!!, | ||
size = size!!, | ||
lastModified = lastModified!! | ||
) | ||
|
||
fun LocalPhoto.toEntity(userId: Long): PhotoEntity = PhotoEntity( | ||
id = id, | ||
title = title, | ||
type = PhotoType.LOCAL, | ||
path = path, | ||
size = size, | ||
lastModified = lastModified, | ||
userId = userId, | ||
) |
57 changes: 57 additions & 0 deletions
57
...src/main/java/co/wawand/composetypesafenavigation/data/repository/CameraRepositoryImpl.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,57 @@ | ||
package co.wawand.composetypesafenavigation.data.repository | ||
|
||
import androidx.camera.core.ImageCapture | ||
import androidx.camera.core.ImageCaptureException | ||
import co.wawand.composetypesafenavigation.core.Constant.GENERIC_ERROR | ||
import co.wawand.composetypesafenavigation.core.util.Resource | ||
import co.wawand.composetypesafenavigation.di.IoDispatcher | ||
import co.wawand.composetypesafenavigation.domain.repository.CameraRepository | ||
import co.wawand.composetypesafenavigation.domain.service.FileService | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import java.util.concurrent.Executor | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
class CameraRepositoryImpl @Inject constructor( | ||
private val fileService: FileService, | ||
@IoDispatcher private val dispatcher: CoroutineDispatcher | ||
) : CameraRepository { | ||
|
||
override suspend fun capturePhoto( | ||
prefix: String, | ||
executor: Executor, | ||
imageCapture: ImageCapture | ||
): Flow<Resource<String>> = | ||
flow { | ||
try { | ||
val photoFile = fileService.generatePhotoFile(prefix) | ||
|
||
suspendCancellableCoroutine { continuation -> | ||
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build() | ||
|
||
imageCapture.takePicture( | ||
outputOptions, | ||
executor, | ||
object : ImageCapture.OnImageSavedCallback { | ||
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { | ||
continuation.resume(photoFile.absolutePath) | ||
} | ||
|
||
override fun onError(exception: ImageCaptureException) { | ||
continuation.resumeWithException(exception) | ||
} | ||
} | ||
) | ||
}.let { uri -> | ||
emit(Resource.Success(uri)) | ||
} | ||
} catch (e: Exception) { | ||
emit(Resource.Error(e.message ?: GENERIC_ERROR)) | ||
} | ||
}.flowOn(dispatcher) | ||
} |
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
Oops, something went wrong.