Skip to content

Commit

Permalink
#3 몰라유 이것저것 했어유
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Oct 6, 2022
1 parent 90b72a6 commit aca9512
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 71 deletions.
3 changes: 3 additions & 0 deletions 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-kapt'
}

android {
Expand Down Expand Up @@ -48,4 +49,6 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.activity:activity-ktx:1.1.0'
implementation 'androidx.fragment:fragment-ktx:1.2.5'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.preference:preference-ktx:1.1.1'
}
6 changes: 3 additions & 3 deletions app/src/main/java/org/sopt/sample/entity/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.sopt.sample.entity
import java.io.Serializable

data class User(
val id: String? = "",
val pwd: String? = "",
val mbti: String? = ""
var id: String? = "",
var pwd: String? = "",
var mbti: String? = ""
) : Serializable
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package org.sopt.sample.presentation.home

import android.os.Bundle
import androidx.activity.viewModels
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivityHomeBinding
import org.sopt.sample.entity.User
import org.sopt.sample.util.binding.BaseActivity
import org.sopt.sample.util.base.BaseActivity

class HomeActivity : BaseActivity<ActivityHomeBinding>(R.layout.activity_home) {
private val homeViewModel: HomeViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.vm = homeViewModel
getUserInfo()
}

private fun getUserInfo() {
val userInfo = intent.getSerializableExtra("userInfo") as User
binding.tvHomeName.text = getString(R.string.home_name_is, userInfo.id)
binding.tvHomeMbti.text = getString(R.string.home_mbti_is, userInfo.mbti)
homeViewModel.id.value = getString(R.string.home_id_is, userInfo.id)
homeViewModel.mbti.value = getString(R.string.home_mbti_is, userInfo.mbti)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sample.presentation.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class HomeViewModel : ViewModel() {
val id = MutableLiveData<String>()
val mbti = MutableLiveData<String>()
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package org.sopt.sample.presentation.login

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.google.android.material.snackbar.Snackbar
import androidx.preference.PreferenceManager
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
import org.sopt.sample.util.base.BaseActivity
import org.sopt.sample.util.extensions.showSnackbar
import org.sopt.sample.util.extensions.showToast

class SignInActivity : BaseActivity<ActivitySignInBinding>(R.layout.activity_sign_in) {
private val signInViewModel: SignInViewModel by viewModels()
private lateinit var sharedPreferences: SharedPreferences
private lateinit var editor: SharedPreferences.Editor
private val resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
Snackbar.make(binding.root, "회원가입이 완료되었습니다.", Snackbar.LENGTH_SHORT).show()
binding.root.showSnackbar(getString(R.string.complete_sign_up))
val userInfo = result.data?.getSerializableExtra("userInfo") as User
signInViewModel.setUserInfo(userInfo)
}
Expand All @@ -26,28 +30,64 @@ class SignInActivity : BaseActivity<ActivitySignInBinding>(R.layout.activity_sig
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.vm = signInViewModel
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
editor = sharedPreferences.edit()
checkSharedPreferences()
observeSucessLogin()
initSignUpBtnOnClickListener()
}

private fun checkSharedPreferences() {
signInViewModel.isAutoLogin.value =
sharedPreferences.getBoolean(getString(R.string.auto_login), false)
signInViewModel.id.value =
sharedPreferences.getString(getString(R.string.sign_in_id_hint), "")
signInViewModel.pwd.value =
sharedPreferences.getString(getString(R.string.sign_in_pwd_hint), "")
}

private fun initSignUpBtnOnClickListener() {
binding.btnSignUp.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
resultLauncher.launch(intent)
binding.etSignInId.text = null
binding.etSignInPwd.text = null
val toSignUp = Intent(this, SignUpActivity::class.java)
resultLauncher.launch(toSignUp)
signInViewModel.id.value = null
signInViewModel.pwd.value = null
signInViewModel.isAutoLogin.value = false
}
}

private fun observeSucessLogin() {
signInViewModel.successLogin.observe(this) { success ->
if (success) {
showToast("로그인에 성공하셨습니다.")
val intent = Intent(this, HomeActivity::class.java)
intent.putExtra("userInfo", signInViewModel.userInfo.value)
startActivity(intent)
observeAutoLogin()
showToast(getString(R.string.success_login))
val toHome = Intent(this, HomeActivity::class.java)
toHome.putExtra("userInfo", signInViewModel.userInfo.value)
startActivity(toHome)
finish()
} else showToast("다시 입력하세요.")
} else showToast(getString(R.string.fail_login))
}
}

private fun observeAutoLogin() {
signInViewModel.isAutoLogin.observe(this) { checked ->
if (checked) {
editor.putBoolean(getString(R.string.auto_login), true)
editor.apply()
editor.putString(getString(R.string.sign_in_id_hint), signInViewModel.id.value)
editor.putString(
getString(R.string.sign_in_pwd_hint),
signInViewModel.pwd.value
)
editor.commit()
} else {
editor.putBoolean(getString(R.string.auto_login), false)
signInViewModel.id.value =
sharedPreferences.getString(getString(R.string.sign_in_id_hint), "")
signInViewModel.pwd.value =
sharedPreferences.getString(getString(R.string.sign_in_pwd_hint), "")
editor.commit()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.sopt.sample.presentation.login

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

Expand All @@ -13,6 +10,7 @@ class SignInViewModel : ViewModel() {

val id = MutableLiveData("")
val pwd = MutableLiveData("")
val isAutoLogin = MutableLiveData<Boolean>()

private val _successLogin = MutableLiveData<Boolean>()
val successLogin: LiveData<Boolean> = _successLogin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package org.sopt.sample.presentation.login
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.google.android.material.snackbar.Snackbar
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivitySignUpBinding
import org.sopt.sample.entity.User
import org.sopt.sample.util.binding.BaseActivity
import org.sopt.sample.util.base.BaseActivity
import org.sopt.sample.util.extensions.showSnackbar

class SignUpActivity : BaseActivity<ActivitySignUpBinding>(R.layout.activity_sign_up) {
private val signUpViewModel: SignUpViewModel by viewModels()
Expand All @@ -16,25 +16,32 @@ class SignUpActivity : BaseActivity<ActivitySignUpBinding>(R.layout.activity_sig
super.onCreate(savedInstanceState)
binding.vm = signUpViewModel
observeSuccessSignUp()
initBackBtnOnClickListener()
}

private fun observeSuccessSignUp() {
signUpViewModel.successSignUp.observe(this) { success ->
if (success) {
putUserInfo()
finish()
} else Snackbar.make(binding.root, "입력한 정보를 확인해주세요.", Snackbar.LENGTH_SHORT).show()
} else binding.root.showSnackbar(getString(R.string.check_your_input))
}
}

private fun putUserInfo() {
val intent = Intent(this, SignInActivity::class.java)
val toSignIn = Intent(this, SignInActivity::class.java)
val userInfo = User(
id = signUpViewModel.id.value,
pwd = signUpViewModel.pwd.value,
mbti = signUpViewModel.mbti.value
)
intent.putExtra("userInfo", userInfo)
setResult(RESULT_OK, intent)
toSignIn.putExtra("userInfo", userInfo)
setResult(RESULT_OK, toSignIn)
}

private fun initBackBtnOnClickListener() {
binding.ivSignUpBack.setOnClickListener {
finish()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.sample.util.binding
package org.sopt.sample.util.base

import android.os.Bundle
import androidx.annotation.LayoutRes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.sample.util.binding
package org.sopt.sample.util.base

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.sample.util
package org.sopt.sample.util.extensions

import android.content.Context
import android.widget.Toast
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/sopt/sample/util/extensions/ViewExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.sample.util.extensions

import android.view.View
import com.google.android.material.snackbar.Snackbar

fun View.showSnackbar(message: String) {
Snackbar.make(this, message, Snackbar.LENGTH_SHORT).show()
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M7.579,12 L14,5.579 15.421,7 9,13.421 7.579,12zM14,18.421 L7.579,12 9,10.579 15.421,17 14,18.421z"
android:fillColor="#C8C8C8"/>
</vector>
97 changes: 67 additions & 30 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,80 @@

<data>

<variable
name="vm"
type="org.sopt.sample.presentation.home.HomeViewModel" />

</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.home.HomeActivity">

<ImageView
android:id="@+id/iv_home_profile"
android:layout_width="100dp"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_title"
android:layout_width="match_parent"
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" />
android:background="@color/black"
android:paddingVertical="50dp"
app:layout_constraintTop_toTopOf="parent">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_home_profile"
android:layout_width="120dp"
android:layout_height="0dp"
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="20dp"
android:text="@={vm.id}"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_home_profile"
tools:text="ID : 2zerozu" />

<TextView
android:id="@+id/tv_home_mbti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@={vm.mbti}"
android:textColor="@color/white"
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>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_body"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/cl_title">

<Button
android:id="@+id/btn_sign_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:layout_marginTop="20dp"
android:text="@string/logout"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>


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

0 comments on commit aca9512

Please sign in to comment.