Skip to content

Commit

Permalink
[nunu/#28] feat: 6주차 구현3
Browse files Browse the repository at this point in the history
- Play 버튼 클릭시 MiniPlayer에 동기화
- SongActivity intent 관련 문제 해결
  • Loading branch information
Ssamssamukja committed May 21, 2024
1 parent 9e62555 commit 2791d2e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
16 changes: 16 additions & 0 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 @@ -14,10 +14,14 @@ import com.example.umc_6th.adapter.ViewPagerAdapter
import com.example.umc_6th.databinding.FragmentHomeBinding
import me.relex.circleindicator.CircleIndicator3
import android.os.Looper
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider


class HomeFragment : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
private lateinit var viewModel: SharedViewModel
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
private lateinit var handler: Handler
Expand All @@ -27,6 +31,7 @@ class HomeFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel = ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)

return binding.root
Expand Down Expand Up @@ -62,8 +67,19 @@ class HomeFragment : Fragment() {
}
}
startAutoSlide()

binding.imgSecondAlbumPlay1.setOnClickListener(){
viewModel.selectedTitle.value = binding.txSecondTitle1.getText().toString()
viewModel.selectedArtist.value = binding.txSecondArtist1.getText().toString()
}
}

class SharedViewModel : ViewModel() {
val selectedTitle = MutableLiveData<String>()
val selectedArtist = MutableLiveData<String>()
}


private fun startAutoSlide() {
handler.postDelayed(runnable, 3000) // 3초에 한번씩 페이지 이동
}
Expand Down
23 changes: 13 additions & 10 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 @@ -7,12 +7,14 @@ import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.umc_6th.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var resultLauncher: ActivityResultLauncher<Intent>

private lateinit var viewModel: HomeFragment.SharedViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -24,8 +26,6 @@ class MainActivity : AppCompatActivity() {
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val albumTitle = result.data?.getStringExtra("albumTitle") ?: "No Title Provided"
Toast.makeText(this, albumTitle, Toast.LENGTH_LONG).show()
val elapsedSeconds = result.data?.getIntExtra("elapsedSeconds", 0) ?: 0
updateSeekBar(elapsedSeconds)
}
Expand All @@ -39,12 +39,20 @@ class MainActivity : AppCompatActivity() {

binding.layoutPlayContainer.setOnClickListener(){
val intent = Intent(this, SongActivity::class.java)
intent.putExtra("songTitle", binding.txPlayTitle.text)
intent.putExtra("songArtist", binding.txPlayArtist.text)
intent.putExtra("songTitle", binding.txPlayTitle.text.toString())
intent.putExtra("songArtist", binding.txPlayArtist.text.toString())
resultLauncher.launch(intent)
}

setupButtonListeners()
viewModel = ViewModelProvider(this).get(HomeFragment.SharedViewModel::class.java)
viewModel.selectedTitle.observe(this, Observer {
binding.txPlayTitle.text = it
})

viewModel.selectedArtist.observe(this, Observer {
binding.txPlayArtist.text = it
})
}

private fun setupButtonListeners() {
Expand All @@ -55,11 +63,6 @@ class MainActivity : AppCompatActivity() {
binding.mainSeekBar.isEnabled = true
}
}

binding.layoutPlayContainer.setOnClickListener {
val intent = Intent(this, SongActivity::class.java)
resultLauncher.launch(intent)
}
}

private fun updateSeekBar(elapsedSeconds: Int) {
Expand Down
3 changes: 3 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent
import android.graphics.PorterDuff
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.graphics.Color
Expand All @@ -30,6 +31,8 @@ class SongActivity : AppCompatActivity() {
val songTitle = intent.getStringExtra("songTitle")
val songArtist = intent.getStringExtra("songArtist")

Log.d("SongActivity", "Received title: $songTitle, artist: $songArtist")

binding.txSongTitle.text = songTitle
binding.txSongArtist.text = songArtist

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.umc_6th.adapter

class AlbumRecyclerAdapter {
}
24 changes: 12 additions & 12 deletions UMC_6th/app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/imgSecondTitle1"
android:id="@+id/txSecondTitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
Expand All @@ -168,13 +168,13 @@
app:layout_constraintTop_toBottomOf="@id/imgSecondAlbum1" />

<TextView
android:id="@+id/imgSecondArtist1"
android:id="@+id/txSecondArtist1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="IU"
app:layout_constraintStart_toStartOf="@id/imgSecondTitle1"
app:layout_constraintTop_toBottomOf="@id/imgSecondTitle1" />
app:layout_constraintStart_toStartOf="@id/txSecondTitle1"
app:layout_constraintTop_toBottomOf="@id/txSecondTitle1" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -203,7 +203,7 @@
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/imgSecondTitle2"
android:id="@+id/txSecondTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
Expand All @@ -214,13 +214,13 @@
app:layout_constraintTop_toBottomOf="@id/imgSecondAlbum2" />

<TextView
android:id="@+id/imgSecondArtist2"
android:id="@+id/txSecondArtist2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="아티스트"
app:layout_constraintStart_toStartOf="@id/imgSecondTitle2"
app:layout_constraintTop_toBottomOf="@id/imgSecondTitle2" />
app:layout_constraintStart_toStartOf="@id/txSecondTitle2"
app:layout_constraintTop_toBottomOf="@id/txSecondTitle2" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -249,7 +249,7 @@
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/imgSecondTitle3"
android:id="@+id/txSecondTitle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
Expand All @@ -260,13 +260,13 @@
app:layout_constraintTop_toBottomOf="@id/imgSecondAlbum3" />

<TextView
android:id="@+id/imgSecondArtist3"
android:id="@+id/txSecondArtist3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="아티스트"
app:layout_constraintStart_toStartOf="@id/imgSecondTitle3"
app:layout_constraintTop_toBottomOf="@id/imgSecondTitle3" />
app:layout_constraintStart_toStartOf="@id/txSecondTitle3"
app:layout_constraintTop_toBottomOf="@id/txSecondTitle3" />
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down

0 comments on commit 2791d2e

Please sign in to comment.