diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt index 628ba58..66a1b5e 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt @@ -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 @@ -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 @@ -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() + val selectedArtist = MutableLiveData() + } + + private fun startAutoSlide() { handler.postDelayed(runnable, 3000) // 3초에 한번씩 페이지 이동 } diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt index d0b96ba..88a9034 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt @@ -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 - + private lateinit var viewModel: HomeFragment.SharedViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -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) } @@ -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() { @@ -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) { diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt index 1a32b5d..d95f3eb 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt @@ -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 @@ -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 diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/adapter/AlbumRecyclerAdapter.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/adapter/AlbumRecyclerAdapter.kt new file mode 100644 index 0000000..2450dd1 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/adapter/AlbumRecyclerAdapter.kt @@ -0,0 +1,4 @@ +package com.example.umc_6th.adapter + +class AlbumRecyclerAdapter { +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/res/layout/fragment_home.xml b/UMC_6th/app/src/main/res/layout/fragment_home.xml index 7b9e877..115e881 100644 --- a/UMC_6th/app/src/main/res/layout/fragment_home.xml +++ b/UMC_6th/app/src/main/res/layout/fragment_home.xml @@ -157,7 +157,7 @@ app:layout_constraintEnd_toEndOf="parent" /> + app:layout_constraintStart_toStartOf="@id/txSecondTitle1" + app:layout_constraintTop_toBottomOf="@id/txSecondTitle1" /> + app:layout_constraintStart_toStartOf="@id/txSecondTitle2" + app:layout_constraintTop_toBottomOf="@id/txSecondTitle2" /> + app:layout_constraintStart_toStartOf="@id/txSecondTitle3" + app:layout_constraintTop_toBottomOf="@id/txSecondTitle3" />