From 65b20a9f93de7f1b821bd07d77d281d95c074c43 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 12 Jun 2021 08:06:11 +0100 Subject: [PATCH] update library in batch after unsharing --- .../com/muwire/gui/MainFrameController.groovy | 1 + .../com/muwire/gui/MainFrameModel.groovy | 48 +++++++++++++++++-- .../com/muwire/gui/RefreshLibraryEvent.groovy | 6 +++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 gui/src/main/groovy/com/muwire/gui/RefreshLibraryEvent.groovy diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 772c4795..e48e710e 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -478,6 +478,7 @@ class MainFrameController { core.eventBus.publish(new FileUnsharedEvent(unsharedFile : sf)) } + core.eventBus.publish(new RefreshLibraryEvent()) } @ControllerAction diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index 3462af31..31d1ea97 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -299,6 +299,7 @@ class MainFrameModel { core.eventBus.register(UIMessageReadEvent.class, this) core.eventBus.register(MessageSentEvent.class, this) core.eventBus.register(MessageFolderLoadingEvent.class, this) + core.eventBus.register(RefreshLibraryEvent.class, this) core.muOptions.watchedKeywords.each { @@ -478,8 +479,19 @@ class MainFrameModel { shared.clear() treeRoot.clear() filterer?.cancel() - filterer = new Filterer() - filterer.execute() + if (filter != null) { + filterer = new Filterer() + filterer.execute() + } else { + synchronized (allSharedFiles) { + shared.addAll(allSharedFiles) + } + shared.each { + insertIntoTree(it, treeRoot, null) + } + view.refreshSharedFiles() + view.magicTreeExpansion() + } } private class Filterer extends SwingWorker,SharedFile> { @@ -530,9 +542,30 @@ class MainFrameModel { allSharedFiles.remove(e.unsharedFile) loadedFiles = allSharedFiles.size() - def dmtn = fileToNode.remove(e.unsharedFile) + boolean wasVisible = shared.remove(e.unsharedFile) + + DefaultMutableTreeNode dmtn = fileToNode.remove(e.unsharedFile) if (dmtn != null) { - loadedFiles = fileToNode.size() + + if (wasVisible) { + Object[] path = dmtn.getUserObjectPath() + DefaultMutableTreeNode otherNode = treeRoot + for (int i = 1; i < path.length; i ++) { + Object o = path[i] + DefaultMutableTreeNode next = null + for (int j = 0; j < otherNode.childCount; j ++) { + if (otherNode.getChildAt(j).getUserObject() == o) { + next = otherNode.getChildAt(j) + break + } + } + if (next == null) + throw new IllegalStateException() + otherNode = next + } + otherNode.removeFromParent() + } + List unshared = new ArrayList<>() while (true) { def parent = dmtn.getParent() @@ -553,7 +586,12 @@ class MainFrameModel { core.eventBus.publish(new DirectoryUnsharedEvent(directory : unsharedRoot)) } } - filterLibrary() + } + } + + void onRefreshLibraryEvent(RefreshLibraryEvent e) { + runInsideUIAsync { + view.refreshSharedFiles() } } diff --git a/gui/src/main/groovy/com/muwire/gui/RefreshLibraryEvent.groovy b/gui/src/main/groovy/com/muwire/gui/RefreshLibraryEvent.groovy new file mode 100644 index 00000000..39ce48e6 --- /dev/null +++ b/gui/src/main/groovy/com/muwire/gui/RefreshLibraryEvent.groovy @@ -0,0 +1,6 @@ +package com.muwire.gui + +import com.muwire.core.Event + +class RefreshLibraryEvent extends Event { +}