Skip to content

Commit

Permalink
feat: #21 - 검색 아이템 북마크 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Mar 5, 2023
1 parent 378b054 commit b2f39fe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, SearchPagingAdapter.ViewHolder>(SearchDiffUtil) {

Expand All @@ -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) {


Expand Down Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions app/src/main/java/org/inu/events/ui/home/SearchActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {

Expand Down Expand Up @@ -40,6 +44,11 @@ class SearchActivity : AppCompatActivity() {
setContentView(binding.root)
}

override fun onStart() {
super.onStart()

vm.search()
}

private fun initRecyclerView() {
binding.rvEvent.adapter = adapter
Expand All @@ -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("로그인을 하셔야 북마크가 가능합니다") })
}
}
32 changes: 31 additions & 1 deletion app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -46,7 +50,7 @@ class SearchViewModel : ViewModel(), KoinComponent {
}
}

private fun search(): Job {
fun search(): Job {
return viewModelScope.launch {
eventRepository.searchEvents(
categoryId = category.value,
Expand All @@ -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))
}
}
}

0 comments on commit b2f39fe

Please sign in to comment.