Skip to content

Commit

Permalink
[Nunu/#38] feat: 8주차 구현1
Browse files Browse the repository at this point in the history
- 로그인 및 로그아웃 구현
- 로그인 상태에 따른 로그인/로그아웃 처리
- 로그인, 회원가입 Acticity 연결
- 회원 가입 구현
- binding null 처리 추가
  • Loading branch information
Ssamssamukja committed Jun 2, 2024
1 parent 95d668e commit afed4a9
Show file tree
Hide file tree
Showing 11 changed files with 724 additions and 4 deletions.
2 changes: 2 additions & 0 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
android:hardwareAccelerated="true"
android:label="@string/title_activity_song"
android:theme="@style/Theme.UMC_6th"></activity>
<activity android:name=".LoginActivity"/>
<activity android:name=".SignUpActivity"/>
</application>

</manifest>
45 changes: 45 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
Expand Down Expand Up @@ -40,6 +41,12 @@ class LockerFragment : Fragment() {
tab.text = information[position]
}.attach()

//로그인 intent
binding.txLogin.setOnClickListener {
val intent = Intent(requireActivity(), LoginActivity::class.java)
startActivity(intent)
}

return binding.root
}

Expand Down Expand Up @@ -82,4 +89,42 @@ class LockerFragment : Fragment() {
_dislikeAllEvent.value = false
}
}

override fun onStart() {
super.onStart()
initViews()
}

private fun getJwt() : Int {
val spf = requireActivity().getSharedPreferences("auth", AppCompatActivity.MODE_PRIVATE)
return spf!!.getInt("jwt", 0)
}

private fun initViews() {
val jwt: Int = getJwt()
if (jwt == 0) {
binding.txLogin.text="로그인"
binding.txLogin.setOnClickListener{
startActivity(Intent(requireActivity(), LoginActivity::class.java))
}
}else {
binding.txLogin.text="로그아웃"
binding.txLogin.setOnClickListener{
logout()
startActivity(Intent(requireActivity(), MainActivity::class.java))
}
}
}

private fun logout() {
val spf = activity?.getSharedPreferences("auth", AppCompatActivity.MODE_PRIVATE)
val editor = spf!!.edit()
editor.remove("jwt")
editor.apply()
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import com.example.umc_6th.databinding.FragmentLockerSavedSongBinding
import com.google.gson.Gson

class LockerSavedSongFragment : Fragment(){

private var _binding: FragmentLockerSavedSongBinding? = null
private val binding get() = _binding!!
private var songDatas = ArrayList<Song>()
lateinit var binding : FragmentLockerSavedSongBinding
lateinit var songDB: SongDatabase
val lockerAlbumRecyclerAdapter = LockerAlbumRecyclerAdapter()

Expand All @@ -28,7 +30,7 @@ class LockerSavedSongFragment : Fragment(){
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentLockerSavedSongBinding.inflate(inflater, container, false)
_binding = FragmentLockerSavedSongBinding.inflate(inflater, container, false)
/*
albumDatas.apply {
add(Album(0, "Love wins all", "아이유 (IU)", R.drawable.img_album_lovewinsall))
Expand Down Expand Up @@ -125,6 +127,6 @@ class LockerSavedSongFragment : Fragment(){

override fun onDestroy() {
super.onDestroy()

_binding = null
}
}
75 changes: 75 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivityLoginBinding

class LoginActivity : AppCompatActivity() {

private var _binding : ActivityLoginBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.loginSignUpTv.setOnClickListener{
val intent = Intent(this, SignUpActivity::class.java)
startActivity(intent)
}

binding.loginCloseIv.setOnClickListener {
finish()
}
binding.loginSignInBtn.setOnClickListener {
login()
}
}

private fun login() {
if (binding.loginIdEt.text.toString().isEmpty() || binding.loginDirectInputEt.text.toString().isEmpty()) {
Toast.makeText(this, "이메일을 입력해주세요.", Toast.LENGTH_SHORT).show()
return
}

if (binding.loginPasswordEt.text.toString().isEmpty()) {
Toast.makeText(this, "비밀번호를 입력해주세요.", Toast.LENGTH_SHORT).show()
return
}

val email : String = binding.loginIdEt.text.toString() + "@" + binding.loginDirectInputEt.text.toString()
val pwd : String = binding.loginPasswordEt.text.toString()

val songDB = SongDatabase.getInstance(this)!!
val user = songDB.userDao().getUser(email, pwd)

if (user != null) {
Log.d("LoginActivity", user.id.toString())
saveJwt(user.id)
startMainActivity()
} else {
Toast.makeText(this, "회원 정보가 존재하지 않습니다", Toast.LENGTH_SHORT).show()
}
}

private fun startMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}

private fun saveJwt(jwt: Int) {
val spf = getSharedPreferences("auth" , MODE_PRIVATE)
val editor = spf.edit()

editor.putInt("jwt", jwt)
editor.apply()
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
55 changes: 55 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.umc_6th

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivityLoginBinding
import com.example.umc_6th.databinding.ActivitySignupBinding
import com.example.umc_6th.User

class SignUpActivity : AppCompatActivity() {
lateinit var binding : ActivitySignupBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignupBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.signUpSignUpBtn.setOnClickListener {
val signUpCompletion = signUp()
if(signUpCompletion) {
finish()
}
}
}

private fun getUser() : User {
val email : String = binding.signUpIdEt.text.toString() + "@" + binding.signUpDirectInputEt.text.toString()
val password : String = binding.signUpPasswordEt.text.toString()
val name : String = binding.signUpNameEt.text.toString()

return User(email, password, name)
}

private fun signUp() : Boolean {
if(binding.signUpIdEt.text.toString().isEmpty() || binding.signUpDirectInputEt.text.toString().isEmpty()) {
Toast.makeText(this, "이메일 형식이 잘못되었습니다.", Toast.LENGTH_SHORT).show()
return false
}

if(binding.signUpPasswordEt.text.toString() != binding.signUpPasswordCheckEt.text.toString()) {
Toast.makeText(this, "비밀번호가 일치하지 않습니다.", Toast.LENGTH_SHORT).show()
return false
}

val userDB = SongDatabase.getInstance(this)!!
userDB.userDao().insert(getUser())

val user = userDB.userDao().getUsers()
Log.d("sign-up", user.toString())

Toast.makeText(this, "회원가입이 완료되었습니다.", Toast.LENGTH_SHORT).show()
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class SongActivity : AppCompatActivity() {
val editor = sharedPreferences.edit()
editor.putInt("songId", songs[nowPos].id)
editor.apply()
Log.d("songProgress", songs[nowPos].second.toString())
}
override fun onDestroy() {
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Song::class, Album::class], version = 1)
@Database(entities = [Song::class, Album::class, User::class], version = 1)
abstract class SongDatabase: RoomDatabase() {
abstract fun albumDao(): AlbumDao
abstract fun songDao(): SongDao
abstract fun userDao(): UserDao

companion object {
private var instance: SongDatabase? = null
Expand Down
14 changes: 14 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.umc_6th

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName

@Entity(tableName = "UserTable")
data class User (
@SerializedName(value = "email")val email: String,
@SerializedName(value = "password")val password: String,
@SerializedName(value = "name")val name: String
){
@PrimaryKey(autoGenerate = true) var id : Int = 0
}
17 changes: 17 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/UserDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.umc_6th

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query

@Dao
interface UserDao {
@Insert
fun insert(user : User)

@Query("select * from UserTable")
fun getUsers() : List<User>

@Query("select * from UserTable where email =:email and password = :password")
fun getUser(email : String, password : String) : User?
}
Loading

0 comments on commit afed4a9

Please sign in to comment.