Skip to content

Commit eb2c69b

Browse files
committed
feat: allow to disable editor notifications (#763)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 8a91c04 commit eb2c69b

File tree

10 files changed

+784
-293
lines changed

10 files changed

+784
-293
lines changed

Diff for: src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/EditorResource.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ open class EditorResource(
3333
private val resourceModel: IResourceModel,
3434
private val resourceChangedListener: IResourceModelListener?,
3535
// for mocking purposes
36-
private val clusterResourceFactory: (resource: HasMetadata, context: IActiveContext<out HasMetadata, out KubernetesClient>?) -> ClusterResource? =
36+
private val clusterResourceFactory: (
37+
resource: HasMetadata,
38+
context: IActiveContext<out HasMetadata, out KubernetesClient>?
39+
) -> ClusterResource? =
3740
ClusterResource.Factory::create
3841
) {
3942
var disposed: Boolean = false

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

+63-132
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,9 @@ 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.Settings
39+
import com.redhat.devtools.intellij.kubernetes.settings.Settings.Companion.PROP_EDITOR_SYNC_ENABLED
40+
import com.redhat.devtools.intellij.kubernetes.settings.SettingsChangeListener
4241
import io.fabric8.kubernetes.api.model.HasMetadata
4342
import java.util.concurrent.CompletableFuture
4443

@@ -57,18 +56,7 @@ open class ResourceEditor(
5756
// for mocking purposes
5857
private val createResourceFileForVirtual: (file: VirtualFile?) -> ResourceFile? =
5958
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),
59+
private val notifications: Notifications = Notifications(editor, project),
7260
// for mocking purposes
7361
private val getDocument: (FileEditor) -> Document? = ::getDocument,
7462
// for mocking purposes
@@ -81,14 +69,19 @@ open class ResourceEditor(
8169
// for mocking purposes
8270
private val diff: ResourceDiff = ResourceDiff(project),
8371
// for mocking purposes
84-
protected val editorResources: EditorResources = EditorResources(resourceModel)
72+
protected val editorResources: EditorResources = EditorResources(resourceModel),
73+
private val settings: Settings = Settings.getInstance(),
74+
private val connection: MessageBusConnection = ApplicationManager.getApplication().messageBus.connect()
8575
) : Disposable {
8676

8777
init {
8878
Disposer.register(editor, this)
8979
editorResources.resourceChangedListener = onResourceChanged()
9080
resourceModel.addListener(onNamespaceOrContextChanged())
91-
runAsync { enableEditingNonProjectFile() }
81+
runAsync {
82+
enableEditingNonProjectFile()
83+
connection.subscribe(SettingsChangeListener.CHANGED, onSettingsChanged())
84+
}
9285
}
9386

9487
companion object {
@@ -113,19 +106,11 @@ open class ResourceEditor(
113106
resourceModel.getCurrentNamespace()
114107
)
115108
val editorResources = editorResources.setResources(resources)
116-
117-
if (editorResources.size == 1) {
118-
// show notification for 1 resource
119-
val editorResource = editorResources.firstOrNull() ?: return@runAsync
120-
showNotification(editorResource)
121-
} else if (editorResources.size > 1) {
122-
// show notification for multiple resources
123-
showNotification(editorResources)
124-
}
109+
showNotifications(editorResources)
125110
} catch (e: Exception) {
126111
runInUI {
127-
hideNotifications()
128-
errorNotification.show(
112+
notifications.hideAll()
113+
notifications.showError(
129114
toTitle(e),
130115
toMessage(e.cause)
131116
)
@@ -134,106 +119,24 @@ open class ResourceEditor(
134119
}
135120
}
136121

137-
open protected fun updateDeleted(deleted: HasMetadata?) {
138-
if (deleted == null) {
139-
return
122+
private fun showNotifications(editorResources: Collection<EditorResource>) {
123+
val syncEnabled = Settings.getInstance().isEditorSyncEnabled()
124+
if (editorResources.size == 1) {
125+
// show notification for 1 resource
126+
val editorResource = editorResources.firstOrNull() ?: return
127+
notifications.show(editorResource, syncEnabled)
128+
} else if (editorResources.size > 1) {
129+
// show notification for multiple resources
130+
notifications.show(editorResources, syncEnabled)
140131
}
141-
editorResources.setDeleted(deleted)
142-
update()
143132
}
144133

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)
134+
protected open fun updateDeleted(deleted: HasMetadata?) {
135+
if (deleted == null) {
174136
return
175137
}
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()
138+
editorResources.setDeleted(deleted)
139+
update()
237140
}
238141

239142
/**
@@ -248,7 +151,7 @@ open class ResourceEditor(
248151
}
249152
runInUI {
250153
// hide before running pull. Pull may take quite some time on remote cluster
251-
hideNotifications()
154+
notifications.hideAll()
252155
}
253156
val resource = resources.firstOrNull() ?: return
254157

@@ -293,7 +196,7 @@ open class ResourceEditor(
293196
fun push(all: Boolean) {
294197
runInUI {
295198
// hide before running push. Push may take quite some time on remote cluster
296-
hideNotifications()
199+
notifications.hideAll()
297200
}
298201
runAsync {
299202
if (all) {
@@ -355,7 +258,9 @@ open class ResourceEditor(
355258
}
356259

357260
fun startWatch(): ResourceEditor {
358-
editorResources.watchAll()
261+
if (settings.isEditorSyncEnabled()) {
262+
editorResources.watchAll()
263+
}
359264
return this
360265
}
361266

@@ -370,7 +275,7 @@ open class ResourceEditor(
370275
}
371276
runInUI {
372277
replaceDocument(resources)
373-
hideNotifications()
278+
notifications.hideAll()
374279
}
375280
}
376281

@@ -404,11 +309,15 @@ open class ResourceEditor(
404309
}
405310

406311
override fun removed(removed: Any) {
407-
updateDeleted(removed as? HasMetadata)
312+
if (settings.isEditorSyncEnabled()) {
313+
updateDeleted(removed as? HasMetadata)
314+
}
408315
}
409316

410317
override fun modified(modified: Any) {
411-
update()
318+
if (settings.isEditorSyncEnabled()) {
319+
update()
320+
}
412321
}
413322
}
414323
}
@@ -431,6 +340,27 @@ open class ResourceEditor(
431340
}
432341
}
433342

343+
protected open fun onSettingsChanged(): SettingsChangeListener {
344+
return object : SettingsChangeListener {
345+
override fun changed(property: String, value: String?) {
346+
if (value == null) {
347+
return
348+
}
349+
350+
if (PROP_EDITOR_SYNC_ENABLED == property) {
351+
val enabled = value.toBoolean()
352+
if (enabled) {
353+
editorResources.watchAll()
354+
update()
355+
} else {
356+
editorResources.stopWatchAll()
357+
notifications.hideSyncNotifications()
358+
}
359+
}
360+
}
361+
}
362+
}
363+
434364
/**
435365
* Closes this instance and cleans up references to it.
436366
* - Removes the resource model listener,
@@ -496,5 +426,6 @@ open class ResourceEditor(
496426
override fun dispose() {
497427
resourceModel.removeListener(onNamespaceContextChanged)
498428
editorResources.dispose()
429+
connection.dispose()
499430
}
500431
}

0 commit comments

Comments
 (0)