mirror of https://github.com/iSoron/uhabits.git
parent
136ec5b49b
commit
b21eb3f118
@ -1,238 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Habit, NotificationData> 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<Habit> 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];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<Habit, NotificationData>
|
||||
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()
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue