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 bf8cd86 commit adb68bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.test.tripfriend.ui.home

import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
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
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.archit.calendardaterangepicker.customviews.CalendarListener
Expand All @@ -29,6 +35,8 @@ 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 kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
Expand All @@ -41,6 +49,7 @@ class HomeMainFragment : Fragment() {
lateinit var viewPager: ViewPager2
lateinit var viewPagerAdapter: HomeMainFragment.ViewPagerAdapter
lateinit var bottomSheetMainFilterBinding: BottomSheetMainFilterBinding
lateinit var homeViewModel: HomeViewModel


val spinnerList = arrayOf(
Expand All @@ -58,6 +67,8 @@ class HomeMainFragment : Fragment() {

mainActivity.activityMainBinding.bottomNavigationViewMain.visibility = View.VISIBLE

homeViewModel = ViewModelProvider(mainActivity)[HomeViewModel::class.java]

val sharedPreferences =
mainActivity.getSharedPreferences("user_info", Context.MODE_PRIVATE)
val userClass = UserRepository.getUserInfo(sharedPreferences)
Expand All @@ -73,6 +84,25 @@ class HomeMainFragment : Fragment() {
spinnerClick()
}

textInputEditTextHomeMain.run {
addTextChangedListener {
val searchFilter = spinnerList[spinnerHomeMainSearch.selectedItemPosition]
lifecycleScope.launch {
delay(500)
homeViewModel.getSearchedPostList(it.toString(), searchFilter)
}
}
setOnEditorActionListener { textView, action, keyEvent ->
if (action == EditorInfo.IME_ACTION_DONE) {
// 키보드 내리기
val inputMethodManager = mainActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(textInputEditTextHomeMain.windowToken, 0)
true
}
false
}
}

// 필터
imageButtonHomeMainFilter.run {
setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HomeViewModel : ViewModel() {
// 홈 목록 가져오기
fun getTripPostData() {

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

val scope = CoroutineScope(Dispatchers.Default)
Expand All @@ -31,7 +31,7 @@ class HomeViewModel : ViewModel() {
homePostInfoList.addAll(currentTripPostSnapshot.await().documents)

withContext(Dispatchers.Main) {
for(document in homePostInfoList) {
for (document in homePostInfoList) {
val tripPostObj = document.toObject(TripPost::class.java)

if (tripPostObj != null) {
Expand Down Expand Up @@ -67,7 +67,7 @@ class HomeViewModel : ViewModel() {

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

for(document in filteredPostInfoList) {
for (document in filteredPostInfoList) {
val tripPostObj = document.toObject(TripPost::class.java)
if (tripPostObj != null) {
tripPostObj.tripPostDocumentId = document.id
Expand All @@ -76,7 +76,7 @@ class HomeViewModel : ViewModel() {
}

// 카테고리 필터링
if ( categoryArray.size != 0) {
if (categoryArray.size != 0) {
resultList = resultList.filter { tripPost ->
categoryArray.any { category ->
tripPost.tripPostTripCategory?.contains(category) ?: false
Expand All @@ -85,7 +85,7 @@ class HomeViewModel : ViewModel() {
}

// 성별 필터링
if(genderList[0] == true) {
if (genderList[0] == true) {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostGender[0] == genderList[0])
}.toMutableList()
Expand All @@ -98,16 +98,53 @@ class HomeViewModel : ViewModel() {
// 시작 날짜 기준 필터링
if (dateList[0] != 0) {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostDate?.get(0)?.toInt()!! >= dateList[0] && tripPost.tripPostDate[0]
(tripPost.tripPostDate?.get(0)
?.toInt()!! >= dateList[0] && tripPost.tripPostDate[0]
.toInt() <= dateList[1])
}.toMutableList()
}

withContext(Dispatchers.Main) {
for(result in resultList) {
tripPostList.value = resultList
tripPostList.value = resultList
}

scope.cancel()
}
}

fun getSearchedPostList(query: String, searchFilter: String) {
val filteredPostInfoList = mutableListOf<DocumentSnapshot>()
val scope = CoroutineScope(Dispatchers.Default)

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

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

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

if (searchFilter == "제목+내용") {
resultList = resultList.filter { tripPost ->
(tripPost.tripPostTitle.contains(query) || tripPost.tripPostContent.contains(
query
))
}.toMutableList()
} else {
resultList = resultList.filter { tripPost ->
tripPost.tripPostHashTag.contains(query)
}.toMutableList()
}


withContext(Dispatchers.Main) {
tripPostList.value = resultList
}

scope.cancel()
Expand Down

0 comments on commit adb68bc

Please sign in to comment.