From 5622853b80389f91543aee9db3fba6678c8b1ad2 Mon Sep 17 00:00:00 2001 From: Ofek Ben-Yaish Date: Mon, 27 Nov 2023 17:19:01 +0200 Subject: [PATCH] Rename and get current state --- .../capabilities/Capabilities.kt | 3 ++ .../java/com/tabnine/chat/ChatEnabledState.kt | 35 +++++++++++-------- .../java/com/tabnineSelfHosted/Initializer.kt | 4 +-- ...State.kt => SelfHostedChatEnabledState.kt} | 4 +-- .../chat/TabnineChatWebViewFactory.kt | 2 +- .../chat/actions/TabnineQuickFixAction.kt | 4 +-- 6 files changed, 30 insertions(+), 22 deletions(-) rename TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/{ChatEnabledState.kt => SelfHostedChatEnabledState.kt} (92%) diff --git a/Common/src/main/java/com/tabnineCommon/capabilities/Capabilities.kt b/Common/src/main/java/com/tabnineCommon/capabilities/Capabilities.kt index be4b37232..2e286250a 100644 --- a/Common/src/main/java/com/tabnineCommon/capabilities/Capabilities.kt +++ b/Common/src/main/java/com/tabnineCommon/capabilities/Capabilities.kt @@ -9,4 +9,7 @@ data class Capabilities( fun isReady() = experimentSource == null || experimentSource.isRemoteBasedSource() fun isEnabled(capability: Capability) = features.contains(capability) + + fun anyEnabled(vararg capabilities: Capability) = + features.intersect(setOf(capabilities)).isNotEmpty() } diff --git a/Tabnine/src/main/java/com/tabnine/chat/ChatEnabledState.kt b/Tabnine/src/main/java/com/tabnine/chat/ChatEnabledState.kt index 6fcb9ab91..eddfcbc7a 100644 --- a/Tabnine/src/main/java/com/tabnine/chat/ChatEnabledState.kt +++ b/Tabnine/src/main/java/com/tabnine/chat/ChatEnabledState.kt @@ -2,7 +2,8 @@ package com.tabnine.chat import com.intellij.openapi.Disposable import com.intellij.util.messages.Topic -import com.tabnineCommon.capabilities.CapabilitiesService +import com.tabnineCommon.binary.requests.config.StateResponse +import com.tabnineCommon.capabilities.Capabilities import com.tabnineCommon.capabilities.Capability import com.tabnineCommon.chat.ChatDisabledReason import com.tabnineCommon.chat.ChatFrame @@ -16,7 +17,6 @@ class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState, TopicBasedNonNullState( ENABLED_TOPIC, ChatState.loading() ) { - companion object { private val ENABLED_TOPIC: Topic = Topic.create("ChatEnabled", ChatEnabledChanged::class.java) @@ -25,34 +25,39 @@ class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState, } init { - updateEnabled() + updateEnabled( + BinaryStateSingleton.instance.get(), + CapabilitiesStateSingleton.instance.get() + ) BinaryStateSingleton.instance.onChange { - updateEnabled() + updateEnabled(it, CapabilitiesStateSingleton.instance.get()) } CapabilitiesStateSingleton.instance.onChange { - updateEnabled() + updateEnabled(BinaryStateSingleton.instance.get(), it) } } - private fun updateEnabled() { - if (!CapabilitiesService.getInstance().isReady) { + private fun updateEnabled( + binaryState: StateResponse?, + capabilities: Capabilities? + ) { + if (capabilities == null || binaryState == null || !capabilities.isReady()) { return } - val isLoggedIn = BinaryStateSingleton.instance.get()?.isLoggedIn ?: return + val isLoggedIn = binaryState.isLoggedIn ?: return - val alphaEnabled = CapabilitiesService.getInstance().isCapabilityEnabled(Capability.ALPHA) - val chatCapabilityEnabled = - CapabilitiesService.getInstance().isCapabilityEnabled(Capability.TABNINE_CHAT) + val hasCapability = + capabilities.anyEnabled(Capability.ALPHA, Capability.TABNINE_CHAT) - if (isLoggedIn && (alphaEnabled || chatCapabilityEnabled)) { + if (isLoggedIn && hasCapability) { set(ChatState.enabled()) - } else if (!isLoggedIn) { - set(ChatState.disabled(ChatDisabledReason.AUTHENTICATION_REQUIRED)) - } else { + } else if (isLoggedIn) { set(ChatState.disabled(ChatDisabledReason.FEATURE_REQUIRED)) + } else { + set(ChatState.disabled(ChatDisabledReason.AUTHENTICATION_REQUIRED)) } } diff --git a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/Initializer.kt b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/Initializer.kt index 0bf49cd59..0d4dd8528 100644 --- a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/Initializer.kt +++ b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/Initializer.kt @@ -12,7 +12,7 @@ import com.tabnineCommon.logging.initTabnineLogger import com.tabnineCommon.notifications.ConnectionLostNotificationHandler import com.tabnineCommon.userSettings.AppSettingsState import com.tabnineSelfHosted.binary.lifecycle.UserInfoService -import com.tabnineSelfHosted.chat.ChatEnabledState +import com.tabnineSelfHosted.chat.SelfHostedChatEnabledState import java.util.concurrent.atomic.AtomicBoolean class Initializer : PreloadingActivity(), StartupActivity { @@ -35,7 +35,7 @@ class Initializer : PreloadingActivity(), StartupActivity { AppSettingsState.instance.cloud2Url = it } AskChatAction.register { - ChatEnabledState.instance.get().enabled + SelfHostedChatEnabledState.instance.get().enabled } initializeLifecycleEndpoints() ServiceManager.getService(UserInfoService::class.java).startUpdateLoop() diff --git a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/ChatEnabledState.kt b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/SelfHostedChatEnabledState.kt similarity index 92% rename from TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/ChatEnabledState.kt rename to TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/SelfHostedChatEnabledState.kt index c56037cb3..050f85c61 100644 --- a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/ChatEnabledState.kt +++ b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/SelfHostedChatEnabledState.kt @@ -11,7 +11,7 @@ import com.tabnineSelfHosted.binary.lifecycle.UserInfoStateSingleton import com.tabnineSelfHosted.binary.requests.userInfo.UserInfoResponse import java.util.function.Consumer -class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState, +class SelfHostedChatEnabledState private constructor() : ChatFrame.UseChatEnabledState, TopicBasedNonNullState( ENABLED_TOPIC, ChatState.loading() ) { @@ -20,7 +20,7 @@ class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState, private val ENABLED_TOPIC: Topic = Topic.create("ChatEnabled", ChatEnabledChanged::class.java) - val instance = ChatEnabledState() + val instance = SelfHostedChatEnabledState() } init { diff --git a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/TabnineChatWebViewFactory.kt b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/TabnineChatWebViewFactory.kt index 2150501c4..0386bd69f 100644 --- a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/TabnineChatWebViewFactory.kt +++ b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/TabnineChatWebViewFactory.kt @@ -12,7 +12,7 @@ class TabnineChatWebViewFactory : ToolWindowFactory, Disposable { private val binaryRequestFacade = instanceOfBinaryRequestFacade() override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { - val chatFrame = ChatFrame(project, binaryRequestFacade, ChatEnabledState.instance) + val chatFrame = ChatFrame(project, binaryRequestFacade, SelfHostedChatEnabledState.instance) Disposer.register(toolWindow.disposable, chatFrame) toolWindow.component.add(chatFrame) } diff --git a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/actions/TabnineQuickFixAction.kt b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/actions/TabnineQuickFixAction.kt index 4e535d2e7..11d4d3cb7 100644 --- a/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/actions/TabnineQuickFixAction.kt +++ b/TabnineSelfHosted/src/main/java/com/tabnineSelfHosted/chat/actions/TabnineQuickFixAction.kt @@ -1,8 +1,8 @@ package com.tabnineSelfHosted.chat.actions import com.tabnineCommon.chat.actions.AbstractTabnineQuickFixAction -import com.tabnineSelfHosted.chat.ChatEnabledState +import com.tabnineSelfHosted.chat.SelfHostedChatEnabledState class TabnineQuickFixAction : AbstractTabnineQuickFixAction() { - override fun isChatEnabled() = ChatEnabledState.instance.get().enabled + override fun isChatEnabled() = SelfHostedChatEnabledState.instance.get().enabled }