mirror of
https://github.com/skyline-emu/skyline.git
synced 2025-01-06 06:05:29 +03:00
Show an undo button after deleting a gpu driver
After a driver has been deleted, a snackbar will be shown confirming the deletion, with an button to undo it.
This commit is contained in:
parent
59c60df993
commit
c812de48ea
@ -73,6 +73,11 @@ open class GenericAdapter : RecyclerView.Adapter<GenericViewHolder<ViewBinding>>
|
|||||||
filter.filter(currentSearchTerm)
|
filter.filter(currentSearchTerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open fun addItemAt(position : Int, item : GenericListItem<out ViewBinding>) {
|
||||||
|
allItems.add(position, item)
|
||||||
|
filter.filter(currentSearchTerm)
|
||||||
|
}
|
||||||
|
|
||||||
fun setOnFilterPublishedListener(listener : OnFilterPublishedListener) {
|
fun setOnFilterPublishedListener(listener : OnFilterPublishedListener) {
|
||||||
onFilterPublishedListener = listener
|
onFilterPublishedListener = listener
|
||||||
}
|
}
|
||||||
@ -168,7 +173,7 @@ class SelectableGenericAdapter(private val defaultPosition : Int) : GenericAdapt
|
|||||||
/**
|
/**
|
||||||
* Removes the item at [position] from the list and updates the selected position accordingly
|
* Removes the item at [position] from the list and updates the selected position accordingly
|
||||||
*/
|
*/
|
||||||
override fun removeItemAt(position: Int) {
|
override fun removeItemAt(position : Int) {
|
||||||
super.removeItemAt(position)
|
super.removeItemAt(position)
|
||||||
if (position < selectedPosition)
|
if (position < selectedPosition)
|
||||||
selectedPosition--
|
selectedPosition--
|
||||||
|
@ -15,8 +15,8 @@ object GpuDriverBindingFactory : ViewBindingFactory {
|
|||||||
|
|
||||||
open class GpuDriverViewItem(
|
open class GpuDriverViewItem(
|
||||||
val driverMetadata : GpuDriverMetadata,
|
val driverMetadata : GpuDriverMetadata,
|
||||||
private val onDelete : ((wasChecked : Boolean) -> Unit)? = null,
|
var onDelete : ((position : Int, wasChecked : Boolean) -> Unit)? = null,
|
||||||
private val onClick : (() -> Unit)? = null
|
var onClick : (() -> Unit)? = null
|
||||||
) : SelectableGenericListItem<GpuDriverItemBinding>() {
|
) : SelectableGenericListItem<GpuDriverItemBinding>() {
|
||||||
private var position = -1
|
private var position = -1
|
||||||
|
|
||||||
@ -42,15 +42,15 @@ open class GpuDriverViewItem(
|
|||||||
onClick?.invoke()
|
onClick?.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onDelete != null) {
|
onDelete?.let { onDelete ->
|
||||||
binding.deleteButton.visibility = ViewGroup.VISIBLE
|
binding.deleteButton.visibility = ViewGroup.VISIBLE
|
||||||
binding.deleteButton.setOnClickListener {
|
binding.deleteButton.setOnClickListener {
|
||||||
val wasChecked = position == selectableAdapter?.selectedPosition
|
val wasChecked = position == selectableAdapter?.selectedPosition
|
||||||
selectableAdapter?.removeItemAt(position)
|
selectableAdapter?.removeItemAt(position)
|
||||||
|
|
||||||
onDelete.invoke(wasChecked)
|
onDelete.invoke(position, wasChecked)
|
||||||
}
|
}
|
||||||
} else {
|
} ?: run {
|
||||||
binding.deleteButton.visibility = ViewGroup.GONE
|
binding.deleteButton.visibility = ViewGroup.GONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,17 +79,36 @@ class GpuDriverActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GpuDriverHelper.getInstalledDrivers(this).onEachIndexed { index, (file, metadata) ->
|
GpuDriverHelper.getInstalledDrivers(this).onEachIndexed { index, (file, metadata) ->
|
||||||
items.add(GpuDriverViewItem(metadata, { wasChecked ->
|
items.add(GpuDriverViewItem(metadata).apply {
|
||||||
if (wasChecked) {
|
onDelete = { position, wasChecked ->
|
||||||
// If the deleted driver was the selected one, select the system driver
|
// If the driver was selected, select the system driver as the active one
|
||||||
preferenceSettings.gpuDriver = PreferenceSettings.SYSTEM_GPU_DRIVER
|
if (wasChecked)
|
||||||
|
preferenceSettings.gpuDriver = PreferenceSettings.SYSTEM_GPU_DRIVER
|
||||||
|
|
||||||
|
Snackbar.make(binding.root, "${metadata.label} deleted", Snackbar.LENGTH_LONG).setAction(R.string.undo) {
|
||||||
|
this@GpuDriverActivity.adapter.run {
|
||||||
|
addItemAt(position, this@apply)
|
||||||
|
// If the item was selected before removal, set it back as the active one when undoing
|
||||||
|
if (wasChecked) {
|
||||||
|
// Only notify previous to avoid notifying items before indexes have updated, the newly inserted item will be updated on bind
|
||||||
|
selectAndNotifyPrevious(position)
|
||||||
|
preferenceSettings.gpuDriver = metadata.label
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.addCallback(object : Snackbar.Callback() {
|
||||||
|
override fun onDismissed(transientBottomBar : Snackbar?, event : Int) {
|
||||||
|
// Only delete the driver directory if the user didn't undo the deletion
|
||||||
|
if (event != DISMISS_EVENT_ACTION) {
|
||||||
|
file.deleteRecursively()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).show()
|
||||||
}
|
}
|
||||||
if (!file.deleteRecursively()) {
|
|
||||||
Snackbar.make(binding.root, getString(R.string.gpu_driver_delete_failed), Snackbar.LENGTH_LONG).show()
|
onClick = {
|
||||||
|
preferenceSettings.gpuDriver = metadata.label
|
||||||
}
|
}
|
||||||
}, {
|
})
|
||||||
preferenceSettings.gpuDriver = metadata.label
|
|
||||||
}))
|
|
||||||
|
|
||||||
if (preferenceSettings.gpuDriver == metadata.label) {
|
if (preferenceSettings.gpuDriver == metadata.label) {
|
||||||
adapter.selectedPosition = index + 1 // Add 1 to account for the system driver entry
|
adapter.selectedPosition = index + 1 // Add 1 to account for the system driver entry
|
||||||
|
@ -184,4 +184,5 @@
|
|||||||
<!-- Misc -->
|
<!-- Misc -->
|
||||||
<!--suppress AndroidLintUnusedResources -->
|
<!--suppress AndroidLintUnusedResources -->
|
||||||
<string name="expand_button_title" tools:override="true">Expand</string>
|
<string name="expand_button_title" tools:override="true">Expand</string>
|
||||||
|
<string name="undo">Undo</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user