Skip to content

Commit

Permalink
[feat/search_group]: 검색된 그룹 중 일부를 가져오는 API 전체 검색 화면에 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
soopeach committed Mar 15, 2024
1 parent ba0099d commit c188257
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class AllSearchFragment : Fragment() {
binding.cgRecentSearch.removeAllViews()
keywordList.forEach {
if (it.isNotBlank()) {
binding.tvRecentSearch.visibility = View.VISIBLE
binding.tvDelete.visibility = View.VISIBLE
binding.cgRecentSearch.addView(
newChip(it,
{ keyword ->
Expand All @@ -64,6 +66,9 @@ class AllSearchFragment : Fragment() {
navigateToResultPage()
}
)
} else {
binding.tvRecentSearch.visibility = View.GONE
binding.tvDelete.visibility = View.GONE
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import kotlinx.coroutines.launch
import org.gdsc.domain.DrinkPossibility
import org.gdsc.domain.FoodCategory
import org.gdsc.domain.SortType
import org.gdsc.domain.model.GroupPreview
import org.gdsc.domain.model.Location
import org.gdsc.domain.model.RegisteredRestaurant
import org.gdsc.domain.usecase.GetGroupBySearchWithLimitCountUseCase
import org.gdsc.domain.usecase.GetRestaurantBySearchUseCase
import org.gdsc.domain.usecase.GetRestaurantBySearchWithLimitCountUseCase
import org.gdsc.domain.usecase.user.DeleteSearchedKeywordUseCase
Expand All @@ -32,41 +34,12 @@ class AllSearchViewModel @Inject constructor(
private val updateSearchedKeywordUseCase: UpdateSearchedKeywordUseCase,
private val deleteSearchedKeywordUseCase: DeleteSearchedKeywordUseCase,
private val initSearchedKeywordUseCase: InitSearchedKeywordUseCase,
private val getRestaurantBySearchWithLimitCountUseCase: GetRestaurantBySearchWithLimitCountUseCase
private val getRestaurantBySearchWithLimitCountUseCase: GetRestaurantBySearchWithLimitCountUseCase,
private val getGroupBySearchWithLimitCountUseCase: GetGroupBySearchWithLimitCountUseCase
) : ViewModel() {

init {

viewModelScope.launch {
val location = locationManager.getCurrentLocation()

if (location == null) {
_searchedRestaurantPreviewState.value = emptyList()
} else {

val userLoc = Location(location.longitude.toString(), location.latitude.toString())

_searchedRestaurantPreviewState.value =
getRestaurantBySearchWithLimitCountUseCase(searchKeyword.value, userLoc, 3)
}

}

viewModelScope.launch {
val location = locationManager.getCurrentLocation()

if (location == null) {
_searchedRestaurantState.value = PagingData.empty()
} else {
val userLoc = Location(location.longitude.toString(), location.latitude.toString())

getRestaurantBySearchUseCase(searchKeyword.value, userLoc).distinctUntilChanged()
.collect {
_searchedRestaurantState.value = it
}
}
}

viewModelScope.launch {
val keywords = getSearchedKeywordsUseCase()
if (keywords.isNotEmpty()) {
Expand Down Expand Up @@ -101,6 +74,12 @@ class AllSearchViewModel @Inject constructor(
val searchedRestaurantPreviewState: StateFlow<List<RegisteredRestaurant>>
get() = _searchedRestaurantPreviewState

private var _searchedGroupPreviewState =
MutableStateFlow<List<GroupPreview>>(emptyList())

val searchedGroupPreviewState: StateFlow<List<GroupPreview>>
get() = _searchedGroupPreviewState


private var _searchedKeywordsState = MutableStateFlow<List<String>>(emptyList())
val searchedKeywordsState: StateFlow<List<String>>
Expand Down Expand Up @@ -140,5 +119,46 @@ class AllSearchViewModel @Inject constructor(
}
}

fun searchRestaurantPreviewWithKeyword() {
viewModelScope.launch {
val location = locationManager.getCurrentLocation()

if (location == null) {
_searchedRestaurantPreviewState.value = emptyList()
} else {

val userLoc = Location(location.longitude.toString(), location.latitude.toString())

_searchedRestaurantPreviewState.value =
getRestaurantBySearchWithLimitCountUseCase(searchKeyword.value, userLoc, 3)
}

}
}

fun searchRestaurantWithKeyword() {
viewModelScope.launch {
val location = locationManager.getCurrentLocation()

if (location == null) {
_searchedRestaurantState.value = PagingData.empty()
} else {
val userLoc = Location(location.longitude.toString(), location.latitude.toString())

getRestaurantBySearchUseCase(searchKeyword.value, userLoc).distinctUntilChanged()
.collect {
_searchedRestaurantState.value = it
}
}
}
}

fun searchGroupPreviewWithKeyword() {
viewModelScope.launch {
val result = getGroupBySearchWithLimitCountUseCase(searchKeyword.value, 3)
_searchedGroupPreviewState.value = result
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class SearchCategoryAllFragment(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.searchRestaurantPreviewWithKeyword()
viewModel.searchGroupPreviewWithKeyword()

binding.restaurantRecyclerView.adapter = searchCategoryRestaurantPreviewAdapter
binding.restaurantRecyclerView.layoutManager = LinearLayoutManager(requireContext())

Expand All @@ -68,44 +71,12 @@ class SearchCategoryAllFragment(
}
}

// Todo: Real APi
searchCategoryGroupPreviewAdapter.submitList(
listOf(
GroupInfo(
"https://avatars.githubusercontent.com/u/58663494?v=4",
1,
"햄버거 먹으러 갈 사람 여기여기 모여라",
"버거 대마왕",
"https://avatars.githubusercontent.com/u/58663494?v=4",
false,
(0 .. 500).shuffled().first(),
false,
(0 .. 500).shuffled().first()
),
GroupInfo(
"https://avatars.githubusercontent.com/u/58663494?v=4",
2,
"햄버거 먹으러 갈 사람 여기여기 모여라",
"버거 대마왕",
"https://avatars.githubusercontent.com/u/58663494?v=4",
false,
(0 .. 500).shuffled().first(),
false,
(0 .. 500).shuffled().first()
),
GroupInfo(
"https://avatars.githubusercontent.com/u/58663494?v=4",
3,
"햄버거 먹으러 갈 사람 여기여기 모여라",
"버거 대마왕",
"https://avatars.githubusercontent.com/u/58663494?v=4",
false,
(0 .. 500).shuffled().first(),
false,
(0 .. 500).shuffled().first()
),
)
)
repeatWhenUiStarted {
viewModel.searchedGroupPreviewState.collect {
searchCategoryGroupPreviewAdapter.submitList(it)
}
}

}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class SearchCategoryRestaurantFragment(
setSpinners()

viewModel.setSearchKeyword(searchKeyword)
viewModel.searchRestaurantWithKeyword()

binding.restaurantRecyclerView.adapter = searchCategoryRestaurantAdapter
binding.restaurantRecyclerView.layoutManager = LinearLayoutManager(requireContext())

}

private fun observeState() {

repeatWhenUiStarted {
viewModel.searchedRestaurantState.collect {
searchCategoryRestaurantAdapter.submitData(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import org.gdsc.domain.model.GroupInfo
import org.gdsc.domain.model.GroupPreview
import org.gdsc.presentation.R
import org.gdsc.presentation.databinding.ItemSearchGroupBinding

class SearchCategoryGroupPreviewAdapter
: ListAdapter<GroupInfo, SearchCategoryGroupPreviewAdapter.GroupWithSearchPreviewViewHolder>(
: ListAdapter<GroupPreview, SearchCategoryGroupPreviewAdapter.GroupWithSearchPreviewViewHolder>(
diffCallback
) {

companion object {
val diffCallback = object : DiffUtil.ItemCallback<GroupInfo>() {
val diffCallback = object : DiffUtil.ItemCallback<GroupPreview>() {
override fun areItemsTheSame(
oldItem: GroupInfo,
newItem: GroupInfo
oldItem: GroupPreview,
newItem: GroupPreview
): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(
oldItem: GroupInfo,
newItem: GroupInfo
oldItem: GroupPreview,
newItem: GroupPreview
): Boolean {
return oldItem == newItem
}
Expand All @@ -36,10 +36,10 @@ class SearchCategoryGroupPreviewAdapter
class GroupWithSearchPreviewViewHolder(
private val binding: ItemSearchGroupBinding,
): RecyclerView.ViewHolder(binding.root) {
fun bind(item: GroupInfo) {
fun bind(item: GroupPreview) {
binding.run {
Glide.with(itemView.context)
.load(item.groupProfileImageUrl)
.load("https://picsum.photos/200")
.placeholder(R.drawable.base_profile_image)
.into(ivGroupImage)

Expand Down

0 comments on commit c188257

Please sign in to comment.