Skip to content

Commit 2ebe180

Browse files
authored
Merge pull request #24 from Coffee-ing/feat-filter-api-connection
[feat] 검색, 정렬, 필터링 api 연동
2 parents 21b7878 + 71e01fb commit 2ebe180

File tree

9 files changed

+138
-6
lines changed

9 files changed

+138
-6
lines changed

app/src/main/java/com/coffeeing/client/data/datasource/remote/MainDataSource.kt

+12
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ class MainDataSource @Inject constructor(
2828
suspend fun postRegistration(
2929
postId: Int
3030
): ResponseRegistration = mainService.postRegistration(postId)
31+
32+
suspend fun getSearch(
33+
keyword: String
34+
): ResponseHomeList = mainService.getSearch(keyword)
35+
36+
suspend fun getSort(
37+
sort: String
38+
): ResponseHomeList = mainService.getSort(sort)
39+
40+
suspend fun getFilter(
41+
tag: String
42+
): ResponseHomeList = mainService.getFilter(tag)
3143
}

app/src/main/java/com/coffeeing/client/data/repository/MainRepositoryImpl.kt

+12
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ class MainRepositoryImpl @Inject constructor(
3636
override suspend fun postRegistration(postId: Int): Result<Registration> = runCatching {
3737
mainDataSource.postRegistration(postId).toRegistration()
3838
}
39+
40+
override suspend fun getSearch(keyword: String): Result<List<HomeCoffeeing>> = runCatching {
41+
mainDataSource.getSearch(keyword).toHomeList()
42+
}
43+
44+
override suspend fun getSort(sort: String): Result<List<HomeCoffeeing>> = runCatching {
45+
mainDataSource.getSort(sort).toHomeList()
46+
}
47+
48+
override suspend fun getFilter(tag: String): Result<List<HomeCoffeeing>> = runCatching {
49+
mainDataSource.getFilter(tag).toHomeList()
50+
}
3951
}

app/src/main/java/com/coffeeing/client/data/service/MainService.kt

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import retrofit2.http.Body
1010
import retrofit2.http.GET
1111
import retrofit2.http.POST
1212
import retrofit2.http.Path
13+
import retrofit2.http.Query
1314

1415
interface MainService {
1516
@GET("list")
@@ -35,4 +36,19 @@ interface MainService {
3536
suspend fun postRegistration(
3637
@Path("post_id") postId: Int
3738
): ResponseRegistration
39+
40+
@GET("search")
41+
suspend fun getSearch(
42+
@Query("keyword") keyword: String
43+
): ResponseHomeList
44+
45+
@GET("sort")
46+
suspend fun getSort(
47+
@Query("sort") sort: String
48+
): ResponseHomeList
49+
50+
@GET("filter")
51+
suspend fun getFilter(
52+
@Query("tag") tag: String
53+
): ResponseHomeList
3854
}

app/src/main/java/com/coffeeing/client/domain/repository/MainRepository.kt

+12
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ interface MainRepository {
2323
suspend fun postRegistration(
2424
postId: Int
2525
): Result<Registration>
26+
27+
suspend fun getSearch(
28+
keyword: String
29+
): Result<List<HomeCoffeeing>>
30+
31+
suspend fun getSort(
32+
sort: String
33+
): Result<List<HomeCoffeeing>>
34+
35+
suspend fun getFilter(
36+
tag: String
37+
): Result<List<HomeCoffeeing>>
2638
}

app/src/main/java/com/coffeeing/client/presentation/home/HomeActivity.kt

+28-3
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,33 @@ class HomeActivity : BindingActivity<ActivityHomeBinding>(R.layout.activity_home
6262
binding.ivHomeMyPage.setOnClickListener {
6363
moveToMypage()
6464
}
65+
66+
binding.ivHomeSearch.setOnClickListener {
67+
viewModel.getSearch(binding.etHomeSearch.text.toString())
68+
}
69+
70+
binding.cgHomeCoffeeingTypeFilter.setOnCheckedChangeListener { _, checkedId ->
71+
when (binding.cgHomeCoffeeingTypeFilter.checkedChipId) {
72+
R.id.chip_home_coffeeing_original -> viewModel.setFilterType("original")
73+
R.id.chip_home_coffeeing_friend -> viewModel.setFilterType("friend")
74+
R.id.chip_home_coffeeing_tour -> viewModel.setFilterType("tour")
75+
R.id.chip_home_coffeeing_worker -> viewModel.setFilterType("worker")
76+
R.id.chip_home_coffeeing_beginner -> viewModel.setFilterType("beginner")
77+
else -> viewModel.setFilterType("original")
78+
}
79+
}
6580
}
6681

6782
private fun addObservers() {
6883
viewModel.homeSort.observe(this) { homeSortType ->
6984
binding.tvHomeSort.text =
70-
viewModel.homeSort.value?.sortType ?: HomeSortType.RECENT.sortType
85+
viewModel.homeSort.value?.sortType ?: "최신순"
7186
}
7287
}
7388

7489
private fun collectData() {
7590
viewModel.homeList.flowWithLifecycle(lifecycle).onEach {
7691
homeCoffeeingAdapter.submitList(viewModel.homeList.value)
77-
viewModel.getHomeList()
7892

7993
if (viewModel.homeList.value.isNullOrEmpty()) {
8094
binding.rvHomeCoffeeing.visibility = View.INVISIBLE
@@ -88,13 +102,24 @@ class HomeActivity : BindingActivity<ActivityHomeBinding>(R.layout.activity_home
88102
viewModel.likeState.flowWithLifecycle(lifecycle).onEach {
89103
viewModel.getHomeList()
90104
}.launchIn(lifecycleScope)
105+
106+
viewModel.filterType.flowWithLifecycle(lifecycle).onEach {
107+
viewModel.filterType.value?.let { tag ->
108+
viewModel.getFilter(tag)
109+
}
110+
}.launchIn(lifecycleScope)
91111
}
92112

93113
private fun showHomeSortDialog() {
94114
viewModel.homeSort.value?.let {
95115
HomeSortBottomSheetDialog(
96116
currentSortType = it,
97-
sort = { sortType -> viewModel.setHomeSort(sortType) }
117+
sort = { sortType -> viewModel.setHomeSort(sortType) },
118+
onDialogClosed = {
119+
viewModel.getSort(
120+
viewModel.homeSort.value?.sortType ?: "최신순"
121+
)
122+
}
98123
).show(supportFragmentManager, HOME_SORT_DIALOG)
99124
}
100125
}

app/src/main/java/com/coffeeing/client/presentation/home/HomeSortBottomSheetDialog.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coffeeing.client.presentation.home
22

3+
import android.content.DialogInterface
34
import android.os.Bundle
45
import android.view.View
56
import com.coffeeing.client.R
@@ -9,7 +10,8 @@ import com.coffeeing.client.util.binding.BindingBottomSheetDialogFragment
910

1011
class HomeSortBottomSheetDialog(
1112
private val currentSortType: HomeSortType,
12-
private val sort: (HomeSortType) -> Unit
13+
private val sort: (HomeSortType) -> Unit,
14+
private val onDialogClosed: () -> Unit
1315
) : BindingBottomSheetDialogFragment<DialogBottomHomeSortBinding>(R.layout.dialog_bottom_home_sort) {
1416

1517
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -19,6 +21,11 @@ class HomeSortBottomSheetDialog(
1921
addListeners()
2022
}
2123

24+
override fun onDismiss(dialog: DialogInterface) {
25+
super.onDismiss(dialog)
26+
onDialogClosed.invoke()
27+
}
28+
2229
private fun initLayout() {
2330
with(binding) {
2431
tvHomeSortDeadline.isSelected = currentSortType == HomeSortType.RECENT

app/src/main/java/com/coffeeing/client/presentation/home/HomeViewModel.kt

+42
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class HomeViewModel @Inject constructor(
2424
val homeList get() = _homeList.asStateFlow()
2525
private val _likeState = MutableStateFlow<Like?>(null)
2626
val likeState get() = _likeState.asStateFlow()
27+
private val _filterType = MutableStateFlow<String?>(null)
28+
val filterType get() = _filterType.asStateFlow()
2729

2830
fun setHomeSort(homeSortType: HomeSortType) {
2931
_homeSort.value = homeSortType
@@ -41,6 +43,42 @@ class HomeViewModel @Inject constructor(
4143
}
4244
}
4345

46+
fun getSearch(keyword: String) {
47+
viewModelScope.launch {
48+
mainRepository.getSearch(keyword)
49+
.onSuccess {
50+
_homeList.value = it
51+
}
52+
.onFailure { exception ->
53+
Timber.e(exception.message)
54+
}
55+
}
56+
}
57+
58+
fun getSort(sort: String) {
59+
viewModelScope.launch {
60+
mainRepository.getSort(sort)
61+
.onSuccess {
62+
_homeList.value = it
63+
}
64+
.onFailure { exception ->
65+
Timber.e(exception.message)
66+
}
67+
}
68+
}
69+
70+
fun getFilter(tag: String) {
71+
viewModelScope.launch {
72+
mainRepository.getFilter(tag)
73+
.onSuccess {
74+
_homeList.value = it
75+
}
76+
.onFailure { exception ->
77+
Timber.e(exception.message)
78+
}
79+
}
80+
}
81+
4482
fun postLike(postId: Int) {
4583
viewModelScope.launch {
4684
mainRepository.postLike(postId)
@@ -52,4 +90,8 @@ class HomeViewModel @Inject constructor(
5290
}
5391
}
5492
}
93+
94+
fun setFilterType(tag: String) {
95+
_filterType.value = tag
96+
}
5597
}

app/src/main/java/com/coffeeing/client/presentation/type/HomeSortType.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.coffeeing.client.presentation.type
33
enum class HomeSortType(val sortType: String) {
44
RECENT("최신순"),
55
POPULARITY("인기순"),
6-
DEADLINE("마감임박 순")
6+
DEADLINE("마감임박순")
77
}

app/src/main/res/layout/activity_home.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:tools="http://schemas.android.com/tools">
55

66
<data>
7+
78
<variable
89
name="viewModel"
910
type="com.coffeeing.client.presentation.home.HomeViewModel" />
@@ -66,9 +67,10 @@
6667
app:layout_constraintEnd_toEndOf="parent"
6768
app:layout_constraintStart_toStartOf="@+id/horizontalScrollView"
6869
app:singleLine="true"
69-
app:singleSelection="false">
70+
app:singleSelection="true">
7071

7172
<com.google.android.material.chip.Chip
73+
android:id="@+id/chip_home_coffeeing_original"
7274
style="@style/Style.Material3.Chip.Coffeeing.Type"
7375
android:layout_width="wrap_content"
7476
android:layout_height="wrap_content"
@@ -77,6 +79,7 @@
7779
android:textSize="7dp" />
7880

7981
<com.google.android.material.chip.Chip
82+
android:id="@+id/chip_home_coffeeing_friend"
8083
style="@style/Style.Material3.Chip.Coffeeing.Type"
8184
android:layout_width="wrap_content"
8285
android:layout_height="wrap_content"
@@ -85,6 +88,7 @@
8588
android:textSize="7dp" />
8689

8790
<com.google.android.material.chip.Chip
91+
android:id="@+id/chip_home_coffeeing_tour"
8892
style="@style/Style.Material3.Chip.Coffeeing.Type"
8993
android:layout_width="wrap_content"
9094
android:layout_height="wrap_content"
@@ -93,6 +97,7 @@
9397
android:textSize="7dp" />
9498

9599
<com.google.android.material.chip.Chip
100+
android:id="@+id/chip_home_coffeeing_worker"
96101
style="@style/Style.Material3.Chip.Coffeeing.Type"
97102
android:layout_width="wrap_content"
98103
android:layout_height="wrap_content"
@@ -101,6 +106,7 @@
101106
android:textSize="7dp" />
102107

103108
<com.google.android.material.chip.Chip
109+
android:id="@+id/chip_home_coffeeing_beginner"
104110
style="@style/Style.Material3.Chip.Coffeeing.Type"
105111
android:layout_width="wrap_content"
106112
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)