Skip to content

Commit

Permalink
#3 중간커밋
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Oct 5, 2022
1 parent 15ace7b commit 94716e6
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 226 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ android {
}
buildFeatures {
viewBinding true
dataBinding true
}
}

Expand All @@ -41,6 +42,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-*:2.5.1'
implementation 'androidx.activity:activity-ktx:1.6.0'
implementation 'androidx.fragment:fragment-ktx:1.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/sopt/sample/entity/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sample.entity

import java.io.Serializable

data class User(
val id: String,
val pwd: String,
val mbti: String
) : Serializable
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package org.sopt.sample.presentation.home

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivityHomeBinding
import org.sopt.sample.util.binding.BaseActivity

class HomeActivity : AppCompatActivity() {
private lateinit var binding: ActivityHomeBinding

class HomeActivity : BaseActivity<ActivityHomeBinding>(R.layout.activity_home) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
getUserInfo()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,34 @@ package org.sopt.sample.presentation.login

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.activity.viewModels
import com.google.android.material.snackbar.Snackbar
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivitySignInBinding
import org.sopt.sample.entity.User
import org.sopt.sample.presentation.home.HomeActivity
import org.sopt.sample.util.binding.BaseActivity
import org.sopt.sample.util.showToast

class SignInActivity : AppCompatActivity() {
private lateinit var binding: ActivitySignInBinding
private var userId: String? = null
private var userPwd: String? = null
private var userMbti: String? = null
class SignInActivity : BaseActivity<ActivitySignInBinding>(R.layout.activity_sign_in) {
private val signInViewModel: SignInViewModel by viewModels()
private val resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
Snackbar.make(binding.root, "회원가입이 완료되었습니다.", Snackbar.LENGTH_SHORT).show()
userId = result.data?.getStringExtra("userId") ?: ""
userPwd = result.data?.getStringExtra("userPwd") ?: ""
userMbti = result.data?.getStringExtra("userMbti") ?: ""
val userInfo = result.data?.getSerializableExtra("userInfo") as User
signInViewModel.setUserInfo(userInfo)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignInBinding.inflate(layoutInflater)
setContentView(binding.root)
initLoginBtnOnClickListener()
binding.vm = signInViewModel
sucessLogin()
initSignUpBtnOnClickListener()
}

private fun checkLogin(): Boolean {
return binding.etSignInId.text.toString() == userId && binding.etSignInPwd.text.toString() == userPwd
}

private fun initSignUpBtnOnClickListener() {
binding.btnSignUp.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
Expand All @@ -45,21 +39,15 @@ class SignInActivity : AppCompatActivity() {
}
}

private fun initLoginBtnOnClickListener() {
binding.btnLogin.setOnClickListener {
if (checkLogin()) {
Toast.makeText(this, "로그인에 성공하셨습니다.", Toast.LENGTH_SHORT).show()
private fun sucessLogin() {
signInViewModel.successLogin.observe(this) { success ->
if (success) {
showToast(("로그인에 성공하셨습니다."))
val intent = Intent(this, HomeActivity::class.java)
putUserInfo(intent)
intent.putExtra("userInfo", signInViewModel.userInfo.value)
startActivity(intent)
finish()
} else Toast.makeText(this, "다시 입력하세요.", Toast.LENGTH_SHORT).show()
} else showToast("다시 입력하세요.")
}
}

private fun putUserInfo(intent: Intent) {
intent.putExtra("userId", userId)
intent.putExtra("userMbti", userMbti)
setResult(RESULT_OK, intent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sopt.sample.presentation.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.sopt.sample.entity.User

class SignInViewModel : ViewModel() {
private val _userInfo = MutableLiveData<User>()
val userInfo: LiveData<User> = _userInfo

private val _id = MutableLiveData<String>()
val id: LiveData<String> = _id

private val _pwd = MutableLiveData<String>()
val pwd: LiveData<String> = _pwd

private val _successLogin = MutableLiveData(false)
val successLogin: LiveData<Boolean> = _successLogin

fun loginOnClick() {
viewModelScope.launch {
_successLogin.value =
id.value == userInfo.value?.id && pwd.value == userInfo.value?.pwd
}
}

fun setUserInfo(user: User) {
_userInfo.value = user
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package org.sopt.sample.presentation.login

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivitySignUpBinding
import org.sopt.sample.util.binding.BaseActivity

class SignUpActivity : AppCompatActivity() {
private lateinit var binding: ActivitySignUpBinding
class SignUpActivity : BaseActivity<ActivitySignUpBinding>(R.layout.activity_sign_up) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignUpBinding.inflate(layoutInflater)
setContentView(binding.root)
initSignUpBtnOnClickListener()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sopt.sample.presentation.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

class SignUpViewModel : ViewModel() {
private val _id = MutableLiveData<String>()
val id: LiveData<String> = _id

private val _pwd = MutableLiveData<String>()
val pwd: LiveData<String> = _pwd

private val _mbti = MutableLiveData<String>()
val mbti: LiveData<String> = _mbti

private val _successSignUp = MutableLiveData<Boolean>()

fun signUpOnClick() {
viewModelScope.launch {
//if (id.value?.length in 6..10)
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/org/sopt/sample/util/ToastUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.sample.util

import android.content.Context
import android.widget.Toast

fun Context.showToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
19 changes: 19 additions & 0 deletions app/src/main/java/org/sopt/sample/util/binding/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sopt.sample.util.binding

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

abstract class BaseActivity<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int
) : AppCompatActivity() {
protected lateinit var binding: T

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, layoutRes)
binding.lifecycleOwner = this
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/org/sopt/sample/util/binding/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sopt.sample.util.binding

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import org.sopt.sample.R

abstract class BaseFragment<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int
) : Fragment() {
private var _binding: T? = null
val binding get() = _binding ?: error(getString(R.string.binding_error))

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
75 changes: 41 additions & 34 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.home.HomeActivity">
xmlns:tools="http://schemas.android.com/tools">

<ImageView
android:id="@+id/iv_home_profile"
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_marginTop="50dp"
android:src="@drawable/img_profile"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<data>

<TextView
android:id="@+id/tv_home_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_home_profile"
tools:text="이름 : 이영주" />
</data>

<TextView
android:id="@+id/tv_home_mbti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_home_name"
tools:text="MBTI : ISFP" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.home.HomeActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/iv_home_profile"
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_marginTop="50dp"
android:src="@drawable/img_profile"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_home_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_home_profile"
tools:text="이름 : 이영주" />

<TextView
android:id="@+id/tv_home_mbti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_home_name"
tools:text="MBTI : ISFP" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Loading

0 comments on commit 94716e6

Please sign in to comment.