From 4954f93cd878d269582ebe6ccc5486a3f8a5918b Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Tue, 7 Nov 2023 01:46:18 +0100 Subject: tracker core (#697) Tracker API #697 --- .../skyhanni/utils/tracker/DisplayMode.kt | 7 +++ .../skyhanni/utils/tracker/SharedTracker.kt | 18 +++++++ .../skyhanni/utils/tracker/TrackerData.kt | 5 ++ .../skyhanni/utils/tracker/TrackerUtils.kt | 62 ++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/tracker/DisplayMode.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/tracker/SharedTracker.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerData.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerUtils.kt (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/tracker/DisplayMode.kt b/src/main/java/at/hannibal2/skyhanni/utils/tracker/DisplayMode.kt new file mode 100644 index 000000000..6c0e14439 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/tracker/DisplayMode.kt @@ -0,0 +1,7 @@ +package at.hannibal2.skyhanni.utils.tracker + +enum class DisplayMode(val displayName: String) { + TOTAL("Total"), + CURRENT("This Session"), + ; +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/tracker/SharedTracker.kt b/src/main/java/at/hannibal2/skyhanni/utils/tracker/SharedTracker.kt new file mode 100644 index 000000000..341600467 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/tracker/SharedTracker.kt @@ -0,0 +1,18 @@ +package at.hannibal2.skyhanni.utils.tracker + +class SharedTracker( + private val total: T, + private val currentSession: T, +) { + fun modify(modifyFunction: (T) -> Unit) { + modifyFunction(total) + modifyFunction(currentSession) + } + + fun get(displayMode: DisplayMode) = when (displayMode) { + DisplayMode.TOTAL -> total + DisplayMode.CURRENT -> currentSession + } + + fun getCurrent() = get(TrackerUtils.currentDisplayMode) +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerData.kt b/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerData.kt new file mode 100644 index 000000000..3c4a8bbd0 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerData.kt @@ -0,0 +1,5 @@ +package at.hannibal2.skyhanni.utils.tracker + +abstract class TrackerData { + abstract fun reset() +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerUtils.kt new file mode 100644 index 000000000..c223f17e7 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/tracker/TrackerUtils.kt @@ -0,0 +1,62 @@ +package at.hannibal2.skyhanni.utils.tracker + +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.LorenzUtils.addAsSingletonList +import at.hannibal2.skyhanni.utils.LorenzUtils.addSelector +import at.hannibal2.skyhanni.utils.renderables.Renderable + +object TrackerUtils { + var currentDisplayMode = DisplayMode.TOTAL + + fun MutableList>.addDisplayModeToggle(update: () -> Unit) { + addSelector( + "§7Display Mode: ", + getName = { type -> type.displayName }, + isCurrent = { it == currentDisplayMode }, + onChange = { + currentDisplayMode = it + update() + } + ) + } + + fun MutableList>.addSessionResetButton(name: String, data: SharedTracker<*>?, update: () -> Unit) { + addAsSingletonList( + Renderable.clickAndHover( + "§cReset session!", + listOf( + "§cThis will reset your", + "§ccurrent session for", + "§c$name" + ), + ) { + data?.get(DisplayMode.CURRENT)?.let { + reset(it) { + update() + } + } + }) + } + + fun resetCommand(name: String, command: String, args: Array, data: SharedTracker<*>?, update: () -> Unit) { + if (args.size == 1 && args[0].lowercase() == "confirm") { + reset(data?.get(DisplayMode.TOTAL)) { + update() + LorenzUtils.chat("§e[SkyHanni] You reset your $name data!") + } + return + } + + LorenzUtils.clickableChat( + "§e[SkyHanni] Are you sure you want to reset all your $name data? Click here to confirm.", + "$command confirm" + ) + } + + fun reset(data: TrackerData?, update: () -> Unit) { + data?.let { + it.reset() + update() + } + } +} -- cgit