From 2caa4de55dcbbbc6424b57e028dc076f21e8cbca Mon Sep 17 00:00:00 2001 From: MarKco Date: Sat, 2 Jan 2021 17:16:26 +0100 Subject: [PATCH] Convert MidnightTimer to Kotlin Part of the conversion to Kotlin of the whole org.jsoron.uhabits.core.utils package. --- .../uhabits/core/utils/MidnightTimer.java | 77 ------------------- .../uhabits/core/utils/MidnightTimer.kt | 63 +++++++++++++++ 2 files changed, 63 insertions(+), 77 deletions(-) delete mode 100644 android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.java create mode 100644 android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.kt diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.java deleted file mode 100644 index 0beee5cd8..000000000 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2017 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.core.utils; - -import org.isoron.uhabits.core.*; - -import java.util.*; -import java.util.concurrent.*; - -import javax.inject.*; - -/** - * A class that emits events when a new day starts. - */ -@AppScope -public class MidnightTimer -{ - private final List listeners; - - private ScheduledExecutorService executor; - - @Inject - public MidnightTimer() - { - this.listeners = new LinkedList<>(); - } - - public synchronized void addListener(MidnightListener listener) - { - this.listeners.add(listener); - } - - public synchronized void onPause() - { - executor.shutdownNow(); - } - - public synchronized void onResume() - { - executor = Executors.newSingleThreadScheduledExecutor(); - executor.scheduleAtFixedRate(() -> notifyListeners(), - DateUtils.millisecondsUntilTomorrowWithOffset() + 1000, - DateUtils.DAY_LENGTH, TimeUnit.MILLISECONDS); - } - - public synchronized void removeListener(MidnightListener listener) - { - this.listeners.remove(listener); - } - - private synchronized void notifyListeners() - { - for (MidnightListener l : listeners) l.atMidnight(); - } - - public interface MidnightListener - { - void atMidnight(); - } -} diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.kt b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.kt new file mode 100644 index 000000000..25ee9270b --- /dev/null +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/utils/MidnightTimer.kt @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2017 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.core.utils + +import org.isoron.uhabits.core.AppScope +import java.util.LinkedList +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService +import java.util.concurrent.TimeUnit +import javax.inject.Inject + +/** + * A class that emits events when a new day starts. + */ +@AppScope +class MidnightTimer @Inject constructor() { + private val listeners: MutableList = LinkedList() + private lateinit var executor: ScheduledExecutorService + + @Synchronized fun addListener(listener: MidnightListener) { + this.listeners.add(listener) + } + + @Synchronized fun onPause() = executor.shutdownNow() + + @Synchronized fun onResume() { + executor = Executors.newSingleThreadScheduledExecutor() + executor.scheduleAtFixedRate( + { notifyListeners() }, + DateUtils.millisecondsUntilTomorrowWithOffset() + 1000, + DateUtils.DAY_LENGTH, + TimeUnit.MILLISECONDS + ) + } + + @Synchronized fun removeListener(listener: MidnightListener) = this.listeners.remove(listener) + + @Synchronized private fun notifyListeners() { + for (l in listeners) { + l.atMidnight() + } + } + + interface MidnightListener { + fun atMidnight() + } +}