Skip to content

Commit

Permalink
update: use Allergen Set to handle UserInfo and Food
Browse files Browse the repository at this point in the history
  • Loading branch information
moon0900 committed Dec 16, 2024
1 parent cd6d565 commit 7600c5f
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 384 deletions.
11 changes: 8 additions & 3 deletions app/src/main/java/com/dna/beyoureyes/AssignActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.dna.beyoureyes.model.Allergen
import com.dna.beyoureyes.model.FirebaseHelper
import com.dna.beyoureyes.model.UserInfo
import com.dna.beyoureyes.ui.FragmentNavigationListener
Expand All @@ -21,6 +22,7 @@ class AssignActivity : AppCompatActivity(), FragmentNavigationListener {
private var currentStep = 0
private var disease : ArrayList<String> = ArrayList<String>()
private var allergy : ArrayList<String> = ArrayList<String>()
private var allergens : MutableSet<Allergen> = mutableSetOf()
private var profile : String = ""

override fun onNavigateToFragment(fragment: Fragment) {
Expand All @@ -43,8 +45,8 @@ class AssignActivity : AppCompatActivity(), FragmentNavigationListener {
this.disease = userDiseaseList
}

override fun onAllergyInputRecieved(userAllergyList: ArrayList<String>) {
this.allergy = userAllergyList
override fun onAllergyInputRecieved(userAllergySet: MutableSet<Allergen>) {
this.allergens = userAllergySet
}

override fun onBackPressed() {
Expand Down Expand Up @@ -97,7 +99,7 @@ class AssignActivity : AppCompatActivity(), FragmentNavigationListener {
val dateFormat = SimpleDateFormat("yyyyMMdd", Locale.getDefault())
val birthDate = dateFormat.parse(birth ?: "") ?: Date()
val birthTimeStamp = Timestamp(birthDate)
AppUser.info = UserInfo(name?:"", gender?:0, birthTimeStamp, disease, allergy)
AppUser.info = UserInfo(name?:"", gender?:0, birthTimeStamp, disease, allergens.ifEmpty { null })
val userInfo = hashMapOf(
"userId" to Firebase.auth.currentUser?.uid,
"userName" to name!!,
Expand All @@ -107,6 +109,9 @@ class AssignActivity : AppCompatActivity(), FragmentNavigationListener {
"userAllergy" to allergy,
"userProfile" to profile
)
if (allergens.isNotEmpty()) {
userInfo["userAllergens"] = allergens.map{ it.name }
}
FirebaseHelper.sendData(userInfo, "userInfo")
}

Expand Down
110 changes: 47 additions & 63 deletions app/src/main/java/com/dna/beyoureyes/AssignAllergyFragment.kt
Original file line number Diff line number Diff line change
@@ -1,73 +1,80 @@
package com.dna.beyoureyes

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 com.dna.beyoureyes.databinding.FragmentAssignAllergyBinding
import com.dna.beyoureyes.databinding.FragmentAssignBirthBinding
import com.dna.beyoureyes.model.Allergen
import com.dna.beyoureyes.ui.CustomToolbar
import com.dna.beyoureyes.ui.FragmentNavigationListener
import com.google.android.material.chip.Chip

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
* A simple [Fragment] subclass.
* Use the [AssignAllergyFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AssignAllergyFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null

private lateinit var binding : FragmentAssignAllergyBinding
private var allergyArray : ArrayList<String> = ArrayList()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
private var allergenSet : MutableSet<Allergen> = mutableSetOf()
private val chipIdToAllergenMap = HashMap<Int, Allergen>() // 칩 ID와 Allergen Enum 값을 매핑하는 HashMap

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
binding = FragmentAssignAllergyBinding.inflate(inflater, container, false)
binding.assignAllergyChipGroup.setOnCheckedStateChangeListener{ group, checkedIds ->
// "없음" 클릭 시 다른 칩 해제
if (checkedIds.contains(R.id.chip_none)) {
for (chipId in group.checkedChipIds) {
if (chipId != R.id.chip_none) {
group.findViewById<Chip>(chipId).isChecked = false
}

Allergen.entries.forEach { alg ->
// 칩 스타일 및 표시 관련 설정
val chip = Chip(context)
chip.text =if (alg.displayName.length == 1) " ${alg.displayName} " else alg.displayName
chip.isCheckable = true
chip.layoutParams = LinearLayout.LayoutParams(
resources.getDimensionPixelSize(R.dimen.checkable_chip_width),
resources.getDimensionPixelSize(R.dimen.checkable_chip_height)
).also { it.gravity = Gravity.CENTER }

// 칩 ID 할당
chip.id = View.generateViewId()
chipIdToAllergenMap[chip.id] = alg // [칩 ID] - [알러지 정보] hashMap 등록

// 알레르기 칩 check 상태 변경 리스너
chip.setOnCheckedChangeListener { _, isChecked ->
if(isChecked) {
allergenSet.add(alg)
binding.chipNone.isChecked = false // "없음" 칩 해제
} else {
allergenSet.remove(alg)
}
}
// 다른 칩 클릭 시 "없음" 해제
else {
group.findViewById<Chip>(R.id.chip_none)?.isChecked = false
Log.d("ASSIGN_ALLERGY",
"selected chips: ${allergenSet.joinToString(", ") { it.displayName }}")
}

// 선택된 칩 리스트 업데이트
allergyArray.clear()
for (chipId in group.checkedChipIds) {
val chip: Chip = group.findViewById(chipId)
allergyArray.add(resources.getResourceEntryName(chip.id).substring(5))
}
// 그룹에 칩 추가
binding.assignAllergyChipGroup.addView(chip)
}

// 없음 칩 check 상태 변경 리스너
val chipGroup = binding.assignAllergyChipGroup
binding.chipNone.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
for (id in chipGroup.checkedChipIds.intersect(chipIdToAllergenMap.keys)){
chipGroup.findViewById<Chip>(id).isChecked = false // 선택된 알레르기 칩 해제
}
allergenSet.clear() // 저장된 거 비우기
Log.d("ASSIGN_ALLERGY",
"selected chips: ${allergenSet.joinToString(", ") { it.displayName }}")
}
}

// Inflate the layout for this fragment
val listener = activity as? FragmentNavigationListener
binding.nextBtn.setOnClickListener {
listener?.onAllergyInputRecieved(allergyArray)
listener?.onAllergyInputRecieved(allergenSet)
listener?.onBtnClick(this, true)
}
binding.toolbar.backButtonClickListener = object : CustomToolbar.ButtonClickListener {
Expand All @@ -78,27 +85,4 @@ class AssignAllergyFragment : Fragment() {
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AssignAllergyFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AssignAllergyFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
42 changes: 2 additions & 40 deletions app/src/main/java/com/dna/beyoureyes/AssignBirthFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,11 @@ import com.dna.beyoureyes.ui.CustomToolbar
import com.dna.beyoureyes.ui.FragmentNavigationListener
import java.util.Calendar

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
* A simple [Fragment] subclass.
* Use the [AssignBirthFragment.newInstance] factory method to
* create an instance of this fragment.
*/

class AssignBirthFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null

private lateinit var binding : FragmentAssignBirthBinding
private var birth = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down Expand Up @@ -129,24 +111,4 @@ class AssignBirthFragment : Fragment() {
return displayValues.toTypedArray()
}


companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AssignBirthFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AssignBirthFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
4 changes: 0 additions & 4 deletions app/src/main/java/com/dna/beyoureyes/AssignDiseaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import com.dna.beyoureyes.ui.FragmentNavigationListener
class AssignDiseaseFragment : Fragment() {
private lateinit var binding : FragmentAssignDiseaseBinding
private lateinit var diseaseArray: Array<diseaseInfo>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down
42 changes: 0 additions & 42 deletions app/src/main/java/com/dna/beyoureyes/AssignGenderFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,12 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.dna.beyoureyes.databinding.FragmentAssignGenderBinding
import com.dna.beyoureyes.databinding.FragmentAssignNameBinding
import com.dna.beyoureyes.ui.CustomToolbar
import com.dna.beyoureyes.ui.FragmentNavigationListener

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
* A simple [Fragment] subclass.
* Use the [AssignGenderFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AssignGenderFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var binding : FragmentAssignGenderBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -79,24 +57,4 @@ class AssignGenderFragment : Fragment() {
else if(binding.chipMale.isChecked) return 1
else return -1
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AssignGenderFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AssignGenderFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
40 changes: 1 addition & 39 deletions app/src/main/java/com/dna/beyoureyes/AssignNameFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,11 @@ import com.dna.beyoureyes.ui.CustomToolbar
import com.dna.beyoureyes.ui.FragmentNavigationListener
import kotlinx.coroutines.launch

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
private var isDuplicateNickname = 0

/**
* A simple [Fragment] subclass.
* Use the [AssignNameFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AssignNameFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var binding: FragmentAssignNameBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
private lateinit var binding: FragmentAssignNameBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand Down Expand Up @@ -177,23 +158,4 @@ class AssignNameFragment : Fragment() {
return binding.root
}

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment AssignNameFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AssignNameFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/dna/beyoureyes/model/FirebaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class FirebaseHelper {
val userGender = document.data.get("userGender") as Long
val userBirth = document.data.get("userBirth") as Timestamp
val userDisease = document.data.get("userDisease") as ArrayList<String>
val userAllergy = document.data.get("userAllergy") as ArrayList<String>
val userAllergens = document.data.get("userAllergens") as? ArrayList<String>
val profile = document.data.get("userProfile") as String?
AppUser.setInfo(userName, userGender.toInt(), userBirth, userDisease, userAllergy, profile)
AppUser.setInfo(userName, userGender.toInt(), userBirth, userDisease, userAllergens, profile)

// 프로필 사진 uri 로드하여 저장
profile?.let{
Expand Down
Loading

0 comments on commit 7600c5f

Please sign in to comment.