Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
android:supportsRtl="true"
android:theme="@style/Theme.UMC_9th"
tools:targetApi="31">
<activity
android:name="com.example.umc_9th.LookFragment"
android:exported="false" />

<service
android:name="com.example.umc_9th.MusicService"
android:enabled="true"
android:exported="true"
android:foregroundServiceType="mediaPlayback" >
</service>
android:foregroundServiceType="mediaPlayback"></service>

<activity
android:name="com.example.umc_9th.LoginActivity"
android:exported="false" />
<activity
android:name="com.example.umc_9th.SignUpActivity"
android:exported="false" />
<activity
android:name="com.example.umc_9th.MainActivity"
android:exported="false" />
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/com/example/umc_9th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,68 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.umc_9th.data.firebase.FirebaseManager
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import umc.study.umc_8th.R

class AlbumFragment : Fragment(R.layout.fragment_album) {
private lateinit var firebaseManager: FirebaseManager
private var isLiked = false
private var albumId = 0

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

firebaseManager = FirebaseManager.getInstance()

val title = arguments?.getString("title") ?: "제목 없음"
val artist = arguments?.getString("artist") ?: "가수 없음"
val albumResId = arguments?.getInt("albumResId") ?: R.drawable.img_album_exp3

val albumCover = view.findViewById<ImageView>(R.id.album_cover)
val albumTitle = view.findViewById<TextView>(R.id.album_title)
val albumArtist = view.findViewById<TextView>(R.id.album_artist)
val btnLike = view.findViewById<ImageButton>(R.id.album_btn_like)

albumCover.setImageResource(albumResId)
albumTitle.text = title
albumArtist.text = artist

// 🔥 좋아요 상태 확인
firebaseManager.checkIfAlbumLiked(albumId) { liked ->
activity?.runOnUiThread {
isLiked = liked
btnLike.setImageResource(
if (liked) R.drawable.ic_my_like_on
else R.drawable.ic_my_like_off
)
}
}

// 🔥 좋아요 버튼 클릭
btnLike.setOnClickListener {
val album = Album(albumId, title, artist, albumResId)

if (isLiked) {
firebaseManager.removeLikedAlbum(albumId,
onSuccess = {
isLiked = false
btnLike.setImageResource(R.drawable.ic_my_like_off)
},
onFailure = {}
)
} else {
firebaseManager.addLikedAlbum(album,
onSuccess = {
isLiked = true
btnLike.setImageResource(R.drawable.ic_my_like_on)
},
onFailure = {}
)
}
}

val tabLayout = view.findViewById<TabLayout>(R.id.album_tabLayout)
val viewPager = view.findViewById<ViewPager2>(R.id.album_viewPager)
val btnBack = view.findViewById<ImageButton>(R.id.album_btn_back)
Expand Down
107 changes: 107 additions & 0 deletions app/src/main/java/com/example/umc_9th/AuthManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.example.umc_9th

import android.content.Context
import android.content.SharedPreferences
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser

class AuthManager private constructor(context: Context) {

private val auth: FirebaseAuth = FirebaseAuth.getInstance()
private val prefs: SharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE)

companion object {
@Volatile
private var instance: AuthManager? = null

fun getInstance(context: Context): AuthManager {
return instance ?: synchronized(this) {
instance ?: AuthManager(context.applicationContext).also { instance = it }
}
}
}

// 회원가입
fun signUp(
email: String,
password: String,
onSuccess: (FirebaseUser) -> Unit,
onFailure: (String) -> Unit
) {
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener { result ->
val user = result.user
if (user != null) {
saveUserToPrefs(user.uid, user.email ?: "")
onSuccess(user)
} else {
onFailure("회원가입 실패")
}
}
.addOnFailureListener { exception ->
onFailure(exception.message ?: "알 수 없는 오류")
}
}

// 로그인
fun signIn(
email: String,
password: String,
onSuccess: (FirebaseUser) -> Unit,
onFailure: (String) -> Unit
) {
auth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener { result ->
val user = result.user
if (user != null) {
saveUserToPrefs(user.uid, user.email ?: "")
onSuccess(user)
} else {
onFailure("로그인 실패")
}
}
.addOnFailureListener { exception ->
onFailure(exception.message ?: "알 수 없는 오류")
}
}

// 로그아웃
fun signOut() {
auth.signOut()
clearUserPrefs()
}

// 현재 로그인된 사용자
fun getCurrentUser(): FirebaseUser? {
return auth.currentUser
}

// 로그인 상태 확인
fun isLoggedIn(): Boolean {
return auth.currentUser != null
}

// SharedPreferences에 유저 정보 저장
private fun saveUserToPrefs(userId: String, email: String) {
prefs.edit().apply {
putString("user_id", userId)
putString("user_email", email)
putBoolean("is_logged_in", true)
apply()
}
}

// SharedPreferences에서 유저 정보 가져오기
fun getUserId(): String? {
return prefs.getString("user_id", null)
}

fun getUserEmail(): String? {
return prefs.getString("user_email", null)
}

// SharedPreferences 초기화
private fun clearUserPrefs() {
prefs.edit().clear().apply()
}
}
60 changes: 60 additions & 0 deletions app/src/main/java/com/example/umc_9th/FirebaseManager.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.umc_9th.data.firebase

import com.example.umc_9th.Album
import com.google.firebase.database.*
import com.example.umc_9th.Song

Expand All @@ -8,6 +9,7 @@ class FirebaseManager {
private val database: DatabaseReference = FirebaseDatabase.getInstance().reference
private val songsRef = database.child("songs")
private val likedSongsRef = database.child("likedSongs")
private val likedAlbumsRef = database.child("likedAlbums")

companion object {
@Volatile
Expand Down Expand Up @@ -128,6 +130,64 @@ class FirebaseManager {
}
})
}

// 🔥 ========== 앨범 관련 함수들 추가 ==========

// 앨범 좋아요 추가
fun addLikedAlbum(album: Album, onSuccess: () -> Unit, onFailure: (String) -> Unit) {
val albumData = mapOf(
"id" to album.id,
"title" to album.title,
"artist" to album.artist,
"albumResId" to album.albumResId
)

database.child("likedAlbums").child(album.id.toString()).setValue(albumData)
.addOnSuccessListener { onSuccess() }
.addOnFailureListener { onFailure(it.message ?: "오류") }
}

// 앨범 좋아요 제거
fun removeLikedAlbum(albumId: Int, onSuccess: () -> Unit, onFailure: (String) -> Unit) {
database.child("likedAlbums").child(albumId.toString()).removeValue()
.addOnSuccessListener { onSuccess() }
.addOnFailureListener { onFailure(it.message ?: "오류") }
}

// 앨범 좋아요 상태 확인
fun checkIfAlbumLiked(albumId: Int, callback: (Boolean) -> Unit) {
database.child("likedAlbums").child(albumId.toString())
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
callback(snapshot.exists())
}
override fun onCancelled(error: DatabaseError) {
callback(false)
}
})
}

// 좋아요한 앨범 목록
fun getLikedAlbums(onSuccess: (List<Album>) -> Unit, onFailure: (String) -> Unit) {
database.child("likedAlbums").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val albums = mutableListOf<Album>()
for (child in snapshot.children) {
val id = child.child("id").getValue(Int::class.java) ?: 0
val title = child.child("title").getValue(String::class.java) ?: ""
val artist = child.child("artist").getValue(String::class.java) ?: ""
val albumResId = child.child("albumResId").getValue(Int::class.java) ?: 0

albums.add(Album(id, title, artist, albumResId, true))
}
onSuccess(albums)
}
override fun onCancelled(error: DatabaseError) {
onFailure(error.message)
}
})
}


// ===== 노래 재생 위치 저장 =====

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/umc_9th/HomeAlbumAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import androidx.recyclerview.widget.RecyclerView
import umc.study.umc_8th.R

data class Album(
val id: Int = 0,
val title: String,
val artist: String,
val albumResId: Int
val albumResId: Int,
var isLike: Boolean = false
)

class HomeAlbumAdapter(
Expand Down
Loading