feat: display notification on plugin updates

This commit is contained in:
Carl-Robert Linnupuu 2023-12-02 00:54:57 +02:00
parent 10b090e2d2
commit 1392775940
8 changed files with 125 additions and 4 deletions

View file

@ -0,0 +1,68 @@
package ee.carlrobert.codegpt;
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.ConfigurationState;
import ee.carlrobert.codegpt.util.OverlayUtil;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
public class CodeGPTUpdateStartupActivity implements StartupActivity.Background {
@Override
public void runActivity(@NotNull Project project) {
schedulePluginUpdateChecks(project);
}
private void schedulePluginUpdateChecks(Project project) {
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(() -> {
if (project != null && ConfigurationState.getInstance().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()) {
CodeGPTPlugin.tryFindAvailableUpdate(indicator)
.ifPresent((update) -> 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"),
() -> ConfigurationState.getInstance().setCheckForPluginUpdates(false)))
.notify(myProject));
}
}
}
}