Skip to content

Commit

Permalink
[simba/#28] feat:: 6주차 구현 (에러 존재)
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed May 21, 2024
1 parent b4ee152 commit 80d1f60
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 75 deletions.
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>
14 changes: 10 additions & 4 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumRVAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class AlbumRVAdapter(private val albumList:ArrayList<Album>) : RecyclerView.Adap
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
Expand All @@ -35,8 +40,12 @@ class AlbumRVAdapter(private val albumList:ArrayList<Album>) : RecyclerView.Adap

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

override fun getItemCount(): Int = albumList.size
Expand All @@ -48,7 +57,4 @@ class AlbumRVAdapter(private val albumList:ArrayList<Album>) : RecyclerView.Adap
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)
}
}
}
12 changes: 11 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.ArrayList
import java.util.Timer
import java.util.TimerTask

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

lateinit var binding: FragmentHomeBinding

Expand All @@ -29,6 +29,13 @@ class HomeFragment : Fragment() {
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?
Expand Down Expand Up @@ -61,6 +68,9 @@ class HomeFragment : Fragment() {
override fun onRemoveAlbum(position: Int) {
albumRVAdapter.removeItem(position)
}
override fun onPlayAlbum(album: Album) {
sendData(album)
}
})

val bannerAdapter = BannerVPAdapter(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.umc_6th

import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Recycler
import com.example.umc_6th.databinding.ItemLockerAlbumBinding

class LockerAlbumRVAdapter (private var albumList: ArrayList<Album>) : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHolder>(){

private val switchStatus = SparseBooleanArray()
private lateinit var itemClickListener : OnItemClickListener

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): LockerAlbumRVAdapter.ViewHolder {
val binding: ItemLockerAlbumBinding = ItemLockerAlbumBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: LockerAlbumRVAdapter.ViewHolder, position: Int) {
holder.bind(albumList[position])
holder.itemView.setOnClickListener {
itemClickListener.onItemClick(albumList[position])
}

holder.binding.itemLockerAlbumMoreIv.setOnClickListener {
itemClickListener.onRemoveAlbum(position)
}

val switch = holder.binding.switchRV
switch.isChecked = switchStatus[position]
switch.setOnClickListener {
if(switch.isChecked){
switchStatus.put(position,true)
}
else{
switchStatus.put(position,false)
}
notifyItemChanged(position)
}
}

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


fun setItemClickListener(onItemClickListener: OnItemClickListener) {
this.itemClickListener = onItemClickListener
}

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

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

override fun getItemCount(): Int = albumList.size

inner class ViewHolder(val binding:ItemLockerAlbumBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(album: Album){
binding.itemLockerAlbumTitleTv.text = album.title
binding.itemLockerAlbumSingerTv.text = album.singer
binding.itemLockerAlbumCoverImgIv.setImageResource(album.coverImg!!)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ import com.google.android.material.tabs.TabLayoutMediator
class LockerFragment : Fragment() {

lateinit var binding: FragmentLockerBinding
private var information = arrayListOf("저장한 곡", "음악파일", "저장앨범")
private var information = arrayListOf("저장한곡", "음악파일", "저장앨범")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentLockerBinding.inflate(inflater, container, false)

val lockerAdapter = LockerVPAdapter(this)
binding.lockerContentVp.adapter = lockerAdapter
TabLayoutMediator(binding.lockerContentTb,binding.lockerContentVp){
tab,position ->
tab.text = information[position]
}.attach()

return binding.root
}

}
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ class MainActivity : AppCompatActivity() {
binding.mainMiniplayerSingerTv.text = song.singer
binding.mainMiniplayerProgressSb.progress = (song.second * 100000 / song.playTime)
}

fun updateMainPlayerCl(album: Album) {
binding.mainMiniplayerTitleTv.text = album.title
binding.mainMiniplayerSingerTv.text = album.singer
binding.mainMiniplayerProgressSb.progress = 0
}
}
54 changes: 54 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SavedSongFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,72 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.umc_6th.databinding.FragmentLockerSavedsongBinding
import com.google.gson.Gson

class SavedSongFragment : Fragment() {

private var albumDatas = ArrayList<Album>()
lateinit var binding : FragmentLockerSavedsongBinding

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

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

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))
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))
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 lockerAlbumRVAdapter = LockerAlbumRVAdapter(albumDatas)
binding.lockerMusicAlbumRv.adapter = lockerAlbumRVAdapter
binding.lockerMusicAlbumRv.layoutManager = LinearLayoutManager(requireActivity())

lockerAlbumRVAdapter.setItemClickListener(object : LockerAlbumRVAdapter.OnItemClickListener {
override fun onItemClick(album: Album) {
changeAlbumFragment(album)
}

override fun onRemoveAlbum(position: Int) {
lockerAlbumRVAdapter.removeItem(position)
}
})
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 albumToJson = gson.toJson(album)
putString("album", albumToJson)
}
})
.commitAllowingStateLoss()
}
}
Loading

0 comments on commit 80d1f60

Please sign in to comment.