From fd7eb774ed29c0af616a24691c61b56ebda898d0 Mon Sep 17 00:00:00 2001 From: HyunWoo Lee Date: Wed, 5 Jul 2023 18:19:49 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=EC=AA=BD?= =?UTF-8?q?=20Refactoring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pophory/feature/signup/SignUpActivity.kt | 32 ++---- .../feature/signup/SignUpButtonInterface.kt | 6 +- .../feature/signup/SignUpDialogFragment.kt | 26 ++--- .../feature/signup/SignUpFirstFragment.kt | 97 ++++++----------- .../feature/signup/SignUpSecondFragment.kt | 101 +++++++----------- .../feature/signup/SignUpThirdFragment.kt | 18 +--- .../pophory/feature/signup/SignUpViewModel.kt | 4 +- .../feature/signup/StartPophoryActivity.kt | 5 +- .../signup/adapter/SignUpViewPagerAdapter.kt | 10 +- 9 files changed, 106 insertions(+), 193 deletions(-) diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpActivity.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpActivity.kt index 13836bfc..f3d27c79 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpActivity.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpActivity.kt @@ -14,8 +14,6 @@ import com.teampophory.pophory.feature.signup.adapter.SignUpViewPagerAdapter class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { private val binding by viewBinding(ActivitySignUpBinding::inflate) - private lateinit var viewPager: ViewPager2 - private lateinit var viewPagerAdapter: SignUpViewPagerAdapter private val viewModel by viewModels() private var currentPosition = 0 @@ -29,11 +27,11 @@ class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { toast(it) } - //다음 버튼 + // 다음 버튼 clickNextButton() - //뷰페이저 + // 뷰페이저 setViewPager() - //백버튼 클릭 + // 백버튼 클릭 clickToolbarBackButton() } @@ -63,15 +61,14 @@ class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { binding.btnNext.setOnClickListener { when (currentPosition) { 0 -> { - val nextPosition = currentPosition + 1 - binding.viewpager.currentItem = nextPosition + binding.viewpager.currentItem = currentPosition + 1 } 1 -> { - //todo 중복된 아이디가 존재하는 경우 + // TODO 중복된 아이디가 존재하는 경우 val dialog = SignUpDialogFragment() dialog.show(supportFragmentManager, "") - //중복된 아이디가 존재하지 않을 경우 +// 중복된 아이디가 존재하지 않을 경우 // val nextPosition = currentPosition + 1 // binding.viewpager.currentItem = nextPosition } @@ -86,17 +83,13 @@ class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() { - override fun onPageScrollStateChanged(state: Int) { - super.onPageScrollStateChanged(state) - } - override fun onPageScrolled( position: Int, positionOffset: Float, positionOffsetPixels: Int ) { super.onPageScrolled(position, positionOffset, positionOffsetPixels) - //tablayout 변경 + // tablayout 변경 with(binding) { tabFirst.isSelected = position == 0 tabSecond.isSelected = position == 1 @@ -118,14 +111,11 @@ class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { } private fun setViewPager() { - viewPagerAdapter = SignUpViewPagerAdapter(this, this) - viewPager = binding.viewpager - - viewPager.apply { - adapter = viewPagerAdapter + binding.viewpager.apply { + adapter = SignUpViewPagerAdapter(this@SignUpActivity, this@SignUpActivity) isUserInputEnabled = false } - viewPager.registerOnPageChangeCallback(pageChangeCallback) + binding.viewpager.registerOnPageChangeCallback(pageChangeCallback) } private fun setButton(buttonText: String, isEnabled: Boolean) { @@ -133,7 +123,7 @@ class SignUpActivity : AppCompatActivity(), SignUpButtonInterface { binding.btnNext.isEnabled = isEnabled } - override fun setButtonState(state: Boolean) { + override fun onChangeState(state: Boolean) { binding.btnNext.isEnabled = state } } \ No newline at end of file diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpButtonInterface.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpButtonInterface.kt index 9ef1ea51..dc410d25 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpButtonInterface.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpButtonInterface.kt @@ -1,5 +1,5 @@ package com.teampophory.pophory.feature.signup -interface SignUpButtonInterface { - fun setButtonState(state : Boolean) -} \ No newline at end of file +fun interface SignUpButtonInterface { + fun onChangeState(state: Boolean) +} diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpDialogFragment.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpDialogFragment.kt index f8891be7..3e54785e 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpDialogFragment.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpDialogFragment.kt @@ -7,28 +7,26 @@ import android.graphics.Point import android.graphics.drawable.ColorDrawable import android.os.Build import android.os.Bundle -import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.WindowInsets import android.view.WindowManager import androidx.fragment.app.DialogFragment -import com.teampophory.pophory.R -import com.teampophory.pophory.common.view.dp +import com.teampophory.pophory.common.view.viewBinding import com.teampophory.pophory.databinding.FragmentSignUpDialogBinding class SignUpDialogFragment : DialogFragment() { + private val binding by viewBinding(FragmentSignUpDialogBinding::bind) - private var _binding: FragmentSignUpDialogBinding? = null - private val binding get() = _binding!! - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - _binding = FragmentSignUpDialogBinding.inflate(inflater, container, false) - val view = binding.root + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { // 레이아웃 배경을 투명하게 해줌 dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) - return view + return FragmentSignUpDialogBinding.inflate(inflater, container, false).root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -45,6 +43,7 @@ class SignUpDialogFragment : DialogFragment() { dialogWidthPercent(it, dialog) } } + private fun dialogWidthPercent(context: Context, dialog: Dialog?, percent: Double = 0.8) { val deviceSize = getDeviceSize(context) dialog?.window?.run { @@ -79,9 +78,4 @@ class SignUpDialogFragment : DialogFragment() { return intArrayOf(size.x, size.y) } } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpFirstFragment.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpFirstFragment.kt index 8f0f746b..690fd8f4 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpFirstFragment.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpFirstFragment.kt @@ -1,49 +1,40 @@ package com.teampophory.pophory.feature.signup import android.os.Bundle -import android.text.Editable -import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isGone import androidx.core.view.isVisible +import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.Fragment import com.teampophory.pophory.R +import com.teampophory.pophory.common.view.viewBinding import com.teampophory.pophory.databinding.FragmentSignUpFirstBinding import java.util.regex.Pattern class SignUpFirstFragment : Fragment() { - - private var _binding: FragmentSignUpFirstBinding? = null - private var buttonState:SignUpButtonInterface? = null - private val binding: FragmentSignUpFirstBinding - get() = requireNotNull(_binding) { "앗 ! _binding이 null이다 !" } + private val binding by viewBinding(FragmentSignUpFirstBinding::bind) + private var buttonState: SignUpButtonInterface? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - _binding = FragmentSignUpFirstBinding.inflate(inflater, container, false) - return binding.root + ): View { + return FragmentSignUpFirstBinding.inflate(inflater, container, false).root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.tvErrorMessage.isVisible = false binding.btnDeleteEditText.isGone = true - //edittext 상태 + // edittext 상태 setEditText() - //edittext 삭제 버튼 + // edittext 삭제 버튼 deleteAllEditText() } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } - private fun deleteAllEditText() { binding.btnDeleteEditText.setOnClickListener { binding.editTvName.text.clear() @@ -53,65 +44,47 @@ class SignUpFirstFragment : Fragment() { private fun setEditText() { //텍스트창 활성화 binding.editTvName.apply { - setOnFocusChangeListener { view, hasFocus -> - //포커스가 주어졌을 때 + setOnFocusChangeListener { _, hasFocus -> + // 포커스가 주어졌을 때 if (hasFocus) { setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) } else { setBackgroundResource(R.drawable.bg_sign_up_edit_text_default) } } - //edittext text 변화 감지 - addTextChangedListener(object : TextWatcher { - override fun beforeTextChanged( - s: CharSequence?, - start: Int, - count: Int, - after: Int - ) { - } - - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - //X버튼 생성 여부 - binding.btnDeleteEditText.isGone = count < 1 - //글자 수 계산 - binding.tvTextCount.text = "(${s.toString().length}/6)" - - //에러 메시지 : 현재 한국어만 지원함! - val pattern = Pattern.compile(HANGUL_PATTERN) - val matcher = pattern.matcher(binding.editTvName.text) - if (!matcher.find()) { - binding.tvErrorMessage.text = "현재 한국어만 지원하고 있어요." - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) - binding.tvErrorMessage.isVisible = true - buttonState?.setButtonState(false) - } else if (s.toString().length < 2) { - binding.tvErrorMessage.text = "2-6글자 이내로 작성해주세요." - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) - binding.tvErrorMessage.isVisible = true - buttonState?.setButtonState(false) - } else { - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) - binding.tvErrorMessage.isVisible = false - buttonState?.setButtonState(true) - } - - } - - override fun afterTextChanged(s: Editable?) { - + doAfterTextChanged { + binding.btnDeleteEditText.isGone = it?.isEmpty() == true + // 글자 수 계산 + binding.tvTextCount.text = "(${it.toString().length}/6)" + + // 에러 메시지 : 현재 한국어만 지원함! + val matcher = HANGUL_REGEX.matcher(binding.editTvName.text) + if (!matcher.find()) { + binding.tvErrorMessage.text = "현재 한국어만 지원하고 있어요." + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) + binding.tvErrorMessage.isVisible = true + buttonState?.onChangeState(false) + } else if (it.toString().length < 2) { + binding.tvErrorMessage.text = "2-6글자 이내로 작성해주세요." + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) + binding.tvErrorMessage.isVisible = true + buttonState?.onChangeState(false) + } else { + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) + binding.tvErrorMessage.isVisible = false + buttonState?.onChangeState(true) } - - }) + } } } - fun setSignUpButtonInterface(buttonState: SignUpButtonInterface){ + fun setSignUpButtonInterface(buttonState: SignUpButtonInterface) { this.buttonState = buttonState } companion object { - const val HANGUL_PATTERN = "^[ㄱ-ㅎㅏ-ㅣ가-힣]*\$" + private const val HANGUL_PATTERN = "^[ㄱ-ㅎㅏ-ㅣ가-힣]*\$" + val HANGUL_REGEX: Pattern = Pattern.compile(HANGUL_PATTERN) } } diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpSecondFragment.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpSecondFragment.kt index 7d4ae279..c9458766 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpSecondFragment.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpSecondFragment.kt @@ -1,49 +1,38 @@ package com.teampophory.pophory.feature.signup import android.os.Bundle -import android.text.Editable -import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isGone import androidx.core.view.isVisible +import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.Fragment import com.teampophory.pophory.R +import com.teampophory.pophory.common.view.viewBinding import com.teampophory.pophory.databinding.FragmentSignUpSecondBinding import java.util.regex.Pattern class SignUpSecondFragment : Fragment() { - - private var _binding: FragmentSignUpSecondBinding? = null - private var buttonState:SignUpButtonInterface? = null - private val binding: FragmentSignUpSecondBinding - get() = requireNotNull(_binding) { "앗 ! _binding이 null이다 !" } + private val binding by viewBinding(FragmentSignUpSecondBinding::bind) + private var buttonState: SignUpButtonInterface? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - _binding = FragmentSignUpSecondBinding.inflate(inflater, container, false) - binding.tvErrorMessage.isVisible = false - binding.btnDeleteEditText.isGone = true - return binding.root + ): View { + return FragmentSignUpSecondBinding.inflate(inflater, container, false).root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - + binding.tvErrorMessage.isVisible = false + binding.btnDeleteEditText.isGone = true setEditText() - deleteAllEditText() } - override fun onDestroyView() { - super.onDestroy() - _binding = null - } - private fun deleteAllEditText() { binding.btnDeleteEditText.setOnClickListener { binding.editTvName.text.clear() @@ -51,60 +40,44 @@ class SignUpSecondFragment : Fragment() { } private fun setEditText() { - //텍스트창 활성화 + // 텍스트창 활성화 binding.editTvName.apply { - setOnFocusChangeListener { view, b -> - //포커스가 주어졌을 때 - if (b) { + setOnFocusChangeListener { _, hasFocus -> + // 포커스가 주어졌을 때 + if (hasFocus) { setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) } else { setBackgroundResource(R.drawable.bg_sign_up_edit_text_default) } } - - //edittext text 변화 감지 - addTextChangedListener(object : TextWatcher { - override fun beforeTextChanged( - s: CharSequence?, - start: Int, - count: Int, - after: Int - ) { - } - - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - //X버튼 생성 여부 - binding.btnDeleteEditText.isGone = count < 1 - //글자 수 계산 - binding.tvTextCount.text = "(${s.toString().length}/12)" - - val pattern = Pattern.compile(ID_PATTERN) - val matcher = pattern.matcher(binding.editTvName.text) - if (!matcher.find()) { - binding.tvErrorMessage.text = "*올바른 형식의 아이디가 아닙니다" - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) - binding.tvErrorMessage.isVisible = true - buttonState?.setButtonState(false) - } else if (s.toString().length < 4) { - binding.tvErrorMessage.text = "4-12글자 이내로 작성해주세요." - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) - binding.tvErrorMessage.isVisible = true - buttonState?.setButtonState(false) - } else { - binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) - binding.tvErrorMessage.isVisible = false - buttonState?.setButtonState(true) - } - } - - override fun afterTextChanged(s: Editable?) { - + doAfterTextChanged { + //X버튼 생성 여부 + binding.btnDeleteEditText.isGone = it?.isEmpty() == true + //글자 수 계산 + binding.tvTextCount.text = "(${it.toString().length}/12)" + + val pattern = Pattern.compile(ID_PATTERN) + val matcher = pattern.matcher(binding.editTvName.text) + if (!matcher.find()) { + binding.tvErrorMessage.text = "*올바른 형식의 아이디가 아닙니다" + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) + binding.tvErrorMessage.isVisible = true + buttonState?.onChangeState(false) + } else if (it.toString().length < 4) { + binding.tvErrorMessage.text = "4-12글자 이내로 작성해주세요." + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_error) + binding.tvErrorMessage.isVisible = true + buttonState?.onChangeState(false) + } else { + binding.editTvName.setBackgroundResource(R.drawable.bg_sign_up_edit_text_selected) + binding.tvErrorMessage.isVisible = false + buttonState?.onChangeState(true) } - - }) + } } } - fun setSignUpButtonInterface(buttonState: SignUpButtonInterface){ + + fun setSignUpButtonInterface(buttonState: SignUpButtonInterface) { this.buttonState = buttonState } diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpThirdFragment.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpThirdFragment.kt index db83d751..c418ee12 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpThirdFragment.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpThirdFragment.kt @@ -1,39 +1,29 @@ package com.teampophory.pophory.feature.signup -import android.graphics.PorterDuff import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isVisible import androidx.fragment.app.Fragment +import com.teampophory.pophory.common.view.viewBinding import com.teampophory.pophory.databinding.FragmentSignUpThirdBinding class SignUpThirdFragment : Fragment() { - - private var _binding: FragmentSignUpThirdBinding? = null - private val binding: FragmentSignUpThirdBinding - get() = requireNotNull(_binding) { "앗 ! _binding이 null이다 !" } + private val binding by viewBinding(FragmentSignUpThirdBinding::bind) override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - _binding = FragmentSignUpThirdBinding.inflate(inflater, container, false) - - return binding.root + ): View { + return FragmentSignUpThirdBinding.inflate(inflater, container, false).root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - selectAlbumCover() } - override fun onDestroyView() { - super.onDestroy() - _binding = null - } private fun selectAlbumCover() { binding.ivAlbumSelect1.isVisible = true binding.ivAlbumSelect2.isVisible = false diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpViewModel.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpViewModel.kt index 907155a4..4ba674fb 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpViewModel.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/SignUpViewModel.kt @@ -7,9 +7,9 @@ import androidx.lifecycle.ViewModel class SignUpViewModel : ViewModel() { private val _sampleLiveData = MutableLiveData() - val sampleLiveData : LiveDataget() = _sampleLiveData + val sampleLiveData: LiveData get() = _sampleLiveData fun getToastMessage() { _sampleLiveData.value = "테스트 입니다." } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/StartPophoryActivity.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/StartPophoryActivity.kt index ec1e88b0..24afcdaf 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/StartPophoryActivity.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/StartPophoryActivity.kt @@ -1,9 +1,8 @@ package com.teampophory.pophory.feature.signup import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import com.teampophory.pophory.R +import androidx.appcompat.app.AppCompatActivity import com.teampophory.pophory.common.view.viewBinding import com.teampophory.pophory.databinding.ActivityStartPophoryBinding import com.teampophory.pophory.feature.home.HomeActivity @@ -21,4 +20,4 @@ class StartPophoryActivity : AppCompatActivity() { startActivity(intent) } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/teampophory/pophory/feature/signup/adapter/SignUpViewPagerAdapter.kt b/app/src/main/java/com/teampophory/pophory/feature/signup/adapter/SignUpViewPagerAdapter.kt index b41af1fa..09b0feba 100644 --- a/app/src/main/java/com/teampophory/pophory/feature/signup/adapter/SignUpViewPagerAdapter.kt +++ b/app/src/main/java/com/teampophory/pophory/feature/signup/adapter/SignUpViewPagerAdapter.kt @@ -3,7 +3,6 @@ package com.teampophory.pophory.feature.signup.adapter import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.viewpager2.adapter.FragmentStateAdapter -import com.teampophory.pophory.feature.signup.SignUpActivity import com.teampophory.pophory.feature.signup.SignUpButtonInterface import com.teampophory.pophory.feature.signup.SignUpFirstFragment import com.teampophory.pophory.feature.signup.SignUpSecondFragment @@ -12,12 +11,9 @@ import com.teampophory.pophory.feature.signup.SignUpThirdFragment class SignUpViewPagerAdapter( fragmentActivity: FragmentActivity, private val buttonState: SignUpButtonInterface -) : - FragmentStateAdapter(fragmentActivity) { +) : FragmentStateAdapter(fragmentActivity) { - override fun getItemCount(): Int { - return 3 - } + override fun getItemCount() = 3 override fun createFragment(position: Int): Fragment { return when (position) { @@ -37,7 +33,5 @@ class SignUpViewPagerAdapter( SignUpThirdFragment() } } - } - } \ No newline at end of file