Skip to content

Commit

Permalink
timetracking: add ability to sort timers
Browse files Browse the repository at this point in the history
Lets the user sort by ascending or descending time.

Co-authored-by: jakewilson <[email protected]>
  • Loading branch information
2 people authored and Adam- committed Mar 18, 2020
1 parent 902078c commit 59361c0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ default int defaultTimerMinutes()
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ private void updatePanel()
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +38,7 @@
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
Expand Down Expand Up @@ -131,6 +134,33 @@ public boolean checkCompletion()
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);
Expand Down

0 comments on commit 59361c0

Please sign in to comment.