|
| 1 | +package org.ole.planet.myplanet.ui.courses |
| 2 | + |
| 3 | +import android.text.Editable |
| 4 | +import android.text.TextWatcher |
| 5 | +import android.view.View |
| 6 | +import android.widget.AdapterView |
| 7 | +import android.widget.ArrayAdapter |
| 8 | +import android.widget.EditText |
| 9 | +import android.widget.Spinner |
| 10 | +import android.widget.TextView |
| 11 | +import kotlinx.coroutines.CoroutineScope |
| 12 | +import kotlinx.coroutines.Job |
| 13 | +import kotlinx.coroutines.delay |
| 14 | +import kotlinx.coroutines.launch |
| 15 | +import org.ole.planet.myplanet.R |
| 16 | +import org.ole.planet.myplanet.model.RealmTag |
| 17 | + |
| 18 | +data class FilterState( |
| 19 | + val searchText: String, |
| 20 | + val grade: String, |
| 21 | + val subject: String, |
| 22 | + val tagNames: List<String> |
| 23 | +) { |
| 24 | + val isActive: Boolean |
| 25 | + get() = searchText.isNotEmpty() || grade.isNotEmpty() || subject.isNotEmpty() || tagNames.isNotEmpty() |
| 26 | +} |
| 27 | + |
| 28 | +class CourseFilterController( |
| 29 | + private val rootView: View, |
| 30 | + private val scope: CoroutineScope, |
| 31 | + private val onFilterChanged: (FilterState) -> Unit, |
| 32 | + private val onScrollToTop: () -> Unit |
| 33 | +) { |
| 34 | + private lateinit var etSearch: EditText |
| 35 | + private lateinit var spnGrade: Spinner |
| 36 | + private lateinit var spnSubject: Spinner |
| 37 | + private lateinit var tvSelected: TextView |
| 38 | + val searchTags: MutableList<RealmTag> = ArrayList() |
| 39 | + private var searchJob: Job? = null |
| 40 | + private var searchTextWatcher: TextWatcher? = null |
| 41 | + |
| 42 | + fun setup() { |
| 43 | + etSearch = rootView.findViewById(R.id.et_search) |
| 44 | + spnGrade = rootView.findViewById(R.id.spn_grade) |
| 45 | + spnSubject = rootView.findViewById(R.id.spn_subject) |
| 46 | + tvSelected = rootView.findViewById(R.id.tv_selected) |
| 47 | + setupSpinners() |
| 48 | + setupSearchWatcher() |
| 49 | + setupClearTagsButton() |
| 50 | + } |
| 51 | + |
| 52 | + private fun setupSpinners() { |
| 53 | + val ctx = rootView.context |
| 54 | + val gradeAdapter = ArrayAdapter.createFromResource(ctx, R.array.grade_level, R.layout.spinner_item) |
| 55 | + gradeAdapter.setDropDownViewResource(R.layout.custom_simple_list_item_1) |
| 56 | + spnGrade.adapter = gradeAdapter |
| 57 | + |
| 58 | + val subjectAdapter = ArrayAdapter.createFromResource(ctx, R.array.subject_level, R.layout.spinner_item) |
| 59 | + subjectAdapter.setDropDownViewResource(R.layout.custom_simple_list_item_1) |
| 60 | + spnSubject.adapter = subjectAdapter |
| 61 | + |
| 62 | + val spinnerListener = object : AdapterView.OnItemSelectedListener { |
| 63 | + override fun onItemSelected(parent: AdapterView<*>?, view: View?, i: Int, l: Long) { |
| 64 | + if (view == null) return |
| 65 | + onFilterChanged(currentState()) |
| 66 | + onScrollToTop() |
| 67 | + } |
| 68 | + override fun onNothingSelected(parent: AdapterView<*>?) {} |
| 69 | + } |
| 70 | + spnGrade.onItemSelectedListener = spinnerListener |
| 71 | + spnSubject.onItemSelectedListener = spinnerListener |
| 72 | + } |
| 73 | + |
| 74 | + private fun setupSearchWatcher() { |
| 75 | + searchTextWatcher = object : TextWatcher { |
| 76 | + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} |
| 77 | + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { |
| 78 | + if (!etSearch.isFocused) return |
| 79 | + searchJob?.cancel() |
| 80 | + searchJob = scope.launch { |
| 81 | + delay(300) |
| 82 | + onFilterChanged(currentState()) |
| 83 | + } |
| 84 | + } |
| 85 | + override fun afterTextChanged(s: Editable) {} |
| 86 | + } |
| 87 | + etSearch.addTextChangedListener(searchTextWatcher) |
| 88 | + } |
| 89 | + |
| 90 | + private fun setupClearTagsButton() { |
| 91 | + rootView.findViewById<View>(R.id.btn_clear_tags).setOnClickListener { clearAll() } |
| 92 | + } |
| 93 | + |
| 94 | + fun addTag(tag: RealmTag) { |
| 95 | + if (!searchTags.any { it.name == tag.name }) searchTags.add(tag) |
| 96 | + onFilterChanged(currentState()) |
| 97 | + refreshTagText() |
| 98 | + onScrollToTop() |
| 99 | + } |
| 100 | + |
| 101 | + fun setTags(list: List<RealmTag>) { |
| 102 | + searchTags.clear() |
| 103 | + list.forEach { tag -> if (!searchTags.any { it.name == tag.name }) searchTags.add(tag) } |
| 104 | + onFilterChanged(currentState()) |
| 105 | + onScrollToTop() |
| 106 | + } |
| 107 | + |
| 108 | + fun setSingleTag(tag: RealmTag) { |
| 109 | + searchTags.clear() |
| 110 | + searchTags.add(tag) |
| 111 | + tvSelected.text = tvSelected.context.getString(R.string.tag_selected, tag.name) |
| 112 | + onFilterChanged(currentState()) |
| 113 | + onScrollToTop() |
| 114 | + } |
| 115 | + |
| 116 | + fun clearAll() { |
| 117 | + searchTags.clear() |
| 118 | + etSearch.setText("") |
| 119 | + tvSelected.text = "" |
| 120 | + spnGrade.setSelection(0) |
| 121 | + spnSubject.setSelection(0) |
| 122 | + onFilterChanged(currentState()) |
| 123 | + onScrollToTop() |
| 124 | + } |
| 125 | + |
| 126 | + fun filterApplied(): Boolean = currentState().isActive |
| 127 | + |
| 128 | + fun currentState(): FilterState { |
| 129 | + val grade = spnGrade.selectedItem?.toString()?.takeIf { it != "All" } ?: "" |
| 130 | + val subject = spnSubject.selectedItem?.toString()?.takeIf { it != "All" } ?: "" |
| 131 | + return FilterState( |
| 132 | + searchText = etSearch.text.toString().trim(), |
| 133 | + grade = grade, |
| 134 | + subject = subject, |
| 135 | + tagNames = searchTags.mapNotNull { it.name } |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + fun setListVisible(visible: Boolean) { |
| 140 | + val visibility = if (visible) View.VISIBLE else View.GONE |
| 141 | + etSearch.visibility = visibility |
| 142 | + rootView.findViewById<View>(R.id.filter).visibility = visibility |
| 143 | + if (!visible) tvSelected.visibility = View.GONE |
| 144 | + } |
| 145 | + |
| 146 | + private fun refreshTagText() { |
| 147 | + tvSelected.text = searchTags.joinToString( |
| 148 | + separator = ",", |
| 149 | + prefix = tvSelected.context.getString(R.string.selected) |
| 150 | + ) { it.name.orEmpty() } |
| 151 | + } |
| 152 | + |
| 153 | + fun detach() { |
| 154 | + searchTextWatcher?.let { etSearch.removeTextChangedListener(it) } |
| 155 | + searchTextWatcher = null |
| 156 | + searchJob?.cancel() |
| 157 | + } |
| 158 | +} |
0 commit comments