Skip to content

Commit db647f0

Browse files
committed
feat: implemented settings to disable editor push notification (redhat-developer#763)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 9c69e50 commit db647f0

File tree

8 files changed

+590
-283
lines changed

8 files changed

+590
-283
lines changed

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/ResourceEditor.kt

Lines changed: 35 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ import com.intellij.openapi.util.Disposer
2323
import com.intellij.openapi.util.Key
2424
import com.intellij.openapi.vfs.VirtualFile
2525
import com.intellij.psi.PsiDocumentManager
26+
import com.intellij.util.messages.MessageBusConnection
2627
import com.redhat.devtools.intellij.common.utils.MetadataClutter
2728
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
28-
import com.redhat.devtools.intellij.kubernetes.editor.notification.DeletedNotification
29-
import com.redhat.devtools.intellij.kubernetes.editor.notification.ErrorNotification
30-
import com.redhat.devtools.intellij.kubernetes.editor.notification.PullNotification
31-
import com.redhat.devtools.intellij.kubernetes.editor.notification.PulledNotification
32-
import com.redhat.devtools.intellij.kubernetes.editor.notification.PushNotification
33-
import com.redhat.devtools.intellij.kubernetes.editor.notification.PushedNotification
29+
import com.redhat.devtools.intellij.kubernetes.editor.notification.Notifications
3430
import com.redhat.devtools.intellij.kubernetes.editor.util.getDocument
3531
import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource
3632
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
@@ -39,6 +35,8 @@ import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext
3935
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
4036
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
4137
import com.redhat.devtools.intellij.kubernetes.model.util.toTitle
38+
import com.redhat.devtools.intellij.kubernetes.settings.SettingsChangeListener
39+
import com.redhat.devtools.intellij.kubernetes.settings.SettingsConfigurable
4240
import io.fabric8.kubernetes.api.model.HasMetadata
4341
import java.util.concurrent.CompletableFuture
4442

@@ -57,18 +55,7 @@ open class ResourceEditor(
5755
// for mocking purposes
5856
private val createResourceFileForVirtual: (file: VirtualFile?) -> ResourceFile? =
5957
ResourceFile.Factory::create,
60-
// for mocking purposes
61-
private val pushNotification: PushNotification = PushNotification(editor, project),
62-
// for mocking purposes
63-
private val pushedNotification: PushedNotification = PushedNotification(editor, project),
64-
// for mocking purposes
65-
private val pullNotification: PullNotification = PullNotification(editor, project),
66-
// for mocking purposes
67-
private val pulledNotification: PulledNotification = PulledNotification(editor, project),
68-
// for mocking purposes
69-
private val deletedNotification: DeletedNotification = DeletedNotification(editor, project),
70-
// for mocking purposes
71-
private val errorNotification: ErrorNotification = ErrorNotification(editor, project),
58+
private val notifications: Notifications = Notifications(editor, project),
7259
// for mocking purposes
7360
private val getDocument: (FileEditor) -> Document? = ::getDocument,
7461
// for mocking purposes
@@ -81,14 +68,18 @@ open class ResourceEditor(
8168
// for mocking purposes
8269
private val diff: ResourceDiff = ResourceDiff(project),
8370
// for mocking purposes
84-
protected val editorResources: EditorResources = EditorResources(resourceModel)
71+
protected val editorResources: EditorResources = EditorResources(resourceModel),
72+
private val connection: MessageBusConnection = ApplicationManager.getApplication().messageBus.connect()
8573
) : Disposable {
8674

8775
init {
8876
Disposer.register(editor, this)
8977
editorResources.resourceChangedListener = onResourceChanged()
9078
resourceModel.addListener(onNamespaceOrContextChanged())
91-
runAsync { enableEditingNonProjectFile() }
79+
runAsync {
80+
enableEditingNonProjectFile()
81+
connection.subscribe(SettingsChangeListener.CHANGED, onSettingsChanged())
82+
}
9283
}
9384

9485
companion object {
@@ -117,15 +108,15 @@ open class ResourceEditor(
117108
if (editorResources.size == 1) {
118109
// show notification for 1 resource
119110
val editorResource = editorResources.firstOrNull() ?: return@runAsync
120-
showNotification(editorResource)
111+
notifications.show(editorResource)
121112
} else if (editorResources.size > 1) {
122113
// show notification for multiple resources
123-
showNotification(editorResources)
114+
notifications.show(editorResources)
124115
}
125116
} catch (e: Exception) {
126117
runInUI {
127-
hideNotifications()
128-
errorNotification.show(
118+
notifications.hideAll()
119+
notifications.showError(
129120
toTitle(e),
130121
toMessage(e.cause)
131122
)
@@ -134,108 +125,14 @@ open class ResourceEditor(
134125
}
135126
}
136127

137-
open protected fun updateDeleted(deleted: HasMetadata?) {
128+
protected open fun updateDeleted(deleted: HasMetadata?) {
138129
if (deleted == null) {
139130
return
140131
}
141132
editorResources.setDeleted(deleted)
142133
update()
143134
}
144135

145-
private fun showNotification(editorResource: EditorResource) {
146-
val state = editorResource.getState()
147-
val resource = editorResource.getResource()
148-
when (state) {
149-
is Error ->
150-
showErrorNotification(state.title, state.message)
151-
is Pushed ->
152-
showPushedNotification(listOf(editorResource))
153-
is DeletedOnCluster ->
154-
showDeletedNotification(resource)
155-
/**
156-
* avoid too many notifications, don't notify outdated
157-
is Outdated ->
158-
showPullNotification(resource)
159-
*/
160-
is Modified ->
161-
showPushNotification(true, listOf(editorResource))
162-
163-
else ->
164-
runInUI {
165-
hideNotifications()
166-
}
167-
}
168-
}
169-
170-
private fun showNotification(editorResources: Collection<EditorResource>) {
171-
val toPush = editorResources.filter(FILTER_TO_PUSH)
172-
if (toPush.isNotEmpty()) {
173-
showPushNotification(false, toPush)
174-
return
175-
}
176-
val inError = editorResources.filter(FILTER_ERROR)
177-
if (inError.isNotEmpty()) {
178-
showErrorNotification(inError)
179-
} else {
180-
runInUI {
181-
hideNotifications()
182-
}
183-
}
184-
}
185-
186-
private fun showErrorNotification(title: String, message: String?) {
187-
runInUI {
188-
hideNotifications()
189-
errorNotification.show(title, message)
190-
}
191-
}
192-
193-
private fun showErrorNotification(editorResources: Collection<EditorResource>) {
194-
val inError = editorResources.filter(FILTER_ERROR)
195-
val toDisplay = inError.firstOrNull()?.getState() as? Error ?: return
196-
showErrorNotification(toDisplay.title, toDisplay.message)
197-
}
198-
199-
private fun showPushNotification(showPull: Boolean, editorResources: Collection<EditorResource>) {
200-
runInUI {
201-
// hide & show in the same UI thread runnable avoid flickering
202-
hideNotifications()
203-
pushNotification.show(showPull, editorResources)
204-
}
205-
}
206-
207-
private fun showPushedNotification(editorResources: Collection<EditorResource>) {
208-
runInUI {
209-
// hide & show in the same UI thread runnable avoid flickering
210-
hideNotifications()
211-
pushedNotification.show(editorResources)
212-
}
213-
}
214-
215-
private fun showPullNotification(resource: HasMetadata) {
216-
runInUI {
217-
hideNotifications()
218-
pullNotification.show(resource)
219-
}
220-
}
221-
222-
private fun showDeletedNotification(resource: HasMetadata) {
223-
runInUI {
224-
// hide & show in the same UI thread runnable avoid flickering
225-
hideNotifications()
226-
deletedNotification.show(resource)
227-
}
228-
}
229-
230-
private fun hideNotifications() {
231-
errorNotification.hide()
232-
pullNotification.hide()
233-
deletedNotification.hide()
234-
pushNotification.hide()
235-
pushedNotification.hide()
236-
pulledNotification.hide()
237-
}
238-
239136
/**
240137
* Pulls the resource from the cluster and replaces the content of this editor.
241138
* Does nothing if it doesn't exist.
@@ -248,7 +145,7 @@ open class ResourceEditor(
248145
}
249146
runInUI {
250147
// hide before running pull. Pull may take quite some time on remote cluster
251-
hideNotifications()
148+
notifications.hideAll()
252149
}
253150
val resource = resources.firstOrNull() ?: return
254151

@@ -293,7 +190,7 @@ open class ResourceEditor(
293190
fun push(all: Boolean) {
294191
runInUI {
295192
// hide before running push. Push may take quite some time on remote cluster
296-
hideNotifications()
193+
notifications.hideAll()
297194
}
298195
runAsync {
299196
if (all) {
@@ -370,7 +267,7 @@ open class ResourceEditor(
370267
}
371268
runInUI {
372269
replaceDocument(resources)
373-
hideNotifications()
270+
notifications.hideAll()
374271
}
375272
}
376273

@@ -431,6 +328,21 @@ open class ResourceEditor(
431328
}
432329
}
433330

331+
protected open fun onSettingsChanged(): SettingsChangeListener {
332+
return object : SettingsChangeListener {
333+
override fun changed(property: String, value: String?) {
334+
if (value != null
335+
&& SettingsConfigurable.EDITOR_NOTIFICATIONS_DISABLED == property) {
336+
if (true == value.toBoolean()) {
337+
notifications.hideAll()
338+
} else {
339+
update()
340+
}
341+
}
342+
}
343+
}
344+
}
345+
434346
/**
435347
* Closes this instance and cleans up references to it.
436348
* - Removes the resource model listener,
@@ -496,5 +408,6 @@ open class ResourceEditor(
496408
override fun dispose() {
497409
resourceModel.removeListener(onNamespaceContextChanged)
498410
editorResources.dispose()
411+
connection.dispose()
499412
}
500413
}

0 commit comments

Comments
 (0)