forked from redhat-developer/intellij-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented settings to disable editor push notification (redha…
…t-developer#763) Signed-off-by: Andre Dietisheim <[email protected]>
- Loading branch information
Showing
8 changed files
with
584 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/notification/Notifications.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.kubernetes.editor.notification | ||
|
||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.fileEditor.FileEditor | ||
import com.intellij.openapi.project.Project | ||
import com.redhat.devtools.intellij.kubernetes.editor.DeletedOnCluster | ||
import com.redhat.devtools.intellij.kubernetes.editor.EditorResource | ||
import com.redhat.devtools.intellij.kubernetes.editor.Error | ||
import com.redhat.devtools.intellij.kubernetes.editor.FILTER_ERROR | ||
import com.redhat.devtools.intellij.kubernetes.editor.FILTER_TO_PUSH | ||
import com.redhat.devtools.intellij.kubernetes.editor.Modified | ||
import com.redhat.devtools.intellij.kubernetes.editor.Pushed | ||
import io.fabric8.kubernetes.api.model.HasMetadata | ||
|
||
open class Notifications( | ||
editor: FileEditor, | ||
project: Project, | ||
// for mocking purposes | ||
private val pushNotification: PushNotification = PushNotification(editor, project), | ||
// for mocking purposes | ||
private val pushedNotification: PushedNotification = PushedNotification(editor, project), | ||
// for mocking purposes | ||
private val pullNotification: PullNotification = PullNotification(editor, project), | ||
// for mocking purposes | ||
private val pulledNotification: PulledNotification = PulledNotification(editor, project), | ||
// for mocking purposes | ||
private val deletedNotification: DeletedNotification = DeletedNotification(editor, project), | ||
// for mocking purposes | ||
private val errorNotification: ErrorNotification = ErrorNotification(editor, project), | ||
) { | ||
|
||
fun show(editorResource: EditorResource) { | ||
val state = editorResource.getState() | ||
val resource = editorResource.getResource() | ||
when (state) { | ||
is Error -> | ||
showError(state.title, state.message) | ||
|
||
is Pushed -> | ||
showPushed(listOf(editorResource)) | ||
|
||
is DeletedOnCluster -> | ||
showDeleted(resource) | ||
|
||
/** | ||
* avoid too many notifications, don't notify outdated | ||
is Outdated -> | ||
showPullNotification(resource) | ||
*/ | ||
|
||
is Modified -> | ||
showPush(true, listOf(editorResource)) | ||
|
||
else -> | ||
hideAll() | ||
} | ||
} | ||
|
||
fun show(editorResources: Collection<EditorResource>) { | ||
val toPush = editorResources.filter(FILTER_TO_PUSH) | ||
if (toPush.isNotEmpty()) { | ||
showPush(false, toPush) | ||
return | ||
} | ||
val inError = editorResources.filter(FILTER_ERROR) | ||
if (inError.isNotEmpty()) { | ||
showError(inError) | ||
} else { | ||
hideAll() | ||
} | ||
} | ||
|
||
fun showError(title: String, message: String?) { | ||
runInUI { | ||
hideAll() | ||
errorNotification.show(title, message) | ||
} | ||
} | ||
|
||
fun showError(editorResources: Collection<EditorResource>) { | ||
val inError = editorResources.filter(FILTER_ERROR) | ||
val toDisplay = inError.firstOrNull()?.getState() as? Error ?: return | ||
showError(toDisplay.title, toDisplay.message) | ||
} | ||
|
||
fun showPush(showPull: Boolean, editorResources: Collection<EditorResource>) { | ||
runInUI { | ||
// hide & show in the same UI thread runnable avoid flickering | ||
hideAll() | ||
pushNotification.show(showPull, editorResources) | ||
} | ||
} | ||
|
||
fun showPushed(editorResources: Collection<EditorResource>) { | ||
runInUI { | ||
// hide & show in the same UI thread runnable avoid flickering | ||
hideAll() | ||
pushedNotification.show(editorResources) | ||
} | ||
} | ||
|
||
fun showPull(resource: HasMetadata) { | ||
runInUI { | ||
hideAll() | ||
pullNotification.show(resource) | ||
} | ||
} | ||
|
||
fun showDeleted(resource: HasMetadata) { | ||
runInUI { | ||
// hide & show in the same UI thread runnable avoid flickering | ||
hideAll() | ||
deletedNotification.show(resource) | ||
} | ||
} | ||
|
||
fun hideAll() { | ||
runInUI { | ||
errorNotification.hide() | ||
pullNotification.hide() | ||
deletedNotification.hide() | ||
pushNotification.hide() | ||
pushedNotification.hide() | ||
pulledNotification.hide() | ||
} | ||
} | ||
|
||
protected open fun runInUI(runnable: () -> Unit) { | ||
if (ApplicationManager.getApplication().isDispatchThread) { | ||
runnable.invoke() | ||
} else { | ||
ApplicationManager.getApplication().invokeLater(runnable) | ||
} | ||
} | ||
} |
Oops, something went wrong.