From 2fab64fea45c0bd08fedfefeaaaaeb2a40ffa117 Mon Sep 17 00:00:00 2001 From: winter223 Date: Mon, 20 Sep 2021 10:53:42 +0900 Subject: [PATCH] fix: Prevent duplicated request on sign up --- .../user/signin/fragment/ConditionFragment.kt | 39 +++++++++++++------ .../signin/viewmodel/ConditionViewModel.kt | 10 +++++ .../main/res/layout/fragment_condition.xml | 5 +++ .../data/controller/FakeUserController.kt | 16 ++++++-- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/sopt/santamanitto/user/signin/fragment/ConditionFragment.kt b/app/src/main/java/org/sopt/santamanitto/user/signin/fragment/ConditionFragment.kt index 84d6c10c..82b4c3d8 100644 --- a/app/src/main/java/org/sopt/santamanitto/user/signin/fragment/ConditionFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/user/signin/fragment/ConditionFragment.kt @@ -16,9 +16,11 @@ import org.sopt.santamanitto.main.MainActivity import org.sopt.santamanitto.R import org.sopt.santamanitto.databinding.FragmentConditionBinding import org.sopt.santamanitto.user.signin.viewmodel.ConditionViewModel +import androidx.activity.OnBackPressedCallback + @AndroidEntryPoint -class ConditionFragment: Fragment() { +class ConditionFragment : Fragment() { private lateinit var binding: FragmentConditionBinding @@ -26,18 +28,31 @@ class ConditionFragment: Fragment() { private val viewModel: ConditionViewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + // 요청 대기 중에 뒤로 가기 버튼을 눌러 화면을 벗어나는 것을 방지 + requireActivity().onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + + override fun handleOnBackPressed() { + if (!viewModel.isWaitingForResponse) { + findNavController().navigateUp() + } + } + }) + } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { binding = FragmentConditionBinding.inflate(inflater, container, false).apply { viewModel = this@ConditionFragment.viewModel lifecycleOwner = this@ConditionFragment } + return binding.root + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + binding.userName = args.userName initView() - subscribeUi() - - return binding.root } private fun subscribeUi() { @@ -51,7 +66,7 @@ class ConditionFragment: Fragment() { } viewModel.userSaveFail.observe(viewLifecycleOwner) { - if(it) { + if (it) { //Todo: 계정 생성에 실패했다는 다이얼로그 띄우기 Log.e("ConditionFragment", "Fail to create new account") } @@ -70,19 +85,21 @@ class ConditionFragment: Fragment() { } binding.santacheckboxCondition1.setOnClickListener { + if (viewModel.isWaitingForResponse) { + return@setOnClickListener + } val directions = ConditionFragmentDirections - .actionConditionFragmentToWebViewFragment(BuildConfig.TOS_URL) + .actionConditionFragmentToWebViewFragment(BuildConfig.TOS_URL) findNavController().navigate(directions) } binding.santacheckboxCondition2.setOnClickListener { + if (viewModel.isWaitingForResponse) { + return@setOnClickListener + } val directions = ConditionFragmentDirections - .actionConditionFragmentToWebViewFragment(BuildConfig.PRIVACY_POLICY_RUL) + .actionConditionFragmentToWebViewFragment(BuildConfig.PRIVACY_POLICY_RUL) findNavController().navigate(directions) } - - binding.santabottombuttonCondition.setOnClickListener { - viewModel.signIn(args.userName) - } } } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/user/signin/viewmodel/ConditionViewModel.kt b/app/src/main/java/org/sopt/santamanitto/user/signin/viewmodel/ConditionViewModel.kt index ed1b87ef..2b9efa1a 100644 --- a/app/src/main/java/org/sopt/santamanitto/user/signin/viewmodel/ConditionViewModel.kt +++ b/app/src/main/java/org/sopt/santamanitto/user/signin/viewmodel/ConditionViewModel.kt @@ -20,7 +20,15 @@ class ConditionViewModel @ViewModelInject constructor( val userSaveFail = MutableLiveData(false) + private var _isWaitingForResponse = false + val isWaitingForResponse: Boolean + get() = _isWaitingForResponse + fun signIn(userName: String) { + if (_isWaitingForResponse) { + return + } + _isWaitingForResponse = true userController.createAccount(userName, serialNumber, object : UserController.CreateAccountCallback { override fun onCreateAccountSuccess(loginUserResponse: LoginUserResponse) { @@ -32,10 +40,12 @@ class ConditionViewModel @ViewModelInject constructor( } } userSaveSuccess.value = true + _isWaitingForResponse = false } override fun onCreateAccountFailed() { userSaveFail.value = true + _isWaitingForResponse = false } }) } diff --git a/app/src/main/res/layout/fragment_condition.xml b/app/src/main/res/layout/fragment_condition.xml index b633bcad..f1f32cfb 100644 --- a/app/src/main/res/layout/fragment_condition.xml +++ b/app/src/main/res/layout/fragment_condition.xml @@ -5,6 +5,10 @@ + + @@ -133,6 +137,7 @@ android:layout_marginBottom="@dimen/margin_signin_button_bottom" android:enabled="@{viewModel.isReady()}" android:text="@string/button_next" + android:onClick="@{() -> viewModel.signIn(userName)}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> diff --git a/app/src/mock/java/org/sopt/santamanitto/user/data/controller/FakeUserController.kt b/app/src/mock/java/org/sopt/santamanitto/user/data/controller/FakeUserController.kt index 40879ae0..c5a27b26 100644 --- a/app/src/mock/java/org/sopt/santamanitto/user/data/controller/FakeUserController.kt +++ b/app/src/mock/java/org/sopt/santamanitto/user/data/controller/FakeUserController.kt @@ -1,8 +1,10 @@ package org.sopt.santamanitto.user.data.controller +import android.util.Log +import kotlinx.coroutines.* import org.sopt.santamanitto.user.data.LoginUserResponse -class FakeUserController: UserController { +class FakeUserController : UserController { private val fakeLoginUser = LoginUserResponse( "fakeUser", @@ -12,7 +14,8 @@ class FakeUserController: UserController { ) override fun login(serialNumber: String, callback: UserController.LoginCallback) { - callback.onLoginSuccess(fakeLoginUser) +// callback.onLoginSuccess(fakeLoginUser) + callback.onLoginFailed(false) } override fun createAccount( @@ -20,6 +23,13 @@ class FakeUserController: UserController { serialNumber: String, callback: UserController.CreateAccountCallback ) { - callback.onCreateAccountSuccess(fakeLoginUser) + GlobalScope.launch { + Log.d(javaClass.simpleName, "createAccount(): request ...") + delay(2000) + Log.d(javaClass.simpleName, "createAccount(): response!") + withContext(Dispatchers.Main) { + callback.onCreateAccountSuccess(fakeLoginUser) + } + } } } \ No newline at end of file