From daa2af3255f814b341c5a08bcac511614e4fbf63 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 12 Sep 2022 17:24:32 +0300 Subject: [PATCH] reindexing progress bar --- .../muwire/gui/LibrarySyncController.groovy | 3 +- gui/griffon-app/i18n/messages.properties | 3 +- .../com/muwire/gui/LibrarySyncModel.groovy | 26 +++++++++++ .../com/muwire/gui/LibrarySyncView.groovy | 44 +++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/gui/griffon-app/controllers/com/muwire/gui/LibrarySyncController.groovy b/gui/griffon-app/controllers/com/muwire/gui/LibrarySyncController.groovy index 46f0484a..3fb6db47 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/LibrarySyncController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/LibrarySyncController.groovy @@ -29,6 +29,7 @@ class LibrarySyncController { @ControllerAction void reindex() { - + view.startReindex() + model.startReindex() } } diff --git a/gui/griffon-app/i18n/messages.properties b/gui/griffon-app/i18n/messages.properties index e3edb58f..3fcf998a 100644 --- a/gui/griffon-app/i18n/messages.properties +++ b/gui/griffon-app/i18n/messages.properties @@ -778,7 +778,8 @@ LIBRARY_SCAN_PREVIEW_COLUMN_FILE=File LIBRARY_SCAN_PREVIEW_COLUMN_SHARED=Shared LIBRARY_SCAN_PREVIEW_COLUMN_MODIFIED=Modified LIBRARY_SCAN_PREVIEW_COLLECTIONS_BODY=The following collections may be affected -LIBRARY_SCAN_PREVIEW_REINDEX=Re-Index +LIBRARY_SCAN_PREVIEW_REINDEX=Re-index +LIBRARY_SCAN_REINDEX_TITLE=Re-indexing files that are out of sync ## Tooltips diff --git a/gui/griffon-app/models/com/muwire/gui/LibrarySyncModel.groovy b/gui/griffon-app/models/com/muwire/gui/LibrarySyncModel.groovy index 102e192f..35638acd 100644 --- a/gui/griffon-app/models/com/muwire/gui/LibrarySyncModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/LibrarySyncModel.groovy @@ -4,6 +4,8 @@ import com.muwire.core.Core import com.muwire.core.InfoHash import com.muwire.core.SharedFile import com.muwire.core.collections.FileCollection +import com.muwire.core.files.FileHashedEvent +import com.muwire.core.files.FileModifiedEvent import griffon.core.artifact.GriffonModel import griffon.inject.MVCMember import griffon.metadata.ArtifactProviderFor @@ -72,4 +74,28 @@ class LibrarySyncModel { view.scanFinished() } } + + void startReindex() { + def observer = new ReindexObserver() + core.eventBus.register(FileHashedEvent.class, observer) + def event = new FileModifiedEvent(sharedFiles: staleFiles) + core.eventBus.publish(event) + } + + private class ReindexObserver { + private final Set remaining = new HashSet<>(staleFiles) + private final int total = remaining.size() + void onFileHashedEvent(FileHashedEvent event) { + runInsideUIAsync { + if (!remaining.remove(event.original)) + return + int percentage = (int)((total - remaining.size()) * 100 / total) + view.updateReindexProgressBar(percentage) + if (remaining.isEmpty()) { + core.eventBus.unregister(FileHashedEvent.class, this) + view.reindexComplete() + } + } + } + } } diff --git a/gui/griffon-app/views/com/muwire/gui/LibrarySyncView.groovy b/gui/griffon-app/views/com/muwire/gui/LibrarySyncView.groovy index 6e01e441..1a1c95fa 100644 --- a/gui/griffon-app/views/com/muwire/gui/LibrarySyncView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/LibrarySyncView.groovy @@ -16,6 +16,8 @@ import javax.swing.JProgressBar import javax.swing.JTable import java.awt.BorderLayout import java.awt.Dimension +import java.awt.event.WindowAdapter +import java.awt.event.WindowEvent import static com.muwire.gui.Translator.trans @@ -43,6 +45,8 @@ class LibrarySyncView { private JProgressBar scanProgressBar JProgressBar reindexProgressBar + private final CloseAdapter closeAdapter = new CloseAdapter() + void initUI() { mainFrame = (JFrame) application.windowManager.findWindow("main-frame") rowHeight = (int)application.context.get("row-height") @@ -61,6 +65,7 @@ class LibrarySyncView { } scanDialog.with { + addWindowListener(closeAdapter) getContentPane().add(scanPanel) pack() setLocationRelativeTo(mainFrame) @@ -146,6 +151,7 @@ class LibrarySyncView { Dimension dimension = mainFrame.getSize() previewDialog.with { + addWindowListener(closeAdapter) getContentPane().add(previewPanel) setSize((int)(dimension.getWidth() - 100), (int)(dimension.getHeight() - 100)) setLocationRelativeTo(mainFrame) @@ -157,4 +163,42 @@ class LibrarySyncView { nextDialog = null previewDialog.setVisible(false) } + + void startReindex() { + previewDialog.setVisible(false) + reindexDialog = new JDialog(mainFrame, trans("LIBRARY_SCAN_REINDEX_TITLE"), true) + nextDialog = reindexDialog + + JPanel reindexPanel = builder.panel { + borderLayout() + panel(constraints: BorderLayout.NORTH) { + label(text: trans("LIBRARY_SCAN_REINDEX_TITLE")) + } + reindexProgressBar = progressBar(constraints: BorderLayout.CENTER) + } + + reindexDialog.with { + addWindowListener(closeAdapter) + getContentPane().add(reindexPanel) + pack() + setLocationRelativeTo(mainFrame) + setDefaultCloseOperation(DISPOSE_ON_CLOSE) + } + } + + void updateReindexProgressBar(int value) { + reindexProgressBar.setValue(value) + } + + void reindexComplete() { + nextDialog = null + reindexDialog.setVisible(false) + } + + private class CloseAdapter extends WindowAdapter { + void windowClosed(WindowEvent e) { + nextDialog?.setVisible(false) + nextDialog = null + } + } }