Skip to content

Commit

Permalink
[nunu/#43] feat: HomeFragment API 연동
Browse files Browse the repository at this point in the history
- RetrofitInstance 추가
- AlbumApi, Album Response, AlbumService 구현
- HomeFragment 연결
  • Loading branch information
Ssamssamukja committed Jun 18, 2024
1 parent 7473c1e commit b8d6929
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
9 changes: 9 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.umc_6th

import retrofit2.Call
import retrofit2.http.GET

interface AlbumApi {
@GET("/albums")
fun getAlbums(): Call<AlbumResponse>
}
20 changes: 20 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumResponse.kt
Original file line number Diff line number Diff line change
@@ -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<TodayAlbum>
)

data class TodayAlbum(
@SerializedName("albumIdx") val albumIdx: Int,
@SerializedName("singer") val singer: String,
@SerializedName("title") val title:String,
@SerializedName("coverImgUrl") val coverImgUrl : String
)
40 changes: 40 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumService.kt
Original file line number Diff line number Diff line change
@@ -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<AlbumResponse> {
override fun onResponse(call: Call<AlbumResponse>, response: Response<AlbumResponse>) {
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<AlbumResponse>, t: Throwable) {
homeAlbumView.onGetAlbumFailure(400, "네트워크 오류가 발생했습니다.")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 36 additions & 2 deletions UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ 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!!
private lateinit var handler: Handler
private lateinit var runnable: Runnable
private var albumDatas = ArrayList<Album>()
private lateinit var songDB: SongDatabase
// 앨범 api 연결
private lateinit var todayAlbumAdapter: AlbumRecyclerAdapter


override fun onCreateView(
Expand Down Expand Up @@ -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)
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b8d6929

Please sign in to comment.