Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
refactor(actions): use ServiceLoader to load DefaultActionsRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Apr 30, 2023
1 parent b950c34 commit 360bbb7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.itsaky.androidide.actions

import android.view.Menu
import com.itsaky.androidide.actions.internal.DefaultActionsRegistry
import com.itsaky.androidide.utils.ServiceLoader

/** @author Akash Yadav */
abstract class ActionsRegistry {
Expand All @@ -29,7 +29,7 @@ abstract class ActionsRegistry {
@JvmStatic
fun getInstance(): ActionsRegistry {
if (instance == null) {
instance = DefaultActionsRegistry()
instance = ServiceLoader.load(ActionsRegistry::class.java).findFirstOrThrow()
}

return instance as ActionsRegistry
Expand Down Expand Up @@ -77,6 +77,7 @@ abstract class ActionsRegistry {

/** 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)

Expand Down
Binary file modified app/src/main/assets/data/common/tooling-api-all.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@
* 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.internal
package com.itsaky.androidide.actions

import android.content.Context
import android.graphics.PorterDuff.Mode.SRC_ATOP
import android.graphics.PorterDuffColorFilter
import android.view.Menu
import android.view.MenuItem
import com.blankj.utilcode.util.ThreadUtils
import com.itsaky.androidide.actions.ActionData
import com.itsaky.androidide.actions.ActionItem
import com.itsaky.androidide.actions.ActionMenu
import com.itsaky.androidide.actions.ActionsRegistry
import com.itsaky.androidide.actions.R
import com.google.auto.service.AutoService
import com.itsaky.androidide.actions.locations.CodeActionsMenu
import com.itsaky.androidide.utils.ILogger
import com.itsaky.androidide.utils.resolveAttr
Expand All @@ -38,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap
*
* @author Akash Yadav
*/
@AutoService(ActionsRegistry::class)
class DefaultActionsRegistry : ActionsRegistry() {

private val log = ILogger.newInstance("DefaultActionsRegistry")
Expand Down Expand Up @@ -117,33 +114,31 @@ class DefaultActionsRegistry : ActionsRegistry() {

private fun addActionToMenu(menu: Menu, action: ActionItem, data: ActionData) {
val context = data[Context::class.java]
val item: MenuItem =
if (action is ActionMenu) {
val sub = menu.addSubMenu(action.label)

var shouldBeEnabled = false
for (subItem in action.children) {
subItem.prepare(data)
if (subItem.visible) {
addActionToMenu(sub, subItem, data)
}

if (action.enabled && subItem.enabled && !shouldBeEnabled) {
shouldBeEnabled = true
}
val item: MenuItem = if (action is ActionMenu) {
val sub = menu.addSubMenu(action.label)

var shouldBeEnabled = false
for (subItem in action.children) {
subItem.prepare(data)
if (subItem.visible) {
addActionToMenu(sub, subItem, data)
}

action.enabled = shouldBeEnabled
sub.item
} else {
menu.add(action.label)
if (action.enabled && subItem.enabled && !shouldBeEnabled) {
shouldBeEnabled = true
}
}

action.enabled = shouldBeEnabled
sub.item
} else {
menu.add(action.label)
}

item.isEnabled = action.enabled
item.icon =
action.icon?.apply {
colorFilter = PorterDuffColorFilter(context!!.resolveAttr(R.attr.colorOnSurface), SRC_ATOP)
}
item.icon = action.icon?.apply {
colorFilter = PorterDuffColorFilter(context!!.resolveAttr(R.attr.colorOnSurface), SRC_ATOP)
}

if (item.icon != null) {
item.icon!!.alpha = if (action.enabled) 255 else 76
Expand Down Expand Up @@ -182,13 +177,11 @@ class DefaultActionsRegistry : ActionsRegistry() {

private fun execInBackground(action: ActionItem, data: ActionData) {
val start = System.currentTimeMillis()
CompletableFuture.supplyAsync { action.execAction(data) }
.whenComplete { result, error ->
CompletableFuture.supplyAsync { action.execAction(data) }.whenComplete { result, error ->
if (result == null || (result is Boolean && !result) || error != null) {
log.error(
"An error occurred when performing action '${action.id}'. Action failed in ${System.currentTimeMillis() - start}ms",
error
)
error)
} else {
log.info("Action '${action.id}' completed in ${System.currentTimeMillis() - start}ms")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.itsaky.androidide.actions.ActionData
import com.itsaky.androidide.actions.ActionItem.Location.EDITOR_FILE_TREE
import com.itsaky.androidide.actions.ActionMenu
import com.itsaky.androidide.actions.ActionsRegistry
import com.itsaky.androidide.actions.internal.DefaultActionsRegistry
import com.itsaky.androidide.actions.DefaultActionsRegistry
import com.itsaky.androidide.activities.editor.EditorHandlerActivity
import com.itsaky.androidide.eventbus.events.filetree.FileClickEvent
import com.itsaky.androidide.eventbus.events.filetree.FileLongClickEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,9 @@

package com.itsaky.androidide.lookup

import com.itsaky.androidide.lookup.Lookup.Key

/**
* For all calls to the provided lookup instance.
* Forwards all calls to the provided lookup instance.
*
* @author Akash Yadav
*/
abstract class ForwardingLookup(protected val lookup: Lookup) : Lookup {
override fun <T : Any?> register(klass: Class<T>?, instance: T) = lookup.register(klass, instance)

override fun <T : Any?> update(klass: Class<T>?, instance: T) = lookup.update(klass, instance)

override fun <T : Any?> unregister(klass: Class<T>?) = lookup.unregister(klass)

override fun <T : Any?> lookup(klass: Class<T>?): T? = lookup.lookup(klass)

override fun <T : Any?> register(key: Key<T>?, instance: T) = lookup.register(key, instance)

override fun <T : Any?> unregister(key: Key<T>?) = lookup.unregister(key)

override fun <T : Any?> lookup(key: Key<T>?): T? = lookup.lookup(key)

override fun <T : Any?> update(key: Key<T>?, instance: T) = lookup.update(key, instance)
}
open class ForwardingLookup(lookup: Lookup) : Lookup by lookup

0 comments on commit 360bbb7

Please sign in to comment.