From b72ddfccd6fbe452bec0fbf0c70fbd85416f97fd Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Wed, 27 Mar 2024 21:48:48 +0200 Subject: [PATCH] fix: migrate from StartupActivity to ProjectActivity --- .../codegpt/CodeGPTUpdateStartupActivity.java | 76 ------------------ .../codegpt/PluginStartupActivity.java | 63 --------------- .../codegpt/CodeGPTProjectActivity.kt | 57 ++++++++++++++ .../codegpt/CodeGPTUpdateActivity.kt | 78 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 4 +- 5 files changed, 137 insertions(+), 141 deletions(-) delete mode 100644 src/main/java/ee/carlrobert/codegpt/CodeGPTUpdateStartupActivity.java delete mode 100644 src/main/java/ee/carlrobert/codegpt/PluginStartupActivity.java create mode 100644 src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt create mode 100644 src/main/kotlin/ee/carlrobert/codegpt/CodeGPTUpdateActivity.kt diff --git a/src/main/java/ee/carlrobert/codegpt/CodeGPTUpdateStartupActivity.java b/src/main/java/ee/carlrobert/codegpt/CodeGPTUpdateStartupActivity.java deleted file mode 100644 index f8ee4c6c..00000000 --- a/src/main/java/ee/carlrobert/codegpt/CodeGPTUpdateStartupActivity.java +++ /dev/null @@ -1,76 +0,0 @@ -package ee.carlrobert.codegpt; - -import static ee.carlrobert.codegpt.CodeGPTPlugin.CODEGPT_ID; - -import com.intellij.ide.plugins.InstalledPluginsState; -import com.intellij.notification.NotificationAction; -import com.intellij.notification.NotificationType; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.openapi.progress.Task; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.startup.StartupActivity; -import com.intellij.openapi.updateSettings.impl.UpdateChecker; -import com.intellij.openapi.updateSettings.impl.UpdateSettings; -import com.intellij.util.concurrency.AppExecutorUtil; -import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings; -import ee.carlrobert.codegpt.ui.OverlayUtil; -import java.util.concurrent.TimeUnit; -import org.jetbrains.annotations.NotNull; - -public class CodeGPTUpdateStartupActivity implements StartupActivity.Background { - - @Override - public void runActivity(@NotNull Project project) { - if (ApplicationManager.getApplication().isUnitTestMode()) { - return; - } - - schedulePluginUpdateChecks(project); - } - - private void schedulePluginUpdateChecks(Project project) { - AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> { - if (project != null && ConfigurationSettings.getCurrentState().isCheckForPluginUpdates()) { - new CheckForUpdatesTask(project).queue(); - } - - }, 0, 4L, TimeUnit.HOURS); - } - - private static class CheckForUpdatesTask extends Task.Backgroundable { - - public CheckForUpdatesTask(@NotNull Project project) { - super(project, CodeGPTBundle.get("checkForUpdatesTask.title"), true); - } - - private static void installCodeGPTUpdate(Project project) { - var settingsCopy = new UpdateSettings(); - var settingsState = settingsCopy.getState(); - settingsState.copyFrom(UpdateSettings.getInstance().getState()); - settingsState.setCheckNeeded(true); - settingsState.setPluginsCheckNeeded(true); - settingsState.setShowWhatsNewEditor(true); - settingsState.setThirdPartyPluginsAllowed(true); - UpdateChecker.updateAndShowResult(project, settingsCopy); - } - - public void run(@NotNull ProgressIndicator indicator) { - if (!myProject.isDisposed()) { - if (InstalledPluginsState.getInstance().hasNewerVersion(CODEGPT_ID)) { - OverlayUtil.getDefaultNotification( - CodeGPTBundle.get("checkForUpdatesTask.notification.message"), - NotificationType.IDE_UPDATE) - .addAction(NotificationAction.createSimpleExpiring( - CodeGPTBundle.get("checkForUpdatesTask.notification.installButton"), - () -> ApplicationManager.getApplication() - .executeOnPooledThread(() -> installCodeGPTUpdate(myProject)))) - .addAction(NotificationAction.createSimpleExpiring( - CodeGPTBundle.get("checkForUpdatesTask.notification.hideButton"), - () -> ConfigurationSettings.getCurrentState().setCheckForPluginUpdates(false))) - .notify(myProject); - } - } - } - } -} diff --git a/src/main/java/ee/carlrobert/codegpt/PluginStartupActivity.java b/src/main/java/ee/carlrobert/codegpt/PluginStartupActivity.java deleted file mode 100644 index 8a259ad3..00000000 --- a/src/main/java/ee/carlrobert/codegpt/PluginStartupActivity.java +++ /dev/null @@ -1,63 +0,0 @@ -package ee.carlrobert.codegpt; - -import com.intellij.notification.NotificationType; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.startup.StartupActivity; -import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil; -import ee.carlrobert.codegpt.completions.you.YouUserManager; -import ee.carlrobert.codegpt.completions.you.auth.AuthenticationHandler; -import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationError; -import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationService; -import ee.carlrobert.codegpt.completions.you.auth.response.YouAuthenticationResponse; -import ee.carlrobert.codegpt.credentials.YouCredentialManager; -import ee.carlrobert.codegpt.settings.GeneralSettings; -import ee.carlrobert.codegpt.settings.service.you.YouSettings; -import ee.carlrobert.codegpt.ui.OverlayUtil; -import org.jetbrains.annotations.NotNull; - -public class PluginStartupActivity implements StartupActivity { - - private static final Logger LOG = Logger.getInstance(PluginStartupActivity.class); - - @Override - public void runActivity(@NotNull Project project) { - EditorActionsUtil.refreshActions(); - var authenticationResponse = YouUserManager.getInstance().getAuthenticationResponse(); - if (authenticationResponse == null) { - ApplicationManager.getApplication() - .executeOnPooledThread(this::handleYouServiceAuthentication); - } - } - - private void handleYouServiceAuthentication() { - var settings = YouSettings.getCurrentState(); - var password = YouCredentialManager.getInstance().getCredential(); - if (!settings.getEmail().isEmpty() && password != null && !password.isEmpty()) { - YouAuthenticationService.getInstance() - .signInAsync(settings.getEmail(), password, new AuthenticationHandler() { - @Override - public void handleAuthenticated(YouAuthenticationResponse authenticationResponse) { - OverlayUtil.showNotification( - "Authentication successful.", - NotificationType.INFORMATION); - } - - @Override - public void handleGenericError() { - OverlayUtil.showNotification( - "Something went wrong while trying to authenticate.", - NotificationType.ERROR); - } - - @Override - public void handleError(YouAuthenticationError youAuthenticationError) { - OverlayUtil.showNotification( - youAuthenticationError.getErrorMessage(), - NotificationType.ERROR); - } - }); - } - } -} diff --git a/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt new file mode 100644 index 00000000..025edae4 --- /dev/null +++ b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt @@ -0,0 +1,57 @@ +package ee.carlrobert.codegpt + +import com.intellij.notification.NotificationType +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.ProjectActivity +import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil +import ee.carlrobert.codegpt.completions.you.YouUserManager +import ee.carlrobert.codegpt.completions.you.auth.AuthenticationHandler +import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationError +import ee.carlrobert.codegpt.completions.you.auth.YouAuthenticationService +import ee.carlrobert.codegpt.completions.you.auth.response.YouAuthenticationResponse +import ee.carlrobert.codegpt.credentials.YouCredentialManager +import ee.carlrobert.codegpt.settings.service.you.YouSettings +import ee.carlrobert.codegpt.ui.OverlayUtil + +class CodeGPTProjectActivity : ProjectActivity { + + override suspend fun execute(project: Project) { + EditorActionsUtil.refreshActions() + + if (YouUserManager.getInstance().authenticationResponse == null) { + ApplicationManager.getApplication() + .executeOnPooledThread { this.handleYouServiceAuthentication() } + } + } + + private fun handleYouServiceAuthentication() { + val settings = YouSettings.getCurrentState() + val password = YouCredentialManager.getInstance().credential + if (settings.email.isNotEmpty() && password != null && password.isNotEmpty()) { + YouAuthenticationService.getInstance() + .signInAsync(settings.email, password, object : AuthenticationHandler { + override fun handleAuthenticated(authenticationResponse: YouAuthenticationResponse) { + OverlayUtil.showNotification( + "Authentication successful.", + NotificationType.INFORMATION + ) + } + + override fun handleGenericError() { + OverlayUtil.showNotification( + "Something went wrong while trying to authenticate.", + NotificationType.ERROR + ) + } + + override fun handleError(youAuthenticationError: YouAuthenticationError) { + OverlayUtil.showNotification( + youAuthenticationError.errorMessage, + NotificationType.ERROR + ) + } + }) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTUpdateActivity.kt b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTUpdateActivity.kt new file mode 100644 index 00000000..af31c937 --- /dev/null +++ b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTUpdateActivity.kt @@ -0,0 +1,78 @@ +package ee.carlrobert.codegpt; + +import com.intellij.ide.plugins.InstalledPluginsState +import com.intellij.notification.NotificationAction +import com.intellij.notification.NotificationType +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.progress.ProgressIndicator +import com.intellij.openapi.progress.Task +import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.ProjectActivity +import com.intellij.openapi.updateSettings.impl.UpdateChecker.updateAndShowResult +import com.intellij.openapi.updateSettings.impl.UpdateSettings +import com.intellij.util.concurrency.AppExecutorUtil +import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings +import ee.carlrobert.codegpt.ui.OverlayUtil +import java.util.concurrent.TimeUnit + +class CodeGPTUpdateActivity : ProjectActivity { + + override suspend fun execute(project: Project) { + if (ApplicationManager.getApplication().isUnitTestMode) { + return + } + + schedulePluginUpdateChecks(project) + } + + private fun schedulePluginUpdateChecks(project: Project) { + val command = { + if (ConfigurationSettings.getCurrentState().isCheckForPluginUpdates) { + CheckForUpdatesTask(project).queue() + } + } + AppExecutorUtil.getAppScheduledExecutorService() + .scheduleWithFixedDelay(command, 0, 4L, TimeUnit.HOURS) + } + + private class CheckForUpdatesTask(project: Project) : + Task.Backgroundable(project, CodeGPTBundle.get("checkForUpdatesTask.title"), true) { + override fun run(indicator: ProgressIndicator) { + val isLatestVersion = + !InstalledPluginsState.getInstance().hasNewerVersion(CodeGPTPlugin.CODEGPT_ID); + if (project.isDisposed || isLatestVersion) { + return + } + + OverlayUtil.getDefaultNotification( + CodeGPTBundle.get("checkForUpdatesTask.notification.message"), + NotificationType.IDE_UPDATE + ) + .addAction(NotificationAction.createSimpleExpiring( + CodeGPTBundle.get("checkForUpdatesTask.notification.installButton") + ) { + ApplicationManager.getApplication() + .executeOnPooledThread { installCodeGPTUpdate(project) } + }) + .addAction(NotificationAction.createSimpleExpiring( + CodeGPTBundle.get("checkForUpdatesTask.notification.hideButton") + ) { + ConfigurationSettings.getCurrentState().isCheckForPluginUpdates = false + }) + .notify(project) + } + + companion object { + private fun installCodeGPTUpdate(project: Project) { + val settingsCopy = UpdateSettings() + val settingsState = settingsCopy.state + settingsState.copyFrom(UpdateSettings.getInstance().state) + settingsState.isCheckNeeded = true + settingsState.isPluginsCheckNeeded = true + settingsState.isShowWhatsNewEditor = true + settingsState.isThirdPartyPluginsAllowed = true + updateAndShowResult(project, settingsCopy) + } + } + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index ba5f10f3..d8539467 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -12,8 +12,8 @@ - - + +