Skip to content

Commit 68c4b12

Browse files
david-allisonlukstbit
authored andcommitted
chore: convert EmptyCardsDialogFragment to ViewBinding
Issue 11116
1 parent 6765c6f commit 68c4b12

File tree

2 files changed

+26
-39
lines changed

2 files changed

+26
-39
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EmptyCardsDialogFragment.kt

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import android.view.View
3535
import android.view.ViewGroup
3636
import android.view.WindowInsets.Type.displayCutout
3737
import android.view.WindowInsets.Type.navigationBars
38-
import android.widget.CheckBox
3938
import android.widget.ScrollView
4039
import android.widget.TextView
4140
import androidx.appcompat.app.AlertDialog
@@ -51,6 +50,7 @@ import com.ichi2.anki.CardBrowser
5150
import com.ichi2.anki.CollectionManager.TR
5251
import com.ichi2.anki.DeckPicker
5352
import com.ichi2.anki.R
53+
import com.ichi2.anki.databinding.DialogEmptyCardsBinding
5454
import com.ichi2.anki.dialogs.EmptyCardsUiState.EmptyCardsSearchFailure
5555
import com.ichi2.anki.dialogs.EmptyCardsUiState.EmptyCardsSearchResult
5656
import com.ichi2.anki.dialogs.EmptyCardsUiState.SearchingForEmptyCards
@@ -73,28 +73,15 @@ import timber.log.Timber
7373
*/
7474
class EmptyCardsDialogFragment : DialogFragment() {
7575
private val viewModel by viewModels<EmptyCardsViewModel>()
76-
private val loadingContainer: View?
77-
get() = dialog?.findViewById(R.id.loading_container)
78-
private val loadingMessage: TextView?
79-
get() = dialog?.findViewById(R.id.text)
80-
private val emptyCardsResultsContainer: View?
81-
get() = dialog?.findViewById(R.id.empty_cards_results_container)
82-
private val emptyReportMessage: TextView?
83-
get() = dialog?.findViewById(R.id.empty_report_label)
84-
private val keepNotesWithNoValidCards: CheckBox?
85-
get() = dialog?.findViewById(R.id.preserve_notes)
86-
private val reportScrollView: ScrollView?
87-
get() = dialog?.findViewById(R.id.reportScrollView)
88-
private val reportView: TextView?
89-
get() = dialog?.findViewById(R.id.report)
76+
77+
private lateinit var binding: DialogEmptyCardsBinding
9078

9179
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
80+
binding = DialogEmptyCardsBinding.inflate(layoutInflater)
9281
bindToState()
9382
viewModel.searchForEmptyCards()
94-
val dialogView = layoutInflater.inflate(R.layout.dialog_empty_cards, null)
95-
dialogView.findViewById<CheckBox>(R.id.preserve_notes)?.text =
96-
TR.emptyCardsPreserveNotesCheckbox()
97-
dialogView.findViewById<TextView>(R.id.report).movementMethod = LinkMovementMethod.getInstance()
83+
binding.keepNotesWithNoValidCards.text = TR.emptyCardsPreserveNotesCheckbox()
84+
binding.report.movementMethod = LinkMovementMethod.getInstance()
9885

9986
return AlertDialog
10087
.Builder(requireContext())
@@ -113,14 +100,14 @@ class EmptyCardsDialogFragment : DialogFragment() {
113100
if (state.emptyCardsReport.emptyCids().isEmpty()) return@setPositiveButton
114101
(requireActivity() as DeckPicker).startDeletingEmptyCards(
115102
state.emptyCardsReport,
116-
keepNotesWithNoValidCards?.isChecked ?: true,
103+
binding.keepNotesWithNoValidCards.isChecked,
117104
)
118105
}
119106
}
120107
setNegativeButton(R.string.dialog_cancel) { _, _ ->
121108
Timber.i("Empty cards dialog cancelled")
122109
}
123-
setView(dialogView)
110+
setView(binding.root)
124111
}.also {
125112
// the initial start state is a loading state as we are looking for the empty cards,
126113
// so there's no "action" for ok just yet
@@ -134,37 +121,37 @@ class EmptyCardsDialogFragment : DialogFragment() {
134121
viewModel.uiState.collect { state ->
135122
when (state) {
136123
is SearchingForEmptyCards -> {
137-
loadingMessage?.text = getString(R.string.emtpy_cards_finding)
138-
loadingContainer?.isVisible = true
139-
emptyCardsResultsContainer?.isVisible = false
124+
binding.loadingMessage.text = getString(R.string.emtpy_cards_finding)
125+
binding.loadingContainer.isVisible = true
126+
binding.emptyCardsResultsContainer.isVisible = false
140127
(dialog as? AlertDialog)?.positiveButton?.apply {
141128
isEnabled = false
142129
text = getString(R.string.dialog_ok)
143130
}
144131
}
145132

146133
is EmptyCardsSearchResult -> {
147-
loadingContainer?.isVisible = false
134+
binding.loadingContainer.isVisible = false
148135
val emptyCards = state.emptyCardsReport.emptyCids()
149136
if (emptyCards.isEmpty()) {
150-
emptyReportMessage?.text = TR.emptyCardsNotFound()
151-
emptyReportMessage?.isVisible = true
137+
binding.emptyReportMessage.text = TR.emptyCardsNotFound()
138+
binding.emptyReportMessage.isVisible = true
152139
// nothing to delete so also hide the preserve notes check box
153-
keepNotesWithNoValidCards?.isVisible = false
140+
binding.keepNotesWithNoValidCards.isVisible = false
154141
(dialog as? AlertDialog)?.positiveButton?.text =
155142
getString(R.string.dialog_ok)
156143
(dialog as? AlertDialog)?.negativeButton?.visibility = View.GONE
157144
} else {
158-
reportScrollView?.updateViewHeight()
159-
reportView?.setText(
145+
binding.reportScrollView.updateViewHeight()
146+
binding.report.setText(
160147
state.emptyCardsReport.asActionableReport(),
161148
TextView.BufferType.SPANNABLE,
162149
)
163-
keepNotesWithNoValidCards?.isVisible = true
164-
emptyReportMessage?.isVisible = false
150+
binding.keepNotesWithNoValidCards.isVisible = true
151+
binding.emptyReportMessage.isVisible = false
165152
(dialog as? AlertDialog)?.positiveButton?.text =
166153
getString(R.string.dialog_positive_delete)
167-
emptyCardsResultsContainer?.isVisible = true
154+
binding.emptyCardsResultsContainer.isVisible = true
168155
}
169156
(dialog as? AlertDialog)?.positiveButton?.isEnabled = true
170157
}
@@ -313,7 +300,7 @@ class EmptyCardsDialogFragment : DialogFragment() {
313300

314301
override fun onConfigurationChanged(newConfig: Configuration) {
315302
super.onConfigurationChanged(newConfig)
316-
reportScrollView?.updateViewHeight()
303+
binding.reportScrollView.updateViewHeight()
317304
}
318305

319306
companion object {

AnkiDroid/src/main/res/layout/dialog_empty_cards.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
app:layout_constraintTop_toTopOf="parent"
3535
app:layout_constraintBottom_toBottomOf="parent"
3636
app:layout_constraintStart_toStartOf="parent"
37-
app:layout_constraintEnd_toStartOf="@+id/text"
37+
app:layout_constraintEnd_toStartOf="@+id/loading_message"
3838
app:layout_constraintHorizontal_chainStyle="packed"
3939
app:layout_constraintHorizontal_bias="0.0"
4040
android:indeterminate="true" />
@@ -46,7 +46,7 @@
4646
~ Using "wrap_content" here may cause message text overflow.
4747
-->
4848
<TextView
49-
android:id="@+id/text"
49+
android:id="@+id/loading_message"
5050
android:layout_width="0dp"
5151
android:layout_height="wrap_content"
5252
android:layout_marginStart="16dp"
@@ -59,7 +59,7 @@
5959
</androidx.constraintlayout.widget.ConstraintLayout>
6060

6161
<com.ichi2.ui.FixedTextView
62-
android:id="@+id/empty_report_label"
62+
android:id="@+id/empty_report_message"
6363
android:layout_width="match_parent"
6464
android:layout_height="wrap_content"
6565
android:paddingTop="16dp"
@@ -85,15 +85,15 @@
8585
app:layout_constraintTop_toTopOf="parent"
8686
android:layout_marginTop="8dp"
8787
android:paddingHorizontal="8dp"
88-
app:layout_constraintBottom_toTopOf="@+id/preserve_notes">
88+
app:layout_constraintBottom_toTopOf="@+id/keep_notes_with_no_valid_cards">
8989
<TextView
9090
android:id="@+id/report"
9191
android:layout_width="match_parent"
9292
android:layout_height="wrap_content"/>
9393
</ScrollView>
9494

9595
<CheckBox
96-
android:id="@+id/preserve_notes"
96+
android:id="@+id/keep_notes_with_no_valid_cards"
9797
android:layout_width="match_parent"
9898
android:layout_height="wrap_content"
9999
android:checked="true"

0 commit comments

Comments
 (0)