-
-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AppCleaner, Deduplicator: Allow manual input for "minimum size"
- Loading branch information
Showing
4 changed files
with
142 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/src/main/java/eu/darken/sdmse/appcleaner/ui/settings/SizeInputDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package eu.darken.sdmse.appcleaner.ui.settings | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.text.format.Formatter | ||
import androidx.annotation.StringRes | ||
import androidx.core.widget.addTextChangedListener | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.google.android.material.slider.Slider | ||
import eu.darken.sdmse.databinding.ViewPreferenceInputSizeBinding | ||
|
||
class SizeInputDialog( | ||
private val activity: Activity, | ||
@StringRes private val titleRes: Int, | ||
private val minimumSize: Long = 0, | ||
private val maximumSize: Long = 100 * 1000 * 1024L, | ||
private val currentSize: Long, | ||
private val onReset: () -> Unit, | ||
private val onCancel: () -> Unit = {}, | ||
private val onSave: (Long) -> Unit, | ||
) { | ||
|
||
private val context: Context | ||
get() = activity | ||
|
||
private fun parseSize(input: String): Long? { | ||
val match = SIZE_UNITS_REGEX.matchEntire(input.trim()) ?: return null | ||
val (value, _, unit) = match.destructured | ||
val factor = SIZE_UNITS[unit.uppercase()] ?: return null | ||
return (value.toDoubleOrNull()?.times(factor))?.toLong() | ||
} | ||
|
||
private val dialogLayout = ViewPreferenceInputSizeBinding.inflate(activity.layoutInflater, null, false).apply { | ||
slider.valueFrom = minimumSize.toFloat() | ||
slider.valueTo = (maximumSize / KB_MULTIPLIER).toFloat() | ||
|
||
fun setSliderSize(size: Long) { | ||
slider.value = (size.coerceAtMost(maximumSize) / KB_MULTIPLIER) | ||
.coerceAtLeast(minimumSize / KB_MULTIPLIER) | ||
.toFloat() | ||
} | ||
|
||
fun getSliderSize(): Long = slider.value.toLong() * KB_MULTIPLIER | ||
fun setSizeText(size: Long): Unit = sizeText.setText(Formatter.formatShortFileSize(context, size)) | ||
|
||
setSliderSize(currentSize) | ||
setSizeText(currentSize) | ||
|
||
slider.setLabelFormatter { Formatter.formatShortFileSize(context, getSliderSize()) } | ||
|
||
sizeText.addTextChangedListener { | ||
parseSize(it.toString())?.let { setSliderSize(it) } | ||
} | ||
|
||
slider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener { | ||
override fun onStartTrackingTouch(slider: Slider) { | ||
sizeText.clearFocus() | ||
setSizeText(getSliderSize()) | ||
} | ||
|
||
override fun onStopTrackingTouch(slider: Slider) { | ||
setSizeText(getSliderSize()) | ||
} | ||
}) | ||
} | ||
private val dialog = MaterialAlertDialogBuilder(context).apply { | ||
setTitle(titleRes) | ||
setView(dialogLayout.root) | ||
setPositiveButton(eu.darken.sdmse.common.R.string.general_save_action) { _, _ -> | ||
onSave(dialogLayout.slider.value.toLong() * KB_MULTIPLIER) | ||
} | ||
setNegativeButton(eu.darken.sdmse.common.R.string.general_cancel_action) { _, _ -> | ||
onCancel() | ||
} | ||
setNeutralButton(eu.darken.sdmse.common.R.string.general_reset_action) { _, _ -> | ||
onReset() | ||
} | ||
} | ||
|
||
fun show() { | ||
dialog.show() | ||
} | ||
|
||
companion object { | ||
private const val KB_MULTIPLIER = 1024L | ||
private val SIZE_UNITS_REGEX = Regex("(\\d+(\\.\\d+)?)\\s*(B|KB|MB|GB)", RegexOption.IGNORE_CASE) | ||
private val SIZE_UNITS = mapOf( | ||
"B" to 1L, | ||
"KB" to 1_000L, | ||
"MB" to 1_000_000L, | ||
"GB" to 1_000_000_000L, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:padding="16dp"> | ||
|
||
<com.google.android.material.textfield.TextInputEditText | ||
android:id="@+id/size_text" | ||
style="@style/ThemeOverlay.Material3.TextInputEditText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="16dp" | ||
android:ems="6" | ||
android:gravity="center" | ||
app:layout_constraintBottom_toTopOf="@id/slider" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:text="100.00 MB" /> | ||
|
||
<com.google.android.material.slider.Slider | ||
android:id="@+id/slider" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:contentDescription="Slider for minimum app junk size" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/size_text" | ||
tools:ignore="HardcodedText" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
e9fb92a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice