Skip to content

Commit

Permalink
Merge pull request #7 from NOW-SOPT-ANDROID/feat/week2_xml
Browse files Browse the repository at this point in the history
feat/#5: week2 xml 과제 구현
  • Loading branch information
youjin09222 authored May 2, 2024
2 parents f183c72 + 70c7e82 commit 6c5fcd1
Show file tree
Hide file tree
Showing 23 changed files with 515 additions and 172 deletions.
11 changes: 10 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-parcelize'
}

android {
Expand Down Expand Up @@ -37,12 +38,20 @@ android {
}

dependencies {

implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// recyclerview
implementation 'androidx.recyclerview:recyclerview:1.3.2'

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"

implementation "androidx.fragment:fragment-ktx:1.6.1"
implementation "androidx.activity:activity-ktx:1.8.0"
}
7 changes: 4 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="false">
</activity>
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:exported="false"/>
<activity
android:name=".SignUpActivity"
android:exported="false"/>
Expand Down
86 changes: 50 additions & 36 deletions app/src/main/java/com/sopt/now/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,85 @@ package com.sopt.now
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.sopt.now.databinding.ActivityLoginBinding
import com.sopt.now.test.data.UserData
import com.sopt.now.test.data.UserPreference

class LoginActivity : AppCompatActivity() {

private lateinit var binding: ActivityLoginBinding
var userId = ""
var userPw = ""
var userName = ""
var userMbti = ""
private lateinit var signUpLauncher: ActivityResultLauncher<Intent>
private lateinit var userPreference: UserPreference

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)

userPreference = UserPreference(this)
setSignUpLauncher()
setupLoginButton()
setupSignUpTextView()
}

// 로그인
private fun setupLoginButton() {
binding.btnLogin.setOnClickListener {
checkLogin()
}
}

// 회원가입 페이지로 이동
// 회원가입 페이지로 이동
private fun setupSignUpTextView() {
binding.tvSignUp.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
signUpLauncher.launch(intent) // ActivityResultLauncher 실행
Intent(this, SignUpActivity::class.java).also {
signUpLauncher.launch(it) // ActivityResultLauncher 실행
}
}
}

// ActivityResultLauncher 초기화
private val signUpLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
handleSignUpResult(result)
}
// 로그인
private fun checkLogin() {
val inputId = binding.etLoginId.text.toString()
val inputPw = binding.etLoginPw.text.toString()
val userData = userPreference.getUserData()

// 회원가입 결과 처리
private fun handleSignUpResult(result: ActivityResult) {
when (result.resultCode) {
RESULT_OK -> {
userId = result.data!!.getStringExtra("userId").toString()
userPw = result.data!!.getStringExtra("userPw").toString()
userName = result.data!!.getStringExtra("userName").toString()
userMbti = result.data!!.getStringExtra("userMbti").toString()
// 아이디, 비번 확인
if (userData != null) {
if (inputId == userData.userId && inputPw == userData.userPw) {
showToast("로그인 성공!")
moveToMain()
} else {
showToast("아이디 또는 비밀번호가 잘못되었습니다.")
}
}
}

// 로그인
private fun checkLogin(){
var inputId = binding.etId.text.toString()
var inputPw = binding.etPw.text.toString()

if(inputId == userId && inputPw == userPw){
moveToMain(userId, userPw, userName, userMbti)
// 메인 페이지로 이동
private fun moveToMain() {
Intent(this, MainActivity::class.java).apply {
startActivity(this)
finish()
}
}

// 메인 페이지로 이동
private fun moveToMain(userId: String, userPw: String, userName: String, userMbti: String) {
val intent = Intent(this, MainActivity::class.java).apply {
putExtra("userId", userId)
putExtra("userPw", userPw)
putExtra("userName", userName)
putExtra("userMbti", userMbti)
// 사용자 데이터 저장
private fun setSignUpLauncher(){
signUpLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
result.data?.getParcelableExtra<UserData>("userData")?.let {
userPreference.saveUserData(it)
}
}
}
startActivity(intent)
Toast.makeText(this, "로그인 성공!", Toast.LENGTH_SHORT).show()
}

private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
8 changes: 3 additions & 5 deletions app/src/main/java/com/sopt/now/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.sopt.now.databinding.ActivityMainBinding
import com.sopt.now.test.HomeFragment
import com.sopt.now.test.MyPageFragment
import com.sopt.now.test.SearchFragment

class MainActivity : AppCompatActivity() {
Expand All @@ -25,8 +26,6 @@ class MainActivity : AppCompatActivity() {
clickBottomNavigation()
}



private fun clickBottomNavigation() {
binding.bnvHome.setOnItemSelectedListener{
when (it.itemId) {
Expand All @@ -41,10 +40,9 @@ class MainActivity : AppCompatActivity() {
}

R.id.menu_mypage-> {
replaceFragment(SearchFragment())
replaceFragment(MyPageFragment())
true
}

else -> false
}
}
Expand All @@ -55,4 +53,4 @@ class MainActivity : AppCompatActivity() {
.replace(R.id.fcv_home, fragment)
.commit()
}
}
}
59 changes: 37 additions & 22 deletions app/src/main/java/com/sopt/now/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.sopt.now.databinding.ActivitySignUpBinding
import com.sopt.now.test.data.UserData

class SignUpActivity : AppCompatActivity() {

Expand All @@ -15,46 +16,60 @@ class SignUpActivity : AppCompatActivity() {
binding = ActivitySignUpBinding.inflate(layoutInflater)
setContentView(binding.root)

// 회원 가입
setupSignUpButton()
}

// 회원 가입
private fun setupSignUpButton() {
binding.btnSignUp.setOnClickListener {
val userId = binding.etId.text.toString()
val userPw = binding.etPw.text.toString()
val userName = binding.etName.text.toString()
val userMbti = binding.etMbti.text.toString()
with(binding) {
val userId = etSignUpId.text.toString()
val userPw = etSignUpPw.text.toString()
val userName = etSignUpName.text.toString()
val selfDescription = etSignUpDescription.text.toString()

checkSignUp(userId, userPw, userName, userMbti)
val userData = UserData(userId, userPw, userName, selfDescription)
checkSignUp(userData)
}
}
}

// 회원 가입 가능 여부 체크
private fun checkSignUp(userId: String, userPw: String, userName: String, userMbti: String) {
val isValidId = userId.length in 6..10
val isValidPw = userPw.length in 8..12
val isValidName = userName.trim().isEmpty() // 공백으로만 이루어진 경우 판단
val isEmpty = userId.isEmpty() || userId.isEmpty() || userId.isEmpty() || userMbti.isEmpty()
private fun checkSignUp(userData: UserData) {
val isValidId = userData.userId.length in MIN_ID_LENGTH..MAX_ID_LENGTH
val isValidPw = userData.userPw.length in MIN_PW_LENGTH..MAX_PW_LENGTH
val isValidName = userData.userName.isBlank() // 공백으로만 이루어진 경우 판단

val message = when {
!isValidId -> "ID는 6~10 글자여야 합니다."
!isValidPw -> "Password는 8~12 글자여야 합니다."
isValidName -> "공백으로만 이루어진 닉네임은 불가합니다."
isEmpty -> "모든 정보를 입력해주세요."
else -> {
moveToLogin(userId, userPw, userName, userMbti)
moveToLogin(userData)
"회원가입 성공!"
}
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
showToast(message)
}

// 로그인 페이지로 이동
private fun moveToLogin(userId: String, userPw: String, userName: String, userMbti: String) {
val intent = Intent(this, LoginActivity::class.java).apply {
putExtra("userId", userId)
putExtra("userPw", userPw)
putExtra("userName", userName)
putExtra("userMbti", userMbti)
private fun moveToLogin(userData: UserData) {
Intent(this, LoginActivity::class.java).apply {
putExtra("userData", userData)
setResult(RESULT_OK, this)
finish()
}
setResult(RESULT_OK, intent)
finish()
}

private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

// 글자 수 제한
companion object {
private const val MIN_ID_LENGTH = 6
private const val MAX_ID_LENGTH = 10
private const val MIN_PW_LENGTH = 8
private const val MAX_PW_LENGTH = 12
}
}
56 changes: 33 additions & 23 deletions app/src/main/java/com/sopt/now/test/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.sopt.now.R
import com.sopt.now.databinding.FragmentHomeBinding
import com.sopt.now.test.friend.Friend
import com.sopt.now.test.data.Friend
import com.sopt.now.test.data.HomeViewModel
import com.sopt.now.test.data.UserPreference
import com.sopt.now.test.friend.FriendAdapter

class HomeFragment: Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding: FragmentHomeBinding
get() = requireNotNull(_binding) { "바인딩 객체 좀 생성해주세요 제발!!" }

private val viewModel by viewModels<HomeViewModel>()
private lateinit var userPreference: UserPreference

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -27,34 +33,38 @@ class HomeFragment: Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val friendAdapter = FriendAdapter()

userPreference = UserPreference(requireContext())

setFriendList()
setRecyclerView()
}

// 리스트에 사용자 정보 추가
private fun setFriendList(){
val userData = userPreference.getUserData()

userData?.let {
val newFriend = Friend(
profileImage = R.drawable.iv_user_profile,
name = it.userName,
selfDescription = it.selfDescription
)
viewModel.mockFriendList.add(0, newFriend)
}
}

// FriendAdapter 연결
private fun setRecyclerView(){
val friendAdapter = FriendAdapter(viewModel.mockFriendList)
binding.rvFriends.run {
adapter = friendAdapter
layoutManager = LinearLayoutManager(requireContext())
setAdapter(friendAdapter)
}
friendAdapter.setFriendList(mockFriendList)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private val mockFriendList = listOf<Friend>(
Friend(
profileImage = R.drawable.img_profile,
name = "이의경",
selfDescription = "ㅎㅎ 아직 반도 안왔어 ^&^",
),
Friend(
profileImage = R.drawable.img_profile,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
),
Friend(
profileImage = R.drawable.img_profile,
name = "배지현",
selfDescription = "표정 풀자 ^^",
),
)
}
}
Loading

0 comments on commit 6c5fcd1

Please sign in to comment.