From 6a9f8d3e382682835a7dca8429b78cdef3bbe0d6 Mon Sep 17 00:00:00 2001 From: Ishan Taldekar Date: Tue, 28 Jan 2025 14:29:16 -0500 Subject: [PATCH] Add ViewRouter POC --- .../eclipse/amazonq/views/router/ViewId.java | 9 ++ .../amazonq/views/router/ViewRouter.java | 87 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewId.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewRouter.java diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewId.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewId.java new file mode 100644 index 00000000..8ff6272e --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewId.java @@ -0,0 +1,9 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package software.aws.toolkits.eclipse.amazonq.views.router; + +public enum ViewId { + TOOLKIT_LOGIN_VIEW, CHAT_VIEW, DEPENDENCY_MISSING_VIEW, RE_AUTHENTICATE_VIEW, CHAT_ASSET_MISSING_VIEW, + CODE_REFERENCE_VIEW, LSP_STARTUP_FAILED_VIEW, LSP_INITIALIZING_VIEW +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewRouter.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewRouter.java new file mode 100644 index 00000000..289b7f0e --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/router/ViewRouter.java @@ -0,0 +1,87 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package software.aws.toolkits.eclipse.amazonq.views.router; + +import software.aws.toolkits.eclipse.amazonq.broker.api.EventObserver; +import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState; +import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspState; +import software.aws.toolkits.eclipse.amazonq.plugin.Activator; + +public final class ViewRouter { + + private final EventObserver authStateObserver = new EventObserver<>() { + @Override + public void onEvent(final AuthState newAuthState) { + authState = newAuthState; + refreshActiveView(); + } + }; + + private final EventObserver lspStateObserver = new EventObserver<>() { + @Override + public void onEvent(final LspState newLspState) { + lspState = newLspState; + refreshActiveView(); + } + }; + + private ViewId activeViewId; + + // this state needs to be maintained to ensure correct resolution in refreshActiveView + private LspState lspState; + private AuthState authState; + + public ViewRouter() { + activeViewId = ViewId.TOOLKIT_LOGIN_VIEW; + lspState = null; + authState = null; + + Activator.getEventBroker().subscribe(AuthState.class, authStateObserver); + Activator.getEventBroker().subscribe(LspState.class, lspStateObserver); + } + + private void refreshActiveView() { + ViewId newActiveViewId = ViewId.TOOLKIT_LOGIN_VIEW; + + if (isDependencyMissing()) { // TODO: dependency missing check logic needs to be implemented + newActiveViewId = ViewId.DEPENDENCY_MISSING_VIEW; + } else if (lspState != null) { + if (lspState == LspState.FAILED) { + newActiveViewId = ViewId.LSP_STARTUP_FAILED_VIEW; + } else if (lspState == LspState.PENDING) { + newActiveViewId = ViewId.LSP_INITIALIZING_VIEW; + } + } else if (isChatUIAssetMissing()) { // TODO: chat missing logic needs to be implemented + newActiveViewId = ViewId.CHAT_ASSET_MISSING_VIEW; + } else if (authState != null) { + if (authState.isLoggedOut()) { + newActiveViewId = ViewId.TOOLKIT_LOGIN_VIEW; + } else if (authState.isExpired()) { + newActiveViewId = ViewId.RE_AUTHENTICATE_VIEW; + } + } else { + newActiveViewId = ViewId.CHAT_VIEW; + } + + if (activeViewId != newActiveViewId) { + activeViewId = newActiveViewId; + notifyActiveViewChange(); + } + } + + private void notifyActiveViewChange() { + Activator.getEventBroker().post(activeViewId); + } + + // TODO: replace with relevant checks + private boolean isDependencyMissing() { + return false; + } + + // TODO: replace with relevant checks + private boolean isChatUIAssetMissing() { + return false; + } + +}