Skip to content

Commit

Permalink
[FEAT/#1] 회원가입 및 로그인 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Apr 3, 2023
1 parent fef6255 commit 062f409
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/org/android/go/sopt/data/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import org.android.go.sopt.util.type.MBTI
@Parcelize
data class User(
val id: String,
val password: String,
val name: String,
val specialty: String,
val mbti: MBTI
val pwd: String,
val name: String?,
val specialty: String?,
val mbti: MBTI?
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package org.android.go.sopt.presentation.login

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import org.android.go.sopt.R
import org.android.go.sopt.data.User
import org.android.go.sopt.databinding.ActivityLoginBinding
import org.android.go.sopt.presentation.main.MainActivity
import org.android.go.sopt.presentation.signup.SignupActivity
import org.android.go.sopt.util.binding.BindingActivity
import org.android.go.sopt.util.extension.hideKeyboard
import org.android.go.sopt.util.extension.setOnSingleClickListener
import org.android.go.sopt.util.extension.showSnackbar
import org.android.go.sopt.util.extension.* // ktlint-disable no-wildcard-imports
import timber.log.Timber

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

private lateinit var signupResultLauncher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.vm = viewModel
Expand All @@ -31,13 +37,30 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo

private fun initLoginBtnClickListener() {
binding.btnLoginLogin.setOnSingleClickListener {
showSnackbar(binding.root, "login button clicked")
if (viewModel.isValidInput()) {
showToast(getString(R.string.login_login_success_msg))
startActivity(Intent(this, MainActivity::class.java))
finish()
return@setOnSingleClickListener
}
showSnackbar(binding.root, getString(R.string.wrong_input_msg))
}
}

private fun initSignupBtnClickListener() {
signupResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val resultData = result.data ?: return@registerForActivityResult
resultData.getCompatibleParcelableExtra<User>("user")?.let { userData ->
viewModel.setSavedUser(userData)
showSnackbar(binding.root, getString(R.string.login_signup_success_msg))
}
}
}

binding.btnLoginSignup.setOnSingleClickListener {
startActivity(Intent(this, SignupActivity::class.java))
signupResultLauncher.launch(Intent(this, SignupActivity::class.java))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ package org.android.go.sopt.presentation.login
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.android.go.sopt.data.User
import javax.inject.Inject

@HiltViewModel
class LoginViewModel : ViewModel() {
class LoginViewModel @Inject constructor() : ViewModel() {
private lateinit var savedUser: User

val id = MutableLiveData("")
val password = MutableLiveData("")
val pwd = MutableLiveData("")

fun setSavedUser(savedUser: User) {
this.savedUser = savedUser
}

fun isValidInput() = !id.value.isNullOrBlank() && id.value == savedUser.id && !pwd.value.isNullOrBlank() && pwd.value == savedUser.pwd
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package org.android.go.sopt.presentation.signup

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import org.android.go.sopt.R
import org.android.go.sopt.data.User
import org.android.go.sopt.databinding.ActivitySignupBinding
import org.android.go.sopt.presentation.login.LoginActivity
import org.android.go.sopt.util.binding.BindingActivity
import org.android.go.sopt.util.extension.hideKeyboard
import org.android.go.sopt.util.extension.setOnSingleClickListener
import org.android.go.sopt.util.extension.showSnackbar
import org.android.go.sopt.util.safeValueOf
import org.android.go.sopt.util.type.MBTI

@AndroidEntryPoint
class SignupActivity : BindingActivity<ActivitySignupBinding>(R.layout.activity_signup) {
Expand All @@ -18,9 +25,27 @@ class SignupActivity : BindingActivity<ActivitySignupBinding>(R.layout.activity_
binding.vm = viewModel

initLayout()
initSignupCompleteBtnClickListener()
}

private fun initLayout() {
binding.layoutSignup.setOnSingleClickListener { hideKeyboard() }
}

private fun initSignupCompleteBtnClickListener() {
binding.btnSignupSignupComplete.setOnSingleClickListener {
with(viewModel) {
if (isValidId(id.value) && isValidPwd(pwd.value)) {
Intent(this@SignupActivity, LoginActivity::class.java).apply {
val user = User(id.value!!, pwd.value!!, name.value, specialty.value, safeValueOf<MBTI>(mbti.value))
this.putExtra("user", user)
setResult(Activity.RESULT_OK, this)
}
finish()
return@setOnSingleClickListener
}
}
showSnackbar(binding.root, getString(R.string.wrong_input_msg))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package org.android.go.sopt.presentation.signup
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class SignupViewModel : ViewModel() {
class SignupViewModel @Inject constructor() : ViewModel() {
val id = MutableLiveData("")
val password = MutableLiveData("")
val pwd = MutableLiveData("")
val name = MutableLiveData("")
val specialty = MutableLiveData("")
val mbti = MutableLiveData("")

fun isValidId(id: String?) = !id.isNullOrBlank() && id.length in 6..10
fun isValidPwd(pwd: String?) = !pwd.isNullOrBlank() && pwd.length in 8..12
}
9 changes: 9 additions & 0 deletions app/src/main/java/org/android/go/sopt/util/EnumUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.android.go.sopt.util

inline fun <reified T : Enum<T>> safeValueOf(type: String?): T? {
return try {
java.lang.Enum.valueOf(T::class.java, type ?: return null)
} catch (e: IllegalArgumentException) {
null
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/org/android/go/sopt/util/UiState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.android.go.sopt.util

sealed class UiState {
object Success : UiState()
data class Failure(val code: Int?) : UiState()
object Error : UiState()
}
11 changes: 11 additions & 0 deletions app/src/main/java/org/android/go/sopt/util/extension/IntentExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.android.go.sopt.util.extension

import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import android.os.Parcelable

/** Retrieve extended data from the intent and support app compatibility */
inline fun <reified T : Parcelable> Intent.getCompatibleParcelableExtra(key: String): T? = when {
SDK_INT >= 33 -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T // ktlint-disable annotation
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
android:hint="@string/login_pwd_hint"
android:inputType="textPassword"
android:paddingVertical="12dp"
android:text="@={vm.password}"
android:text="@={vm.pwd}"
app:layout_constraintTop_toBottomOf="@id/tv_login_pwd_label" />

<Button
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_signup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
android:hint="@string/login_pwd_hint"
android:inputType="textPassword"
android:paddingVertical="12dp"
android:text="@={vm.password}"
android:text="@={vm.pwd}"
app:layout_constraintTop_toBottomOf="@id/tv_signup_pwd_label" />

<TextView
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<resources>
<string name="app_name">GO SOPT Android</string>
<!-- common -->
<string name="wrong_input_msg">잘못된 입력입니다.</string>

<!-- main -->
<string name="main_test">Hello World!</string>

Expand All @@ -11,6 +14,7 @@
<string name="login_pwd_hint">비밀번호를 입력하세요.</string>
<string name="login_login_btn">LOG IN</string>
<string name="login_signup_btn">SIGN UP</string>
<string name="login_signup_success_msg">회원가입이 완료되었습니다.</string>

<!-- signup -->
<string name="signup_mbti_label">MBTI</string>
Expand All @@ -20,4 +24,5 @@
<string name="signup_specialty_hint">특기를 입력하세요.</string>
<string name="signup_name_label">NAME</string>
<string name="signup_name_hint">이름을 입력하세요.</string>
<string name="login_login_success_msg">로그인에 성공했습니다.</string>
</resources>

0 comments on commit 062f409

Please sign in to comment.