Skip to content

Commit

Permalink
Simba/#28 :: feat 6주차 구현 (#31)
Browse files Browse the repository at this point in the history
* [simba/#28] feat::리사이클러뷰 구현

* [simba/#28] feat:: recyclerView->albumFragment 클릭 이벤트 구현

* [simba/#28] feat:: recyclerView->albumFragment 클릭 이벤트 구현 with 데이터

* [simba/#28] feat:: 6주차 구현 완료

* [simba/#28] feat:: 6주차 구현 (에러 존재)

* [simba/#28] feat:: 6주차 구현
  • Loading branch information
BAEK0111 authored May 21, 2024
1 parent e693a85 commit 518ade4
Show file tree
Hide file tree
Showing 22 changed files with 1,010 additions and 255 deletions.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

12 changes: 10 additions & 2 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -12,20 +16,24 @@
android:supportsRtl="true"
android:theme="@style/Theme.UMC_6th"
tools:targetApi="31">
<service
android:name=".ForegroundService"
android:enabled="true"
android:exported="true"></service>

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".SongActivity"
android:exported="true"/>
android:exported="true" />
</application>

</manifest>
10 changes: 10 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.umc_6th

import java.util.ArrayList

data class Album(
var title: String? = "",
var singer: String? = "",
var coverImg: Int? = null,
var songs: ArrayList<Song>? = null
)
12 changes: 12 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import com.example.umc_6th.databinding.FragmentAlbumBinding
import com.example.umc_6th.databinding.FragmentAlbumBinding.inflate
import com.example.umc_6th.databinding.FragmentSongBinding
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson

class AlbumFragment : Fragment() {

lateinit var binding : FragmentAlbumBinding
private var gson : Gson = Gson()

private val information = arrayListOf("수록곡","상세정보","영상")
override fun onCreateView(
Expand All @@ -24,6 +26,10 @@ class AlbumFragment : Fragment() {
): View? {
binding = inflate(inflater,container,false)

val albumJson = arguments?.getString("album")
val album = gson.fromJson(albumJson, Album::class.java)
setInit(album)

binding.albumBackIv.setOnClickListener{
(context as MainActivity).supportFragmentManager.beginTransaction().
replace(R.id.main_frm,HomeFragment()).
Expand Down Expand Up @@ -72,4 +78,10 @@ class AlbumFragment : Fragment() {
// }
return binding.root
}
private fun setInit(album: Album){
binding.albumAlbumIv.setImageResource(album.coverImg!!)
binding.albumMusicTitleTv.text = album.title.toString()
binding.albumSingerNameTv.text = album.singer.toString()
}

}
60 changes: 60 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumRVAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.umc_6th

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.umc_6th.databinding.ItemAlbumBinding

class AlbumRVAdapter(private val albumList:ArrayList<Album>) : RecyclerView.Adapter<AlbumRVAdapter.ViewHolder>() {

interface MyItemClickListener{
fun onItemClick(album: Album)
fun onRemoveAlbum(position: Int)
fun onPlayAlbum(album: Album)
}

interface CommunicationInterface{
fun sendData(album: Album)
}

private lateinit var mItemClickListener : MyItemClickListener
fun setMyItemClickListener(itemClickListener: MyItemClickListener){
mItemClickListener = itemClickListener
}

fun addItem(album: Album){
albumList.add(album)
notifyDataSetChanged()
}

fun removeItem(position: Int){
albumList.removeAt(position)
notifyDataSetChanged()
}

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

return ViewHolder(binding)
}

override fun onBindViewHolder(holder: AlbumRVAdapter.ViewHolder, position: Int) {
holder.bind(albumList[position])
holder.itemView.setOnClickListener{
mItemClickListener.onItemClick(albumList[position]) }
// holder.binding.itemAlbumTitleTv.setOnClickListener{mItemClickListener.onRemoveAlbum(position)}
holder.binding.itemAlbumPlayImgIv.setOnClickListener{
mItemClickListener.onPlayAlbum(albumList[position])
}
}

override fun getItemCount(): Int = albumList.size

inner class ViewHolder(val binding:ItemAlbumBinding): RecyclerView.ViewHolder(binding.root){
fun bind(album: Album){
binding.itemAlbumTitleTv.text = album.title
binding.itemAlbumSingerTv.text = album.singer
binding.itemAlbumCoverImgIv.setImageResource(album.coverImg!!)
}
}
}
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Constant.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.umc_6th

object Constant {
const val CHANNEL_ID = "ch123"
const val MUSIC_NOTIFICATION_ID = 123
}
62 changes: 62 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/ForegroundService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.umc_6th

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi

class ForegroundService : Service() {

override fun onBind(intent: Intent): IBinder? {
return null
}

override fun onCreate() {
super.onCreate()
createNotificationChannel()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
showNotification()
}
return START_STICKY
}

@SuppressLint("ForegroundServiceType")
@RequiresApi(Build.VERSION_CODES.O)
private fun showNotification() {
val notificationIntent = Intent(this,SongActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,0,notificationIntent,PendingIntent.FLAG_IMMUTABLE
)
val notification = Notification
.Builder(this, Constant.CHANNEL_ID)
.setContentText("현재 음악이 재생 중 입니다.")
.setSmallIcon(R.drawable.ic_flo_logo)
.setContentIntent(pendingIntent)
.build()

startForeground(Constant.MUSIC_NOTIFICATION_ID, notification)
}

private fun createNotificationChannel() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val serviceChannel = NotificationChannel(
Constant.CHANNEL_ID,"Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(
NotificationManager::class.java
)

manager.createNotificationChannel(serviceChannel)
}
}
}
58 changes: 53 additions & 5 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 @@ -9,34 +9,69 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.example.umc_6th.databinding.FragmentHomeBinding
import com.google.gson.Gson
import java.util.ArrayList
import java.util.Timer
import java.util.TimerTask

class HomeFragment : Fragment() {
class HomeFragment : Fragment(), AlbumRVAdapter.CommunicationInterface {

lateinit var binding: FragmentHomeBinding

private val timer = Timer()
private val handler = Handler(Looper.getMainLooper())
private var albumDatas = ArrayList<Album>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun sendData(album: Album) {
if (activity is MainActivity) {
val activity = activity as MainActivity
activity.updateMainPlayerCl(album)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentHomeBinding.inflate(inflater,container,false)

binding.homeAlbumImg01Iv1.setOnClickListener{
setFragmentResult("TitleInfo", bundleOf("title" to binding.titleLilac.text.toString()))
setFragmentResult("SingerInfo", bundleOf("singer" to binding.singerIu.text.toString()))
(context as MainActivity).supportFragmentManager.beginTransaction().replace(R.id.main_frm,AlbumFragment()).commitAllowingStateLoss()
// binding.homeAlbumImg01Iv1.setOnClickListener{
// setFragmentResult("TitleInfo", bundleOf("title" to binding.titleLilac.text.toString()))
// setFragmentResult("SingerInfo", bundleOf("singer" to binding.singerIu.text.toString()))
// (context as MainActivity).supportFragmentManager.beginTransaction().replace(R.id.main_frm,AlbumFragment()).commitAllowingStateLoss()
// }

albumDatas.apply{
add(Album("Butter","방탄소년단(BTS)",R.drawable.img_album_exp))
add(Album("Lilac","아이유(IU)",R.drawable.img_album_exp2))
add(Album("Next Level","에스파(AESPA)",R.drawable.img_album_exp3))
add(Album("Boy with Luv","방탄소년단(BTS)",R.drawable.img_album_exp4))
add(Album("BBoom BBoom","모모랜드(MOMOLAND)",R.drawable.img_album_exp5))
add(Album("Weekend","태연(Tae Yeon)",R.drawable.img_album_exp6))
}

val albumRVAdapter = AlbumRVAdapter(albumDatas)
binding.homeTodayMusicAlbumRv.adapter = albumRVAdapter
binding.homeTodayMusicAlbumRv.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)

albumRVAdapter.setMyItemClickListener(object : AlbumRVAdapter.MyItemClickListener{
override fun onItemClick(album: Album) {
changeAlbumFragment(album)
}
override fun onRemoveAlbum(position: Int) {
albumRVAdapter.removeItem(position)
}
override fun onPlayAlbum(album: Album) {
sendData(album)
}
})

val bannerAdapter = BannerVPAdapter(this)
bannerAdapter.addFragment(BannerFragment(R.drawable.img_home_viewpager_exp))
Expand All @@ -60,6 +95,19 @@ class HomeFragment : Fragment() {

return binding.root
}

private fun changeAlbumFragment(album: Album) {
(context as MainActivity).supportFragmentManager.beginTransaction()
.replace(R.id.main_frm, AlbumFragment().apply {
arguments = Bundle().apply {
val gson = Gson()
val albumJson = gson.toJson(album)
putString("album", albumJson)
}
})
.commitAllowingStateLoss()
}

private fun autoSlide(adapter: BannerVPAdapter) {
timer.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
Expand Down
Loading

0 comments on commit 518ade4

Please sign in to comment.