diff --git a/TripFriend/app/src/main/java/com/test/tripfriend/ui/home/HomeMainFragment.kt b/TripFriend/app/src/main/java/com/test/tripfriend/ui/home/HomeMainFragment.kt index 19cdff5..5de9aa0 100644 --- a/TripFriend/app/src/main/java/com/test/tripfriend/ui/home/HomeMainFragment.kt +++ b/TripFriend/app/src/main/java/com/test/tripfriend/ui/home/HomeMainFragment.kt @@ -1,6 +1,7 @@ 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 @@ -8,12 +9,17 @@ 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 @@ -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 @@ -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( @@ -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) @@ -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 { diff --git a/TripFriend/app/src/main/java/com/test/tripfriend/viewmodel/HomeViewModel.kt b/TripFriend/app/src/main/java/com/test/tripfriend/viewmodel/HomeViewModel.kt index c4d3125..72b902a 100644 --- a/TripFriend/app/src/main/java/com/test/tripfriend/viewmodel/HomeViewModel.kt +++ b/TripFriend/app/src/main/java/com/test/tripfriend/viewmodel/HomeViewModel.kt @@ -21,7 +21,7 @@ class HomeViewModel : ViewModel() { // 홈 목록 가져오기 fun getTripPostData() { - val homePostInfoList= mutableListOf() + val homePostInfoList = mutableListOf() val resultList = mutableListOf() val scope = CoroutineScope(Dispatchers.Default) @@ -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) { @@ -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 @@ -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 @@ -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() @@ -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() + val scope = CoroutineScope(Dispatchers.Default) + + scope.launch { + var resultList = mutableListOf() + 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()