fix: migrate from StartupActivity to ProjectActivity

This commit is contained in:
Carl-Robert Linnupuu 2024-03-27 21:48:48 +02:00
parent 9d28e3e009
commit b72ddfccd6
5 changed files with 137 additions and 141 deletions

View file

@ -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);
}
}
}
}
}

View file

@ -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);
}
});
}
}
}