Skip to content

Commit

Permalink
[Simba/#38] feat :: 8주차 완료 (#42)
Browse files Browse the repository at this point in the history
* [simba/#38] feat:: 회원가입, 로그인 구현

* [simba/#38] feat:: 회원가입, 로그인 구현2

* [simba/#38] feat:: 회원가입, 로그인 구현2

* [simba/#38] feat:: 특정 위치로 스크롤하기
  • Loading branch information
BAEK0111 authored Jun 5, 2024
1 parent d3434ad commit 1f4c3c2
Show file tree
Hide file tree
Showing 22 changed files with 1,324 additions and 36 deletions.
3 changes: 3 additions & 0 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
android:enabled="true"
android:exported="true"></service>

<activity android:name=".SignUpActivity"/>
<activity android:name=".LoginActivity"/>

<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
12 changes: 12 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ interface AlbumDao {
@Query("SELECT * FROM AlbumTable WHERE id = :id")
fun getAlbum(id : Int): Album

@Insert
fun likeAlbum(like : Like)

@Query("select id from LikeTable where userId =:userId and albumId = :albumId")
fun isLikedAlbum(userId : Int, albumId : Int) : Int?

@Query("delete from LikeTable where userId =:userId and albumId = :albumId")
fun dislikedAlbum(userId : Int, albumId : Int)

@Query("select at.* from LikeTable as lt left join AlbumTable as at on lt.albumId = at.id where lt.userId = :userId")
fun getLikedAlbums(userId : Int) : List<Album>

}
49 changes: 49 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResultListener
import com.example.umc_6th.databinding.FragmentAlbumBinding
Expand All @@ -18,7 +19,9 @@ class AlbumFragment : Fragment() {
lateinit var binding : FragmentAlbumBinding
private var gson : Gson = Gson()

private var isLiked : Boolean = false
private val information = arrayListOf("수록곡","상세정보","영상")

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -28,14 +31,18 @@ class AlbumFragment : Fragment() {

val albumJson = arguments?.getString("album")
val album = gson.fromJson(albumJson, Album::class.java)
isLiked = isLikedAlbum(album.id)
setInit(album)
setOnClickListener(album)

binding.albumBackIv.setOnClickListener{
(context as MainActivity).supportFragmentManager.beginTransaction().
replace(R.id.main_frm,HomeFragment()).
commitAllowingStateLoss()
}



val albumAdapter = AlbumVPAdapter(this)
binding.albumContentVp.adapter = albumAdapter

Expand Down Expand Up @@ -84,4 +91,46 @@ class AlbumFragment : Fragment() {
binding.albumSingerNameTv.text = album.singer.toString()
}

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

private fun likeAlbum(userId : Int, albumId : Int) {
val songDB = SongDatabase.getInstance(requireActivity())!!
val like = Like(userId, albumId)

songDB.albumDao().likeAlbum(like)
}

private fun isLikedAlbum(albumId : Int) : Boolean {
val songDB = SongDatabase.getInstance(requireActivity())!!
val userId = getJwt()

val likeId : Int? = songDB.albumDao().isLikedAlbum(userId, albumId)
return likeId != null
}

private fun disLikeAlbum(albumId : Int) {
val songDB = SongDatabase.getInstance(requireActivity())!!
val userId = getJwt()

songDB.albumDao().dislikedAlbum(userId, albumId)
}

private fun setOnClickListener(album : Album) {
val userId = getJwt()
binding.albumLikeIv.setOnClickListener {
if(isLiked) {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_off)
disLikeAlbum(album.id)
}

else {
binding.albumLikeIv.setImageResource((R.drawable.ic_my_like_on))
likeAlbum(userId, album.id)
}
}

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

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "LikeTable")
data class Like(
var userId : Int,
var albumId : Int
){
@PrimaryKey(autoGenerate = true) var id : Int = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LockerAlbumRVAdapter () : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHo

private val switchStatus = SparseBooleanArray()
private val songs = ArrayList<Song>()
private var isGrayOverlayEnabled = false

override fun onCreateViewHolder(
parent: ViewGroup,
Expand All @@ -31,6 +32,14 @@ class LockerAlbumRVAdapter () : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHo
removeSong(position) // 화면에서 아이템 제거
}

// 항목 배경색 변경
// if (isGrayOverlayEnabled) {
// holder.binding.root.setBackgroundColor(holder.binding.root.context.getColor(R.color.gray_transparent))
// } else {
// holder.binding.root.setBackgroundColor(holder.binding.root.context.getColor(R.color.transparent))
// }


val switch = holder.binding.switchRV
switch.isChecked = switchStatus[position]
switch.setOnClickListener {
Expand Down Expand Up @@ -78,5 +87,4 @@ class LockerAlbumRVAdapter () : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHo
songs.removeAt(position)
notifyDataSetChanged()
}

}
39 changes: 39 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,10 +1,12 @@
package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.FragmentLockerBinding
import com.google.android.material.tabs.TabLayoutMediator

Expand Down Expand Up @@ -37,6 +39,43 @@ class LockerFragment : Fragment() {
bottomSheetFragment.show(requireFragmentManager(),"BottomSheetDialog")
}

binding.lockerLoginTv.setOnClickListener {
startActivity(Intent(activity, LoginActivity::class.java))
}

return binding.root
}
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.lockerLoginTv.text="로그인"
binding.lockerLoginTv.setOnClickListener {
startActivity(Intent(requireActivity(), LoginActivity::class.java))
}
}

else {
binding.lockerLoginTv.text = "로그아웃"
binding.lockerLoginTv.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()
}
}
65 changes: 65 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,65 @@
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() {

lateinit var binding : ActivityLoginBinding

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

binding.loginSignUpTv.setOnClickListener {
startActivity(Intent(this, SignUpActivity::class.java))
}
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()
}

}
Loading

0 comments on commit 1f4c3c2

Please sign in to comment.