Skip to content

Commit

Permalink
Add warning if timer is running for more than 10 s already and someon…
Browse files Browse the repository at this point in the history
…e tries to cancel it
  • Loading branch information
MrApplejuice committed May 28, 2024
1 parent b810d09 commit baedf11
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -757,44 +757,11 @@ class LoggingButtonController(
controller.postInit()

logicMap[activity]?.addStateListener { state, userInduced ->
if (state) {
controlsInterface.insertControls(controller.controlsView)
if (userInduced && (controller is TimerBase)) {
cachedTimers.firstOrNull { it.name == activity }?.let {
runningTimerMods += 1
timerControl.startTimer(
it,
object : Promise<Timer, TranslatedException> {
override fun succeeded(t: Timer) {
runningTimerMods -= 1
}

override fun failed(f: TranslatedException?) {
runningTimerMods -= 1
newTimerListLoaded(cachedTimers)
}
})
}
}
} else {
controlsInterface.removeControls(controller.controlsView)
if (userInduced && (controller is TimerBase)) {
controller.updateTimer(null)
cachedTimers.firstOrNull { it.name == activity }?.let {
runningTimerMods += 1
timerControl.stopTimer(
it,
object : Promise<Any, TranslatedException> {
override fun succeeded(s: Any?) {
runningTimerMods -= 1
}

override fun failed(f: TranslatedException?) {
runningTimerMods -= 1
newTimerListLoaded(cachedTimers)
}
})
}
fragment.mainActivity.scope.launch {
if (state) {
startTimerFromSwitch(controller, userInduced, activity)
} else {
stopTimerFromSwitch(controller, userInduced, activity)
}
}
}
Expand All @@ -821,6 +788,93 @@ class LoggingButtonController(
timerHandler()
}

private fun startTimerFromSwitch(
controller: LoggingControls,
userInduced: Boolean,
activity: String
) {
controlsInterface.insertControls(controller.controlsView)
if (userInduced && (controller is TimerBase)) {
cachedTimers.firstOrNull { it.name == activity }?.let {
runningTimerMods += 1
timerControl.startTimer(
it,
object : Promise<Timer, TranslatedException> {
override fun succeeded(t: Timer) {
runningTimerMods -= 1
}

override fun failed(f: TranslatedException?) {
runningTimerMods -= 1
newTimerListLoaded(cachedTimers)
}
})
}
}
}

private suspend fun stopTimerFromSwitch(
controller: LoggingControls,
userInduced: Boolean,
activity: String
) {
val timer = cachedTimers.firstOrNull { it.name == activity }
if (AsyncPromise.call<Boolean, TranslatedException> { promise ->
var defaultSucceed = true
timer?.let { timer ->
if (timer.active && userInduced) {
val timeMs = nowServer().time - timer.start.time
if (timeMs > 10000) {
defaultSucceed = false;

val message = Phrase.from(
fragment.requireContext(),
R.string.cancel_timer_warning_message
)
.put("activity", fragment.translateActivityName(activity))
.format().toString()

fragment.showQuestion(
true,
fragment.getString(R.string.cancel_timer_warning_title),
message,
fragment.getString(R.string.cancel_timer_warning_stop),
fragment.getString(R.string.cancel_timer_warning_keep),
object : DialogCallback {
override fun call(b: Boolean) {
promise.succeeded(b)
}
}
);
}
}
}
if (defaultSucceed) {
promise.succeeded(true)
}
}) {
controlsInterface.removeControls(controller.controlsView)
if (userInduced && (controller is TimerBase)) {
controller.updateTimer(null)
timer?.let {
runningTimerMods += 1
timerControl.stopTimer(
it,
object : Promise<Any, TranslatedException> {
override fun succeeded(s: Any?) {
runningTimerMods -= 1
}

override fun failed(f: TranslatedException?) {
runningTimerMods -= 1
newTimerListLoaded(cachedTimers)
}
})
}
}
}
}

private fun timerHandler() {
timerHandler?.let {
it.postDelayed(Runnable { timerHandler() }, 500)
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
<string name="activity_store_failure_failed_to_stop_message">Stopping timer failed</string>
<string name="activity_store_failure_start_timer_failed">Start timer failed</string>

<!--- Cancel timer warning -->
<string name="cancel_timer_warning_title">Stop timer?</string>
<string name="cancel_timer_warning_message">The {activity} timer is running for a long time already. Do you really want to stop it or keep it running?</string>
<string name="cancel_timer_warning_stop">Stop</string>
<string name="cancel_timer_warning_keep">Keep</string>


<!--- Store activities: Conflicts -->
<string name="conflicting_activity_title">Overlapping time entries</string>
<string name="conflicting_activity_text">
Expand Down

0 comments on commit baedf11

Please sign in to comment.