mirror of https://github.com/iSoron/uhabits.git
parent
3d039db61c
commit
26d91a2d1d
@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.receivers;
|
||||
|
||||
import android.content.*;
|
||||
import android.util.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.core.preferences.*;
|
||||
import org.isoron.uhabits.core.ui.widgets.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.sync.*;
|
||||
|
||||
import dagger.*;
|
||||
|
||||
/**
|
||||
* The Android BroadcastReceiver for Loop Habit Tracker.
|
||||
* <p>
|
||||
* All broadcast messages are received and processed by this class.
|
||||
*/
|
||||
public class WidgetReceiver extends BroadcastReceiver
|
||||
{
|
||||
public static final String ACTION_ADD_REPETITION =
|
||||
"org.isoron.uhabits.ACTION_ADD_REPETITION";
|
||||
|
||||
public static final String ACTION_DISMISS_REMINDER =
|
||||
"org.isoron.uhabits.ACTION_DISMISS_REMINDER";
|
||||
|
||||
public static final String ACTION_REMOVE_REPETITION =
|
||||
"org.isoron.uhabits.ACTION_REMOVE_REPETITION";
|
||||
|
||||
public static final String ACTION_TOGGLE_REPETITION =
|
||||
"org.isoron.uhabits.ACTION_TOGGLE_REPETITION";
|
||||
|
||||
public static final String ACTION_SET_NUMERICAL_VALUE =
|
||||
"org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE";
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent)
|
||||
{
|
||||
HabitsApplication app =
|
||||
(HabitsApplication) context.getApplicationContext();
|
||||
|
||||
WidgetComponent component = DaggerWidgetReceiver_WidgetComponent
|
||||
.builder()
|
||||
.habitsApplicationComponent(app.getComponent())
|
||||
.build();
|
||||
|
||||
IntentParser parser = app.getComponent().getIntentParser();
|
||||
WidgetBehavior controller = component.getWidgetController();
|
||||
Preferences prefs = app.getComponent().getPreferences();
|
||||
|
||||
if(prefs.isSyncEnabled())
|
||||
context.startService(new Intent(context, SyncService.class));
|
||||
|
||||
try
|
||||
{
|
||||
IntentParser.CheckmarkIntentData data;
|
||||
data = parser.parseCheckmarkIntent(intent);
|
||||
|
||||
switch (intent.getAction())
|
||||
{
|
||||
case ACTION_ADD_REPETITION:
|
||||
controller.onAddRepetition(data.getHabit(),
|
||||
data.getTimestamp());
|
||||
break;
|
||||
|
||||
case ACTION_TOGGLE_REPETITION:
|
||||
controller.onToggleRepetition(data.getHabit(),
|
||||
data.getTimestamp());
|
||||
break;
|
||||
|
||||
case ACTION_REMOVE_REPETITION:
|
||||
controller.onRemoveRepetition(data.getHabit(),
|
||||
data.getTimestamp());
|
||||
break;
|
||||
|
||||
case ACTION_SET_NUMERICAL_VALUE:
|
||||
//controller.onToggleRepetition(data.getHabit(),
|
||||
// data.getTimestamp());
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (RuntimeException e)
|
||||
{
|
||||
Log.e("WidgetReceiver", "could not process intent", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ReceiverScope
|
||||
@Component(dependencies = HabitsApplicationComponent.class)
|
||||
interface WidgetComponent
|
||||
{
|
||||
WidgetBehavior getWidgetController();
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.receivers
|
||||
|
||||
import android.content.*
|
||||
import android.util.*
|
||||
|
||||
import org.isoron.uhabits.*
|
||||
import org.isoron.uhabits.core.preferences.*
|
||||
import org.isoron.uhabits.core.ui.widgets.*
|
||||
import org.isoron.uhabits.intents.*
|
||||
import org.isoron.uhabits.sync.*
|
||||
|
||||
import dagger.*
|
||||
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
|
||||
import org.isoron.uhabits.widgets.activities.NumericalCheckmarkWidgetActivity
|
||||
import android.content.Intent
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The Android BroadcastReceiver for Loop Habit Tracker.
|
||||
*
|
||||
*
|
||||
* All broadcast messages are received and processed by this class.
|
||||
*/
|
||||
class WidgetReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val app = context.applicationContext as HabitsApplication
|
||||
|
||||
val component = DaggerWidgetReceiver_WidgetComponent
|
||||
.builder()
|
||||
.habitsApplicationComponent(app.component)
|
||||
.build()
|
||||
|
||||
val parser = app.component.intentParser
|
||||
val controller = component.widgetController
|
||||
val prefs = app.component.preferences
|
||||
|
||||
if (prefs.isSyncEnabled)
|
||||
context.startService(Intent(context, SyncService::class.java))
|
||||
|
||||
try {
|
||||
val data: IntentParser.CheckmarkIntentData = parser.parseCheckmarkIntent(intent)
|
||||
|
||||
when (intent.action) {
|
||||
ACTION_ADD_REPETITION -> controller.onAddRepetition(data.habit, data.timestamp)
|
||||
|
||||
ACTION_TOGGLE_REPETITION -> controller.onToggleRepetition(data.habit, data.timestamp)
|
||||
|
||||
ACTION_REMOVE_REPETITION -> controller.onRemoveRepetition(data.habit, data.timestamp)
|
||||
|
||||
ACTION_SET_NUMERICAL_VALUE -> {
|
||||
val numberSelectorIntent = Intent(context, NumericalCheckmarkWidgetActivity::class.java)
|
||||
numberSelectorIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
numberSelectorIntent.action = NumericalCheckmarkWidgetActivity.ACTION_SHOW_NUMERICAL_VALUE_ACTIVITY
|
||||
parser.copyIntentData(intent,numberSelectorIntent)//give the habit and timestamp data to the numericalCheckmarkWidgetActivity
|
||||
context.startActivity(numberSelectorIntent)
|
||||
}
|
||||
}
|
||||
} catch (e: RuntimeException) {
|
||||
Log.e("WidgetReceiver", "could not process intent", e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ReceiverScope
|
||||
@Component(dependencies = [HabitsApplicationComponent::class])
|
||||
internal interface WidgetComponent {
|
||||
val widgetController: WidgetBehavior
|
||||
}
|
||||
|
||||
companion object {
|
||||
val ACTION_ADD_REPETITION = "org.isoron.uhabits.ACTION_ADD_REPETITION"
|
||||
|
||||
val ACTION_DISMISS_REMINDER = "org.isoron.uhabits.ACTION_DISMISS_REMINDER"
|
||||
|
||||
val ACTION_REMOVE_REPETITION = "org.isoron.uhabits.ACTION_REMOVE_REPETITION"
|
||||
|
||||
val ACTION_TOGGLE_REPETITION = "org.isoron.uhabits.ACTION_TOGGLE_REPETITION"
|
||||
|
||||
val ACTION_SET_NUMERICAL_VALUE = "org.isoron.uhabits.ACTION_SET_NUMERICAL_VALUE"
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package org.isoron.uhabits.widgets.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager
|
||||
import org.isoron.uhabits.R
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.Window
|
||||
import org.isoron.androidbase.AppContextModule
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior
|
||||
import org.isoron.uhabits.core.ui.widgets.NumericalCheckmarkWidgetBehavior
|
||||
import org.isoron.uhabits.intents.IntentFactory
|
||||
import org.isoron.uhabits.intents.IntentParser
|
||||
import org.isoron.uhabits.intents.PendingIntentFactory
|
||||
import org.isoron.uhabits.widgets.WidgetUpdater
|
||||
|
||||
|
||||
class NumericalCheckmarkWidgetActivity : Activity() {
|
||||
|
||||
//private var activityComponent : NumericalCheckmarkWidgetActivityComponent? = null
|
||||
private lateinit var behavior : NumericalCheckmarkWidgetBehavior
|
||||
private var data: IntentParser.CheckmarkIntentData? = null
|
||||
private lateinit var widgetUpdater : WidgetUpdater
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE)
|
||||
setContentView(R.layout.widget_checkmark_number_screen)
|
||||
val app = this.applicationContext as HabitsApplication
|
||||
val component = app.component
|
||||
val parser = app.component.intentParser
|
||||
data = parser.parseCheckmarkIntent(intent)
|
||||
behavior = NumericalCheckmarkWidgetBehavior(component.habitList,component.commandRunner)
|
||||
widgetUpdater = component.widgetUpdater
|
||||
showNumberSelector(this)
|
||||
}
|
||||
|
||||
|
||||
class CallBackReceiver : ListHabitsBehavior.NumberPickerCallback{
|
||||
|
||||
private val activity : NumericalCheckmarkWidgetActivity
|
||||
|
||||
constructor(activity: NumericalCheckmarkWidgetActivity){
|
||||
this.activity=activity
|
||||
|
||||
}
|
||||
|
||||
override fun onNumberPicked(newValue: Double) {
|
||||
activity.saveNewNumer(newValue)
|
||||
}
|
||||
}
|
||||
|
||||
fun saveNewNumer(newValue: Double){
|
||||
behavior.setNumericValue(data!!.habit,data!!.timestamp,(newValue*1000).toInt())
|
||||
widgetUpdater.updateWidgets()
|
||||
finish()
|
||||
}
|
||||
|
||||
|
||||
fun showNumberSelector(context: Context) {
|
||||
var localData = data
|
||||
if(behavior!=null && localData!=null) {//if a blank screen shows up without a popup when pressing the widget, you should check if this check passes.
|
||||
val numberPickerFactory = NumberPickerFactory(context)
|
||||
numberPickerFactory.create( data!!.habit.checkmarks.today!!.value.toDouble()/1000, "This is a test", CallBackReceiver(this)).show()
|
||||
}
|
||||
}
|
||||
|
||||
companion object{
|
||||
val ACTION_SHOW_NUMERICAL_VALUE_ACTIVITY = "org.isoron.uhabits.ACTION_SHOW_NUMERICAL_VALUE_ACTIVITY"
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package org.isoron.uhabits.widgets.activities;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.isoron.androidbase.AppContext;
|
||||
import org.isoron.androidbase.AppContextModule;
|
||||
import org.isoron.uhabits.HabitsModule;
|
||||
import org.isoron.uhabits.core.AppScope;
|
||||
import org.isoron.uhabits.core.commands.CommandRunner;
|
||||
import org.isoron.uhabits.core.models.HabitList;
|
||||
import org.isoron.uhabits.core.preferences.Preferences;
|
||||
import org.isoron.uhabits.core.tasks.TaskRunner;
|
||||
import org.isoron.uhabits.core.ui.widgets.NumericalCheckmarkWidgetBehavior;
|
||||
import org.isoron.uhabits.tasks.AndroidTaskRunner;
|
||||
|
||||
import dagger.Component;
|
||||
|
||||
@AppScope
|
||||
@Component(modules = {
|
||||
AppContextModule.class,
|
||||
HabitsModule.class,
|
||||
AndroidTaskRunner.class,
|
||||
})
|
||||
public interface NumericalCheckmarkWidgetActivityComponent {
|
||||
NumericalCheckmarkWidgetBehavior getNumericalCheckmarkWidgetBehavior();
|
||||
|
||||
CommandRunner getCommandRunner();
|
||||
|
||||
@AppContext
|
||||
Context getContext();
|
||||
|
||||
HabitList getHabitList();
|
||||
|
||||
Preferences getPreferences();
|
||||
|
||||
TaskRunner getTaskRunner();
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
~
|
||||
~ 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/>.
|
||||
-->
|
||||
<!--
|
||||
This layout is made so whenever the number selection popup shows up, there is some layout available.
|
||||
The dark backdrop is supplied by @style/Theme.AppCompat.Light.Dialog
|
||||
The selector dialog is supplied by numberPickerFactory.
|
||||
-->
|
||||
|
||||
<FrameLayout
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/frame"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
</FrameLayout>
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.commands;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.isoron.uhabits.core.models.Habit;
|
||||
import org.isoron.uhabits.core.models.HabitList;
|
||||
import org.isoron.uhabits.core.models.HabitNotFoundException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Command to update a habit.
|
||||
*/
|
||||
public class UpdateHabitCommand extends Command
|
||||
{
|
||||
@NonNull
|
||||
private HabitList list;
|
||||
|
||||
@NonNull
|
||||
final Habit habit;
|
||||
|
||||
public UpdateHabitCommand(@NonNull HabitList list,
|
||||
@NonNull Habit habit)
|
||||
{
|
||||
super();
|
||||
this.list = list;
|
||||
this.habit = habit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute()
|
||||
{
|
||||
list.update(habit);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Habit getHabit()
|
||||
{
|
||||
return habit;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
throw new RuntimeException("Update command cannot be undone");
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "Update";
|
||||
|
||||
public long habit;
|
||||
|
||||
public Record(@NonNull UpdateHabitCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
Long habitId = command.habit.getId();
|
||||
if (habitId == null) throw new RuntimeException("Habit not saved");
|
||||
|
||||
this.habit = habitId;
|
||||
}
|
||||
|
||||
public UpdateHabitCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
Habit h = habitList.getById(habit);
|
||||
if (h == null) throw new HabitNotFoundException();
|
||||
|
||||
UpdateHabitCommand command;
|
||||
command = new UpdateHabitCommand(
|
||||
habitList, h);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package org.isoron.uhabits.core.ui.widgets;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.isoron.uhabits.core.commands.CommandRunner;
|
||||
import org.isoron.uhabits.core.models.HabitList;
|
||||
|
||||
public class BaseWidgetBehavior {
|
||||
protected HabitList getHabitList() {
|
||||
return habitList;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
protected CommandRunner getCommandRunner() {
|
||||
return commandRunner;
|
||||
}
|
||||
|
||||
private HabitList habitList;
|
||||
|
||||
@NonNull
|
||||
private final CommandRunner commandRunner;
|
||||
|
||||
public BaseWidgetBehavior(HabitList habitList, @NonNull CommandRunner commandRunner) {
|
||||
this.habitList = habitList;
|
||||
this.commandRunner = commandRunner;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.isoron.uhabits.core.ui.widgets;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.isoron.uhabits.core.commands.CommandRunner;
|
||||
import org.isoron.uhabits.core.commands.CreateRepetitionCommand;
|
||||
import org.isoron.uhabits.core.models.Habit;
|
||||
import org.isoron.uhabits.core.models.HabitList;
|
||||
import org.isoron.uhabits.core.models.Timestamp;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class NumericalCheckmarkWidgetBehavior extends BaseWidgetBehavior{
|
||||
@Inject
|
||||
public NumericalCheckmarkWidgetBehavior(@NonNull HabitList habitList,
|
||||
@NonNull CommandRunner commandRunner){
|
||||
super(habitList, commandRunner);
|
||||
}
|
||||
|
||||
public void setNumericValue(@NonNull Habit habit, Timestamp timestamp, int newValue) {
|
||||
getCommandRunner().execute(
|
||||
new CreateRepetitionCommand(habit, timestamp, newValue),
|
||||
habit.getId());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue