Skip to content

Commit

Permalink
Merge branch 'main' into frontend for updating friendlist
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgnaohel committed Dec 9, 2024
2 parents 938ba85 + 0065197 commit d1ad046
Show file tree
Hide file tree
Showing 19 changed files with 838 additions and 113 deletions.
9 changes: 0 additions & 9 deletions app/src/main/java/com/example/harmonyhub/HarmonyHubApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,9 @@ fun HarmonyHubApp(

composable(route = HarmonyHubScreen.Friends.name) {
FriendsScreen(
friends = listOf(
Friend("Anh", "[email protected]", R.drawable.hip),
Friend("Minh", "[email protected]", R.drawable.hip)
),
onBackButtonClicked = { navController.popBackStack() },
onAddButtonClicked = { },
onUnfriendClicked = { },
onWatchPlaylistClicked = { },
friendRequests = listOf(
Friend("Anh", "[email protected]", R.drawable.hip),
Friend("Minh", "[email protected]", R.drawable.hip)
)
)

}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.example.harmonyhub.domain.repository

import com.example.harmonyhub.presentation.viewmodel.DataFetchingState
import com.example.harmonyhub.presentation.viewmodel.FavoriteSongFetchingState
import com.example.harmonyhub.presentation.viewmodel.FriendListFetchingState
import com.example.harmonyhub.presentation.viewmodel.PlaylistSongFetchingState
import com.example.harmonyhub.ui.components.Song

interface UserDataRepo {
fun getUserInfor(callback: (String?, String?) -> Unit)
fun getUserInfor(uid: String? = null, callback: (String?, String?) -> Unit)
fun setUserInfor(userName: String, email: String, userId: String?)

fun getAlbums(callback: (DataFetchingState) -> Unit)
Expand All @@ -17,7 +18,23 @@ interface UserDataRepo {
fun getFavoriteSongs(callback: (FavoriteSongFetchingState) -> Unit)

fun addSongToPlayList(song: Song, playlistName: String, callback: (PlaylistSongFetchingState) -> Unit)
// fun removeSongFromPlayList()
// fun getSongFromPlayList()
// fun deletePlayList()
}
fun getPlaylistSongs(playlistName: String, callback: (PlaylistSongFetchingState) -> Unit)
fun removeSongFromPlayList(song: Song, playlistName: String, callback: (PlaylistSongFetchingState) -> Unit)
fun deletePlayList(playlistName: String)

fun getUsers(callback: (List<FirebaseUser>) -> Unit)
fun searchForEmail(email: String, callback: (FriendListFetchingState) -> Unit)

fun sendFriendRequest(uid: String, callback: (FriendListFetchingState) -> Unit)
fun getFriendRequests(callback: (FriendListFetchingState) -> Unit)
fun acceptFriendRequest(uid: String, callback: (FriendListFetchingState) -> Unit)
fun declineFriendRequest(uid: String, callback: (FriendListFetchingState) -> Unit)
fun getFriends(callback: (FriendListFetchingState) -> Unit)
fun removeFriend(uid: String, callback: (FriendListFetchingState) -> Unit)
}

data class FirebaseUser(
val email: String = "",
val uid: String = "",
val userName: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.example.harmonyhub.presentation.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.harmonyhub.domain.repository.UserDataRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class FriendListViewModel @Inject constructor(
private val userRepo: UserDataRepo,
): ViewModel() {

private val _dataFetchingState = MutableLiveData<FriendListFetchingState>()
val dataFetchingState: MutableLiveData<FriendListFetchingState> get() = _dataFetchingState

init {
resetDataFetchingState()
}

fun resetDataFetchingState() {
_dataFetchingState.value = FriendListFetchingState.Pending
}

fun searchForEmail(email: String) {
val emailPattern = "[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"

if (!email.matches(emailPattern.toRegex())) {
_dataFetchingState.value = FriendListFetchingState.Error("Invalid email format")
return
}

userRepo.searchForEmail(email) { state ->
_dataFetchingState.value = state
}
}

fun getFriendRequests() {
userRepo.getFriendRequests { state ->
_dataFetchingState.value = state
}
}

fun acceptFriendRequest(uid: String) {
userRepo.acceptFriendRequest(uid) { state ->
_dataFetchingState.value = state
}
}

fun declineFriendRequest(uid: String) {
userRepo.declineFriendRequest(uid) { state ->
_dataFetchingState.value = state
}
}

fun getFriends() {
userRepo.getFriends { state ->
_dataFetchingState.value = state
}
}

fun removeFriend(uid: String) {
userRepo.removeFriend(uid) { state ->
_dataFetchingState.value = state
}
}
}

sealed class FriendListFetchingState {
object Pending : FriendListFetchingState()
data class Success(val data: Any) : FriendListFetchingState()
data class SuccessOnGetFriends(val data: Any) : FriendListFetchingState()
data class Error(val message: String) : FriendListFetchingState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ class PlaylistViewModel @Inject constructor(
})
}

fun getPlaylistSongs(playlistName: String) {
userRepo.getPlaylistSongs(playlistName, callback = {
_dataFetchingState.value = it
})
}

fun removeSongFromPlayList(song: Song, playlistName: String) {
userRepo.removeSongFromPlayList(song, playlistName, callback = {
_dataFetchingState.value = it
})
}

fun deletePlayList(playlistName: String) {
userRepo.deletePlayList(playlistName)
}

fun resetDataFetchingState() {
_dataFetchingState.value = PlaylistSongFetchingState.Pending
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import com.example.harmonyhub.ui.theme.NotoSans
data class Friend(
val name: String,
val email: String,
val imageResId: Int
val imageResId: Int,
val uid: String = ""
)

fun Friend.contains(query: String, ignoreCase: Boolean = true): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.example.harmonyhub.R
import com.example.harmonyhub.presentation.viewmodel.FavoriteSongsViewModel
import com.example.harmonyhub.presentation.viewmodel.PlaylistViewModel
import com.example.harmonyhub.ui.theme.NotoSans

data class Song(
Expand Down Expand Up @@ -123,7 +125,7 @@ fun BottomSheetContent(
onDeleteClicked: () -> Unit,
onShareClicked: () -> Unit,
onDownloadClicked: () -> Unit,
favoriteSongsViewModel: FavoriteSongsViewModel? = null
favoriteSongsViewModel: FavoriteSongsViewModel = hiltViewModel()
) {

Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,40 @@ import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.lifecycleScope
import com.example.harmonyhub.R
import com.example.harmonyhub.data.SongRepository
import com.example.harmonyhub.data.repository.FirebasePlaylist
import com.example.harmonyhub.presentation.viewmodel.DataFetchingState
import com.example.harmonyhub.presentation.viewmodel.PlaylistSongFetchingState
import com.example.harmonyhub.presentation.viewmodel.PlaylistViewModel
import com.example.harmonyhub.presentation.viewmodel.UserDataViewModel
import com.example.harmonyhub.ui.components.Song
import com.example.harmonyhub.ui.theme.NotoSans
import kotlinx.coroutines.launch
import retrofit2.http.Url

private val gradientBackground = Brush.verticalGradient(
colors = listOf(Color(0xFF04A8A3), Color(0xFF0A91BD))
)

private fun getSongByUrl(url: String?): Song? {

SongRepository.allSongs.forEach {
if (it.url == url) {
return it
}
}

return null
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddToPlaylistFromSongScreen(
url: String?,
onBackButtonClicked: () -> Unit,
userDataViewModel: UserDataViewModel = hiltViewModel(),
playlistViewModel: PlaylistViewModel = hiltViewModel(),
song: Song = Song("1", "Song 1", "Artist 1", "Album 1", "Genre 1")
) {
val song = getSongByUrl(url)
val focusManager = LocalFocusManager.current

var query by remember { mutableStateOf("") }
Expand All @@ -90,9 +102,11 @@ fun AddToPlaylistFromSongScreen(
var selectedPlaylists by remember { mutableStateOf(setOf<String>())}

val albumsFetchingState = userDataViewModel.dataFetchingState.observeAsState()
val addingSongState = playlistViewModel.dataFetchingState.observeAsState()
val context = LocalContext.current

val allPlaylists = remember { mutableListOf<String>() }
val playlistSongs = remember { mutableListOf<Int>() }

val lifecycleOwner = LocalLifecycleOwner.current

Expand All @@ -108,7 +122,11 @@ fun AddToPlaylistFromSongScreen(
when (albumsFetchingState.value) {
is DataFetchingState.Success -> {
allPlaylists.clear()
allPlaylists.addAll((albumsFetchingState.value as DataFetchingState.Success).data as List<String>)
playlistSongs.clear()
((albumsFetchingState.value as DataFetchingState.Success).data as List<FirebasePlaylist>).forEach {
allPlaylists.add(it.name)
playlistSongs.add(it.songCount)
}
userDataViewModel.resetDataFetchingState()
}
is DataFetchingState.Error -> {
Expand Down Expand Up @@ -221,7 +239,7 @@ fun AddToPlaylistFromSongScreen(
items(allPlaylists) { playlist ->
PlaylistItem(
playlistName = playlist,
songCount = "2 bài hát",
songCount = "${playlistSongs[allPlaylists.indexOf(playlist)]} bài hát",
onPlaylistClicked = {
if (selectedPlaylists.contains(playlist)) {
selectedPlaylists -= playlist
Expand All @@ -238,22 +256,31 @@ fun AddToPlaylistFromSongScreen(
// Nút hoàn tất
Button(
onClick = {
// Launch a coroutine to handle the asynchronous operations
selectedPlaylists.forEach { playlist ->
lifecycleOwner.lifecycleScope.launch {
playlistViewModel.addSongToPlayList(song, playlist)
if (selectedPlaylists.size > 1) {
Toast.makeText(context, "Chỉ được chọn 1 danh sách phát", Toast.LENGTH_SHORT).show()
} else {
// Launch a coroutine to handle the asynchronous operations
selectedPlaylists.forEach { playlist ->
lifecycleOwner.lifecycleScope.launch {
if (song != null) {
playlistViewModel.addSongToPlayList(song, playlist)

when (val state = playlistViewModel.dataFetchingState.value) {
is PlaylistSongFetchingState.Error -> {
val message = state.message
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
playlistViewModel.resetDataFetchingState()
when (val state = playlistViewModel.dataFetchingState.value) {
is PlaylistSongFetchingState.Error -> {
val message = state.message
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
is PlaylistSongFetchingState.Success -> {
userDataViewModel.getAlbums()
Toast.makeText(context, "Successfully added to playlist", Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
else -> {}
}
}

}
}
}
// onBackButtonClicked()
},
colors = ButtonDefaults.buttonColors(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ fun AlbumScreen(
onShareClicked = onShareClicked,
onDownloadClicked = onDownloadClicked,
onDeleteClicked = {},
favoriteSongsViewModel = null
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ fun ArtistScreen(
onShareClicked = onShareClicked,
onDownloadClicked = onDownloadClicked,
onDeleteClicked = {},
favoriteSongsViewModel = null
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ fun ChartsScreen(
onAddToFavoriteClicked = onAddToFavoriteClicked,
onShareClicked = onShareClicked,
onDownloadClicked = onDownloadClicked,
onDeleteClicked = {},
favoriteSongsViewModel = null
)
onDeleteClicked = {},)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ fun DownloadScreen(
onDeleteClicked = onDeleteClicked,
onShareClicked = {},
onDownloadClicked = {},
favoriteSongsViewModel = null
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.hilt.navigation.compose.hiltViewModel
import com.example.harmonyhub.presentation.viewmodel.FavoriteSongFetchingState
import com.example.harmonyhub.presentation.viewmodel.FavoriteSongsViewModel
import com.example.harmonyhub.ui.components.Song
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

@Composable
fun FavoriteScreen(
Expand Down Expand Up @@ -73,6 +75,15 @@ fun FavoriteScreen(
onDeleteClicked = onDeleteClicked,
onShareClicked = onShareClicked,
onDownloadClicked = onDownloadClicked,
favoriteSongsViewModel = favoriteSongsViewModel
onDeleteAllClicked = {
runBlocking {
launch {
allSongs.forEach {
favoriteSongsViewModel.removeFavoriteSong(it)
}
favoriteSongsViewModel.getFavoriteSongs()
}
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ fun HistoryScreen(
onDeleteClicked = onDeleteClicked,
onShareClicked = onShareClicked,
onDownloadClicked = onDownloadClicked,
favoriteSongsViewModel = null
)
}
Loading

0 comments on commit d1ad046

Please sign in to comment.