Skip to content

Commit

Permalink
Add ViewRouter POC
Browse files Browse the repository at this point in the history
  • Loading branch information
taldekar committed Jan 28, 2025
1 parent 374e549 commit 6a9f8d3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<AuthState> authStateObserver = new EventObserver<>() {
@Override
public void onEvent(final AuthState newAuthState) {
authState = newAuthState;
refreshActiveView();
}
};

private final EventObserver<LspState> 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;
}

}

0 comments on commit 6a9f8d3

Please sign in to comment.