Skip to content

Commit

Permalink
feat: 필터 기능 구현 #144
Browse files Browse the repository at this point in the history
  • Loading branch information
y-d-h committed Sep 22, 2023
1 parent 9457367 commit 537b107
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class HomeListFragment : Fragment() {
}

fun initHomeViewModel() {
homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
homeViewModel = ViewModelProvider(mainActivity)[HomeViewModel::class.java]
homeViewModel.getTripPostData()

homeViewModel.tripPostList.observe(viewLifecycleOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -12,6 +13,7 @@ import android.widget.ArrayAdapter
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.archit.calendardaterangepicker.customviews.CalendarListener
Expand All @@ -26,6 +28,7 @@ import com.test.tripfriend.databinding.BottomSheetMainFilterBinding
import com.test.tripfriend.databinding.FragmentHomeMainBinding
import com.test.tripfriend.repository.UserRepository
import com.test.tripfriend.ui.user.LoginMainActivity
import com.test.tripfriend.viewmodel.HomeViewModel
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
Expand All @@ -39,6 +42,7 @@ class HomeMainFragment : Fragment() {
lateinit var viewPagerAdapter: HomeMainFragment.ViewPagerAdapter
lateinit var bottomSheetMainFilterBinding: BottomSheetMainFilterBinding


val spinnerList = arrayOf(
"제목+내용", "해시태그"
)
Expand Down Expand Up @@ -200,13 +204,20 @@ class HomeMainFragment : Fragment() {
// 필터 바텀시트
class ModalBottomSheet : BottomSheetDialogFragment() {
lateinit var bottomSheetMainFilterBinding: BottomSheetMainFilterBinding
lateinit var mainActivity: MainActivity

// 최대 선택 가능 Chip 갯수
val maxSelectableChips = 3

// 칩 카운트 변수
var chipCount = 0

val categoryList = mutableListOf<String>()
val genderList = mutableListOf<Boolean>()
val dateList = IntArray(2) {0}

lateinit var homeViewModel: HomeViewModel

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -220,6 +231,11 @@ class HomeMainFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

mainActivity = activity as MainActivity

var firstDate = ""
var secondDate = ""

bottomSheetMainFilterBinding.run {

// 초기화
Expand Down Expand Up @@ -255,8 +271,10 @@ class HomeMainFragment : Fragment() {
CalendarListener {
override fun onFirstDateSelected(startDate: Calendar) {
val date = startDate.time
val format =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val format = SimpleDateFormat("yyyyMMdd", Locale.getDefault())

firstDate = format.format(date)
secondDate = format.format(date)
}

override fun onDateRangeSelected(
Expand All @@ -265,8 +283,10 @@ class HomeMainFragment : Fragment() {
) {
val startDate = startDate.time
val endDate = endDate.time
val format =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val format = SimpleDateFormat("yyyyMMdd", Locale.getDefault())

firstDate = format.format(startDate)
secondDate = format.format(endDate)
}
})
}
Expand All @@ -276,14 +296,43 @@ class HomeMainFragment : Fragment() {
}

buttonHomeMainFilterApply.setOnClickListener {
if(chipDialogFilterCategory1.isChecked) {
categoryCheck()
genderList.add(chipDialogFilterGender1.isChecked)
genderList.add(chipDialogFilterGender2.isChecked)

if(firstDate.isNotEmpty() && secondDate.isNotEmpty()) {
dateList[0] = firstDate.toInt()
dateList[1] = secondDate.toInt()
}

homeViewModel = ViewModelProvider(mainActivity)[HomeViewModel::class.java]
homeViewModel.getFilteredPostList(categoryList,genderList, dateList)
dismiss()
}
}
}

private fun categoryCheck() {
bottomSheetMainFilterBinding.run {
val chipArray = arrayOf(
chipDialogFilterCategory1,
chipDialogFilterCategory2,
chipDialogFilterCategory3,
chipDialogFilterCategory4,
chipDialogFilterCategory5,
chipDialogFilterCategory6,
chipDialogFilterCategory7,
chipDialogFilterCategory8,
chipDialogFilterCategory9
)
chipArray.forEach { chip ->
if (chip.isChecked == true) {
categoryList.add(chip.text.toString())
}
}
}
}

private fun unCheckChips() {

bottomSheetMainFilterBinding.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import androidx.lifecycle.ViewModel
import com.google.firebase.firestore.DocumentSnapshot
import com.test.tripfriend.dataclassmodel.TripPost
import com.test.tripfriend.repository.HomeListRepository
import com.test.tripfriend.repository.TripPostRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

class HomeViewModel : ViewModel() {
Expand Down Expand Up @@ -53,29 +51,63 @@ class HomeViewModel : ViewModel() {
}
}

fun getFilteredPostList() {
fun getFilteredPostList(
categoryArray: MutableList<String>,
genderList: MutableList<Boolean>,
dateList: IntArray
) {

val filteredPostInfoList = mutableListOf<DocumentSnapshot>()
val resultList = mutableListOf<TripPost>()


val scope = CoroutineScope(Dispatchers.Default)
scope.launch {
var resultList = mutableListOf<TripPost>()
val currentTripPostSnapshot = async { homeListRepository.getDocumentData() }

filteredPostInfoList.addAll(currentTripPostSnapshot.await().documents)

withContext(Dispatchers.Main) {
for(document in filteredPostInfoList) {
val tripPostObj = document.toObject(TripPost::class.java)
for(document in filteredPostInfoList) {
val tripPostObj = document.toObject(TripPost::class.java)
if (tripPostObj != null) {
tripPostObj.tripPostDocumentId = document.id
resultList.add(tripPostObj)
}
}

if (tripPostObj != null) {
tripPostObj.tripPostDocumentId = document.id
resultList.add(tripPostObj)
// 카테고리 필터링
if ( categoryArray.size != 0) {
resultList = resultList.filter { tripPost ->
categoryArray.any { category ->
tripPost.tripPostTripCategory?.contains(category) ?: false
}
}
}.toMutableList()
}

// 성별 필터링
if(genderList[0] == true) {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostGender[0] == genderList[0])
}.toMutableList()
} else if (genderList[1] == true) {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostGender[1] == genderList[1])
}.toMutableList()
}

// 시작 날짜 기준 필터링
if (dateList[0] != 0) {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostDate?.get(0)?.toInt()!! >= dateList[0] && tripPost.tripPostDate[0]
.toInt() <= dateList[1])
}.toMutableList()
}

withContext(Dispatchers.Main) {
Log.d("qwer", "resultList : ${resultList[0].tripPostTripCategory}")
for(result in resultList) {
tripPostList.value = resultList
}

}

scope.cancel()
Expand Down

0 comments on commit 537b107

Please sign in to comment.