Skip to content

Commit

Permalink
fix: Prevent duplicated request on sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-223 committed Sep 20, 2021
1 parent a9379c2 commit 2fab64f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ 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

private val args: ConditionFragmentArgs by navArgs()

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() {
Expand All @@ -51,7 +66,7 @@ class ConditionFragment: Fragment() {
}

viewModel.userSaveFail.observe(viewLifecycleOwner) {
if(it) {
if (it) {
//Todo: 계정 생성에 실패했다는 다이얼로그 띄우기
Log.e("ConditionFragment", "Fail to create new account")
}
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,10 +40,12 @@ class ConditionViewModel @ViewModelInject constructor(
}
}
userSaveSuccess.value = true
_isWaitingForResponse = false
}

override fun onCreateAccountFailed() {
userSaveFail.value = true
_isWaitingForResponse = false
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/fragment_condition.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

<data>

<variable
name="userName"
type="String" />

<variable
name="viewModel"
type="org.sopt.santamanitto.user.signin.viewmodel.ConditionViewModel" />
Expand Down Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -12,14 +14,22 @@ class FakeUserController: UserController {
)

override fun login(serialNumber: String, callback: UserController.LoginCallback) {
callback.onLoginSuccess(fakeLoginUser)
// callback.onLoginSuccess(fakeLoginUser)
callback.onLoginFailed(false)
}

override fun createAccount(
userName: String,
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)
}
}
}
}

0 comments on commit 2fab64f

Please sign in to comment.