diff --git a/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt b/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt index e0f6d3d..789d481 100644 --- a/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt +++ b/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt @@ -10,7 +10,7 @@ import org.inu.events.databinding.ItemSearchEventBinding class SearchPagingAdapter( val onClickEvent: (event: Event) -> Unit, - val onCLickLikeIcon: (event: Event) -> Boolean + val onCLickLikeIcon: (event: Event) -> Unit ) : PagingDataAdapter(SearchDiffUtil) { @@ -26,8 +26,8 @@ class SearchPagingAdapter( class ViewHolder private constructor( val binding: ItemSearchEventBinding, - val _onClickEvent: (event: Event) -> Unit, - val _onCLickLikeIcon: (event: Event) -> Boolean + private val _onClickEvent: (event: Event) -> Unit, + private val _onCLickLikeIcon: (event: Event) -> Unit ) : RecyclerView.ViewHolder(binding.root) { @@ -57,7 +57,7 @@ class SearchPagingAdapter( fun from( parent: ViewGroup, onClickEvent: (event: Event) -> Unit, - onCLickLikeIcon: (event: Event) -> Boolean + onCLickLikeIcon: (event: Event) -> Unit ): ViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = ItemSearchEventBinding.inflate(layoutInflater, parent, false) diff --git a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt index 8c4ad38..770cf67 100644 --- a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt +++ b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt @@ -1,5 +1,6 @@ package org.inu.events.ui.home +import android.content.Intent import android.os.Bundle import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity @@ -8,10 +9,13 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch +import org.inu.events.common.extension.toast import org.inu.events.data.model.entity.Event import org.inu.events.databinding.ActivitySearchBinding import org.inu.events.ui.adapter.SearchPagingAdapter import org.inu.events.ui.detail.DetailActivity +import org.inu.events.ui.login.LoginActivity +import org.inu.events.ui.util.dialog.LoginDialog class SearchActivity : AppCompatActivity() { @@ -40,6 +44,11 @@ class SearchActivity : AppCompatActivity() { setContentView(binding.root) } + override fun onStart() { + super.onStart() + + vm.search() + } private fun initRecyclerView() { binding.rvEvent.adapter = adapter @@ -65,7 +74,17 @@ class SearchActivity : AppCompatActivity() { startActivity(DetailActivity.callingIntent(this, event.id, event.wroteByMe)) } - private fun onClickLikeIcon(event: Event): Boolean { - return false + private fun onClickLikeIcon(event: Event) { + vm.bookMarkByToggle(event.likedByMe, event.id).run { + if (this.not()) showDialog() + else vm.search() + } + } + + private fun showDialog() { + LoginDialog().show( + context = this, + onOk = { this.startActivity(Intent(this, LoginActivity::class.java)) }, + onCancel = { toast("로그인을 하셔야 북마크가 가능합니다") }) } } \ No newline at end of file diff --git a/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt b/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt index 2494480..38d935f 100644 --- a/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt +++ b/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt @@ -4,18 +4,22 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData import androidx.paging.cachedIn +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch +import org.inu.events.data.model.dto.LikeParam import org.inu.events.data.model.entity.Event import org.inu.events.data.repository.EventRepository +import org.inu.events.data.repository.LikeRepository import org.koin.core.component.KoinComponent import org.koin.core.component.inject class SearchViewModel : ViewModel(), KoinComponent { private val eventRepository: EventRepository by inject() + private val likeRepository: LikeRepository by inject() val searchText = MutableStateFlow("") private val category = MutableStateFlow(0) @@ -46,7 +50,7 @@ class SearchViewModel : ViewModel(), KoinComponent { } } - private fun search(): Job { + fun search(): Job { return viewModelScope.launch { eventRepository.searchEvents( categoryId = category.value, @@ -57,4 +61,30 @@ class SearchViewModel : ViewModel(), KoinComponent { } } } + + fun bookMarkByToggle(onOff: Boolean?, eventId: Int): Boolean { + return when (onOff) { + true -> { + unBookmark(eventId) + true + } + false -> { + bookmark(eventId) + true + } + null -> false + } + } + + private fun bookmark(eventId: Int) { + viewModelScope.launch(Dispatchers.IO) { + likeRepository.postLike(LikeParam(eventId)) + } + } + + private fun unBookmark(eventId: Int) { + viewModelScope.launch(Dispatchers.IO) { + likeRepository.deleteLike(LikeParam(eventId)) + } + } } \ No newline at end of file