Skip to content

Commit

Permalink
[simba/#47] feat:: 9주차 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed Jun 24, 2024
1 parent e75c701 commit e6eecd2
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 324 deletions.
99 changes: 27 additions & 72 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LookFragment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -7,36 +8,20 @@ import android.widget.ScrollView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.umc_6th.FloChartResult
import com.example.umc_6th.LockerAlbumRVAdapter
import com.example.umc_6th.LookView
import com.example.umc_6th.R
import com.example.umc_6th.Song
import com.example.umc_6th.SongDatabase
import com.example.umc_6th.SongRVAdapter
import com.example.umc_6th.SongService
import com.example.umc_6th.databinding.FragmentLookBinding

class LookFragment : Fragment() {
class LookFragment : Fragment(),LookView {

lateinit var binding: FragmentLookBinding
private lateinit var songDB: SongDatabase

private lateinit var chartBtn : Button
private lateinit var videoBtn : Button
private lateinit var genreBtn : Button
private lateinit var situationBtn : Button
private lateinit var audioBtn : Button
private lateinit var atmosphereBtn : Button

private lateinit var buttonList: List<Button>

private lateinit var chartTv : TextView
private lateinit var videoTv : TextView
private lateinit var genreTv : TextView
private lateinit var situationTv : TextView
private lateinit var audioTv : TextView
private lateinit var atmosphereTv : TextView

private lateinit var textList: List<TextView>

lateinit var scrollView : ScrollView
private lateinit var floCharAdapter: SongRVAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -47,68 +32,38 @@ class LookFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
binding = FragmentLookBinding.inflate(inflater, container, false)
songDB = SongDatabase.getInstance(requireContext())!!

// 스크롤 뷰 초기화
scrollView = binding.lookSv

// 버튼 초기화
chartBtn = binding.lookChartBtn
videoBtn = binding.lookVideoBtn
genreBtn = binding.lookGenreBtn
situationBtn = binding.lookSituationBtn
audioBtn = binding.lookAudioBtn
atmosphereBtn = binding.lookAtmostphereBtn

buttonList = listOf(chartBtn, videoBtn, genreBtn, situationBtn, audioBtn, atmosphereBtn)

// 텍스트 초기화
chartTv = binding.lookChartTv
videoTv = binding.lookVideoTv
genreTv = binding.lookGenreTv
situationTv = binding.lookSituationTv
audioTv = binding.lookAudioTv
atmosphereTv = binding.lookAtmostphereTv

textList = listOf(chartTv, videoTv, genreTv, situationTv, audioTv, atmosphereTv)

setButtonClickListeners()

return binding.root
}

override fun onStart() {
super.onStart()
initRecyclerview()
getSongs()
}

private fun initRecyclerview(){
val recyclerView = binding.lookChartSongRv
recyclerView.layoutManager = LinearLayoutManager(requireActivity())
val lookAlbumRVAdapter = LockerAlbumRVAdapter()
private fun initRecyclerView(result: FloChartResult){
floCharAdapter = SongRVAdapter(requireContext(), result)

binding.lookChartSongRv.adapter = lookAlbumRVAdapter
lookAlbumRVAdapter.addSongs(songDB.songDao().getSongs() as ArrayList<Song>)
binding.lookFloChartRv.adapter = floCharAdapter
}
private fun getSongs() {
val songService = SongService()
songService.setLookView(this)

private fun setButtonClickListeners() {
for (i in buttonList.indices) {
val button = buttonList[i]
songService.getSongs()

}

override fun onGetSongLoading() {
binding.lookLoadingPb.visibility = View.VISIBLE
}

button.setOnClickListener {
initButton(i)
}
}
override fun onGetSongSuccess(code: Int, result: FloChartResult) {
binding.lookLoadingPb.visibility = View.GONE
initRecyclerView(result)
}

private fun initButton(idx : Int) {
for(presentBtn : Button in buttonList) {
if(presentBtn == buttonList[idx]) {
presentBtn.setBackgroundResource(R.drawable.selected_button)
} else {
presentBtn.setBackgroundResource(R.drawable.not_selected_button)
}
}
scrollView.smoothScrollTo(0, textList[idx].top)
override fun onGetSongFailure(code: Int, message: String) {
binding.lookLoadingPb.visibility = View.GONE
Log.d("LOOK-FRAG/SONG-RESPONSE", message)
}
}
7 changes: 7 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LookView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.umc_6th

interface LookView {
fun onGetSongLoading()
fun onGetSongSuccess(code: Int, result: FloChartResult)
fun onGetSongFailure(code: Int, message: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class RetrofitInstance {
.build()
}
val authApi = retrofit.create(AuthApi::class.java)
val songApi = retrofit.create(SongApi::class.java)
}
}
9 changes: 9 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongApi.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 SongApi {
@GET("/songs")
fun getSongs(): Call<SongResponse>
}
56 changes: 56 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongRVAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.umc_6th

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.umc_6th.databinding.ItemSongBinding

class SongRVAdapter(val context: Context, val result : FloChartResult) : RecyclerView.Adapter<SongRVAdapter.ViewHolder>() {


override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): SongRVAdapter.ViewHolder {
val binding: ItemSongBinding = ItemSongBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false)

return ViewHolder(binding)
}

override fun onBindViewHolder(holder: SongRVAdapter.ViewHolder, position: Int) {
//holder.bind(result.songs[position])

if(result.songs[position].coverImgUrl == "" || result.songs[position].coverImgUrl == null){

} else {
Log.d("image",result.songs[position].coverImgUrl )
Glide.with(context).load(result.songs[position].coverImgUrl).into(holder.coverImg)
}
holder.title.text = result.songs[position].title
holder.singer.text = result.songs[position].singer

}

override fun getItemCount(): Int = result.songs.size


inner class ViewHolder(val binding: ItemSongBinding) : RecyclerView.ViewHolder(binding.root){

val coverImg : ImageView = binding.itemSongImgIv
val title : TextView = binding.itemSongTitleTv
val singer : TextView = binding.itemSongSingerTv

}

interface MyItemClickListener{
fun onRemoveSong(songId: Int)
}

private lateinit var mItemClickListener: MyItemClickListener

fun setMyItemClickListener(itemClickListener: MyItemClickListener){
mItemClickListener = itemClickListener
}
}
22 changes: 22 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.umc_6th

import com.google.gson.annotations.SerializedName

data class SongResponse(
@SerializedName("isSuccess") val isSuccess: Boolean,
@SerializedName("code") val code: Int,
@SerializedName("message") val message: String,
@SerializedName("result") val result: FloChartResult
)

data class FloChartResult(
@SerializedName("songs") val songs: List<FloChartSongs>
)

data class FloChartSongs(
@SerializedName("songIdx") val songIdx: Int,
@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/SongService.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 SongService {
private lateinit var lookView: LookView

fun setLookView(lookView: LookView) {
this.lookView = lookView
}

fun getSongs() {
lookView.onGetSongLoading()

RetrofitInstance.songApi.getSongs().enqueue(object : Callback<SongResponse> {
override fun onResponse(call: Call<SongResponse>, response: Response<SongResponse>) {
if (response.isSuccessful && response.code() == 200) {
val songResponse: SongResponse = response.body()!!

Log.d("SONG-RESPONSE", songResponse.toString())

when (val code = songResponse.code) {
1000 -> {
lookView.onGetSongSuccess(code, songResponse.result!!)
}

else -> lookView.onGetSongFailure(code, songResponse.message)
}
}
}

override fun onFailure(call: Call<SongResponse>, t: Throwable) {
lookView.onGetSongFailure(400, "네트워크 오류가 발생했습니다.")
}
})
}
}
Loading

0 comments on commit e6eecd2

Please sign in to comment.