From afed4a9bef854057ef1dc723a232ca111838cd05 Mon Sep 17 00:00:00 2001 From: Ssamssamukja <109636635+Ssamssamukja@users.noreply.github.com> Date: Mon, 3 Jun 2024 04:25:16 +0900 Subject: [PATCH] =?UTF-8?q?[Nunu/#38]=20feat:=208=EC=A3=BC=EC=B0=A8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=841?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그인 및 로그아웃 구현 - 로그인 상태에 따른 로그인/로그아웃 처리 - 로그인, 회원가입 Acticity 연결 - 회원 가입 구현 - binding null 처리 추가 --- UMC_6th/app/src/main/AndroidManifest.xml | 2 + .../com/example/umc_6th/LockerFragment.kt | 45 +++ .../umc_6th/LockerSavedSongFragment.kt | 8 +- .../java/com/example/umc_6th/LoginActivity.kt | 75 +++++ .../com/example/umc_6th/SignUpActivity.kt | 55 ++++ .../java/com/example/umc_6th/SongActivity.kt | 1 + .../java/com/example/umc_6th/SongDatabase.kt | 3 +- .../src/main/java/com/example/umc_6th/User.kt | 14 + .../main/java/com/example/umc_6th/UserDao.kt | 17 ++ .../src/main/res/layout/activity_login.xml | 285 ++++++++++++++++++ .../src/main/res/layout/activity_signup.xml | 223 ++++++++++++++ 11 files changed, 724 insertions(+), 4 deletions(-) create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/User.kt create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/UserDao.kt create mode 100644 UMC_6th/app/src/main/res/layout/activity_login.xml create mode 100644 UMC_6th/app/src/main/res/layout/activity_signup.xml diff --git a/UMC_6th/app/src/main/AndroidManifest.xml b/UMC_6th/app/src/main/AndroidManifest.xml index df4179a..a349ee4 100644 --- a/UMC_6th/app/src/main/AndroidManifest.xml +++ b/UMC_6th/app/src/main/AndroidManifest.xml @@ -41,6 +41,8 @@ android:hardwareAccelerated="true" android:label="@string/title_activity_song" android:theme="@style/Theme.UMC_6th"> + + \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt index 98f271f..37d1fd9 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt @@ -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 @@ -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 } @@ -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 + } } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerSavedSongFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerSavedSongFragment.kt index 4a7c781..bc62598 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerSavedSongFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerSavedSongFragment.kt @@ -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() - lateinit var binding : FragmentLockerSavedSongBinding lateinit var songDB: SongDatabase val lockerAlbumRecyclerAdapter = LockerAlbumRecyclerAdapter() @@ -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)) @@ -125,6 +127,6 @@ class LockerSavedSongFragment : Fragment(){ override fun onDestroy() { super.onDestroy() - + _binding = null } } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt new file mode 100644 index 0000000..80b3a5d --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt @@ -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 + } +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt new file mode 100644 index 0000000..987ca86 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt @@ -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 + } + +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt index b4870dc..d0f9346 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt @@ -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() diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt index a1ac77f..a17f4d2 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt @@ -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 diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/User.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/User.kt new file mode 100644 index 0000000..911df06 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/User.kt @@ -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 +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/UserDao.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/UserDao.kt new file mode 100644 index 0000000..8f6b77b --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/UserDao.kt @@ -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 + + @Query("select * from UserTable where email =:email and password = :password") + fun getUser(email : String, password : String) : User? +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/res/layout/activity_login.xml b/UMC_6th/app/src/main/res/layout/activity_login.xml new file mode 100644 index 0000000..9f450c1 --- /dev/null +++ b/UMC_6th/app/src/main/res/layout/activity_login.xml @@ -0,0 +1,285 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UMC_6th/app/src/main/res/layout/activity_signup.xml b/UMC_6th/app/src/main/res/layout/activity_signup.xml new file mode 100644 index 0000000..fc7e834 --- /dev/null +++ b/UMC_6th/app/src/main/res/layout/activity_signup.xml @@ -0,0 +1,223 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file