timetracking: add ability to sort timers

Lets the user sort by ascending or descending time.

Co-authored-by: jakewilson <jakewilsonfl@gmail.com>
This commit is contained in:
melkypie
2020-03-18 13:46:27 +02:00
committed by Adam
parent 902078cb62
commit 59361c0d95
4 changed files with 76 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020, Jake Wilson <https://github.com/jakewilson>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.timetracking;
public enum SortOrder
{
NONE,
ASC,
DESC
}

View File

@@ -83,6 +83,17 @@ public interface TimeTrackingConfig extends Config
return 5;
}
@ConfigItem(
keyName = "sortOrder",
name = "Sort Order",
description = "The order in which to sort the timers",
position = 5
)
default SortOrder sortOrder()
{
return SortOrder.NONE;
}
@ConfigItem(
keyName = "activeTab",
name = "Active Tab",

View File

@@ -224,13 +224,15 @@ public class TimeTrackingPlugin extends Plugin
long unitTime = Instant.now().toEpochMilli() / 200;
boolean clockDataChanged = false;
boolean timerOrderChanged = false;
if (unitTime % 5 == 0)
{
clockDataChanged = clockManager.checkCompletion();
timerOrderChanged = clockManager.checkTimerOrder();
}
if (unitTime % panel.getUpdateInterval() == 0 || clockDataChanged)
if (unitTime % panel.getUpdateInterval() == 0 || clockDataChanged || timerOrderChanged)
{
panel.update();
}

View File

@@ -24,10 +24,12 @@
*/
package net.runelite.client.plugins.timetracking.clocks;
import com.google.common.collect.Comparators;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.inject.Inject;
@@ -36,6 +38,7 @@ import joptsimple.internal.Strings;
import lombok.Getter;
import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.timetracking.SortOrder;
import net.runelite.client.plugins.timetracking.TimeTrackingConfig;
@Singleton
@@ -131,6 +134,33 @@ public class ClockManager
return changed;
}
/**
* Checks to ensure the timers are in the correct order.
* If they are not, sort them and rebuild the clock panel
*
* @return whether the timer order was changed or not
*/
public boolean checkTimerOrder()
{
SortOrder sortOrder = config.sortOrder();
if (sortOrder != SortOrder.NONE)
{
Comparator<Timer> comparator = Comparator.comparingLong(Timer::getDisplayTime);
if (sortOrder == SortOrder.DESC)
{
comparator = comparator.reversed();
}
if (!Comparators.isInOrder(timers, comparator))
{
timers.sort(comparator);
SwingUtilities.invokeLater(clockTabPanel::rebuild);
return true;
}
}
return false;
}
public void loadTimers()
{
final String timersJson = configManager.getConfiguration(TimeTrackingConfig.CONFIG_GROUP, TimeTrackingConfig.TIMERS);