Skip to content

Commit b47424e

Browse files
committed
Fix snapshot auto-remove cancel behavior
Closes #2023
1 parent dfd7c3c commit b47424e

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

app/src/main/kotlin/com/absinthe/libchecker/features/snapshot/ui/TimeNodeBottomSheetDialogFragment.kt

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,35 @@ class TimeNodeBottomSheetDialogFragment : BaseBottomSheetViewDialogFragment<Time
6868
} else {
6969
root.adapter.addHeaderView(
7070
TimeNodeAutoRemoveView(requireContext()).also { autoRemoveView ->
71+
fun recordChanged(checked: Boolean) {
72+
Telemetry.recordEvent(
73+
Constants.Event.SNAPSHOT_ADVANCED_MENU_ITEM_CHANGED,
74+
mapOf(
75+
Telemetry.Param.CONTENT to autoRemoveView.chip.text,
76+
Telemetry.Param.VALUE to checked
77+
)
78+
)
79+
}
80+
7181
autoRemoveView.chip.onCheckedChangeListener = { _, checked ->
7282
autoRemoveView.invalidateText()
7383
if (checked) {
74-
(context as? ContextThemeWrapper)?.let { ctw ->
75-
val dialog = UiUtils.createSnapshotAutoRemoveThresholdDialog(ctw)
76-
dialog.setOnDismissListener {
84+
val ctw = context as? ContextThemeWrapper
85+
if (ctw == null) {
86+
autoRemoveView.syncWithAutoRemoveThreshold()
87+
} else {
88+
var confirmedThreshold: Int? = null
89+
val dialog = UiUtils.createSnapshotAutoRemoveThresholdDialog(ctw) { threshold ->
90+
confirmedThreshold = threshold
7791
autoRemoveView.invalidateText()
78-
if (GlobalValues.snapshotAutoRemoveThreshold > 0) {
92+
recordChanged(true)
93+
}
94+
dialog.setOnDismissListener {
95+
autoRemoveView.syncWithAutoRemoveThreshold()
96+
confirmedThreshold?.let { threshold ->
7997
lifecycleScope.launch(Dispatchers.IO) {
8098
Repositories.lcRepository.retainLatestSnapshotsAndRemoveOld(
81-
count = GlobalValues.snapshotAutoRemoveThreshold,
99+
count = threshold,
82100
forceShowLoading = true,
83101
context = ctw
84102
)
@@ -95,14 +113,9 @@ class TimeNodeBottomSheetDialogFragment : BaseBottomSheetViewDialogFragment<Time
95113
}
96114
} else {
97115
GlobalValues.snapshotAutoRemoveThreshold = -1
116+
autoRemoveView.invalidateText()
117+
recordChanged(false)
98118
}
99-
Telemetry.recordEvent(
100-
Constants.Event.SNAPSHOT_ADVANCED_MENU_ITEM_CHANGED,
101-
mapOf(
102-
Telemetry.Param.CONTENT to autoRemoveView.chip.text,
103-
Telemetry.Param.VALUE to checked
104-
)
105-
)
106119
}
107120
}
108121
)

app/src/main/kotlin/com/absinthe/libchecker/features/snapshot/ui/view/TimeNodeAutoRemoveView.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class TimeNodeAutoRemoveView(context: Context) : AViewGroup(context) {
3737
}
3838
}
3939

40+
fun syncWithAutoRemoveThreshold() {
41+
val listener = chip.onCheckedChangeListener
42+
chip.onCheckedChangeListener = null
43+
chip.isChecked = GlobalValues.snapshotAutoRemoveThreshold > 0
44+
chip.onCheckedChangeListener = listener
45+
invalidateText()
46+
}
47+
4048
override fun generateDefaultLayoutParams(): ViewGroup.LayoutParams {
4149
return ViewGroup.LayoutParams(
4250
ViewGroup.LayoutParams.MATCH_PARENT,

app/src/main/kotlin/com/absinthe/libchecker/utils/UiUtils.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ object UiUtils {
115115
return drawable
116116
}
117117

118-
fun createSnapshotAutoRemoveThresholdDialog(context: ContextThemeWrapper): AlertDialog {
119-
if (GlobalValues.snapshotAutoRemoveThreshold <= 0) {
120-
GlobalValues.snapshotAutoRemoveThreshold = 5
121-
}
118+
fun createSnapshotAutoRemoveThresholdDialog(
119+
context: ContextThemeWrapper,
120+
onThresholdConfirmed: ((threshold: Int) -> Unit)? = null
121+
): AlertDialog {
122+
val threshold = GlobalValues.snapshotAutoRemoveThreshold.takeIf { it > 0 } ?: 5
122123
val slider = Slider(ContextThemeWrapper(context, R.style.App_Widget_M3E_Slider)).apply {
123124
layoutParams =
124125
ViewGroup.MarginLayoutParams(200.dp, ViewGroup.LayoutParams.WRAP_CONTENT).also {
@@ -127,7 +128,7 @@ object UiUtils {
127128
stepSize = 1f
128129
valueFrom = 2f
129130
valueTo = 10f
130-
value = GlobalValues.snapshotAutoRemoveThreshold.toFloat()
131+
value = threshold.toFloat()
131132
}
132133
return BaseAlertDialogBuilder(context)
133134
.setTitle(R.string.album_item_management_snapshot_auto_remove_default_title)
@@ -140,7 +141,9 @@ object UiUtils {
140141
)
141142
.setCancelable(false)
142143
.setPositiveButton(android.R.string.ok) { _, _ ->
143-
GlobalValues.snapshotAutoRemoveThreshold = slider.value.toInt()
144+
val confirmedThreshold = slider.value.toInt()
145+
GlobalValues.snapshotAutoRemoveThreshold = confirmedThreshold
146+
onThresholdConfirmed?.invoke(confirmedThreshold)
144147
}
145148
.setNegativeButton(android.R.string.cancel, null)
146149
.create()

0 commit comments

Comments
 (0)