Skip to content

Commit

Permalink
[FEATURE] #101 : 인식된 알러지 정보 보여주는 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Nov 17, 2023
1 parent dea4f5d commit e031b25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ class ObjectDetectionFragment : Fragment() {
}
}
}
}.launchIn(viewLifecycleOwner.lifecycleScope)

viewModel.detectedAllergiesUiEvent.flowWithLifecycle(viewLifecycleOwner.lifecycle)
.onEach { detectedAllergies ->
if(detectedAllergies.isEmpty()) {
showPositiveDialog()
} else {
showNegativeDialog(detectedAllergies = detectedAllergies)
}
}.launchIn(viewLifecycleOwner.lifecycleScope)
}

Expand Down Expand Up @@ -144,7 +152,12 @@ class ObjectDetectionFragment : Fragment() {
objectDetectionResultList: List<ObjectDetectionResult>,
classes: List<String>
) {
objectDetectionAdapter = ObjectDetectionAdapter(classes)
objectDetectionAdapter = ObjectDetectionAdapter(
classes = classes,
onClick = { detectedObject ->
viewModel.checkAllergies(detectedObject)
}
)
objectDetectionAdapter.submitList(objectDetectionResultList)
with(binding.rvODDetectedObject) {
this.layoutManager = LinearLayoutManager(
Expand Down Expand Up @@ -242,10 +255,10 @@ class ObjectDetectionFragment : Fragment() {
return classes
}

private fun showNegativeDialog(detectedList: List<Allergy>){
private fun showNegativeDialog(detectedAllergies: List<Allergy>){
NegativeSignDialog(
context = requireContext(),
allergyList = detectedList
detectedAllergies = detectedAllergies
).show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import androidx.lifecycle.viewModelScope
import com.healthc.data.model.local.detection.InputImage
import com.healthc.data.model.local.detection.ObjectDetectionResult
import com.healthc.data.source.detection.LocalDetectionDataSource
import com.healthc.domain.model.auth.Allergy
import com.healthc.domain.usecase.detection.CheckAllergiesInImageUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -29,6 +31,9 @@ class ObjectDetectionViewModel @Inject constructor(
MutableStateFlow(ObjectDetectionUiState.Init)
val detectedObjectUiState: StateFlow<ObjectDetectionUiState> = _detectedObjectUiState.asStateFlow()

private val _detectedAllergiesUiEvent: MutableSharedFlow<List<Allergy>> = MutableSharedFlow()
val detectedAllergiesUiEvent: SharedFlow<List<Allergy>> = _detectedAllergiesUiEvent.asSharedFlow()

private val _objectDetectionEvent = MutableSharedFlow<ObjectDetectionEvent>()
val objectDetectionEvent : SharedFlow<ObjectDetectionEvent> get() = _objectDetectionEvent

Expand Down Expand Up @@ -64,7 +69,7 @@ class ObjectDetectionViewModel @Inject constructor(
viewModelScope.launch {
checkAllergiesInImageUseCase(detectedObject)
.onSuccess { result ->
// _objectDetectionEvent.emit(ObjectDetectionEvent.Detected(result))
_detectedAllergiesUiEvent.emit(result)
}
.onFailure { error ->
_objectDetectionEvent.emit(ObjectDetectionEvent.Failure(error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.healthc.domain.model.auth.Allergy

class NegativeSignDialog(
context: Context,
private val allergyList : List<Allergy>,
private val detectedAllergies : List<Allergy>,
) : Dialog(context){

private val binding by lazy { DialogNegativeBinding.inflate(layoutInflater) }
Expand All @@ -22,7 +22,7 @@ class NegativeSignDialog(
}

private fun initViews(){
val detectedList: List<String> = allergyList.map{ it.allergy }
val detectedList: List<String> = detectedAllergies.map{ it.allergy }
val content = "해당 음식에는 ${detectedList.joinToString(", ")}이(가) 포함되어 있습니다."
binding.negativeDialogContent.text = content

Expand Down

0 comments on commit e031b25

Please sign in to comment.