feat(actions): add API to destroy ActionItem instances

This commit is contained in:
Akash Yadav
2023-01-20 20:15:02 +05:30
parent 73b2946439
commit 715ef2e630
3 changed files with 17 additions and 1 deletions
@@ -23,6 +23,7 @@ import android.view.View
/**
* An action that can be registered using the [ActionsRegistry]
* [com.itsaky.androidide.actions.ActionsRegistry]
*
* @author Akash Yadav
*/
interface ActionItem {
@@ -37,6 +38,7 @@ interface ActionItem {
/**
* Prepare the action. Subclasses can modify the visual properties of this action here.
*
* @param data The data containing various information about the event.
*/
fun prepare(data: ActionData)
@@ -57,6 +59,12 @@ interface ActionItem {
*/
fun postExec(data: ActionData, result: Any) = Unit
/**
* Called when the action item is to be destroyed. Any resource references must be released if
* held.
*/
fun destroy() = Unit
/**
* Return the show as action flags for the menu item.
*
@@ -66,6 +66,7 @@ class DefaultActionsRegistry : ActionsRegistry() {
val actions = getActions(action.location)
val older = actions.remove(action.id)
if (older != null) {
older.destroy()
return true
}
@@ -76,6 +77,7 @@ class DefaultActionsRegistry : ActionsRegistry() {
for (locations in this.actions.values) {
val older = locations.remove(id)
if (older != null) {
older.destroy()
return true
}
}
@@ -86,7 +88,13 @@ class DefaultActionsRegistry : ActionsRegistry() {
override fun findAction(location: ActionItem.Location, id: String): ActionItem? =
getActions(location)[id]
override fun clearActions(location: ActionItem.Location) = getActions(location).clear()
override fun clearActions(location: ActionItem.Location) {
val actions = getActions(location)
actions.forEach {
it.value.destroy()
}
actions.clear()
}
override fun registerActionExecListener(listener: ActionExecListener) {
listeners.add(listener)
Binary file not shown.