Skip to content

Commit

Permalink
feat : 닉네임 중복 검사 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-comlinee committed Dec 6, 2024
1 parent 328c6ee commit fe7eda7
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
114 changes: 112 additions & 2 deletions app/src/main/java/com/dna/beyoureyes/AssignNameFragment.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package com.dna.beyoureyes

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import com.dna.beyoureyes.databinding.ActivityAssignBinding
import com.dna.beyoureyes.databinding.FragmentAssignNameBinding
import com.dna.beyoureyes.model.FirebaseHelper
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.
Expand Down Expand Up @@ -48,8 +59,22 @@ class AssignNameFragment : Fragment() {
Toast.makeText(requireContext(), "이름을 입력해주세요.", Toast.LENGTH_LONG).show()
}
else {
listener?.onNameInputRecieved(binding.nameInput.text.toString())
listener?.onBtnClick(this, true)
when(isDuplicateNickname) {
-1 -> {
Toast.makeText(requireContext(), "중복 검사를 해주세요.", Toast.LENGTH_LONG).show()
}
0 -> {
Toast.makeText(requireContext(), "중복된 이름입니다. 다른 이름을 사용해주세요.", Toast.LENGTH_LONG).show()
}
1 -> {
listener?.onNameInputRecieved(binding.nameInput.text.toString())
listener?.onBtnClick(this, true)
}
else -> {
Toast.makeText(requireContext(), "error", Toast.LENGTH_LONG).show()
}
}

}
}

Expand All @@ -64,6 +89,91 @@ class AssignNameFragment : Fragment() {
binding.nameInput.text = null
}

binding.searchIcon.setOnClickListener {
lifecycleScope.launch {
val isDuplicate = FirebaseHelper.checkDuplicateName(binding.nameInput.text.toString().trim())
if (isDuplicate) {
isDuplicateNickname = 0
binding.validationText.setText("중복된 이름입니다. 다시 설정해주세요!")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.alert_500))
YoYo.with(Techniques.Shake)
.duration(700)
.delay(0)
.playOn(binding.validationText)
}
else {
isDuplicateNickname = 1
binding.validationText.setText("사용 가능한 닉네임입니다!")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.blue_500))
}
}
}
/*
binding.nameInput.setOnEditorActionListener { v, actionId, event ->
// 엔터키가 눌러졌을 때
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
// 키보드를 내린 후 중복 검사 실행
val nickname = v.text.toString().trim()
if (nickname.isNotEmpty()) {
lifecycleScope.launch {
val isDuplicate = FirebaseHelper.checkDuplicateName(nickname)
if (isDuplicate) {
isDuplicateNickname = 0
binding.validationText.setText("중복된 이름입니다. 다시 설정해주세요!")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.alert_500))
YoYo.with(Techniques.Shake)
.duration(1000)
.delay(0)
.playOn(binding.validationText)
} else {
isDuplicateNickname = 1
binding.validationText.setText("사용 가능한 닉네임입니다.")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.blue_500))
}
}
}
true // true를 반환하여 기본 엔터키 동작을 막음
} else {
false // 엔터 외의 다른 키 이벤트는 처리하지 않음
}
}
*/

binding.nameInput.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence?, start: Int, count: Int, after: Int) {
// 텍스트가 변경되기 전
}

override fun onTextChanged(charSequence: CharSequence?, start: Int, before: Int, count: Int) {
// 텍스트가 변경되는 중
}

override fun afterTextChanged(editable: Editable?) {
// 텍스트가 변경된 후에 자동으로 중복 검사
val nickname = editable.toString().trim()
if (nickname.isNotEmpty()) {
// 비동기적으로 중복 검사
lifecycleScope.launch {
val isDuplicate = FirebaseHelper.checkDuplicateName(nickname)
if (isDuplicate) {
isDuplicateNickname = 0
binding.validationText.setText("중복된 이름입니다. 다시 설정해주세요!")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.alert_500))
YoYo.with(Techniques.Shake)
.duration(700)
.delay(0)
.playOn(binding.validationText)
} else {
isDuplicateNickname = 1
binding.validationText.setText("사용 가능한 닉네임입니다.")
binding.validationText.setTextColor(ContextCompat.getColor(requireContext(), R.color.blue_500))
}
}
}
}
})


return binding.root
}

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/dna/beyoureyes/model/FirebaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,19 @@ class FirebaseHelper {
false
}
}

suspend fun checkDuplicateName(nickname: String): Boolean {
val db = Firebase.firestore
return try {
val querySnapshot = db.collection("userInfo")
.whereEqualTo("userName", nickname)
.get()
.await()
!querySnapshot.isEmpty // 결과가 비어있지 않으면 중복
} catch (exception: Exception) {
Log.e("REGISTERFIRESTORE", "Error checking nickname", exception)
false // 오류 발생 시 기본값 반환
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/raw/loading.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"v":"5.5.7","fr":29.9700012207031,"ip":0,"op":69.0000028104276,"w":1920,"h":1080,"nm":"Animation2","ddd":0,"assets":[],"layers":[{"ddd":0,"ind":1,"ty":3,"nm":"Null 1","sr":1,"ks":{"o":{"a":0,"k":0,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.74],"y":[0]},"t":0,"s":[0]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":29,"s":[-45]},{"t":70.0000028511585,"s":[0]}],"ix":10},"p":{"a":0,"k":[960,540,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"ip":0,"op":900.000036657751,"st":0,"bm":0,"completed":true},{"ddd":0,"ind":2,"ty":4,"nm":"Circulo1","parent":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.604],"y":[0.605]},"t":16,"s":[0]},{"t":46.0000018736184,"s":[360]}],"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"d":1,"ty":"el","s":{"a":0,"k":[40,40],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"nm":"Ellipse Path 1","mn":"ADBE Vector Shape - Ellipse","hd":false,"_render":true},{"ty":"st","c":{"a":0,"k":[1,1,1,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":0,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false,"_render":true},{"ty":"fl","c":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":0,"s":[1,0.7412,0,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":15,"s":[1,0.3176,0.2,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[0.0824,0.2627,0.8784,1]},{"t":44.0000017921567,"s":[1,0.7412,0,1]}],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false,"_render":true},{"ty":"tr","p":{"a":0,"k":[0,-210],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform","_render":true}],"nm":"Ellipse 1","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false,"_render":true}],"ip":16.0000006516934,"op":916.000037309445,"st":16.0000006516934,"bm":0,"completed":true},{"ddd":0,"ind":3,"ty":4,"nm":"Circulo5","parent":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.604],"y":[0.605]},"t":12,"s":[0]},{"t":42.0000017106951,"s":[360]}],"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"d":1,"ty":"el","s":{"a":0,"k":[55,55],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"nm":"Ellipse Path 1","mn":"ADBE Vector Shape - Ellipse","hd":false,"_render":true},{"ty":"st","c":{"a":0,"k":[1,1,1,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":0,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false,"_render":true},{"ty":"fl","c":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":1,"s":[1,0.7412,0,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":15,"s":[1,0.3176,0.2,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[0.0824,0.2627,0.8784,1]},{"t":44.0000017921567,"s":[1,0.7412,0,1]}],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false,"_render":true},{"ty":"tr","p":{"a":0,"k":[0,-210],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform","_render":true}],"nm":"Ellipse 1","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false,"_render":true}],"ip":12.00000048877,"op":912.000037146522,"st":12.00000048877,"bm":0,"completed":true},{"ddd":0,"ind":4,"ty":4,"nm":"Circulo4","parent":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.604],"y":[0.605]},"t":8,"s":[0]},{"t":38.0000015477717,"s":[360]}],"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"d":1,"ty":"el","s":{"a":0,"k":[70,70],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"nm":"Ellipse Path 1","mn":"ADBE Vector Shape - Ellipse","hd":false,"_render":true},{"ty":"st","c":{"a":0,"k":[1,1,1,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":0,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false,"_render":true},{"ty":"fl","c":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":1,"s":[1,0.7412,0,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":15,"s":[1,0.3176,0.2,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[0.0824,0.2627,0.8784,1]},{"t":44.0000017921567,"s":[1,0.7412,0,1]}],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false,"_render":true},{"ty":"tr","p":{"a":0,"k":[0,-210],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform","_render":true}],"nm":"Ellipse 1","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false,"_render":true}],"ip":8.00000032584668,"op":908.000036983598,"st":8.00000032584668,"bm":0,"completed":true},{"ddd":0,"ind":5,"ty":4,"nm":"Circulo3","parent":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.604],"y":[0.605]},"t":4,"s":[0]},{"t":34.0000013848484,"s":[360]}],"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"d":1,"ty":"el","s":{"a":0,"k":[80,80],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"nm":"Ellipse Path 1","mn":"ADBE Vector Shape - Ellipse","hd":false,"_render":true},{"ty":"st","c":{"a":0,"k":[1,1,1,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":0,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false,"_render":true},{"ty":"fl","c":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":1,"s":[1,0.7412,0,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":15,"s":[1,0.3176,0.2,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[0.0824,0.2627,0.8784,1]},{"t":44.0000017921567,"s":[1,0.7412,0,1]}],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false,"_render":true},{"ty":"tr","p":{"a":0,"k":[0,-210],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform","_render":true}],"nm":"Ellipse 1","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false,"_render":true}],"ip":4.00000016292334,"op":904.000036820675,"st":4.00000016292334,"bm":0,"completed":true},{"ddd":0,"ind":6,"ty":4,"nm":"Circulo2","parent":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.604],"y":[0.605]},"t":0,"s":[0]},{"t":30.0000012219251,"s":[360]}],"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"d":1,"ty":"el","s":{"a":1,"k":[{"i":{"x":[0.833,0.833],"y":[0.833,0.833]},"o":{"x":[0.167,0.167],"y":[0.167,0.167]},"t":-4,"s":[80,80]},{"i":{"x":[0.833,0.833],"y":[0.833,0.833]},"o":{"x":[0.167,0.167],"y":[0.167,0.167]},"t":0,"s":[100,100]},{"i":{"x":[0.833,0.833],"y":[0.833,0.833]},"o":{"x":[0.167,0.167],"y":[0.167,0.167]},"t":17,"s":[80,80]},{"i":{"x":[0.833,0.833],"y":[0.833,0.833]},"o":{"x":[0.167,0.167],"y":[0.167,0.167]},"t":30,"s":[80,80]},{"t":33.0000013441176,"s":[100,100]}],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"nm":"Ellipse Path 1","mn":"ADBE Vector Shape - Ellipse","hd":false,"_render":true},{"ty":"st","c":{"a":0,"k":[1,1,1,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":0,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false,"_render":true},{"ty":"fl","c":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":1,"s":[1,0.7412,0,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":15,"s":[1,0.3176,0.2,1]},{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":30,"s":[0.0824,0.2627,0.8784,1]},{"t":44.0000017921567,"s":[1,0.7412,0,1]}],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false,"_render":true},{"ty":"tr","p":{"a":0,"k":[0,-210],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform","_render":true}],"nm":"Ellipse 1","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false,"_render":true}],"ip":0,"op":900.000036657751,"st":0,"bm":0,"completed":true}],"markers":[],"__complete":true}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<string name="assign_step2_question">고객님의 성별은 무엇인가요?</string>
<string name="assign_step3_question">고객님의 출생연도는\n몇년인가요?</string>
<string name="assign_name_placeholder">닉네임 입력</string>
<string name="assign_name_check_ok">supporting</string>
<string name="assign_name_check_ok">중복 확인을 진행해주세요.</string>
<string name="assign_name_check_no">누군가가 이미 사용 중입니다.</string>
<string name="assign_female">여성</string>
<string name="assign_male">남성</string>
Expand Down

0 comments on commit fe7eda7

Please sign in to comment.