Skip to content

Commit

Permalink
Fix : My search uses divider and fixed padding
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldoshii committed Feb 7, 2024
1 parent 1c784ee commit 389bfc1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.input.input
import com.afollestad.materialdialogs.list.customListAdapter
import com.afollestad.materialdialogs.list.getRecyclerView
import com.ichi2.anki.R
import com.ichi2.anki.analytics.AnalyticsDialogFragment
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.ui.ButtonItemAdapter
import com.ichi2.utils.customListAdapterWithDecoration
import com.ichi2.utils.input
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
import com.ichi2.utils.positiveButton
import com.ichi2.utils.show
import com.ichi2.utils.title
import timber.log.Timber

// TODO: Add different classes for the two different dialogs
Expand All @@ -37,7 +34,7 @@ class CardBrowserMySearchesDialog : AnalyticsDialogFragment() {
@SuppressLint("CheckResult")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreate(savedInstanceState)
val dialog = MaterialDialog(requireActivity())
val alertDialog = AlertDialog.Builder(requireActivity())
val type = requireArguments().getInt("type")
if (type == CARD_BROWSER_MY_SEARCHES_TYPE_LIST) {
savedFilters = requireArguments().getSerializableCompat("savedFilters")
Expand All @@ -51,36 +48,31 @@ class CardBrowserMySearchesDialog : AnalyticsDialogFragment() {
itemCallback = { searchName ->
Timber.d("item clicked: %s", searchName)
mySearchesDialogListener!!.onSelection(searchName)
dialog.dismiss()
dismiss()
},
buttonCallback = { searchName ->
Timber.d("button clicked: %s", searchName)
removeSearch(searchName)
}
).apply {
notifyAdapterDataSetChanged() // so the values are sorted.
dialog.title(text = resources.getString(R.string.card_browser_list_my_searches_title))
.customListAdapter(this, null)
alertDialog.title(text = resources.getString(R.string.card_browser_list_my_searches_title))
.customListAdapterWithDecoration(this, requireActivity())
}
} else if (type == CARD_BROWSER_MY_SEARCHES_TYPE_SAVE) {
val currentSearchTerms = requireArguments().getString("currentSearchTerms")
dialog.title(text = getString(R.string.card_browser_list_my_searches_save))
.positiveButton(android.R.string.ok)
.negativeButton(R.string.dialog_cancel)
.input(hintRes = R.string.card_browser_list_my_searches_new_name) { _: MaterialDialog?, text: CharSequence ->
Timber.d("Saving search with title/terms: %s/%s", text, currentSearchTerms)
mySearchesDialogListener!!.onSaveSearch(text.toString(), currentSearchTerms)
}
}
runCatching { dialog.getRecyclerView() }.onSuccess { recyclerView ->
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val dividerItemDecoration = DividerItemDecoration(recyclerView.context, layoutManager.orientation)
val scale = resources.displayMetrics.density
val dpAsPixels = (5 * scale + 0.5f).toInt()
dialog.view.setPadding(dpAsPixels, 0, dpAsPixels, dpAsPixels)
recyclerView.addItemDecoration(dividerItemDecoration)
AlertDialog.Builder(requireActivity()).show {
title(text = getString(R.string.card_browser_list_my_searches_save))
positiveButton(android.R.string.ok)
negativeButton(R.string.dialog_cancel)
setView(R.layout.dialog_generic_text_input)
}.input(hint = getString(R.string.card_browser_list_my_searches_new_name), allowEmpty = false, displayKeyboard = true, waitForPositiveButton = true) { dialog, text ->
Timber.d("Saving search with title/terms: %s/%s", text, currentSearchTerms)
mySearchesDialogListener?.onSaveSearch(text.toString(), currentSearchTerms)
dialog.dismiss()
}
}
return dialog
return alertDialog.show()
}

private fun removeSearch(searchName: String) {
Expand Down
16 changes: 16 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/utils/AlertDialogFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.ichi2.utils

import android.content.Context
import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.text.InputFilter
Expand All @@ -31,6 +32,7 @@ import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -202,6 +204,20 @@ fun AlertDialog.Builder.customListAdapter(adapter: RecyclerView.Adapter<*>) {
this.setView(recyclerView)
}

/**
* Adds a RecyclerView with a custom adapter and decoration to the AlertDialog.
* @param adapter The adapter for the RecyclerView.
* @param context The context used to access resources and LayoutInflater.
*/
fun AlertDialog.Builder.customListAdapterWithDecoration(adapter: RecyclerView.Adapter<*>, context: Context) {
val recyclerView = LayoutInflater.from(context).inflate(R.layout.dialog_generic_recycler_view, null, false) as RecyclerView
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(context)
val dividerItemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)
recyclerView.addItemDecoration(dividerItemDecoration)
this.setView(recyclerView)
}

/**
* @param hint The hint text to be displayed to the user
* @param prefill The text to initially appear in the [EditText]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:paddingTop="5dp"
Expand Down

0 comments on commit 389bfc1

Please sign in to comment.