@@ -557,6 +557,53 @@ class MainActivity : BaseActivity() {
557557 private fun setupSearchView () {
558558 val liveSearch = preferences.getBoolean(LIVE_SEARCH_PREFERENCE , false )
559559
560+ val doSearch = { query: String ->
561+ val isShowMovieChecked = binding.chipShowMovie.isChecked
562+ val isMovieChecked = binding.chipMovie.isChecked
563+ val isShowChecked = binding.chipShow.isChecked
564+
565+ val currentFragment = getCurrentFragment()
566+ if (currentFragment is HomeFragment || currentFragment is AccountDataFragment || currentFragment is AccountDataFragmentTkt ) {
567+ if (isShowMovieChecked) {
568+ multiSearch(query)
569+ } else if (isMovieChecked) {
570+ showSearch(" movie" , query)
571+ } else if (isShowChecked) {
572+ showSearch(" tv" , query)
573+ }
574+ } else if (currentFragment is ShowFragment ) {
575+ val listType = currentFragment.arguments?.getString(ShowFragment .ARG_LIST_TYPE )
576+ if (listType == " movie" ) {
577+ showSearch(listType, query)
578+ } else if (listType == " tv" ) {
579+ showSearch(listType, query)
580+ }
581+ } else if (currentFragment is ListFragment ) {
582+ databaseSearch(query)
583+ }
584+
585+ fetchKeywords(query)
586+ }
587+
588+ binding.chipShowMovie.setOnCheckedChangeListener { _, isChecked ->
589+ if (isChecked) {
590+ val query = binding.searchView.editText.text.toString()
591+ if (query.isNotEmpty()) doSearch(query)
592+ }
593+ }
594+ binding.chipMovie.setOnCheckedChangeListener { _, isChecked ->
595+ if (isChecked) {
596+ val query = binding.searchView.editText.text.toString()
597+ if (query.isNotEmpty()) doSearch(query)
598+ }
599+ }
600+ binding.chipShow.setOnCheckedChangeListener { _, isChecked ->
601+ if (isChecked) {
602+ val query = binding.searchView.editText.text.toString()
603+ if (query.isNotEmpty()) doSearch(query)
604+ }
605+ }
606+
560607 if (liveSearch) {
561608 binding.searchView.editText.addTextChangedListener(object : TextWatcher {
562609 private val handler = Handler (Looper .getMainLooper())
@@ -567,23 +614,7 @@ class MainActivity : BaseActivity() {
567614 handler.removeCallbacks(workRunnable!! )
568615 }
569616 workRunnable = Runnable {
570- val currentFragment = getCurrentFragment()
571- if (currentFragment is HomeFragment ) {
572- multiSearch(s.toString())
573- } else if (currentFragment is ShowFragment ) {
574- val listType = currentFragment.arguments?.getString(ShowFragment .ARG_LIST_TYPE )
575- if (listType == " movie" ) {
576- showSearch(listType, s.toString())
577- } else if (listType == " tv" ) {
578- showSearch(listType, s.toString())
579- }
580- } else if (currentFragment is ListFragment ) {
581- databaseSearch(s.toString())
582- } else if (currentFragment is AccountDataFragment ) {
583- multiSearch(s.toString())
584- } else if (currentFragment is AccountDataFragmentTkt ) {
585- multiSearch(s.toString())
586- }
617+ doSearch(s.toString())
587618 }
588619 handler.postDelayed(workRunnable!! , 500 )
589620 }
@@ -596,30 +627,92 @@ class MainActivity : BaseActivity() {
596627 actionId == EditorInfo .IME_ACTION_SEARCH ||
597628 event?.action == KeyEvent .ACTION_DOWN && event.keyCode == KeyEvent .KEYCODE_ENTER ) {
598629 val query = v.text.toString()
599- val currentFragment = getCurrentFragment()
600- if (currentFragment is HomeFragment ) {
601- multiSearch(query)
602- } else if (currentFragment is ShowFragment ) {
603- val listType = currentFragment.arguments?.getString(ShowFragment .ARG_LIST_TYPE )
604- if (listType == " movie" ) {
605- showSearch(listType, query)
606- } else if (listType == " tv" ) {
607- showSearch(listType, query)
608- }
609- } else if (currentFragment is ListFragment ) {
610- databaseSearch(query)
611- } else if (currentFragment is AccountDataFragment ) {
612- multiSearch(query)
613- } else if (currentFragment is AccountDataFragmentTkt ) {
614- multiSearch(query)
615- }
630+ doSearch(query)
616631 val imm = getSystemService(INPUT_METHOD_SERVICE ) as InputMethodManager
617632 imm.hideSoftInputFromWindow(v.windowToken, 0 )
618633 true
619634 } else {
620635 false
621636 }
622637 }
638+
639+ binding.searchView.editText.addTextChangedListener(object : TextWatcher {
640+ private val handler = Handler (Looper .getMainLooper())
641+ private var workRunnable: Runnable ? = null
642+ override fun beforeTextChanged (s : CharSequence , start : Int , count : Int , after : Int ) {}
643+ override fun onTextChanged (s : CharSequence , start : Int , before : Int , count : Int ) {
644+ if (workRunnable != null ) {
645+ handler.removeCallbacks(workRunnable!! )
646+ }
647+ workRunnable = Runnable {
648+ fetchKeywords(s.toString())
649+ }
650+ handler.postDelayed(workRunnable!! , 500 )
651+ }
652+
653+ override fun afterTextChanged (s : Editable ) {}
654+ })
655+ }
656+ }
657+
658+ private fun fetchKeywords (query : String ) {
659+ if (query.isEmpty()) {
660+ binding.searchChipGroup.removeViews(3 , binding.searchChipGroup.childCount - 3 )
661+ return
662+ }
663+
664+ lifecycleScope.launch(Dispatchers .IO ) {
665+ try {
666+ val url = java.net.URL (" https://api.themoviedb.org/3/search/keyword?query=${query} &page=1" )
667+ val request = Request .Builder ()
668+ .url(url)
669+ .get()
670+ .addHeader(" Authorization" , " Bearer $apiReadAccessToken " )
671+ .build()
672+
673+ val client = OkHttpClient ()
674+ val response = client.newCall(request).execute()
675+ val responseBody = response.body?.string() ? : " "
676+
677+ if (response.isSuccessful && responseBody.isNotEmpty()) {
678+ val json = JSONObject (responseBody)
679+ val results = json.optJSONArray(" results" )
680+
681+ withContext(Dispatchers .Main ) {
682+ binding.searchChipGroup.removeViews(3 , binding.searchChipGroup.childCount - 3 )
683+
684+ if (results != null ) {
685+ for (i in 0 until Math .min(results.length(), 10 )) {
686+ val item = results.getJSONObject(i)
687+ val keywordId = item.getInt(" id" )
688+ val keywordName = item.getString(" name" )
689+
690+ val chip = Chip (this @MainActivity)
691+ chip.text = keywordName
692+ chip.isCheckable = true
693+ chip.setChipDrawable(com.google.android.material.chip.ChipDrawable .createFromAttributes(
694+ this @MainActivity,
695+ null ,
696+ 0 ,
697+ com.google.android.material.R .style.Widget_Material3_Chip_Filter
698+ ))
699+ chip.setOnCheckedChangeListener { _, isChecked ->
700+ if (isChecked) {
701+ chip.isChecked = false
702+ val intent = Intent (this @MainActivity, KeywordSearchActivity ::class .java)
703+ intent.putExtra(" keywordId" , keywordId)
704+ intent.putExtra(" keywordName" , keywordName)
705+ startActivity(intent)
706+ }
707+ }
708+ binding.searchChipGroup.addView(chip)
709+ }
710+ }
711+ }
712+ }
713+ } catch (e: Exception ) {
714+ e.printStackTrace()
715+ }
623716 }
624717 }
625718
0 commit comments