|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.eclipse.amazonq.views.router; |
| 5 | + |
| 6 | +import software.aws.toolkits.eclipse.amazonq.broker.api.EventObserver; |
| 7 | +import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState; |
| 8 | +import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspState; |
| 9 | +import software.aws.toolkits.eclipse.amazonq.plugin.Activator; |
| 10 | + |
| 11 | +public final class ViewRouter { |
| 12 | + |
| 13 | + private final EventObserver<AuthState> authStateObserver = new EventObserver<>() { |
| 14 | + @Override |
| 15 | + public void onEvent(final AuthState newAuthState) { |
| 16 | + authState = newAuthState; |
| 17 | + refreshActiveView(); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + private final EventObserver<LspState> lspStateObserver = new EventObserver<>() { |
| 22 | + @Override |
| 23 | + public void onEvent(final LspState newLspState) { |
| 24 | + lspState = newLspState; |
| 25 | + refreshActiveView(); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + private ViewId activeViewId; |
| 30 | + |
| 31 | + // this state needs to be maintained to ensure correct resolution in refreshActiveView |
| 32 | + private LspState lspState; |
| 33 | + private AuthState authState; |
| 34 | + |
| 35 | + public ViewRouter() { |
| 36 | + activeViewId = ViewId.TOOLKIT_LOGIN_VIEW; |
| 37 | + lspState = null; |
| 38 | + authState = null; |
| 39 | + |
| 40 | + Activator.getEventBroker().subscribe(AuthState.class, authStateObserver); |
| 41 | + Activator.getEventBroker().subscribe(LspState.class, lspStateObserver); |
| 42 | + } |
| 43 | + |
| 44 | + private void refreshActiveView() { |
| 45 | + ViewId newActiveViewId = ViewId.TOOLKIT_LOGIN_VIEW; |
| 46 | + |
| 47 | + if (isDependencyMissing()) { // TODO: dependency missing check logic needs to be implemented |
| 48 | + newActiveViewId = ViewId.DEPENDENCY_MISSING_VIEW; |
| 49 | + } else if (lspState != null) { |
| 50 | + if (lspState == LspState.FAILED) { |
| 51 | + newActiveViewId = ViewId.LSP_STARTUP_FAILED_VIEW; |
| 52 | + } else if (lspState == LspState.PENDING) { |
| 53 | + newActiveViewId = ViewId.LSP_INITIALIZING_VIEW; |
| 54 | + } |
| 55 | + } else if (isChatUIAssetMissing()) { // TODO: chat missing logic needs to be implemented |
| 56 | + newActiveViewId = ViewId.CHAT_ASSET_MISSING_VIEW; |
| 57 | + } else if (authState != null) { |
| 58 | + if (authState.isLoggedOut()) { |
| 59 | + newActiveViewId = ViewId.TOOLKIT_LOGIN_VIEW; |
| 60 | + } else if (authState.isExpired()) { |
| 61 | + newActiveViewId = ViewId.RE_AUTHENTICATE_VIEW; |
| 62 | + } |
| 63 | + } else { |
| 64 | + newActiveViewId = ViewId.CHAT_VIEW; |
| 65 | + } |
| 66 | + |
| 67 | + if (activeViewId != newActiveViewId) { |
| 68 | + activeViewId = newActiveViewId; |
| 69 | + notifyActiveViewChange(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void notifyActiveViewChange() { |
| 74 | + Activator.getEventBroker().post(activeViewId); |
| 75 | + } |
| 76 | + |
| 77 | + // TODO: replace with relevant checks |
| 78 | + private boolean isDependencyMissing() { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // TODO: replace with relevant checks |
| 83 | + private boolean isChatUIAssetMissing() { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments