-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
ActionsRegistry.kt
121 lines (103 loc) · 3.87 KB
/
ActionsRegistry.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.actions
import android.view.Menu
import com.itsaky.androidide.actions.internal.DefaultActionsRegistry
import com.itsaky.androidide.utils.ServiceLoader
import com.itsaky.androidide.utils.VMUtils
/** @author Akash Yadav */
abstract class ActionsRegistry {
companion object {
private var instance: ActionsRegistry? = null
@JvmStatic
fun getInstance(): ActionsRegistry {
if (instance == null) {
instance = if (VMUtils.isJvm()) {
DefaultActionsRegistry()
} else ServiceLoader.load(ActionsRegistry::class.java).findFirstOrThrow()
}
return instance as ActionsRegistry
}
}
/**
* Register the given action.
*
* @param action The action to register. Must not be `null`.
* @return `true` if the action was registered successfully. `false` otherwise.
*/
abstract fun registerAction(action: ActionItem): Boolean
/**
* Unregister the given action.
*
* @param action The action to unregister. Must not be `null`.
* @return `true` if the action was unregistered successfully. `false` otherwise.
*/
abstract fun unregisterAction(action: ActionItem): Boolean
/**
* Unregister the action with the given ID.
*
* @param id The ID of action to unregister. Must not be `null`.
* @return `true` if the action was unregistered successfully. `false` otherwise.
*/
abstract fun unregisterAction(id: String): Boolean
/**
* Find the action with the given ID.
*
* @param id The ID of the action to find.
* @return The found action or `null`.
*/
abstract fun findAction(location: ActionItem.Location, id: String): ActionItem?
/**
* Find the action with the given unique item ID.
*
* @param itemId The ID of the action to find.
* @return The found action or `null`.
*/
abstract fun findAction(location: ActionItem.Location, itemId: Int): ActionItem?
/**
* Fill the given menu with the registered actions.
*
* Subclasses must first call [ActionItem.prepare] to update the action.
*/
abstract fun fillMenu(params: FillMenuParams)
/** Get all the registered actions at the given location. */
abstract fun getActions(location: ActionItem.Location): Map<String, ActionItem>
/** Clear all the registered actions. */
abstract fun clearActions(location: ActionItem.Location)
/**
* Register a listener that will be called when any action is executed. The callbacks are called
* irrespective of the action's execution result.
*
* @param listener The listener to register.
*/
abstract fun registerActionExecListener(listener: ActionExecListener)
/**
* Unregister the given listener.
*
* @param listener The listener to unregister.
*/
abstract fun unregisterActionExecListener(listener: ActionExecListener)
/** Listener for listening to callbacks for action execution. */
interface ActionExecListener {
/**
* The given action was executed.
* @param action The action that was executed.
* @param result The result of the action execution.
*/
fun onExec(action: ActionItem, result: Any)
}
}