diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/AndroidThemeSwitcher.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/AndroidThemeSwitcher.kt index 0a5e507c4..262db7fda 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/AndroidThemeSwitcher.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/AndroidThemeSwitcher.kt @@ -41,7 +41,7 @@ constructor( preferences: Preferences, ) : ThemeSwitcher(preferences) { - private var currentTheme: Theme = LightTheme() + override var currentTheme: Theme = LightTheme() override fun getSystemTheme(): Int { if (SDK_INT < 29) return THEME_LIGHT @@ -53,9 +53,9 @@ constructor( } } - override fun getCurrentTheme(): Theme { - return currentTheme - } + // override fun getCurrentTheme(): Theme { + // return currentTheme + // } override fun applyDarkTheme() { currentTheme = DarkTheme() diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt index ec6cb5e61..a31b78458 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsActivity.kt @@ -33,7 +33,7 @@ import org.isoron.uhabits.activities.habits.list.views.HabitCardListAdapter import org.isoron.uhabits.core.preferences.Preferences import org.isoron.uhabits.core.sync.SyncManager import org.isoron.uhabits.core.tasks.TaskRunner -import org.isoron.uhabits.core.ui.ThemeSwitcher.THEME_DARK +import org.isoron.uhabits.core.ui.ThemeSwitcher.Companion.THEME_DARK import org.isoron.uhabits.core.utils.MidnightTimer import org.isoron.uhabits.database.AutoBackup import org.isoron.uhabits.inject.ActivityContextModule diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt b/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt index 711d94547..8f697d490 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt @@ -53,7 +53,7 @@ class AndroidNotificationTray ) : NotificationTray.SystemTray { private var active = HashSet() - override fun log(msg: String) { + override fun log(msg: String?) { Log.d("AndroidNotificationTray", msg) } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/receivers/ReminderController.kt b/uhabits-android/src/main/java/org/isoron/uhabits/receivers/ReminderController.kt index 65956bb2d..7e1e18a46 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/receivers/ReminderController.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/receivers/ReminderController.kt @@ -43,7 +43,7 @@ class ReminderController @Inject constructor( fun onShowReminder( habit: Habit, - timestamp: Timestamp?, + timestamp: Timestamp, reminderTime: Long ) { notificationTray.show(habit, timestamp, reminderTime) diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.java deleted file mode 100644 index 74c654794..000000000 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (C) 2016-2021 Á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.ui; - -import androidx.annotation.*; - -import org.isoron.uhabits.core.*; -import org.isoron.uhabits.core.commands.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.preferences.*; -import org.isoron.uhabits.core.tasks.*; -import org.isoron.uhabits.core.utils.*; - -import java.util.*; - -import javax.inject.*; - - -@AppScope -public class NotificationTray - implements CommandRunner.Listener, Preferences.Listener -{ - public static final String REMINDERS_CHANNEL_ID = "REMINDERS"; - - @NonNull - private final TaskRunner taskRunner; - - @NonNull - private final CommandRunner commandRunner; - - @NonNull - private final Preferences preferences; - - private SystemTray systemTray; - - @NonNull - private final HashMap active; - - @Inject - public NotificationTray(@NonNull TaskRunner taskRunner, - @NonNull CommandRunner commandRunner, - @NonNull Preferences preferences, - @NonNull SystemTray systemTray) - { - this.taskRunner = taskRunner; - this.commandRunner = commandRunner; - this.preferences = preferences; - this.systemTray = systemTray; - this.active = new HashMap<>(); - } - - public void cancel(@NonNull Habit habit) - { - int notificationId = getNotificationId(habit); - systemTray.removeNotification(notificationId); - active.remove(habit); - } - - @Override - public void onCommandFinished(@Nullable Command command) - { - if (command instanceof CreateRepetitionCommand) - { - CreateRepetitionCommand createCmd = (CreateRepetitionCommand) command; - Habit habit = createCmd.getHabit(); - cancel(habit); - } - - if (command instanceof DeleteHabitsCommand) - { - DeleteHabitsCommand deleteCommand = (DeleteHabitsCommand) command; - List deleted = deleteCommand.getSelected(); - for (Habit habit : deleted) - cancel(habit); - } - } - - @Override - public void onNotificationsChanged() - { - reshowAll(); - } - - public void show(@NonNull Habit habit, Timestamp timestamp, long reminderTime) - { - NotificationData data = new NotificationData(timestamp, reminderTime); - active.put(habit, data); - taskRunner.execute(new ShowNotificationTask(habit, data)); - } - - public void startListening() - { - commandRunner.addListener(this); - preferences.addListener(this); - } - - public void stopListening() - { - commandRunner.removeListener(this); - preferences.removeListener(this); - } - - private int getNotificationId(Habit habit) - { - Long id = habit.getId(); - if (id == null) return 0; - return (int) (id % Integer.MAX_VALUE); - } - - private void reshowAll() - { - for (Habit habit : active.keySet()) - { - NotificationData data = active.get(habit); - taskRunner.execute(new ShowNotificationTask(habit, data)); - } - } - - public interface SystemTray - { - void removeNotification(int notificationId); - - void showNotification(Habit habit, - int notificationId, - Timestamp timestamp, - long reminderTime); - - void log(String msg); - } - - static class NotificationData - { - public final Timestamp timestamp; - - public final long reminderTime; - - public NotificationData(Timestamp timestamp, long reminderTime) - { - this.timestamp = timestamp; - this.reminderTime = reminderTime; - } - } - - private class ShowNotificationTask implements Task - { - int todayValue; - - private final Habit habit; - - private final Timestamp timestamp; - - private final long reminderTime; - - public ShowNotificationTask(Habit habit, NotificationData data) - { - this.habit = habit; - this.timestamp = data.timestamp; - this.reminderTime = data.reminderTime; - } - - @Override - public void doInBackground() - { - Timestamp today = DateUtils.getTodayWithOffset(); - todayValue = habit.getComputedEntries().get(today).getValue(); - } - - @Override - public void onPostExecute() - { - systemTray.log("Showing notification for habit=" + habit.getId()); - - if (todayValue != Entry.UNKNOWN) { - systemTray.log(String.format( - Locale.US, - "Habit %d already checked. Skipping.", - habit.getId())); - return; - } - - if (!habit.hasReminder()) { - systemTray.log(String.format( - Locale.US, - "Habit %d does not have a reminder. Skipping.", - habit.getId())); - return; - } - - if (habit.isArchived()) - { - systemTray.log(String.format( - Locale.US, - "Habit %d is archived. Skipping.", - habit.getId())); - return; - } - - if (!shouldShowReminderToday()) { - systemTray.log(String.format( - Locale.US, - "Habit %d not supposed to run today. Skipping.", - habit.getId())); - return; - } - - systemTray.showNotification(habit, getNotificationId(habit), timestamp, - reminderTime); - } - - private boolean shouldShowReminderToday() - { - if (!habit.hasReminder()) return false; - Reminder reminder = habit.getReminder(); - - boolean[] reminderDays = Objects.requireNonNull(reminder).getDays().toArray(); - int weekday = timestamp.getWeekday(); - - return reminderDays[weekday]; - } - } -} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.kt new file mode 100644 index 000000000..666947f85 --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/NotificationTray.kt @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2016-2021 Á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.ui + +import org.isoron.uhabits.core.AppScope +import org.isoron.uhabits.core.commands.Command +import org.isoron.uhabits.core.commands.CommandRunner +import org.isoron.uhabits.core.commands.CreateRepetitionCommand +import org.isoron.uhabits.core.commands.DeleteHabitsCommand +import org.isoron.uhabits.core.models.Entry +import org.isoron.uhabits.core.models.Habit +import org.isoron.uhabits.core.models.Timestamp +import org.isoron.uhabits.core.preferences.Preferences +import org.isoron.uhabits.core.tasks.Task +import org.isoron.uhabits.core.tasks.TaskRunner +import org.isoron.uhabits.core.utils.DateUtils.Companion.getTodayWithOffset +import java.util.HashMap +import java.util.Locale +import java.util.Objects +import javax.inject.Inject + +@AppScope +class NotificationTray @Inject constructor( + private val taskRunner: TaskRunner, + private val commandRunner: CommandRunner, + private val preferences: Preferences, + private val systemTray: SystemTray +) : CommandRunner.Listener, Preferences.Listener { + private val active: HashMap + fun cancel(habit: Habit) { + val notificationId = getNotificationId(habit) + systemTray.removeNotification(notificationId) + active.remove(habit) + } + + override fun onCommandFinished(command: Command) { + if (command is CreateRepetitionCommand) { + val (_, habit) = command + cancel(habit) + } + if (command is DeleteHabitsCommand) { + val (_, deleted) = command + for (habit in deleted) cancel(habit) + } + } + + override fun onNotificationsChanged() { + reshowAll() + } + + fun show(habit: Habit, timestamp: Timestamp, reminderTime: Long) { + val data = NotificationData(timestamp, reminderTime) + active[habit] = data + taskRunner.execute(ShowNotificationTask(habit, data)) + } + + fun startListening() { + commandRunner.addListener(this) + preferences.addListener(this) + } + + fun stopListening() { + commandRunner.removeListener(this) + preferences.removeListener(this) + } + + private fun getNotificationId(habit: Habit): Int { + val id = habit.id ?: return 0 + return (id % Int.MAX_VALUE).toInt() + } + + private fun reshowAll() { + for (habit in active.keys) { + val data = active[habit]!! + taskRunner.execute(ShowNotificationTask(habit, data)) + } + } + + interface SystemTray { + fun removeNotification(notificationId: Int) + fun showNotification( + habit: Habit, + notificationId: Int, + timestamp: Timestamp, + reminderTime: Long + ) + + fun log(msg: String?) + } + + internal class NotificationData(val timestamp: Timestamp, val reminderTime: Long) + private inner class ShowNotificationTask(private val habit: Habit, data: NotificationData) : + Task { + var todayValue = 0 + private val timestamp: Timestamp + private val reminderTime: Long + override fun doInBackground() { + val today = getTodayWithOffset() + todayValue = habit.computedEntries.get(today).value + } + + override fun onPostExecute() { + systemTray.log("Showing notification for habit=" + habit.id) + if (todayValue != Entry.UNKNOWN) { + systemTray.log( + String.format( + Locale.US, + "Habit %d already checked. Skipping.", + habit.id + ) + ) + return + } + if (!habit.hasReminder()) { + systemTray.log( + String.format( + Locale.US, + "Habit %d does not have a reminder. Skipping.", + habit.id + ) + ) + return + } + if (habit.isArchived) { + systemTray.log( + String.format( + Locale.US, + "Habit %d is archived. Skipping.", + habit.id + ) + ) + return + } + if (!shouldShowReminderToday()) { + systemTray.log( + String.format( + Locale.US, + "Habit %d not supposed to run today. Skipping.", + habit.id + ) + ) + return + } + systemTray.showNotification( + habit, + getNotificationId(habit), + timestamp, + reminderTime + ) + } + + private fun shouldShowReminderToday(): Boolean { + if (!habit.hasReminder()) return false + val reminder = habit.reminder + val reminderDays = Objects.requireNonNull(reminder)!!.days.toArray() + val weekday = timestamp.weekday + return reminderDays[weekday] + } + + init { + timestamp = data.timestamp + reminderTime = data.reminderTime + } + } + + companion object { + const val REMINDERS_CHANNEL_ID = "REMINDERS" + } + + init { + active = HashMap() + } +} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.java deleted file mode 100644 index ac9fd8dff..000000000 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2016-2021 Á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.ui; - -import androidx.annotation.*; - -import org.isoron.uhabits.core.preferences.*; -import org.isoron.uhabits.core.ui.views.*; - -public abstract class ThemeSwitcher -{ - public static final int THEME_DARK = 1; - - public static final int THEME_LIGHT = 2; - - public static final int THEME_AUTOMATIC = 0; - - private final Preferences preferences; - - public ThemeSwitcher(@NonNull Preferences preferences) - { - this.preferences = preferences; - } - - public void apply() - { - if (isNightMode()) - { - if (preferences.isPureBlackEnabled()) applyPureBlackTheme(); - else applyDarkTheme(); - } - else - { - applyLightTheme(); - } - } - - public abstract void applyDarkTheme(); - - public abstract void applyLightTheme(); - - public abstract void applyPureBlackTheme(); - - public abstract int getSystemTheme(); - - public abstract Theme getCurrentTheme(); - - public boolean isNightMode() - { - int systemTheme = getSystemTheme(); - int userTheme = preferences.getTheme(); - - return (userTheme == THEME_DARK || - (systemTheme == THEME_DARK && userTheme == THEME_AUTOMATIC)); - } - - public void toggleNightMode() - { - int systemTheme = getSystemTheme(); - int userTheme = preferences.getTheme(); - - if(userTheme == THEME_AUTOMATIC) - { - if(systemTheme == THEME_LIGHT) preferences.setTheme(THEME_DARK); - if(systemTheme == THEME_DARK) preferences.setTheme(THEME_LIGHT); - } - else if(userTheme == THEME_LIGHT) - { - if (systemTheme == THEME_LIGHT) preferences.setTheme(THEME_DARK); - if (systemTheme == THEME_DARK) preferences.setTheme(THEME_AUTOMATIC); - } - else if(userTheme == THEME_DARK) - { - if (systemTheme == THEME_LIGHT) preferences.setTheme(THEME_AUTOMATIC); - if (systemTheme == THEME_DARK) preferences.setTheme(THEME_LIGHT); - } - } -} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.kt new file mode 100644 index 000000000..8ad3eb421 --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/ThemeSwitcher.kt @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016-2021 Á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.ui + +import org.isoron.uhabits.core.preferences.Preferences +import org.isoron.uhabits.core.ui.views.Theme + +abstract class ThemeSwitcher(private val preferences: Preferences) { + fun apply() { + if (isNightMode) { + if (preferences.isPureBlackEnabled) applyPureBlackTheme() else applyDarkTheme() + } else { + applyLightTheme() + } + } + + abstract fun applyDarkTheme() + abstract fun applyLightTheme() + abstract fun applyPureBlackTheme() + abstract fun getSystemTheme(): Int + abstract val currentTheme: Theme? + val isNightMode: Boolean + get() { + val systemTheme = getSystemTheme() + val userTheme = preferences.theme + return userTheme == THEME_DARK || + systemTheme == THEME_DARK && userTheme == THEME_AUTOMATIC + } + + fun toggleNightMode() { + val systemTheme = getSystemTheme() + val userTheme = preferences.theme + if (userTheme == THEME_AUTOMATIC) { + if (systemTheme == THEME_LIGHT) preferences.theme = THEME_DARK + if (systemTheme == THEME_DARK) preferences.theme = THEME_LIGHT + } else if (userTheme == THEME_LIGHT) { + if (systemTheme == THEME_LIGHT) preferences.theme = THEME_DARK + if (systemTheme == THEME_DARK) preferences.theme = THEME_AUTOMATIC + } else if (userTheme == THEME_DARK) { + if (systemTheme == THEME_LIGHT) preferences.theme = THEME_AUTOMATIC + if (systemTheme == THEME_DARK) preferences.theme = THEME_LIGHT + } + } + + companion object { + const val THEME_DARK = 1 + const val THEME_LIGHT = 2 + const val THEME_AUTOMATIC = 0 + } +} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.kt similarity index 81% rename from uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.java rename to uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.kt index a34b48ac7..1129fb973 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.java +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnColorPickedCallback.kt @@ -16,12 +16,10 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.core.ui.callbacks -package org.isoron.uhabits.core.ui.callbacks; +import org.isoron.uhabits.core.models.PaletteColor -import org.isoron.uhabits.core.models.*; - -public interface OnColorPickedCallback -{ - void onColorPicked(PaletteColor color); +fun interface OnColorPickedCallback { + fun onColorPicked(color: PaletteColor) } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.kt similarity index 88% rename from uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.java rename to uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.kt index 190232e31..5d6ad813a 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.java +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.kt @@ -16,10 +16,8 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.core.ui.callbacks -package org.isoron.uhabits.core.ui.callbacks; - -public interface OnFinishedCallback -{ - void onFinish(); +fun interface OnConfirmedCallback { + fun onConfirmed() } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.kt similarity index 87% rename from uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.java rename to uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.kt index b43277728..1da8ce98d 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnConfirmedCallback.java +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/callbacks/OnFinishedCallback.kt @@ -16,10 +16,8 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.core.ui.callbacks -package org.isoron.uhabits.core.ui.callbacks; - -public interface OnConfirmedCallback -{ - void onConfirmed(); +interface OnFinishedCallback { + fun onFinish() } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.java b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.java deleted file mode 100644 index 1499674c0..000000000 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2016-2021 Á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.ui.widgets; - -import androidx.annotation.*; - -import org.isoron.uhabits.core.commands.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.preferences.*; -import org.isoron.uhabits.core.ui.*; -import org.jetbrains.annotations.*; - -import javax.inject.*; - -import static org.isoron.uhabits.core.models.Entry.*; - -public class WidgetBehavior -{ - private HabitList habitList; - - @NonNull - private final CommandRunner commandRunner; - - @NonNull - private final NotificationTray notificationTray; - - @NonNull - private final Preferences preferences; - - @Inject - public WidgetBehavior(@NonNull HabitList habitList, - @NonNull CommandRunner commandRunner, - @NonNull NotificationTray notificationTray, - @NonNull Preferences preferences) - { - this.habitList = habitList; - this.commandRunner = commandRunner; - this.notificationTray = notificationTray; - this.preferences = preferences; - } - - public void onAddRepetition(@NonNull Habit habit, Timestamp timestamp) - { - notificationTray.cancel(habit); - setValue(habit, timestamp, YES_MANUAL); - } - - public void onRemoveRepetition(@NonNull Habit habit, Timestamp timestamp) - { - notificationTray.cancel(habit); - setValue(habit, timestamp, NO); - } - - public void onToggleRepetition(@NonNull Habit habit, Timestamp timestamp) - { - int currentValue = habit.getOriginalEntries().get(timestamp).getValue(); - int newValue; - if(preferences.isSkipEnabled()) - newValue = Entry.Companion.nextToggleValueWithSkip(currentValue); - else - newValue = Entry.Companion.nextToggleValueWithoutSkip(currentValue); - setValue(habit, timestamp, newValue); - notificationTray.cancel(habit); - } - - public void onIncrement(@NotNull Habit habit, @NotNull Timestamp timestamp, int amount) { - int currentValue = habit.getComputedEntries().get(timestamp).getValue(); - setValue(habit, timestamp, currentValue + amount); - notificationTray.cancel(habit); - } - - public void onDecrement(@NotNull Habit habit, @NotNull Timestamp timestamp, int amount) { - int currentValue = habit.getComputedEntries().get(timestamp).getValue(); - setValue(habit, timestamp, currentValue - amount); - notificationTray.cancel(habit); - } - - public void setValue(@NonNull Habit habit, Timestamp timestamp, int newValue) { - commandRunner.run( - new CreateRepetitionCommand(habitList, habit, timestamp, newValue) - ); - } -} diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt new file mode 100644 index 000000000..99bad0e84 --- /dev/null +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016-2021 Á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.ui.widgets + +import org.isoron.uhabits.core.commands.CommandRunner +import org.isoron.uhabits.core.commands.CreateRepetitionCommand +import org.isoron.uhabits.core.models.Entry +import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithSkip +import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithoutSkip +import org.isoron.uhabits.core.models.Habit +import org.isoron.uhabits.core.models.HabitList +import org.isoron.uhabits.core.models.Timestamp +import org.isoron.uhabits.core.preferences.Preferences +import org.isoron.uhabits.core.ui.NotificationTray +import javax.inject.Inject + +class WidgetBehavior @Inject constructor( + private val habitList: HabitList, + private val commandRunner: CommandRunner, + private val notificationTray: NotificationTray, + private val preferences: Preferences +) { + fun onAddRepetition(habit: Habit, timestamp: Timestamp?) { + notificationTray.cancel(habit) + setValue(habit, timestamp, Entry.YES_MANUAL) + } + + fun onRemoveRepetition(habit: Habit, timestamp: Timestamp?) { + notificationTray.cancel(habit) + setValue(habit, timestamp, Entry.NO) + } + + fun onToggleRepetition(habit: Habit, timestamp: Timestamp?) { + val currentValue = habit.originalEntries.get(timestamp!!).value + val newValue: Int + newValue = + if (preferences.isSkipEnabled) nextToggleValueWithSkip( + currentValue + ) else nextToggleValueWithoutSkip( + currentValue + ) + setValue(habit, timestamp, newValue) + notificationTray.cancel(habit) + } + + fun onIncrement(habit: Habit, timestamp: Timestamp, amount: Int) { + val currentValue = habit.computedEntries.get(timestamp).value + setValue(habit, timestamp, currentValue + amount) + notificationTray.cancel(habit) + } + + fun onDecrement(habit: Habit, timestamp: Timestamp, amount: Int) { + val currentValue = habit.computedEntries.get(timestamp).value + setValue(habit, timestamp, currentValue - amount) + notificationTray.cancel(habit) + } + + fun setValue(habit: Habit, timestamp: Timestamp?, newValue: Int) { + commandRunner.run( + CreateRepetitionCommand(habitList, habit, timestamp!!, newValue) + ) + } +}