Skip to content

Commit

Permalink
#86 fix : 팔로워 맛집 클릭시 음식점 상세보기 이동 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
BENDENG1 committed Dec 13, 2023
1 parent 3023dc6 commit 70bf63b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal fun UserDetailData.toUiUserDetailData() = UiUserDetailData(


internal fun UserDetailRestaurantData.toUiUserDetailRestaurantData() = UiUserDetailRestaurantData(
id = id,
name = name,
address = address,
phoneNumber = phoneNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data class UiUserDetailData(


data class UiUserDetailRestaurantData(
val id : Int,
val name: String,
val address: String,
val phoneNumber: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.avengers.nibobnebob.R
Expand All @@ -22,11 +23,12 @@ class UserDetailFragment : BaseFragment<FragmentUserDetailBinding>(R.layout.frag
private val viewModel: UserDetailViewModel by viewModels()
private val args: UserDetailFragmentArgs by navArgs()
private val nickName by lazy { args.nickName }
private val adapter = UserDetailRestaurantAdapter { id -> viewModel.restaurantDetail(id) }

override fun initView() {
binding.vm = viewModel
override fun initView() = with(binding) {
vm = viewModel
viewModel.setNick(nickName)
binding.rvRestaurant.adapter = UserDetailRestaurantAdapter()
rvRestaurant.adapter = adapter
customBack(requireActivity(), findNavController())
}

Expand All @@ -39,6 +41,11 @@ class UserDetailFragment : BaseFragment<FragmentUserDetailBinding>(R.layout.frag
repeatOnStarted {
viewModel.events.collect {
when (it) {
is UserDetailEvents.NavigateToRestaurantDetail ->
findNavController().toRestaurantDetail(
it.id
)

is UserDetailEvents.NavigateToBack -> findNavController().navigateUp()
is UserDetailEvents.ShowSnackMessage -> showSnackBar(it.msg)
is UserDetailEvents.ShowToastMessage -> showToastMessage(it.msg)
Expand All @@ -50,4 +57,11 @@ class UserDetailFragment : BaseFragment<FragmentUserDetailBinding>(R.layout.frag
}
}
}


private fun NavController.toRestaurantDetail(id: Int) {
val action =
UserDetailFragmentDirections.actionUserDetailFragmentToRestaurantDetailFragment(id)
navigate(action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sealed class UserDetailEvents {
data class ShowToastMessage(val msg: String) : UserDetailEvents()
data object NavigateToBack : UserDetailEvents()
data class ShowBiggerImageDialog(val img: String) : UserDetailEvents()
data class NavigateToRestaurantDetail(val id: Int) : UserDetailEvents()
}

@HiltViewModel
Expand Down Expand Up @@ -126,4 +127,10 @@ class UserDetailViewModel @Inject constructor(
_events.emit(UserDetailEvents.NavigateToBack)
}
}

fun restaurantDetail(id : Int) {
viewModelScope.launch {
_events.emit(UserDetailEvents.NavigateToRestaurantDetail(id))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import com.avengers.nibobnebob.databinding.ItemUserDetailRestaurantBinding
import com.avengers.nibobnebob.presentation.ui.main.global.model.UiUserDetailRestaurantData
import com.avengers.nibobnebob.presentation.util.DefaultDiffUtil

class UserDetailRestaurantAdapter() :
class UserDetailRestaurantAdapter(
private val restaurantClick: (Int) -> Unit
) :
ListAdapter<UiUserDetailRestaurantData, UserDetailRestaurantViewHolder>(DefaultDiffUtil<UiUserDetailRestaurantData>()) {

override fun onCreateViewHolder(
Expand All @@ -24,14 +26,18 @@ class UserDetailRestaurantAdapter() :
}

override fun onBindViewHolder(holder: UserDetailRestaurantViewHolder, position: Int) {
holder.bind(getItem(position))
holder.bind(getItem(position), restaurantClick)
}

}

class UserDetailRestaurantViewHolder(private val binding: ItemUserDetailRestaurantBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: UiUserDetailRestaurantData) {
fun bind(
item: UiUserDetailRestaurantData,
restaurantClick: (Int) -> Unit
) {
binding.item = item
binding.root.setOnClickListener { restaurantClick(item.id) }
}
}
3 changes: 3 additions & 0 deletions Aos/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
android:name="nickName"
app:argType="string"
android:defaultValue=""/>
<action
android:id="@+id/action_userDetailFragment_to_restaurantDetailFragment"
app:destination="@id/restaurantDetailFragment" />
</fragment>

<fragment
Expand Down

0 comments on commit 70bf63b

Please sign in to comment.