Skip to content

Commit

Permalink
Merge pull request #6 from BeYourEyes/jiyoung
Browse files Browse the repository at this point in the history
merge: added korean subject marker extension
  • Loading branch information
moon0900 authored Dec 15, 2024
2 parents a13baa5 + 7a1a963 commit 1e75534
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/com/dna/beyoureyes/model/UserInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ class UserInfo (
}

fun findMatchingAllergy(foodAllergy: Set<String>) : Set<String> {
return allergic.intersect(foodAllergy)
val engToKor = mutableMapOf(
"buckwheat" to "메밀",
"wheat" to "",
"bean" to "대두",
"peanut" to "땅콩",
"walnut" to "호두",
"pinenut" to "" ,
"acid" to "아황산",
"peach" to "복숭아",
"tomato" to "토마토"
)
return foodAllergy.intersect(allergic.mapNotNull { engToKor[it] }.toSet())
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,19 @@ class FoodResultFragment: Fragment() {
fragment?.view?.findViewById<TextView>(R.id.failText)?.setText("영양성분 인식에 실패했어요.")
Log.d("Result", "영양성분 인식 실패")
}

if (viewModel.isAllergyDataValid()) { // 알레르기 정보 있으면 해당 프래그먼트 추가
resultFragments.add(ResultAllergyFragment())
fragmentManager
.beginTransaction()
.replace(binding.AllergyFragment.id, ResultAllergyFragment())
.commit()
fragmentManager
.beginTransaction()
.replace(binding.AllergyCautionFragment.id, ResultAllergyCautionFragment())
.commit()
if(viewModel.hasAllergiesToAlert()){
fragmentManager
.beginTransaction()
.replace(binding.AllergyCautionFragment.id, ResultAllergyCautionFragment())
.commit()
}
}
else {
binding.AllergyFragment.isVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.net.Uri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import com.dna.beyoureyes.AppUser
import com.dna.beyoureyes.model.Food


Expand All @@ -15,6 +17,14 @@ class FoodViewModel : ViewModel() {
val capturedImageUri: LiveData<Uri> = _capturedImageUri
val processedImageUri: LiveData<Uri> = _processedImageUri

/*
// foodData 라이브 데이터에 map을 활용하여 필요한 상세 정보를 아래와 같이 또 다른 라이브 데이터로 정의 가능
// foodData의 알레르기 정보 중 유저 정보와의 교집합을 라이브 데이터로 정의한 예시.
val allergiesToAlert : LiveData<Set<String>?> = _foodData.map { food ->
food.allergy?.let{ AppUser.info?.findMatchingAllergy(it) }
}
*/

fun isCaptureSuccessful() : Boolean {
return _capturedImageUri.value != null
}
Expand All @@ -23,6 +33,10 @@ class FoodViewModel : ViewModel() {
return _foodData.value?.kcal != null
}

fun hasAllergiesToAlert() : Boolean {
return !(_foodData.value?.allergy?.let{ AppUser.info?.findMatchingAllergy(it) }.isNullOrEmpty())
}

fun isNutritionDataValid() : Boolean {
return _foodData.value?.nutritions != null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package com.dna.beyoureyes.ui.foodDetail

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.activityViewModels
import com.dna.beyoureyes.AppUser
import com.dna.beyoureyes.R
import com.dna.beyoureyes.databinding.FragmentResultAlgBinding
import com.dna.beyoureyes.databinding.FragmentResultAlgCautionBinding
import com.dna.beyoureyes.ui.foodAnalysis.FoodViewModel
import com.google.android.material.chip.Chip
import com.dna.extensions.addSubjectMarker

class ResultAllergyCautionFragment : Fragment() {
private var _binding: FragmentResultAlgCautionBinding? = null
Expand Down Expand Up @@ -56,7 +52,7 @@ class ResultAllergyCautionFragment : Fragment() {
val commonAllergyInKorean = commonAllergies.mapNotNull { engToKor[it] }
allergyCautionString = commonAllergyInKorean.joinToString(", ")
Log.d("Allergy Caution", "${detectedAllergyData} ${userAllergyData} ${commonAllergies}")
binding.algCautionText2.setText("이 식품에 ${allergyCautionString} 함유되어있어요")
binding.algCautionText2.setText("이 식품에 ${allergyCautionString.addSubjectMarker()} 함유되어있어요")
}

binding.algCautionText1.setText(AppUser.info?.name + binding.algCautionText1.text)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/dna/beyoureyes/util/TTSManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.dna.beyoureyes.model.CaloricNutrient
import com.dna.beyoureyes.model.Food
import java.util.LinkedList
import java.util.Locale
import com.dna.extensions.addSubjectMarker

class TTSManager(context: Context) :
TextToSpeech.OnInitListener, UtteranceProgressListener() {
Expand Down Expand Up @@ -89,14 +90,14 @@ class TTSManager(context: Context) :
val calorieMsg: String? = food.kcal?.let {"해당 식품의 칼로리는 $it kcal 입니다. "}

val allergyMsg: String? = food.allergy?.let {
"해당 식품에는 ${it.joinToString(", ")} 성분이 함유되어 있습니다. "
"해당 식품에는 ${it.joinToString(", ").addSubjectMarker()} 함유되어 있습니다. "
}
val commonAllergyMsg: String? = food.allergy?.let { foodAllergy ->
AppUser.info?.findMatchingAllergy(foodAllergy)?.let { commonAllergy -> // 인식 정보 O
if (commonAllergy.isEmpty()) { // 사용자 - 식품 알러지 교집합 X
"당신의 알러지 성분은 함유되어 있지 않네요. "
} else { // 사용자 - 식품 알러지 교집합 O
"주의하세요. 해당 식품에는 당신이 유의해야 할 ${commonAllergy.joinToString()} 성분이 함유되어 있습니다. "
"주의하세요. 해당 식품에는 당신이 유의해야 할, ${commonAllergy.joinToString(", ").addSubjectMarker()} 함유되어 있습니다. "
}
}
} ?: run { null } // 인식 정보 X
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/dna/extensions/StringExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dna.extensions

fun String.hasBatchim(): Boolean {
val lastChar = this.last()
val unicode = lastChar.toInt()
return (unicode - 0xAC00) % 28 != 0
}

fun String.addSubjectMarker(): String {
return if(this.hasBatchim()) "${this}" else "${this}"
}

0 comments on commit 1e75534

Please sign in to comment.