#445: Add LastPlayed SortingStrategies

This commit is contained in:
s0cks 2021-12-02 04:53:34 -07:00
parent db53c6fde5
commit 0b09cbb971
5 changed files with 51 additions and 10 deletions

View file

@ -4,9 +4,11 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="#JAVA_HOME" />
<option name="gradleJvm" value="1.8" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View file

@ -152,6 +152,7 @@ public class Instance extends MinecraftVersion {
public Instance(MinecraftVersion version) {
setValues(version);
this.lastPlayed = Instant.EPOCH;
}
public void setValues(MinecraftVersion version) {
@ -1855,6 +1856,10 @@ public class Instance extends MinecraftVersion {
return this.lastPlayed;
}
public Instant getLastPlayedOrEpoch(){
return this.lastPlayed != null ? this.lastPlayed : Instant.EPOCH;
}
public String getMainClass() {
return mainClass;
}

View file

@ -28,6 +28,10 @@ import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
@ -127,6 +131,11 @@ public class CollapsiblePanel extends JPanel implements ThemeListener {
title = instance.launcher.name;
}
final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale( Locale.US)
.withZone(ZoneId.systemDefault());
title += " - Last Played: " + formatter.format(instance.getLastPlayedOrEpoch());
if (instance.launcher.isPlayable) {
arrow.setText(title);
arrow.setForeground(UIManager.getColor("CollapsiblePanel.normal"));

View file

@ -28,6 +28,7 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@ -58,7 +59,8 @@ public class InstancesTab extends JPanel implements Tab, RelocalizationListener,
private JButton searchButton;
private JComboBox<InstanceSortingStrategy> sortingStrategyComboBox;
private InstanceSortingStrategy sortingStrategy = InstanceSortingStrategies.BY_NAME;
private AtomicReference<InstanceSortingStrategy> sortingStrategy = new AtomicReference<>(InstanceSortingStrategies.BY_NAME);
private String searchText = null;
@ -68,8 +70,6 @@ public class InstancesTab extends JPanel implements Tab, RelocalizationListener,
private NilCard nilCard;
private final List<InstanceCard> cards = new LinkedList<>();
public InstancesTab() {
setLayout(new BorderLayout());
loadContent(false);
@ -110,7 +110,7 @@ public class InstancesTab extends JPanel implements Tab, RelocalizationListener,
});
this.sortingStrategyComboBox = new JComboBox<>(InstanceSortingStrategies.values());
this.sortingStrategyComboBox.setSelectedItem(this.sortingStrategy);
this.sortingStrategyComboBox.setSelectedItem(this.sortingStrategy.get());
this.sortingStrategyComboBox.addItemListener(this);
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
@ -145,11 +145,10 @@ public class InstancesTab extends JPanel implements Tab, RelocalizationListener,
if(keepFilters){
InstanceManager.getInstancesSorted().stream()
.filter(this.createFilter())
.sorted(this.sortingStrategy)
.forEach(this.createInstanceCard(gbc));
.sorted(this.sortingStrategy.get())
.forEachOrdered(this.createInstanceCard(gbc));
} else{
InstanceManager.getInstancesSorted().stream()
.sorted(this.sortingStrategy)
InstanceManager.getInstancesSorted()
.forEach(this.createInstanceCard(gbc));
}
@ -218,7 +217,7 @@ public class InstancesTab extends JPanel implements Tab, RelocalizationListener,
if(e.getStateChange() == ItemEvent.SELECTED){
InstanceSortingStrategy strategy = (InstanceSortingStrategy)e.getItem();
LogManager.info("using " + strategy.getName() + " sorting strategy");
this.sortingStrategy = strategy;
this.sortingStrategy.set(strategy);
this.loadContent(true);
}
}

View file

@ -8,6 +8,32 @@ public enum InstanceSortingStrategies implements InstanceSortingStrategy{
public int compare(Instance lhs, Instance rhs){
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
},
BY_LAST_PLAYED_ASC("By Last Played (Asc)"){
@Override
public int compare(Instance lhs, Instance rhs){
long lhsEpoch = lhs.getLastPlayedOrEpoch().toEpochMilli();
long rhsEpoch = rhs.getLastPlayedOrEpoch().toEpochMilli();
if(lhsEpoch < rhsEpoch){
return -1;
} else if(lhsEpoch > rhsEpoch){
return +1;
}
return 0;
}
},
BY_LAST_PLAYED_DESC("By Last Played (Desc)"){
@Override
public int compare(Instance lhs, Instance rhs){
long lhsEpoch = lhs.getLastPlayedOrEpoch().toEpochMilli();
long rhsEpoch = rhs.getLastPlayedOrEpoch().toEpochMilli();
if(lhsEpoch > rhsEpoch){
return -1;
} else if(lhsEpoch < rhsEpoch){
return +1;
}
return 0;
}
};
private final String name;