Skip to content

Commit

Permalink
Rename and get current state
Browse files Browse the repository at this point in the history
  • Loading branch information
ofekby committed Nov 27, 2023
1 parent 6a4d6db commit 5622853
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
35 changes: 20 additions & 15 deletions Tabnine/src/main/java/com/tabnine/chat/ChatEnabledState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,6 @@ class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState,
TopicBasedNonNullState<ChatState, ChatEnabledChanged>(
ENABLED_TOPIC, ChatState.loading()
) {

companion object {
private val ENABLED_TOPIC: Topic<ChatEnabledChanged> =
Topic.create("ChatEnabled", ChatEnabledChanged::class.java)
Expand All @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatState, ChatEnabledChanged>(
ENABLED_TOPIC, ChatState.loading()
) {
Expand All @@ -20,7 +20,7 @@ class ChatEnabledState private constructor() : ChatFrame.UseChatEnabledState,
private val ENABLED_TOPIC: Topic<ChatEnabledChanged> =
Topic.create("ChatEnabled", ChatEnabledChanged::class.java)

val instance = ChatEnabledState()
val instance = SelfHostedChatEnabledState()
}

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 5622853

Please sign in to comment.