Skip to content

Commit

Permalink
Adjust total progress based on partial task progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Feb 27, 2023
1 parent 37a07ec commit 9200042
Show file tree
Hide file tree
Showing 21 changed files with 359 additions and 844 deletions.
57 changes: 0 additions & 57 deletions src/main/java/listfix/controller/Task.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import java.io.IOException;

/**
* @author jcaron
*/
public class WriteMediaLibraryIniTask extends listfix.controller.Task
public class WriteMediaLibraryIniTask extends Thread
{
private static final Logger _logger = LogManager.getLogger(WriteMediaLibraryIniTask.class);

Expand Down
35 changes: 15 additions & 20 deletions src/main/java/listfix/io/WinampHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -56,15 +57,15 @@ public class WinampHelper
/**
* Generates an exact match batch repair for the cryptically named playlists in Winamp.
*
* @param mediaFiles
* @param mediaLibrary Media-library
* @return A BatchRepair instance
* @see BatchRepair
*/
public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaFiles, IPlaylistOptions filePathOptions)
public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaLibrary, IPlaylistOptions filePathOptions)
{
try
{
final BatchRepair br = new BatchRepair(mediaFiles, new File(WINAMP_PATH));
final BatchRepair br = new BatchRepair(mediaLibrary, new File(WINAMP_PATH));
br.setDescription("Batch Repair: Winamp Playlists");
List<listfix.model.playlists.winamp.generated.Playlist> winLists = getWinampPlaylists();
for (listfix.model.playlists.winamp.generated.Playlist list : winLists)
Expand All @@ -83,19 +84,17 @@ public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaFiles, IPlayli
public static void extractPlaylists(File destDir, IProgressObserver<Void> observer) throws JAXBException, IOException
{
// avoid resetting total if part of batch operation
boolean hasTotal = observer instanceof ProgressAdapter;
ProgressAdapter<Void> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<Void> progress = ProgressAdapter.make(observer);

if (!destDir.exists())
{
destDir.mkdir();
if(!destDir.mkdir()) {
throw new IOException(String.format("Failed to create directory \"%s\"", destDir));
}
}

List<Playlist> winLists = getWinampPlaylists();
if (!hasTotal)
{
progress.setTotal(winLists.size());
}
progress.setTotal(winLists.size());
for (Playlist list : winLists)
{
Path sourceFile = Path.of(WINAMP_PATH, list.getFilename());
Expand All @@ -106,9 +105,6 @@ public static void extractPlaylists(File destDir, IProgressObserver<Void> observ
}
}

/**
* @return
*/
public static boolean isWinampInstalled()
{
return OperatingSystem.isWindows() && !WINAMP_PATH.isEmpty();
Expand All @@ -118,14 +114,13 @@ private static List<listfix.model.playlists.winamp.generated.Playlist> getWinamp
{
String playlistPath = WINAMP_PATH + "playlists.xml";
File listsFile = new File(playlistPath);
if (!listsFile.canRead())
if (listsFile.canRead())
{
return null;
JAXBContext context = JAXBContext.newInstance("listfix.model.playlists.winamp.generated");
Unmarshaller unmarshaller = context.createUnmarshaller();
Playlists lists = (Playlists) unmarshaller.unmarshal(listsFile);
return lists.getPlaylist();
}

JAXBContext context = JAXBContext.newInstance("listfix.model.playlists.winamp.generated");
Unmarshaller unmarshaller = context.createUnmarshaller();
Playlists lists = (Playlists) unmarshaller.unmarshal(listsFile);
return lists.getPlaylist();
return Collections.emptyList();
}
}
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/PlaylistWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void save(Playlist playlist, boolean saveRelative, @Nullable ProgressAdap
{
if (adapter == null) {
// Create a dummy progress-adapter
adapter = ProgressAdapter.wrap(null);
adapter = ProgressAdapter.make(null);
}

final int totalSteps = playlist.getEntries().size() + 3;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/m3u/M3UReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<PlaylistEntry> readPlaylist(IProgressObserver<String> observer) thro
// Line1 holds the metadata about the file that we just hang on to, line2 represents the file reference.

//Initialize the progress adapter if we're given an observer.
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(fileLength);

String line1 = buffer.readLine();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/pls/PLSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<PlaylistEntry> readPlaylist(IProgressObserver<String> observer) thro
// Definition of the PLS format can be found @ http://gonze.com/playlists/playlist-format-survey.html#PLS

// Init a progress adapter if we have a progress observer.
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);

// Load the PLS file into memory (it's basically a glorified INI | java properties file).
PLSProperties propBag = new PLSProperties();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/wpl/WPLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PlaylistType getPlaylistType()
@Override
public List<PlaylistEntry> readPlaylist(IProgressObserver<String> observer) throws IOException
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal((int) fileLength);

_cache = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/io/playlists/xspf/XSPFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public PlaylistType getPlaylistType()
public List<PlaylistEntry> readPlaylist(IProgressObserver<String> observer) throws IOException
{
// Init a progress adapter if we have a progress observer.
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);

List<PlaylistEntry> entriesList = new ArrayList<>();
SpecificPlaylist loadedList = SpecificPlaylistFactory.getInstance().readFrom(playlistPath.toFile());
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/listfix/model/BatchRepair.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void add(BatchRepairItem item)

/**
* Performs an exact matches search on all entries in multiple playlists.
*
* @param observer The progress observer for this operation.
*/
public void performExactMatchRepair(IDualProgressObserver<String> observer, IPlaylistOptions filePathOptions)
Expand All @@ -113,27 +114,28 @@ public void performExactMatchRepair(IDualProgressObserver<String> observer, IPla

/**
* Performs a closest matches search on all entries in multiple playlists.
*
* @param observer The progress observer for this operation.
*/
public void performClosestMatchRepair(IDualProgressObserver<String> observer, IPlaylistOptions filePathOptions)
{
this.performRepair(observer, filePathOptions, (item, list, task) -> {
item.setClosestMatches(list.findClosestMatches(this.mediaLibrary.getNestedMediaFiles(), task));
this.performRepair(observer, filePathOptions, (item, list, progressObserver) -> {
item.setClosestMatches(list.findClosestMatches(this.mediaLibrary.getNestedMediaFiles(), progressObserver));
});
}

public void performRepair(IDualProgressObserver<String> observer, IPlaylistOptions filePathOptions, IRepairItem repairItem)
{
DualProgressAdapter<String> progress = DualProgressAdapter.wrap(observer);
progress.getOverall().setTotal(_items.size() * 2L);
final DualProgressAdapter<String> progress = new DualProgressAdapter<>(observer);
final ProgressAdapter<String> overallProgress = progress.getOverall();
overallProgress.setTotal(_items.size() * 2L);

List<BatchRepairItem> toRemoveFromBatch = new ArrayList<>();
for (BatchRepairItem item : _items)
{
if (!observer.getCancelled())
{
// load
progress.getOverall().stepCompleted();
progress.getTask().reportProgress(0, "Loading \"" + item.getDisplayName() + "\"");
if (item.getPlaylist() == null)
{
Expand All @@ -148,11 +150,11 @@ public void performRepair(IDualProgressObserver<String> observer, IPlaylistOptio
toRemoveFromBatch.add(item);
}
}
overallProgress.stepCompleted();

// repair
if (item.getPlaylist() != null && item.getPlaylist().getMissingCount() > 0)
{
progress.getOverall().stepCompleted();
progress.getTask().reportProgress(0, "Repairing \"" + item.getDisplayName() + "\"");
Playlist list = item.getPlaylist();
repairItem.repair(item, list, progress.getTask());
Expand All @@ -162,6 +164,7 @@ public void performRepair(IDualProgressObserver<String> observer, IPlaylistOptio
// Don't fix playlists that have nothing to fix... instead remove them from the result set.
toRemoveFromBatch.add(item);
}
overallProgress.stepCompleted();
}
else
{
Expand Down Expand Up @@ -198,7 +201,7 @@ public String getDefaultBackupName()
*/
public void save(IPlaylistOptions filePathOptions, boolean isClosestMatchesSave, boolean backup, String destination, IProgressObserver<String> observer) throws Exception
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);

// get included items
int stepCount = 0;
Expand Down Expand Up @@ -258,7 +261,7 @@ public void save(IPlaylistOptions filePathOptions, boolean isClosestMatchesSave,
{
item.getPlaylist().applyClosestMatchSelections(item.getClosestMatches());
}
item.getPlaylist().save(filePathOptions.getSavePlaylistsWithRelativePaths(), progress);
item.getPlaylist().save(filePathOptions.getSavePlaylistsWithRelativePaths(), null); // ToDo nest observer
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/listfix/model/IRepairItem.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package listfix.model;

import listfix.model.playlists.Playlist;
import listfix.view.support.ProgressAdapter;
import listfix.view.support.IProgressObserver;

public interface IRepairItem
{

void repair(BatchRepairItem item, Playlist list, ProgressAdapter<String> task);
void repair(BatchRepairItem item, Playlist list, IProgressObserver<String> progressObserver);
}
20 changes: 8 additions & 12 deletions src/main/java/listfix/model/playlists/Playlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public List<PlaylistEntry> getEntries()

public void copySelectedEntries(List<Integer> entryIndexList, File destinationDirectory, IProgressObserver<String> observer)
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(entryIndexList.size());
PlaylistEntry tempEntry;
Path fileToCopy;
Expand Down Expand Up @@ -553,7 +553,7 @@ public int addAt(int ix, Collection<Path> files, IProgressObserver<String> obser

private List<PlaylistEntry> getEntriesForFiles(Collection<Path> files, IProgressObserver<String> observer) throws IOException
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(files.size());

List<PlaylistEntry> ents = new ArrayList<>();
Expand All @@ -565,7 +565,7 @@ private List<PlaylistEntry> getEntriesForFiles(Collection<Path> files, IProgress
{
// playlist file
IPlaylistReader reader = PlaylistReaderFactory.getPlaylistReader(file, this.playListOptions);
ents.addAll(reader.readPlaylist(progress));
ents.addAll(reader.readPlaylist(null)); // ToDo: nest this progress in overall progress
}
else
{
Expand Down Expand Up @@ -604,7 +604,7 @@ public void changeEntryFileName(int ix, String newName)
*/
public List<Integer> repair(IMediaLibrary mediaLibrary, IProgressObserver<String> observer)
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(this._entries.size());

List<Integer> fixed = new ArrayList<>();
Expand Down Expand Up @@ -669,7 +669,7 @@ public void batchRepair(IMediaLibrary dirLists, IProgressObserver<String> observ
*/
public void batchRepair(Collection<String> fileList, IMediaLibrary dirLists, IProgressObserver<String> observer)
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(_entries.size());

final boolean caseInsensitive = this.playListOptions.getCaseInsensitiveExactMatching();
Expand Down Expand Up @@ -714,7 +714,7 @@ public List<BatchMatchItem> findClosestMatches(Collection<String> libraryFiles,

public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Collection<String> libraryFiles, IProgressObserver<String> observer)
{
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(entries.size());

List<BatchMatchItem> fixed = new LinkedList<>();
Expand Down Expand Up @@ -936,12 +936,8 @@ public void saveAs(File destination, IProgressObserver<String> observer) throws
public final void save(boolean saveRelative, IProgressObserver<String> observer) throws Exception
{
// avoid resetting total if part of batch operation
boolean hasTotal = observer instanceof ProgressAdapter;
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
if (!hasTotal)
{
progress.setTotal(_entries.size());
}
ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(_entries.size());

IPlaylistWriter writer = PlaylistWriterFactory.getPlaylistWriter(_file, this.playListOptions);
writer.save(this, saveRelative, progress);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/listfix/model/playlists/PlaylistEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void play(IPlaylistOptions playListOptions) throws Exception
public List<PotentialPlaylistEntryMatch> findClosestMatches(Collection<String> mediaFiles, IProgressObserver<String> observer, IPlaylistOptions playListOptions)
{
List<PotentialPlaylistEntryMatch> matches = new ArrayList<>();
ProgressAdapter<String> progress = ProgressAdapter.wrap(observer);
ProgressAdapter<String> progress = new ProgressAdapter<>(observer);
progress.setTotal(mediaFiles.size());

matches.clear();
Expand Down
Loading

0 comments on commit 9200042

Please sign in to comment.