Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/src/main/java/com/toyou/toyouandroid/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.toyou.toyouandroid

import android.os.Bundle
import android.view.View
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
Expand All @@ -16,7 +17,8 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
Expand All @@ -25,5 +27,10 @@ class MainActivity : AppCompatActivity() {
findViewById<BottomNavigationView>(R.id.bottom_navi)
.setupWithNavController(navController)


}

fun hideBottomNavigation(state:Boolean){
if(state) binding.bottomNavi.visibility = View.GONE else binding.bottomNavi.visibility=View.VISIBLE
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/com/toyou/toyouandroid/model/CardModel.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.toyou.toyouandroid.model

data class CardModel(val message : String)
data class CardModel(
val message : String,
var isButtonSelected : Boolean = false
)
47 changes: 37 additions & 10 deletions app/src/main/java/com/toyou/toyouandroid/ui/home/CreateFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.toyou.toyouandroid.MainActivity
import com.toyou.toyouandroid.R
import com.toyou.toyouandroid.databinding.FragmentCreateBinding
import com.toyou.toyouandroid.ui.home.adapter.CardAdapter
Expand All @@ -23,6 +27,8 @@ class CreateFragment : Fragment(){
private lateinit var cardAdapter : CardAdapter
private lateinit var cardViewModel: CardViewModel

lateinit var navController: NavController


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -37,12 +43,28 @@ class CreateFragment : Fragment(){
savedInstanceState: Bundle?
): View? {
_binding = FragmentCreateBinding.inflate(inflater, container, false)
cardAdapter = CardAdapter { position ->

cardViewModel.cards.observe(viewLifecycleOwner, Observer { cards ->
Log.d("CreateFragment", "Loading cards: ${cardViewModel.cards.value}") // 디버그 로그 추가
cardAdapter.setCards(cards)
})

cardAdapter = CardAdapter { position, isSelected ->
cardViewModel.updateButtonState(position, isSelected)
Log.d("CreateFragment", "Item clicked at position: $position")
Log.d("CreateFragment", "Item clicked at position: $isSelected, ${cardViewModel.cards.value}")

if (isSelected == true){
binding.nextBtn.isEnabled = true
} else{
binding.nextBtn.isEnabled = false
}

}
//adapter = cardAdapter

cardViewModel.loadCardData()


binding.cardRv.apply {
layoutManager = LinearLayoutManager(requireContext())
Expand All @@ -58,24 +80,29 @@ class CreateFragment : Fragment(){
}


val mainActivity = activity as MainActivity // casting
mainActivity.hideBottomNavigation(true)

cardViewModel = ViewModelProvider(this).get(CardViewModel::class.java)
Log.d("CreateFragment", "ViewModel created: ${cardViewModel}") // 로그 추가

cardViewModel.cards.observe(viewLifecycleOwner, Observer { cards ->
Log.d("CardViewModel", "Loading cards: $cards") // 디버그 로그 추가
cardAdapter.setCards(cards)
})
val root: View = binding.root
return root
}

cardViewModel.loadCardData()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
navController = Navigation.findNavController(view)

binding.backBtn.setOnClickListener {
binding.backBtn.setOnClickListener {
navController.popBackStack()
}
}

val root: View = binding.root
return root
}

override fun onDestroyView() {
super.onDestroyView()
val mainActivity = activity as MainActivity
mainActivity.hideBottomNavigation(false)
_binding = null
}

Expand Down
23 changes: 17 additions & 6 deletions app/src/main/java/com/toyou/toyouandroid/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.toyou.toyouandroid.R
import com.toyou.toyouandroid.databinding.FragmentCreateBinding
import com.toyou.toyouandroid.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {

private var _binding: FragmentCreateBinding? = null
lateinit var navController: NavController

private var _binding: FragmentHomeBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
Expand All @@ -26,16 +31,22 @@ class HomeFragment : Fragment() {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)

_binding = FragmentCreateBinding.inflate(inflater, container, false)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root

/*val textView: TextView = binding.textHome
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}*/
return root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)

binding.textHomeBtn.setOnClickListener {
navController.navigate(R.id.action_navigation_home_to_create_fragment)
}
}


override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.toyou.toyouandroid.R
import com.toyou.toyouandroid.model.CardModel

class CardAdapter(private val onItemClick: (Int) -> Unit) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
class CardAdapter(private val onItemClick: (Int, Boolean) -> Unit) : RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
private var cardList: List<CardModel> = emptyList()

fun setCards(cards : List<CardModel>){
Expand All @@ -30,25 +30,28 @@ class CardAdapter(private val onItemClick: (Int) -> Unit) : RecyclerView.Adapter

override fun getItemCount(): Int = cardList.size

class CardViewHolder(itemView: View, onItemClick: (Int) -> Unit) : RecyclerView.ViewHolder(itemView){
class CardViewHolder(itemView: View, onItemClick: (Int, Boolean) -> Unit) : RecyclerView.ViewHolder(itemView){
private val cardMessageTextView : TextView = itemView.findViewById(R.id.textMessage)
private val button : Button = itemView.findViewById(R.id.button)
private var isSelected: Boolean = false


init {
button.setOnClickListener {
onItemClick(adapterPosition)
button.isEnabled != button.isEnabled
updateButtonBackground(isSelected)
isSelected = !isSelected
//버튼 클릭 업데이트 후 onItemClick 함수 호출하기!
onItemClick(adapterPosition, isSelected)
}
}


fun bind(card : CardModel){
cardMessageTextView.text = card.message
updateButtonBackground(button.isEnabled)
//card.isButtonSelected = isSelected
}
private fun updateButtonBackground(isEnabled: Boolean) {
val backgroundRes = if (isEnabled) R.drawable.create_unclicked_btn else R.drawable.create_clicked_btn
private fun updateButtonBackground(isSelected: Boolean) {
val backgroundRes = if (isSelected) R.drawable.create_clicked_btn else R.drawable.create_unclicked_btn
button.setBackgroundResource(backgroundRes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ class CardViewModel : ViewModel(){

_cards.value = sampleCards
}

fun updateButtonState(position : Int, isSelected : Boolean){
_cards.value = _cards.value?.mapIndexed { index, card ->
if (index == position) {
card.copy(isButtonSelected = isSelected)
} else {
card
}
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/create_selector_btn.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/create_unclicked_btn" />
<item android:state_enabled="false" android:drawable="@drawable/create_clicked_btn" />
<item android:state_selected="true" android:drawable="@drawable/create_unclicked_btn" />
<item android:state_selected="false" android:drawable="@drawable/create_clicked_btn" />
</selector>
6 changes: 4 additions & 2 deletions app/src/main/res/layout/fragment_create.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">


<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -32,15 +33,15 @@
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.86" />


<ImageButton
android:id="@+id/imageButton"
android:id="@+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/create_back"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="@id/guideline2" />


<TextView
android:id="@+id/create_title"
style="@style/sc_r15"
Expand Down Expand Up @@ -103,6 +104,7 @@


<androidx.appcompat.widget.AppCompatButton
android:id="@+id/next_btn"
style="@style/sc_m13"
android:layout_width="0dp"
android:layout_height="0dp"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">

<TextView
android:id="@+id/text_home"
<Button
android:id="@+id/text_home_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:text="일기 카드 생성"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_rv_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:id="@+id/button"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/create_selector_btn"
android:background="@drawable/create_clicked_btn"
app:layout_constraintBottom_toBottomOf="@+id/textMessage"
app:layout_constraintTop_toTopOf="@+id/textMessage" />

Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/toolbar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar">

<ImageButton
android:id="@+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/create_back"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="@id/guideline2" />

</androidx.appcompat.widget.Toolbar>
2 changes: 1 addition & 1 deletion app/src/main/res/menu/bottom_navi_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/create_fragment"
android:id="@+id/navigation_home"
android:icon="@drawable/lets_icons_home"
/>

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
android:id="@+id/navigation_home"
android:name="com.toyou.toyouandroid.ui.home.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_navigation_home_to_create_fragment"
app:destination="@id/create_fragment"
app:popUpTo="@id/navigation_home"/>
</fragment>
<fragment
android:id="@+id/navigation_diary"
android:name="com.toyou.toyouandroid.ui.diary.DiaryFragment"
Expand All @@ -30,7 +35,8 @@
android:id="@+id/create_fragment"
android:name="com.toyou.toyouandroid.ui.home.CreateFragment"
android:label="CreateFragment"
tools:layout="@layout/fragment_create"/>
tools:layout="@layout/fragment_create">
</fragment>


</navigation>