Skip to content

Commit

Permalink
[MERGE/#46] merge develop into ocr-feat-text-recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Jan 12, 2023
2 parents 2a7b240 + 382c6ec commit 450ff69
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.keyneez.presentation.login

import android.os.Bundle
import androidx.activity.viewModels
import com.keyneez.util.binding.BindingActivity
import com.lab.keyneez.R
import com.lab.keyneez.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_login) {
val viewModel by viewModels<LoginViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -19,6 +22,11 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo
binding.vpLogin.isUserInputEnabled = false // disable swipe
}

/** ์ „ํ™”๋ฒˆํ˜ธ ์ €์žฅ */
fun setPhoneNumber(number: String) {
viewModel.setPhoneNumber(number)
}

/** ์ด์ „ ํŽ˜์ด์ง€์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ๋กœ ViewPager ์ „ํ™˜ */
fun intentToPreviousPage() {
binding.vpLogin.currentItem--
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/keyneez/presentation/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.keyneez.presentation.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor() : ViewModel() {
private val _phoneNumber = MutableLiveData("")
val phoneNumber: LiveData<String>
get() = _phoneNumber

/** ์ „ํ™”๋ฒˆํ˜ธ ์ €์žฅ */
fun setPhoneNumber(number: String) {
_phoneNumber.value = number
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PhoneFragment : BindingFragment<FragmentPhoneBinding>(R.layout.fragment_ph

private fun initNextBtnClickListener() {
binding.btnPhoneNext.setOnSingleClickListener {
(activity as LoginActivity).setPhoneNumber(viewModel.phoneNumber.value.toString())
(activity as LoginActivity).intentToNextPage()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import com.keyneez.presentation.login.LoginActivity
import com.keyneez.presentation.login.pin.LoginPinViewModel.Companion.INVALID_PWD_CODE
import com.keyneez.presentation.login.pin.LoginPinViewModel.Companion.UNAUTHORIZED_USER_CODE
import com.keyneez.presentation.main.MainActivity
import com.keyneez.util.UiState
import com.keyneez.util.binding.BindingFragment
import com.keyneez.util.extension.hideKeyboard
import com.keyneez.util.extension.setOnSingleClickListener
import com.keyneez.util.extension.showSnackbar
import com.lab.keyneez.R
import com.lab.keyneez.databinding.FragmentLoginPinBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginPinFragment : BindingFragment<FragmentLoginPinBinding>(R.layout.fragment_login_pin) {
private val viewModel by viewModels<LoginPinViewModel>()

Expand All @@ -23,6 +29,7 @@ class LoginPinFragment : BindingFragment<FragmentLoginPinBinding>(R.layout.fragm
initHideKeyboard()
initBackBtnClickListener()
setupPasswordText()
setupPostUserLoginState()
}

private fun initHideKeyboard() {
Expand All @@ -31,17 +38,51 @@ class LoginPinFragment : BindingFragment<FragmentLoginPinBinding>(R.layout.fragm

private fun initBackBtnClickListener() {
binding.btnLoginPinBack.setOnSingleClickListener {
viewModel.resetPassword()
(activity as LoginActivity).intentToPreviousPage()
}
}

private fun setupPasswordText() {
viewModel.passwordText.observe(viewLifecycleOwner) { pwd ->
if (pwd.length >= 6) {
val toMain = Intent(activity, MainActivity::class.java)
requireActivity().setResult(AppCompatActivity.RESULT_OK, toMain)
startActivity(toMain)
requireActivity().finish()
val phone = (activity as LoginActivity).viewModel.phoneNumber.value.toString()
viewModel.postUserLogin(phone, pwd)
}
}
}

private fun setupPostUserLoginState() {
viewModel.stateMessage.observe(viewLifecycleOwner) {
when (it) {
is UiState.Success -> {
viewModel.resetPassword()
val toMain = Intent(activity, MainActivity::class.java)
requireActivity().setResult(AppCompatActivity.RESULT_OK, toMain)
startActivity(toMain)
requireActivity().finish()
}
is UiState.Failure -> {
when (it.code) {
UNAUTHORIZED_USER_CODE -> requireContext().showSnackbar(
binding.root,
getString(
R.string.login_pin_unauthorized_msg
)
)
INVALID_PWD_CODE -> requireContext().showSnackbar(
binding.root,
getString(R.string.signup_pin_confirm_invalid_pwd_msg)
)
else -> requireContext().showSnackbar(
binding.root,
getString(R.string.msg_error)
)
}
}
is UiState.Error -> {
requireContext().showSnackbar(binding.root, getString(R.string.msg_error))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ package com.keyneez.presentation.login.pin
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.keyneez.data.model.request.RequestPostUserLogInDto
import com.keyneez.data.repository.UserRepository
import com.keyneez.data.source.LocalPrefDataSource
import com.keyneez.util.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import retrofit2.HttpException
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class LoginPinViewModel @Inject constructor() : ViewModel() {
class LoginPinViewModel @Inject constructor(
private val userRepository: UserRepository,
private val localPrefDataSource: LocalPrefDataSource
) : ViewModel() {
private val _stateMessage = MutableLiveData<UiState>()
val stateMessage: LiveData<UiState>
get() = _stateMessage

private val _passwordText = MutableLiveData("")
val passwordText: LiveData<String>
get() = _passwordText
Expand All @@ -20,6 +35,41 @@ class LoginPinViewModel @Inject constructor() : ViewModel() {
rearrangeKeypad()
}

/** ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ ์š”์ฒญ */
fun postUserLogin(phone: String, pwd: String) {
viewModelScope.launch {
userRepository.postUserLogIn(RequestPostUserLogInDto(phone, pwd))
.onSuccess { response ->
Timber.tag(successTag).d("response : $response")

if (response.data == null) {
_stateMessage.value = UiState.Failure(RESPONSE_NULL_CODE)
return@onSuccess
}

localPrefDataSource.setAccessToken(response.data.accessToken)
_stateMessage.value = UiState.Success
}
.onFailure {
Timber.tag(failTag).e("throwable: $it")
if (it is HttpException) {
Timber.tag(failTag).e("code : ${it.code()}")
Timber.tag(failTag).e("message : ${it.message()}")

when (it.code()) {
INVALID_PWD_CODE ->
_stateMessage.value =
UiState.Failure(INVALID_PWD_CODE)
UNAUTHORIZED_USER_CODE -> _stateMessage.value = UiState.Failure(
UNAUTHORIZED_USER_CODE
)
else -> _stateMessage.value = UiState.Error
}
} else _stateMessage.value = UiState.Error
}
}
}

/** ์ˆซ์ž ์žฌ๋ฐฐ์—ด */
fun rearrangeKeypad() {
val numbers = linkedSetOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Expand Down Expand Up @@ -51,4 +101,13 @@ class LoginPinViewModel @Inject constructor() : ViewModel() {
rearrangeKeypad()
_passwordText.value = ""
}

companion object {
const val RESPONSE_NULL_CODE = 100
const val UNAUTHORIZED_USER_CODE = 401
const val INVALID_PWD_CODE = 404

private const val successTag = "POST_LOGIN_SUCCESS"
private const val failTag = "POST_LOGIN_FAIL"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.keyneez.presentation.signup.danal.guide.DanalGuideViewModel.Companion
import com.keyneez.presentation.signup.danal.guide.DanalGuideViewModel.Companion.USER_DATA_NULL_CODE
import com.keyneez.util.UiState
import com.keyneez.util.binding.BindingFragment
import com.keyneez.util.extension.hideKeyboard
import com.keyneez.util.extension.setOnSingleClickListener
import com.keyneez.util.extension.showSnackbar
import com.lab.keyneez.R
Expand All @@ -23,10 +24,17 @@ class DanalGuideFragment :
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel

initHideKeyboard()
initBackBtnClickListener()
setupPostDanalSignup()
}

private fun initHideKeyboard() {
binding.layoutDanalGuide.setOnSingleClickListener {
requireActivity().hideKeyboard(requireView())
}
}

private fun initBackBtnClickListener() {
binding.btnDanalGuideBack.setOnSingleClickListener { requireActivity().finish() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SignupPinFragment : BindingFragment<FragmentSignupPinBinding>(R.layout.fra
private fun initBackBtnClickListener() {
binding.btnSignupPinBack.setOnSingleClickListener {
viewModel.rearrangeKeypad()
viewModel.resetPassword()
(activity as SignupActivity).intentToPreviousPage()
}
}
Expand All @@ -36,15 +37,17 @@ class SignupPinFragment : BindingFragment<FragmentSignupPinBinding>(R.layout.fra
viewModel.passwordText.observe(viewLifecycleOwner) { pwd ->
if (pwd.length == 6) {
viewModel.patchPwdSignup()
viewModel.resetPassword()
}
}
}

private fun setupPatchPwdSignup() {
viewModel.stateMessage.observe(viewLifecycleOwner) {
when (it) {
is UiState.Success -> (activity as SignupActivity).intentToNextPage()
is UiState.Success -> {
viewModel.resetPassword()
(activity as SignupActivity).intentToNextPage()
}
is UiState.Failure -> requireContext().showSnackbar(
binding.root,
getString(R.string.msg_error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SignupPinConfirmFragment :

private fun initBackBtnClickListener() {
binding.btnSignupPinConfirmBack.setOnSingleClickListener {
viewModel.rearrangeKeypad()
viewModel.resetPassword()
(activity as SignupActivity).intentToPreviousPage()
}
Expand All @@ -48,6 +49,7 @@ class SignupPinConfirmFragment :
viewModel.stateMessage.observe(viewLifecycleOwner) {
when (it) {
is UiState.Success -> {
viewModel.resetPassword()
val toMain = Intent(activity, MainActivity::class.java)
requireActivity().setResult(AppCompatActivity.RESULT_OK, toMain)
startActivity(toMain)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// login pin
<string name="login_pin_description">๊ฐ„ํŽธ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ\n์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.</string>
<string name="login_pin_rearrange">์žฌ๋ฐฐ์—ด</string>
<string name="login_pin_unauthorized_msg">์กด์žฌํ•˜์ง€ ์•Š๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค.</string>

// signup (common)
<string name="signup_next">๋‹ค์Œ์œผ๋กœ</string>
Expand Down

0 comments on commit 450ff69

Please sign in to comment.