-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
database-design branch - merging & resolving conflicts from minhnhat_…
…branch
- Loading branch information
Showing
21 changed files
with
822 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package com.example.harmonyhub | ||
|
||
import android.app.Application | ||
import com.example.harmonyhub.data.repository.DefaultHomeScreenRepo | ||
import com.example.harmonyhub.data.repository.HomeScreenRepo | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class MyApplication: Application() { | ||
lateinit var container: HomeScreenRepo | ||
override fun onCreate() { | ||
super.onCreate() | ||
container = DefaultHomeScreenRepo() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/main/java/com/example/harmonyhub/data/network/HomeOverViewDataClassIn.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.example.harmonyhub.data.network | ||
|
||
data class Avatar( | ||
val url: String, | ||
val width: Int, | ||
val height: Int | ||
) | ||
|
||
data class Avatars( | ||
val avatar: List<Avatar> | ||
) | ||
|
||
data class Album( | ||
val type: String, | ||
val id: String, | ||
val name: String, | ||
val cover: List<Avatar>, | ||
val artists: List<Artist> | ||
) | ||
|
||
data class AlbumOut( | ||
val id: String, | ||
val name: String, | ||
val image: String, | ||
val listArtist: List<String>, | ||
) | ||
|
||
data class Artist( | ||
val type: String, | ||
val id: String, | ||
val name: String, | ||
val visuals: Avatars? = null | ||
) | ||
|
||
data class ArtistOut( | ||
val id: String, | ||
val name: String, | ||
val image: String? | ||
) | ||
|
||
|
||
|
||
|
||
data class PopularItem( | ||
val type: String, | ||
val id: String, | ||
val name: String, | ||
val cover: List<Avatar>,//album | ||
val artists: List<Artist>,//album | ||
val visuals: Avatars? = null,//Artist | ||
val images: List<List<Image>>? = null | ||
|
||
|
||
) | ||
|
||
data class Contents( | ||
val totalCount: Int, | ||
val items: List<PopularItem> | ||
) | ||
|
||
data class SectionItem( | ||
val type: String, | ||
val id: String, | ||
val title: String, | ||
val contents: Contents | ||
) | ||
|
||
data class Sections( | ||
val totalCount: Int, | ||
val items: List<SectionItem> | ||
) | ||
|
||
data class Response( | ||
val status: Boolean, | ||
val errorId: String, | ||
val sections: Sections | ||
) | ||
|
||
data class ResponseHomeScreenData( | ||
val listPopularArtist: MutableList<ArtistOut>?, | ||
val listPopularAlbums: MutableList<AlbumOut>?, | ||
val listChart: MutableList<ChartOut>? | ||
) | ||
|
||
// Lớp cho từng playlist | ||
data class PlaylistItem( | ||
val type: String, | ||
val id: String, | ||
val name: String, | ||
val shareUrl: String, | ||
val description: String, | ||
val owner: Owner, | ||
val images: List<List<Image>> | ||
) | ||
|
||
// Lớp owner chứa thông tin về người sở hữu playlist | ||
data class Owner( | ||
val name: String, | ||
val type: String | ||
) | ||
|
||
// Lớp hình ảnh của playlist | ||
data class Image( | ||
val url: String, | ||
val width: Int?, | ||
val height: Int? | ||
) | ||
|
||
data class ChartOut( | ||
val name: String, | ||
val image: String?, | ||
val id: String | ||
) |
93 changes: 93 additions & 0 deletions
93
app/src/main/java/com/example/harmonyhub/data/repository/DefaultHomeScreenRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.example.harmonyhub.data.repository | ||
|
||
|
||
import com.example.harmonyhub.data.network.AlbumOut | ||
import com.example.harmonyhub.data.network.ArtistOut | ||
import com.example.harmonyhub.data.network.ChartOut | ||
|
||
import com.example.harmonyhub.data.network.Response | ||
import com.example.harmonyhub.data.network.ResponseHomeScreenData | ||
import com.google.gson.Gson | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
|
||
class DefaultHomeScreenRepo : HomeScreenRepo { | ||
override suspend fun updatePopularItem(): ResponseHomeScreenData? { | ||
val listPopularArtist : MutableList<ArtistOut>? = mutableListOf() | ||
val listPopularAlbums: MutableList<AlbumOut>? = mutableListOf() | ||
val listChart: MutableList<ChartOut>? = mutableListOf() | ||
|
||
var result : ResponseHomeScreenData? = null | ||
|
||
try { | ||
|
||
val client = OkHttpClient() | ||
|
||
val request = Request.Builder() | ||
.url("https://spotify-scraper.p.rapidapi.com/v1/home") | ||
.get() | ||
.addHeader("x-rapidapi-key", "c18da195b0mshcdebcf46df53015p1a1b64jsn33955d1b96fc") | ||
.addHeader("x-rapidapi-host", "spotify-scraper.p.rapidapi.com") | ||
.build() | ||
|
||
val response = client.newCall(request).execute() | ||
// Kiểm tra xem phản hồi có thành công không | ||
if (response.isSuccessful) { | ||
val jsonResponse = response.body?.string() | ||
//println("Response: $jsonResponse") | ||
// Kiểm tra nếu jsonResponse không null và là chuỗi hợp lệ | ||
if (jsonResponse != null && jsonResponse.isNotEmpty()) { | ||
try { | ||
|
||
val gson = Gson() | ||
val responseData = gson.fromJson(jsonResponse, Response::class.java) | ||
|
||
// Trích xuất dữ liệu | ||
if (responseData.status) { | ||
//trich xuat Popular Artise | ||
val PopularArtist = responseData.sections.items[0].contents.items | ||
val PopularAlbums = responseData.sections.items[1].contents.items | ||
val PopularChart = responseData.sections.items[3].contents.items | ||
listPopularArtist?.addAll(PopularArtist.map { | ||
ArtistOut(it.id, it.name, it.visuals?.avatar?.get(1)?.url) | ||
}) | ||
listPopularAlbums?.addAll(PopularAlbums.map { | ||
AlbumOut(it.id, it.name, it.cover[1].url, it.artists.map { artist -> artist.name }) | ||
}) | ||
listChart?.addAll(PopularChart.map { | ||
ChartOut(it.name, it.images?.get(0)?.get(0)?.url, it.id) | ||
}) | ||
|
||
result = ResponseHomeScreenData(listPopularArtist, listPopularAlbums, listChart) | ||
return result | ||
// for (i in listPopularArtist!!) { | ||
// println("${i.id} - ${i.name} - ${i.image}") | ||
// } | ||
// for (i in listPopularAlbums!!) { | ||
// println("${i.id} - ${i.name} - ${i.image} ") | ||
// for( j in i.listArtist) { | ||
// println("${j}") | ||
// } | ||
// } | ||
|
||
} else { | ||
println("Error: Request was not successful") | ||
} | ||
} catch (e: Exception) { | ||
println("Error parsing JSON: ${e.message}") | ||
} | ||
} else { | ||
println("Error: JSON response is null.") | ||
} | ||
} else { | ||
println("Request failed with code: ${response.code}") | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} finally { | ||
return result | ||
} | ||
|
||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/example/harmonyhub/data/repository/HomeScreenRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.harmonyhub.data.repository | ||
|
||
import com.example.harmonyhub.data.network.ResponseHomeScreenData | ||
|
||
interface HomeScreenRepo { | ||
suspend fun updatePopularItem() : ResponseHomeScreenData? | ||
} |
78 changes: 78 additions & 0 deletions
78
app/src/main/java/com/example/harmonyhub/ui/components/AlbumCard.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.example.harmonyhub.ui.components | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextOverflow.Companion.Ellipsis | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import coil.compose.AsyncImage | ||
import coil.request.ImageRequest | ||
import com.example.harmonyhub.ui.theme.NotoSans | ||
|
||
@Composable | ||
fun AlbumCard(songName: String, albumImg: String, id : String, listArtist: List<String>) { | ||
Surface( | ||
modifier = Modifier | ||
.size(width = 125.dp, height = 180.dp) | ||
.clickable { }, | ||
color = Color.Transparent | ||
) { | ||
Column(modifier = Modifier.padding(4.dp)) | ||
{ | ||
Box( | ||
modifier = Modifier.size(width = 125.dp, height = 125.dp) | ||
) { | ||
AsyncImage( | ||
model = ImageRequest.Builder(context = LocalContext.current) | ||
.data(albumImg) | ||
.crossfade(true) | ||
.build(), | ||
error = painterResource(com.example.harmonyhub.R.drawable.ic_broken_image), | ||
placeholder = painterResource(id = com.example.harmonyhub.R.drawable.loading_img), | ||
contentDescription = "Photo", | ||
contentScale = ContentScale.Crop, | ||
modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(12.dp)), | ||
) | ||
} | ||
Spacer(modifier = Modifier.height(4.dp)) | ||
Text( | ||
text = songName, | ||
style = TextStyle( | ||
fontFamily = NotoSans, | ||
fontWeight = FontWeight.SemiBold, | ||
fontSize = 16.sp | ||
), | ||
maxLines = 1, | ||
overflow = Ellipsis | ||
) | ||
val joinedNames = listArtist.joinToString(separator = ", ") | ||
Text( | ||
text = joinedNames, | ||
style = TextStyle( | ||
fontFamily = NotoSans, | ||
fontSize = 14.sp | ||
), | ||
color = Color.Gray, | ||
maxLines = 1, overflow = Ellipsis | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.