-
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 #89 from FakeDevelopers/feature/BDBD-517
FEAT : BDBD-517 Util 클래스 모듈화
- Loading branch information
Showing
31 changed files
with
445 additions
and
455 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
101 changes: 101 additions & 0 deletions
101
data/src/main/java/com/fakedevelopers/data/repository/ImageRepositoryImpl.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,101 @@ | ||
package com.fakedevelopers.data.repository | ||
|
||
import android.content.ContentResolver | ||
import android.net.Uri | ||
import android.provider.MediaStore | ||
import androidx.exifinterface.media.ExifInterface | ||
import com.fakedevelopers.domain.model.AlbumItem | ||
import com.fakedevelopers.domain.model.MediaInfo | ||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
class ImageRepositoryImpl @Inject constructor( | ||
private val contentResolver: ContentResolver | ||
) : ImageRepository { | ||
override fun isValid(uri: String): Boolean { | ||
contentResolver.runCatching { | ||
openFileDescriptor(Uri.parse(uri), "r")?.use { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun getValidUris(uris: List<String>): List<String> = | ||
uris.filter { uri -> isValid(uri) } | ||
|
||
override suspend fun getBytesByUri(uri: String, dispatcher: CoroutineDispatcher): ByteArray? = | ||
withContext(dispatcher) { | ||
var result: ByteArray? = null | ||
runCatching { | ||
contentResolver.openInputStream(Uri.parse(uri)) | ||
}.onSuccess { inputStream -> | ||
inputStream?.use { | ||
result = it.readBytes() | ||
} | ||
} | ||
result | ||
} | ||
|
||
override fun getMediaInfo(uri: String): MediaInfo { | ||
val mimeType = contentResolver.getType(Uri.parse(uri)).toString() | ||
var extension = mimeType.substringAfter('/') | ||
if (extension == "jpg") { | ||
extension = "jpeg" | ||
} | ||
return MediaInfo(mimeType, extension.uppercase(Locale.getDefault())) | ||
} | ||
|
||
override fun getRotate(uri: String): Float { | ||
runCatching { | ||
ExifInterface(uri.getPath()!!).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) | ||
}.onSuccess { attr -> | ||
return when (attr) { | ||
ExifInterface.ORIENTATION_ROTATE_90 -> 90f | ||
ExifInterface.ORIENTATION_ROTATE_180 -> 180f | ||
ExifInterface.ORIENTATION_ROTATE_270 -> 270f | ||
else -> 0f | ||
} | ||
} | ||
return 0f | ||
} | ||
|
||
private fun String.getPath() = | ||
contentResolver.query( | ||
Uri.parse(this), | ||
arrayOf(MediaStore.Images.Media.DATA), | ||
null, | ||
null, | ||
null | ||
)?.use { cursor -> | ||
cursor.moveToNext() | ||
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)) | ||
} | ||
|
||
override fun getDateModifiedByUri(uri: String): AlbumItem? = | ||
contentResolver.query( | ||
Uri.parse(uri), | ||
arrayOf( | ||
MediaStore.Images.Media.DATA, | ||
MediaStore.Images.ImageColumns.DATE_MODIFIED | ||
), | ||
null, | ||
null, | ||
null | ||
)?.use { cursor -> | ||
var updatedAlbumItem: AlbumItem? = null | ||
if (cursor.moveToNext()) { | ||
val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)) | ||
val relPath = path.substringBeforeLast("/") | ||
updatedAlbumItem = AlbumItem( | ||
uri = uri, | ||
modified = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATE_MODIFIED)), | ||
path = relPath | ||
) | ||
} | ||
updatedAlbumItem | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
domain/src/main/java/com/fakedevelopers/domain/model/AlbumItem.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.fakedevelopers.domain.model | ||
|
||
data class AlbumItem( | ||
val uri: String, | ||
val modified: Long, | ||
val path: String = "" | ||
) |
6 changes: 6 additions & 0 deletions
6
domain/src/main/java/com/fakedevelopers/domain/model/MediaInfo.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,6 @@ | ||
package com.fakedevelopers.domain.model | ||
|
||
data class MediaInfo( | ||
val mimeType: String, | ||
val extension: String | ||
) |
15 changes: 15 additions & 0 deletions
15
domain/src/main/java/com/fakedevelopers/domain/repository/ImageRepository.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,15 @@ | ||
package com.fakedevelopers.domain.repository | ||
|
||
import com.fakedevelopers.domain.model.AlbumItem | ||
import com.fakedevelopers.domain.model.MediaInfo | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
interface ImageRepository { | ||
fun isValid(uri: String): Boolean | ||
fun getValidUris(uris: List<String>): List<String> | ||
fun getDateModifiedByUri(uri: String): AlbumItem? | ||
suspend fun getBytesByUri(uri: String, dispatcher: CoroutineDispatcher = Dispatchers.IO): ByteArray? | ||
fun getMediaInfo(uri: String): MediaInfo | ||
fun getRotate(uri: String): Float | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/fakedevelopers/domain/usecase/GetBytesByUriUseCase.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,10 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class GetBytesByUriUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
suspend operator fun invoke(uri: String) = repository.getBytesByUri(uri) | ||
} |
11 changes: 11 additions & 0 deletions
11
domain/src/main/java/com/fakedevelopers/domain/usecase/GetDateModifiedByUriUseCase.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,11 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.model.AlbumItem | ||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class GetDateModifiedByUriUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
operator fun invoke(uri: String): AlbumItem? = repository.getDateModifiedByUri(uri) | ||
} |
11 changes: 11 additions & 0 deletions
11
domain/src/main/java/com/fakedevelopers/domain/usecase/GetMediaInfoUseCase.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,11 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.model.MediaInfo | ||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class GetMediaInfoUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
operator fun invoke(uri: String): MediaInfo = repository.getMediaInfo(uri) | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/fakedevelopers/domain/usecase/GetRotateUseCase.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,10 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class GetRotateUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
operator fun invoke(uri: String): Float = repository.getRotate(uri) | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/fakedevelopers/domain/usecase/GetValidUrisUseCase.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,10 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class GetValidUrisUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
operator fun invoke(uris: List<String>): List<String> = repository.getValidUris(uris) | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/fakedevelopers/domain/usecase/IsValidUriUseCase.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,10 @@ | ||
package com.fakedevelopers.domain.usecase | ||
|
||
import com.fakedevelopers.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class IsValidUriUseCase @Inject constructor( | ||
private val repository: ImageRepository | ||
) { | ||
operator fun invoke(uri: String): Boolean = repository.isValid(uri) | ||
} |
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
Oops, something went wrong.