Skip to content

Commit

Permalink
[Nunu/#38] feat: 8주차 구현
Browse files Browse the repository at this point in the history
* [Nunu/#38] feat: 8주차 구현1

- 로그인 및 로그아웃 구현
- 로그인 상태에 따른 로그인/로그아웃 처리
- 로그인, 회원가입 Acticity 연결
- 회원 가입 구현
- binding null 처리 추가

* [Nunu/#38] feat: 8주차 구현2

- AlbumFragment에 하트 누르면 User 구분하여 Like Table 업데이트 기능 구현
- 저장앨범 탭에 띄우기 기능 구현
- 앨범 리사이클러뷰 구현
- 기존 item_locker_album.xml -> item_locker_song.xml로 변경
- fragment에 onResume, onStart에 설정하여 바로바로 업데이트 되게 보여줌.

* [Nunu/#38] feat: 8주차 구현3

- look Fragment 구현
  • Loading branch information
Ssamssamukja authored Jun 5, 2024
1 parent 95d668e commit fd4ed78
Show file tree
Hide file tree
Showing 23 changed files with 1,268 additions and 16 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>
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 @@ -18,4 +18,16 @@ interface AlbumDao {

@Query("SELECT * FROM AlbumTable WHERE id = :id")
fun getAlbum(id: Int): Album

@Insert
fun likeAlbum(like: Like)

@Query("DELETE FROM LikeTable WHERE userId = :userId AND albumId = :albumId")
fun disLikeAlbum(userId: Int, albumId: Int)

@Query("SELECT id FROM LikeTable WHERE userId = :userId AND albumId = :albumId")
fun isLikedAlbum(userId: Int, albumId: Int): 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>
}
61 changes: 60 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
Expand All @@ -23,6 +24,8 @@ class AlbumFragment: Fragment(R.layout.fragment_album) {
private val binding get() = _binding!!
private var gson: Gson = Gson()
private val information = arrayListOf("수록곡", "상세정보", "영상")
private var isLiked : Boolean = false

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -33,19 +36,24 @@ class AlbumFragment: Fragment(R.layout.fragment_album) {
//앨범 데이터
val albumToJson = arguments?.getString("album")
val album = gson.fromJson(albumToJson, Album::class.java)
isLiked = isLikedAlbum(album.id)
setInit(album)
setOnClickListener(album)

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

/*
// 뒤로가기 이미지 뷰에 클릭 리스너 설정
binding.albumBackIv.setOnClickListener {
// 이전 프래그먼트로 돌아가기
requireActivity().supportFragmentManager.popBackStack()
}
*/

val adapter = AlbumPagerAdapter(this)
binding.albumViewPager.adapter = adapter

Expand All @@ -71,6 +79,57 @@ class AlbumFragment: Fragment(R.layout.fragment_album) {
binding.imgAlbumAlbumCov.setImageResource(album.coverImg!!)
binding.txAlbumAlbumTitle.text = album.title
binding.txAlbumAlbumArtist.text = album.artist
if(isLiked) {
binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_on)
}else {
binding.albumLikeIv.setImageResource((R.drawable.ic_my_like_off))
}
}

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().disLikeAlbum(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)
}
isLiked = !isLiked
}
binding.albumBackIv.setOnClickListener {
(context as MainActivity).supportFragmentManager.beginTransaction()
.replace(R.id.main_container, HomeFragment())
.commitAllowingStateLoss()
}

}


Expand Down
9 changes: 9 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,9 @@
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
}
48 changes: 47 additions & 1 deletion 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 All @@ -21,7 +22,7 @@ import com.google.android.material.tabs.TabLayoutMediator
class LockerFragment : Fragment() {
private var _binding: FragmentLockerBinding? = null
private val binding get() = _binding!!
private val information = arrayListOf("저장한곡", "음악파일")
private val information = arrayListOf("저장한곡", "음악파일", "저장앨범")

private lateinit var sharedViewModel: SharedViewModel
private lateinit var bottomSheetDialog: BottomSheetDialog
Expand All @@ -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,43 @@ 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
@@ -0,0 +1,60 @@
package com.example.umc_6th

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.umc_6th.adapter.LockerSavedAlbumRecyclerAdapter
import com.example.umc_6th.databinding.FragmentLockerSavedAlbumBinding
import androidx.recyclerview.widget.LinearLayoutManager

class LockerSavedAlbumFragment : Fragment() {
lateinit var binding: FragmentLockerSavedAlbumBinding
lateinit var albumDB: SongDatabase

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentLockerSavedAlbumBinding.inflate(inflater, container, false)

albumDB = SongDatabase.getInstance(requireContext())!!

return binding.root
}
override fun onStart() {
super.onStart()
initRecyclerview()
}

private fun initRecyclerview(){
binding.lockerSavedSongRecyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

val lockerSavedAlbumRecyclerAdapter = LockerSavedAlbumRecyclerAdapter()
//리스너 객체 생성 및 전달

lockerSavedAlbumRecyclerAdapter.setMyItemClickListener(object : LockerSavedAlbumRecyclerAdapter.MyItemClickListener{
override fun onRemoveSong(songId: Int) {
albumDB.albumDao().getLikedAlbums(getJwt())
}
})

binding.lockerSavedSongRecyclerView.adapter = lockerSavedAlbumRecyclerAdapter

lockerSavedAlbumRecyclerAdapter.addAlbums(albumDB.albumDao().getLikedAlbums(getJwt()) as ArrayList)
}

private fun getJwt() : Int {
val spf = activity?.getSharedPreferences("auth" , AppCompatActivity.MODE_PRIVATE)
val jwt = spf!!.getInt("jwt", 0)
Log.d("MAIN_ACT/GET_JWT", "jwt_token: $jwt")

return jwt
}


}
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
}
}
Loading

0 comments on commit fd4ed78

Please sign in to comment.