forked from iSoron/uhabits
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor broadcast receivers and add a public receiver
Fixes iSoron#6
- Loading branch information
Showing
10 changed files
with
356 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/org/isoron/uhabits/receivers/IntentParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.net.*; | ||
import android.support.annotation.*; | ||
|
||
import org.isoron.uhabits.models.*; | ||
import org.isoron.uhabits.utils.*; | ||
|
||
public class IntentParser | ||
{ | ||
private HabitList habits; | ||
|
||
public IntentParser(@NonNull HabitList habits) | ||
{ | ||
this.habits = habits; | ||
} | ||
|
||
public CheckmarkIntentData parseCheckmarkIntent(@NonNull Intent intent) | ||
{ | ||
Uri uri = intent.getData(); | ||
CheckmarkIntentData data = new CheckmarkIntentData(); | ||
data.habit = parseHabit(uri); | ||
data.timestamp = parseTimestamp(intent); | ||
return data; | ||
} | ||
|
||
@NonNull | ||
protected Habit parseHabit(@NonNull Uri uri) | ||
{ | ||
long habitId = ContentUris.parseId(uri); | ||
|
||
Habit habit = habits.getById(habitId); | ||
if (habit == null) | ||
throw new IllegalArgumentException("habit not found"); | ||
|
||
return habit; | ||
} | ||
|
||
@NonNull | ||
protected Long parseTimestamp(@NonNull Intent intent) | ||
{ | ||
long today = DateUtils.getStartOfToday(); | ||
|
||
Long timestamp = intent.getLongExtra("timestamp", today); | ||
timestamp = DateUtils.getStartOfDay(timestamp); | ||
|
||
if (timestamp < 0 || timestamp > today) | ||
throw new IllegalArgumentException("timestamp is not valid"); | ||
|
||
return timestamp; | ||
} | ||
|
||
class CheckmarkIntentData | ||
{ | ||
public Habit habit; | ||
|
||
public Long timestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
app/src/main/java/org/isoron/uhabits/receivers/ReceiverActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.support.annotation.*; | ||
|
||
import org.isoron.uhabits.*; | ||
import org.isoron.uhabits.commands.*; | ||
import org.isoron.uhabits.models.*; | ||
import org.isoron.uhabits.tasks.*; | ||
|
||
import javax.inject.*; | ||
|
||
public class ReceiverActions | ||
{ | ||
@Inject | ||
CommandRunner commandRunner; | ||
|
||
public ReceiverActions() | ||
{ | ||
HabitsApplication.getComponent().inject(this); | ||
} | ||
|
||
public void add_repetition(@NonNull Habit habit, long timestamp) | ||
{ | ||
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp); | ||
if (rep != null) return; | ||
toggle_repetition(habit, timestamp); | ||
} | ||
|
||
public void remove_repetition(@NonNull Habit habit, long timestamp) | ||
{ | ||
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp); | ||
if (rep == null) return; | ||
toggle_repetition(habit, timestamp); | ||
} | ||
|
||
public void toggle_repetition(@NonNull Habit habit, long timestamp) | ||
{ | ||
new SimpleTask(() -> { | ||
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp), | ||
habit.getId()); | ||
}).execute(); | ||
} | ||
} |
Oops, something went wrong.