Skip to content

Commit

Permalink
feat: list searching
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed May 11, 2024
1 parent e1a865c commit 988e4de
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/ani/dantotsu/media/CalendarActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CalendarActivity : AppCompatActivity() {
binding.listTitle.setText(R.string.release_calendar)
binding.listSort.visibility = View.GONE
binding.random.visibility = View.GONE
binding.search.visibility = View.GONE
binding.listTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
this@CalendarActivity.selectedTabIdx = tab?.position ?: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
} catch (e: Exception) {
Injekt.get<CrashlyticsInterface>().logException(e)
Logger.log(e)
toast("Error starting video")
toast("Error starting video: ${e.message}")
dismiss()
}
}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/ani/dantotsu/media/user/ListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import ani.dantotsu.R
Expand All @@ -26,6 +29,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class ListActivity : AppCompatActivity() {
private lateinit var binding: ActivityListBinding
private val scope = lifecycleScope
Expand Down Expand Up @@ -177,5 +181,28 @@ class ListActivity : AppCompatActivity() {
supportFragmentManager.findFragmentByTag("f" + currentTab?.position.toString()) as? ListFragment
currentFragment?.randomOptionClick()
}

binding.search.setOnClickListener {
toggleSearchView(binding.searchView.isVisible)
if (!binding.searchView.isVisible) {
model.unfilterLists()
}
}

binding.searchViewText.addTextChangedListener {
model.searchLists(binding.searchViewText.text.toString())
}
}

private fun toggleSearchView(isVisible: Boolean) {
if (isVisible) {
binding.searchView.visibility = View.GONE
binding.searchViewText.text.clear()
} else {
binding.searchView.visibility = View.VISIBLE
binding.searchViewText.requestFocus()
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(binding.searchViewText, InputMethodManager.SHOW_IMPLICIT)
}
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/ani/dantotsu/media/user/ListViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@ class ListViewModel : ViewModel() {
lists.postValue(filteredLists)
}

fun searchLists(search: String) {
if (search.isEmpty()) {
lists.postValue(unfilteredLists.value)
return
}
val currentLists = unfilteredLists.value ?: return
val filteredLists = currentLists.mapValues { entry ->
entry.value.filter { media ->
media.name?.contains(
search,
ignoreCase = true
) == true || media.synonyms.any { it.contains(search, ignoreCase = true) } ||
media.nameRomaji.contains(
search,
ignoreCase = true
)
} as ArrayList<Media>
}.toMutableMap()

lists.postValue(filteredLists)
}

fun unfilterLists() {
lists.postValue(unfilteredLists.value)
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:visibility="gone"></FrameLayout>
android:visibility="gone"/>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/searchView"
Expand Down
71 changes: 61 additions & 10 deletions app/src/main/res/layout/activity_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,98 @@
<TextView
android:id="@+id/listTitle"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:ellipsize="end"
android:fontFamily="@font/poppins_bold"
android:gravity="center|start"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textColor="?attr/colorOnBackground"
android:textSize="16sp"
android:textSize="14sp"
tools:text="@string/app_name" />

<ImageButton
android:id="@+id/search"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/search"
app:srcCompat="@drawable/ic_round_search_24"
app:tint="?attr/colorOnBackground" />

<ImageButton
android:id="@+id/random"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/random_selection"
app:srcCompat="@drawable/ic_shuffle_24"
app:tint="?attr/colorOnBackground" />

<ImageButton
android:id="@+id/filter"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/filter"
app:srcCompat="@drawable/ic_round_filter_alt_24"
app:tint="?attr/colorOnBackground" />

<ImageButton
android:id="@+id/listSort"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginEnd="8dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="4dp"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/sort_by"
app:srcCompat="@drawable/ic_round_sort_24"
app:tint="?attr/colorOnBackground" />

</LinearLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/searchView"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:transitionName="@string/search"
app:boxBackgroundColor="?android:attr/colorBackground"
app:hintTextColor="?attr/colorOnBackground"
app:boxStrokeColor="?attr/colorOnBackground"
app:cursorColor="?attr/colorOnBackground"
app:boxBackgroundMode="outline"
app:boxCornerRadiusBottomEnd="28dp"
app:boxCornerRadiusBottomStart="28dp"
app:boxCornerRadiusTopEnd="28dp"
app:boxCornerRadiusTopStart="28dp"
app:endIconDrawable="@drawable/ic_round_search_24"
app:endIconTint="?attr/colorOnBackground"
android:visibility="gone"
tools:visibility="visible"
app:hintAnimationEnabled="true">

<AutoCompleteTextView
android:id="@+id/searchViewText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:fontFamily="@font/poppins_bold"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="textPersonName"
android:padding="8dp"
android:paddingBottom="4dp"
android:selectAllOnFocus="true"
android:textSize="14sp"
tools:ignore="LabelFor,TextContrastCheck" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

<com.google.android.material.tabs.TabLayout
Expand Down

0 comments on commit 988e4de

Please sign in to comment.