diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumApi.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumApi.kt new file mode 100644 index 0000000..4ac827d --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumApi.kt @@ -0,0 +1,9 @@ +package com.example.umc_6th + +import retrofit2.Call +import retrofit2.http.GET + +interface AlbumApi { + @GET("/albums") + fun getAlbums(): Call +} diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumResponse.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumResponse.kt new file mode 100644 index 0000000..5c92a87 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumResponse.kt @@ -0,0 +1,20 @@ +package com.example.umc_6th + +import com.google.gson.annotations.SerializedName + +data class AlbumResponse( @SerializedName("isSuccess") val isSuccess: Boolean, + @SerializedName("code") val code: Int, + @SerializedName("message") val message: String, + @SerializedName("result") val result: TodayAlbumResult) + + +data class TodayAlbumResult( + @SerializedName("albums") val albums: List +) + +data class TodayAlbum( + @SerializedName("albumIdx") val albumIdx: Int, + @SerializedName("singer") val singer: String, + @SerializedName("title") val title:String, + @SerializedName("coverImgUrl") val coverImgUrl : String +) diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumService.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumService.kt new file mode 100644 index 0000000..d3f1e81 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumService.kt @@ -0,0 +1,40 @@ +package com.example.umc_6th + +import android.util.Log +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +class AlbumService { + private lateinit var homeAlbumView: HomeAlbumView + + fun setHomeAlbumView(homeAlbumView: HomeAlbumView) { + this.homeAlbumView = homeAlbumView + } + + fun getAlbum() { + homeAlbumView.onGetAlbumLoading() + + RetrofitInstance.albumApi.getAlbums().enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful && response.code() == 200) { + val albumResponse: AlbumResponse = response.body()!! + + Log.d("ALBUM-RESPONSE", albumResponse.toString()) + + when (val code = albumResponse.code) { + 1000 -> { + homeAlbumView.onGetAlbumSuccess(code, albumResponse.result!!) + } + + else -> homeAlbumView.onGetAlbumFailure(code, albumResponse.message) + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + homeAlbumView.onGetAlbumFailure(400, "네트워크 오류가 발생했습니다.") + } + }) + } +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/HomeAlbumView.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeAlbumView.kt new file mode 100644 index 0000000..f45ad47 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeAlbumView.kt @@ -0,0 +1,7 @@ +package com.example.umc_6th + +interface HomeAlbumView { + fun onGetAlbumLoading() + fun onGetAlbumSuccess(code: Int, result: TodayAlbumResult) + fun onGetAlbumFailure(code: Int, message: String) +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt index b02a592..1a96c20 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt @@ -20,11 +20,13 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import com.example.umc_6th.adapter.AlbumRecyclerAdapter +import com.example.umc_6th.adapter.SongRecyclerViewAdapter import com.google.gson.Gson -class HomeFragment : Fragment() { - // 여기에 Fragment의 구현 내용을 작성합니다. +class HomeFragment : Fragment() + //, HomeAlbumView +{ private lateinit var viewModel: SharedViewModel private var _binding: FragmentHomeBinding? = null private val binding get() = _binding!! @@ -32,6 +34,8 @@ class HomeFragment : Fragment() { private lateinit var runnable: Runnable private var albumDatas = ArrayList() private lateinit var songDB: SongDatabase + // 앨범 api 연결 + private lateinit var todayAlbumAdapter: AlbumRecyclerAdapter override fun onCreateView( @@ -188,4 +192,34 @@ class HomeFragment : Fragment() { class FragmentHomeBanner : Fragment(R.layout.fragment_home_banner1) { // 필요한 경우 여기에 로직 추가 } + +/* + // album api에서 가져오기 + private fun loadAlbums() { + val albumService = AlbumService() + albumService.setHomeAlbumView(this) + albumService.getAlbum() + } + + override fun onGetAlbumLoading() { + binding.lookLoadingPb.visibility = View.VISIBLE + } + + private fun initRecyclerView(result: TodayAlbumResult) { + todayAlbumAdapter = AlbumRecyclerAdapter(requireContext(), result) + + binding.homeTodayMusicAlbum.adapter = todayAlbumAdapter + } + + override fun onGetAlbumSuccess(code: Int, result: TodayAlbumResult) { + binding.lookLoadingPb.visibility = View.GONE + initRecyclerView(result) + } + + override fun onGetAlbumFailure(code: Int, message: String) { + binding.lookLoadingPb.visibility = View.GONE + Log.d("HOME-FRAG/ALBUM-RESPONSE", message) + } + + */ } diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt index 2a1d5fb..0effed7 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt @@ -13,5 +13,6 @@ class RetrofitInstance { } val authApi = retrofit.create(AuthApi::class.java) val songApi = retrofit.create(SongApi::class.java) + val albumApi = retrofit.create(AlbumApi::class.java) } } \ No newline at end of file