Skip to content

Commit

Permalink
Merge pull request #89 from FakeDevelopers/feature/BDBD-517
Browse files Browse the repository at this point in the history
FEAT : BDBD-517 Util 클래스 모듈화
  • Loading branch information
minseonglove authored Jan 27, 2023
2 parents ab8cd49 + 2b4af38 commit fbea018
Show file tree
Hide file tree
Showing 31 changed files with 445 additions and 455 deletions.
1 change: 1 addition & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation 'com.orhanobut:logger:2.2.0'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation "androidx.exifinterface:exifinterface:1.3.5"
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
Expand Down
9 changes: 9 additions & 0 deletions data/src/main/java/com/fakedevelopers/data/di/MainModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.fakedevelopers.data.di

import android.content.Context
import com.fakedevelopers.data.repository.ImageRepositoryImpl
import com.fakedevelopers.data.repository.ProductListRepositoryImpl
import com.fakedevelopers.data.service.ProductListService
import com.fakedevelopers.domain.repository.ImageRepository
import com.fakedevelopers.domain.repository.ProductListRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import javax.inject.Singleton
Expand All @@ -23,4 +27,9 @@ object MainModule {
@Provides
fun provideProductListRepository(service: ProductListService): ProductListRepository =
ProductListRepositoryImpl(service)

@Singleton
@Provides
fun provideImageRepository(@ApplicationContext context: Context): ImageRepository =
ImageRepositoryImpl(context.contentResolver)
}
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
}
}
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 = ""
)
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
)
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
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.fakedevelopers.presentation.R
import com.fakedevelopers.presentation.databinding.FragmentProductDetailBinding
import com.fakedevelopers.presentation.model.RemainTime
import com.fakedevelopers.presentation.ui.base.BaseFragment
import com.fakedevelopers.presentation.ui.util.DATE_PATTERN
import com.fakedevelopers.presentation.ui.util.repeatOnStarted
import com.orhanobut.logger.Logger
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -40,7 +41,7 @@ class ProductDetailFragment : BaseFragment<FragmentProductDetailBinding>(
}
}

private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
private val formatter = DateTimeFormatter.ofPattern(DATE_PATTERN)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.fakedevelopers.presentation.databinding.RecyclerProductListBinding
import com.fakedevelopers.presentation.databinding.RecyclerProductListFooterBinding
import com.fakedevelopers.presentation.ui.util.DateUtil
import com.fakedevelopers.presentation.ui.util.ExpirationTimerTask
import com.fakedevelopers.presentation.ui.util.PriceUtil

class ProductListAdapter(
private val dateUtil: DateUtil,
Expand Down Expand Up @@ -55,9 +54,11 @@ class ProductListAdapter(
hopePrice.isVisible = productItem.hopePrice != 0L
textviewProductListHopePrice.isVisible = productItem.hopePrice != 0L
if (productItem.hopePrice != 0L) {
textviewProductListHopePrice.text = PriceUtil.numberToPrice(productItem.hopePrice)
textviewProductListHopePrice.text =
binding.root.context.getString(R.string.price_format, productItem.hopePrice)
}
textviewProductListOpeningBid.text = PriceUtil.numberToPrice(productItem.openingBid)
textviewProductListOpeningBid.text =
binding.root.context.getString(R.string.price_format, productItem.openingBid)
textviewProductListParticipant.text =
if (productItem.bidderCount > 0) "${productItem.bidderCount}명 입찰" else ""
// 상품 상세 정보로 넘어가기
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.text.TextUtils
import android.text.TextWatcher
import android.widget.EditText
import com.fakedevelopers.domain.secret.Constants.Companion.dec
import com.fakedevelopers.presentation.ui.util.priceToLong

class PriceTextWatcher(
private val editText: EditText,
Expand Down Expand Up @@ -47,6 +48,6 @@ class PriceTextWatcher(
editText.addTextChangedListener(PriceTextWatcher(editText) { checkCondition?.invoke() })
}
fun makeComma(price: String) =
price.replace(",", "").toLongOrNull()?.let { dec.format(it) } ?: ""
price.priceToLong()?.let { dec.format(it) } ?: ""
}
}
Loading

0 comments on commit fbea018

Please sign in to comment.