mirror of https://github.com/iSoron/uhabits.git
parent
0a5677211e
commit
8e82f369c7
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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.automation;
|
||||||
|
|
||||||
|
import android.os.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.activities.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
|
||||||
|
public class EditSettingActivity extends BaseActivity
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
|
{
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
HabitsApplication app = (HabitsApplication) getApplicationContext();
|
||||||
|
|
||||||
|
HabitList habits = app.getComponent().getHabitList();
|
||||||
|
habits = habits.getFiltered(new HabitMatcherBuilder()
|
||||||
|
.setArchivedAllowed(false)
|
||||||
|
.setCompletedAllowed(true)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
EditSettingController controller = new EditSettingController(this);
|
||||||
|
EditSettingRootView rootView =
|
||||||
|
new EditSettingRootView(this, habits, controller);
|
||||||
|
|
||||||
|
BaseScreen screen = new BaseScreen(this);
|
||||||
|
screen.setRootView(rootView);
|
||||||
|
setScreen(screen);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* 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.automation;
|
||||||
|
|
||||||
|
import android.app.*;
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.*;
|
||||||
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.automation.FireSettingReceiver.*;
|
||||||
|
|
||||||
|
public class EditSettingController
|
||||||
|
{
|
||||||
|
@NonNull
|
||||||
|
private final Activity activity;
|
||||||
|
|
||||||
|
public EditSettingController(@NonNull Activity activity)
|
||||||
|
{
|
||||||
|
this.activity = activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onSave(Habit habit, int action)
|
||||||
|
{
|
||||||
|
if (habit.getId() == null) return;
|
||||||
|
|
||||||
|
String actionName = getActionName(action);
|
||||||
|
String blurb = String.format("%s: %s", actionName, habit.getName());
|
||||||
|
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putInt("action", action);
|
||||||
|
bundle.putLong("habit", habit.getId());
|
||||||
|
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(EXTRA_STRING_BLURB, blurb);
|
||||||
|
intent.putExtra(EXTRA_BUNDLE, bundle);
|
||||||
|
|
||||||
|
activity.setResult(Activity.RESULT_OK, intent);
|
||||||
|
activity.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getActionName(int action)
|
||||||
|
{
|
||||||
|
switch (action)
|
||||||
|
{
|
||||||
|
case ACTION_CHECK:
|
||||||
|
return activity.getString(R.string.check);
|
||||||
|
|
||||||
|
case ACTION_UNCHECK:
|
||||||
|
return activity.getString(R.string.uncheck);
|
||||||
|
|
||||||
|
case ACTION_TOGGLE:
|
||||||
|
return activity.getString(R.string.toggle);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return "???";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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.automation;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.support.annotation.*;
|
||||||
|
import android.support.v7.widget.*;
|
||||||
|
import android.support.v7.widget.Toolbar;
|
||||||
|
import android.widget.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.activities.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import butterknife.*;
|
||||||
|
|
||||||
|
import static android.R.layout.*;
|
||||||
|
|
||||||
|
public class EditSettingRootView extends BaseRootView
|
||||||
|
{
|
||||||
|
@BindView(R.id.toolbar)
|
||||||
|
Toolbar toolbar;
|
||||||
|
|
||||||
|
@BindView(R.id.habitSpinner)
|
||||||
|
AppCompatSpinner habitSpinner;
|
||||||
|
|
||||||
|
@BindView(R.id.actionSpinner)
|
||||||
|
AppCompatSpinner actionSpinner;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final HabitList habitList;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final EditSettingController controller;
|
||||||
|
|
||||||
|
public EditSettingRootView(@NonNull Context context,
|
||||||
|
@NonNull HabitList habitList,
|
||||||
|
@NonNull EditSettingController controller)
|
||||||
|
{
|
||||||
|
super(context);
|
||||||
|
this.habitList = habitList;
|
||||||
|
this.controller = controller;
|
||||||
|
|
||||||
|
addView(inflate(getContext(), R.layout.automation, null));
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
populateHabitSpinner();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Toolbar getToolbar()
|
||||||
|
{
|
||||||
|
return toolbar;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getToolbarColor()
|
||||||
|
{
|
||||||
|
StyledResources res = new StyledResources(getContext());
|
||||||
|
if (!res.getBoolean(R.attr.useHabitColorAsPrimary))
|
||||||
|
return super.getToolbarColor();
|
||||||
|
|
||||||
|
return res.getColor(R.attr.aboutScreenColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.buttonSave)
|
||||||
|
public void onClickSave()
|
||||||
|
{
|
||||||
|
int action = actionSpinner.getSelectedItemPosition();
|
||||||
|
int habitPosition = habitSpinner.getSelectedItemPosition();
|
||||||
|
Habit habit = habitList.getByPosition(habitPosition);
|
||||||
|
controller.onSave(habit, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void populateHabitSpinner()
|
||||||
|
{
|
||||||
|
List<String> names = new LinkedList<>();
|
||||||
|
for (Habit h : habitList) names.add(h.getName());
|
||||||
|
|
||||||
|
ArrayAdapter<String> adapter =
|
||||||
|
new ArrayAdapter<>(getContext(), simple_spinner_item, names);
|
||||||
|
adapter.setDropDownViewResource(simple_spinner_dropdown_item);
|
||||||
|
habitSpinner.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* 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.automation;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.receivers.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
|
import dagger.*;
|
||||||
|
|
||||||
|
public class FireSettingReceiver extends BroadcastReceiver
|
||||||
|
{
|
||||||
|
public static final int ACTION_CHECK = 0;
|
||||||
|
|
||||||
|
public static final int ACTION_UNCHECK = 1;
|
||||||
|
|
||||||
|
public static final int ACTION_TOGGLE = 2;
|
||||||
|
|
||||||
|
public static final String EXTRA_BUNDLE =
|
||||||
|
"com.twofortyfouram.locale.intent.extra.BUNDLE";
|
||||||
|
|
||||||
|
public static final String EXTRA_STRING_BLURB =
|
||||||
|
"com.twofortyfouram.locale.intent.extra.BLURB";
|
||||||
|
|
||||||
|
private HabitList allHabits;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent)
|
||||||
|
{
|
||||||
|
HabitsApplication app =
|
||||||
|
(HabitsApplication) context.getApplicationContext();
|
||||||
|
|
||||||
|
ReceiverComponent component =
|
||||||
|
DaggerFireSettingReceiver_ReceiverComponent
|
||||||
|
.builder()
|
||||||
|
.appComponent(app.getComponent())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
allHabits = app.getComponent().getHabitList();
|
||||||
|
|
||||||
|
Arguments args = parseIntent(intent);
|
||||||
|
if (args == null) return;
|
||||||
|
|
||||||
|
long timestamp = DateUtils.getStartOfToday();
|
||||||
|
WidgetController controller = component.getWidgetController();
|
||||||
|
|
||||||
|
switch (args.action)
|
||||||
|
{
|
||||||
|
case ACTION_CHECK:
|
||||||
|
controller.onAddRepetition(args.habit, timestamp);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACTION_UNCHECK:
|
||||||
|
controller.onRemoveRepetition(args.habit, timestamp);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACTION_TOGGLE:
|
||||||
|
controller.onToggleRepetition(args.habit, timestamp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Arguments parseIntent(Intent intent)
|
||||||
|
{
|
||||||
|
Arguments args = new Arguments();
|
||||||
|
|
||||||
|
Bundle bundle = intent.getBundleExtra(EXTRA_BUNDLE);
|
||||||
|
if (bundle == null) return null;
|
||||||
|
|
||||||
|
args.action = bundle.getInt("action");
|
||||||
|
if (args.action < 0 || args.action > 2) return null;
|
||||||
|
|
||||||
|
Habit habit = allHabits.getById(bundle.getLong("habit"));
|
||||||
|
if (habit == null) return null;
|
||||||
|
args.habit = habit;
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReceiverScope
|
||||||
|
@Component(dependencies = AppComponent.class)
|
||||||
|
interface ReceiverComponent
|
||||||
|
{
|
||||||
|
WidgetController getWidgetController();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Arguments
|
||||||
|
{
|
||||||
|
int action;
|
||||||
|
|
||||||
|
Habit habit;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
<?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/>.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<android.support.v7.widget.Toolbar
|
||||||
|
android:id="@+id/toolbar"
|
||||||
|
style="@style/Toolbar"
|
||||||
|
android:title="@string/app_name"
|
||||||
|
app:popupTheme="?toolbarPopupTheme"/>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/formPanel"
|
||||||
|
style="@style/dialogFormPanel">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
style="@style/dialogFormRow">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
style="@style/dialogFormLabel"
|
||||||
|
android:text="@string/action"/>
|
||||||
|
|
||||||
|
<android.support.v7.widget.AppCompatSpinner
|
||||||
|
android:id="@+id/actionSpinner"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="25dp"
|
||||||
|
android:entries="@array/actions"
|
||||||
|
android:minWidth="400dp"
|
||||||
|
android:theme="@style/dialogFormText"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
style="@style/dialogFormRow">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
style="@style/dialogFormLabel"
|
||||||
|
android:text="@string/habit"/>
|
||||||
|
|
||||||
|
<android.support.v7.widget.AppCompatSpinner
|
||||||
|
android:id="@+id/habitSpinner"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="25dp"
|
||||||
|
android:entries="@array/actions"
|
||||||
|
android:minWidth="400dp"
|
||||||
|
android:theme="@style/dialogFormText"/>
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
style="?android:attr/buttonBarStyle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="end"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingLeft="0dp"
|
||||||
|
android:paddingRight="16dp"
|
||||||
|
android:paddingStart="0dp">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/buttonSave"
|
||||||
|
style="?android:attr/buttonBarButtonStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/save"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in new issue