Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#40] 판매뷰 / MLKit로 OCR 구현 #41

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ dependencies {
implementation(timber)
implementation(ossLicense)
}
}
}
4 changes: 3 additions & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ object ThirdPartyDependencies {
const val circleIndicator = "me.relex:circleindicator:${Versions.circleIndicatorVersion}"
const val shimmer = "com.facebook.shimmer:shimmer:${Versions.shimmerVersion}"

const val mlkit = "com.google.mlkit:text-recognition-korean:${Versions.mlkitVersion}"

const val kakaoLogin = "com.kakao.sdk:v2-user:${Versions.kakaoVersion}"
const val kakaoAuth = "com.kakao.sdk:v2-auth:${Versions.kakaoVersion}"
const val kakaoTalk = "com.kakao.sdk:v2-talk:${Versions.kakaoVersion}"
Expand All @@ -88,4 +90,4 @@ object FirebaseDependencies {
object KakaoDependencies {
const val user = "com.kakao.sdk:v2-user:${Versions.kakaoVersion}"
const val share = "com.kakao.sdk:v2-share:${Versions.kakaoVersion}"
}
}
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ object Versions {
const val kakaoVersion = "2.19.0"
const val circleIndicatorVersion = "2.1.6"
const val shimmerVersion = "0.5.0"
const val mlkitVersion = "16.0.0"

const val junitVersion = "4.13.2"
const val espressoVersion = "3.5.1"
const val androidTestVersion = "1.1.2"
Expand Down
5 changes: 3 additions & 2 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {

compileOptions {
sourceCompatibility = Versions.javaVersion
targetCompatibility= Versions.javaVersion
targetCompatibility = Versions.javaVersion
}

kotlinOptions {
Expand Down Expand Up @@ -84,5 +84,6 @@ dependencies {
implementation(lottie)
implementation(circularProgressBar)
implementation(circleIndicator)
implementation(mlkit)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
setRecyclerViewDeco()
observeCheckedAgainState()
observeGetHomeDataState()
observeGetProductIdState()
}

private fun initView() {
Expand Down Expand Up @@ -115,10 +116,8 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
activityResult =
registerForActivityResult(PickVisualMedia()) { uri ->
if (uri != null) {
// TODO : OCR 진행 후 실 상품 이미지 대체
viewModel.selectedImageUri = uri
sellProductDialog = SellProductDialog()
sellProductDialog?.show(parentFragmentManager, SELL_PRODUCT_DIALOG)
viewModel.showCaptureImage(uri, requireContext())
}
}
}
Expand Down Expand Up @@ -171,6 +170,20 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
}.launchIn(lifecycleScope)
}

private fun observeGetProductIdState() {
viewModel.getProductIdState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> {
sellProductDialog = SellProductDialog()
sellProductDialog?.show(parentFragmentManager, SELL_PRODUCT_DIALOG)
}

is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}.launchIn(lifecycleScope)
}

override fun onDestroyView() {
super.onDestroyView()
_adapter = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package co.orange.presentation.main.home

import android.content.Context
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.orange.core.state.UiState
import co.orange.domain.entity.response.HomeModel
import co.orange.domain.repository.HomeRepository
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.korean.KoreanTextRecognizerOptions
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -25,13 +29,17 @@ class HomeViewModel
}

var selectedImageUri = Uri.EMPTY
var productId = ""

private val _isCheckedAgain = MutableSharedFlow<Boolean>()
val isCheckedAgain: SharedFlow<Boolean> = _isCheckedAgain

private val _getHomeDataState = MutableStateFlow<UiState<HomeModel>>(UiState.Empty)
val getHomeDataState: StateFlow<UiState<HomeModel>> = _getHomeDataState

private val _getProductIdState = MutableStateFlow<UiState<String>>(UiState.Empty)
val getProductIdState: StateFlow<UiState<String>> = _getProductIdState

fun setCheckedState(state: Boolean) {
viewModelScope.launch {
_isCheckedAgain.emit(state)
Expand All @@ -49,4 +57,23 @@ class HomeViewModel
}
}
}

fun showCaptureImage(
img: Uri,
context: Context,
) {
runCatching {
val image = InputImage.fromFilePath(context, img)
val recognizer =
TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
recognizer.process(image)
.addOnSuccessListener {
// TODO 서버통신으로 ID값 가져오기
_getProductIdState.value = UiState.Success(it.text)
}
.addOnFailureListener {
_getProductIdState.value = UiState.Failure(it.message.toString())
}
}
}
}
Loading