mirror of https://github.com/iSoron/uhabits.git
parent
94c78ebb72
commit
cf2989d587
@ -1,510 +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.core.models;
|
||||
|
||||
import androidx.annotation.*;
|
||||
|
||||
import org.apache.commons.lang3.builder.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.annotation.concurrent.*;
|
||||
import javax.inject.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.Entry.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* The thing that the user wants to track.
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class Habit
|
||||
{
|
||||
public static final int AT_LEAST = 0;
|
||||
|
||||
public static final int AT_MOST = 1;
|
||||
|
||||
public static final String HABIT_URI_FORMAT =
|
||||
"content://org.isoron.uhabits/habit/%d";
|
||||
|
||||
public static final int NUMBER_HABIT = 1;
|
||||
|
||||
public static final int YES_NO_HABIT = 0;
|
||||
|
||||
@Nullable
|
||||
public Long id;
|
||||
|
||||
@NonNull
|
||||
private HabitData data;
|
||||
|
||||
@NonNull
|
||||
private StreakList streaks;
|
||||
|
||||
@NonNull
|
||||
private ScoreList scores;
|
||||
|
||||
@NonNull
|
||||
private RepetitionList repetitions;
|
||||
|
||||
@NonNull
|
||||
private EntryList computedEntries;
|
||||
|
||||
private ModelObservable observable = new ModelObservable();
|
||||
|
||||
/**
|
||||
* Constructs a habit with default data.
|
||||
* <p>
|
||||
* The habit is not archived, not highlighted, has no reminders and is
|
||||
* placed in the last position of the list of habits.
|
||||
*/
|
||||
@Inject
|
||||
Habit(@NonNull ModelFactory factory)
|
||||
{
|
||||
this.data = new HabitData();
|
||||
computedEntries = factory.buildEntryList(this);
|
||||
streaks = factory.buildStreakList(this);
|
||||
scores = factory.buildScoreList(this);
|
||||
repetitions = factory.buildRepetitionList(this);
|
||||
}
|
||||
|
||||
Habit(@NonNull ModelFactory factory, @NonNull HabitData data)
|
||||
{
|
||||
this.data = new HabitData(data);
|
||||
computedEntries = factory.buildEntryList(this);
|
||||
streaks = factory.buildStreakList(this);
|
||||
scores = factory.buildScoreList(this);
|
||||
repetitions = factory.buildRepetitionList(this);
|
||||
observable = new ModelObservable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the reminder for a habit.
|
||||
*/
|
||||
public synchronized void clearReminder()
|
||||
{
|
||||
data.reminder = null;
|
||||
observable.notifyListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all the attributes of the specified habit into this habit
|
||||
*
|
||||
* @param model the model whose attributes should be copied from
|
||||
*/
|
||||
public synchronized void copyFrom(@NonNull Habit model)
|
||||
{
|
||||
this.data = new HabitData(model.data);
|
||||
observable.notifyListeners();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized EntryList getComputedEntries()
|
||||
{
|
||||
return computedEntries;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized PaletteColor getColor()
|
||||
{
|
||||
return data.color;
|
||||
}
|
||||
|
||||
public synchronized void setColor(@NonNull PaletteColor color)
|
||||
{
|
||||
data.color = color;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized String getDescription()
|
||||
{
|
||||
return data.description;
|
||||
}
|
||||
|
||||
public synchronized void setDescription(@NonNull String description)
|
||||
{
|
||||
data.description = description;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized Frequency getFrequency()
|
||||
{
|
||||
return data.frequency;
|
||||
}
|
||||
|
||||
public synchronized void setFrequency(@NonNull Frequency frequency)
|
||||
{
|
||||
data.frequency = frequency;
|
||||
invalidateNewerThan(Timestamp.ZERO);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public synchronized Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public synchronized void setId(@Nullable Long id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized String getName()
|
||||
{
|
||||
return data.name;
|
||||
}
|
||||
|
||||
public synchronized void setName(@NonNull String name)
|
||||
{
|
||||
data.name = name;
|
||||
}
|
||||
|
||||
public ModelObservable getObservable()
|
||||
{
|
||||
return observable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reminder for this habit.
|
||||
* <p>
|
||||
* Before calling this method, you should call {@link #hasReminder()} to
|
||||
* verify that a reminder does exist, otherwise an exception will be
|
||||
* thrown.
|
||||
*
|
||||
* @return the reminder for this habit
|
||||
* @throws IllegalStateException if habit has no reminder
|
||||
*/
|
||||
@NonNull
|
||||
public synchronized Reminder getReminder()
|
||||
{
|
||||
if (data.reminder == null) throw new IllegalStateException();
|
||||
return data.reminder;
|
||||
}
|
||||
|
||||
public synchronized void setReminder(@Nullable Reminder reminder)
|
||||
{
|
||||
data.reminder = reminder;
|
||||
}
|
||||
|
||||
public RepetitionList getOriginalEntries()
|
||||
{
|
||||
return repetitions;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public ScoreList getScores()
|
||||
{
|
||||
return scores;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public StreakList getStreaks()
|
||||
{
|
||||
return streaks;
|
||||
}
|
||||
|
||||
public synchronized int getTargetType()
|
||||
{
|
||||
return data.targetType;
|
||||
}
|
||||
|
||||
public synchronized void setTargetType(int targetType)
|
||||
{
|
||||
if (targetType != AT_LEAST && targetType != AT_MOST)
|
||||
throw new IllegalArgumentException(
|
||||
String.format("invalid targetType: %d", targetType));
|
||||
data.targetType = targetType;
|
||||
}
|
||||
|
||||
public synchronized double getTargetValue()
|
||||
{
|
||||
return data.targetValue;
|
||||
}
|
||||
|
||||
public synchronized void setTargetValue(double targetValue)
|
||||
{
|
||||
if (targetValue < 0) throw new IllegalArgumentException();
|
||||
data.targetValue = targetValue;
|
||||
}
|
||||
|
||||
public synchronized int getType()
|
||||
{
|
||||
return data.type;
|
||||
}
|
||||
|
||||
public synchronized void setType(int type)
|
||||
{
|
||||
if (type != YES_NO_HABIT && type != NUMBER_HABIT)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
data.type = type;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public synchronized String getUnit()
|
||||
{
|
||||
return data.unit;
|
||||
}
|
||||
|
||||
public synchronized void setUnit(@NonNull String unit)
|
||||
{
|
||||
data.unit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public URI that identifies this habit
|
||||
*
|
||||
* @return the URI
|
||||
*/
|
||||
public String getUriString()
|
||||
{
|
||||
return String.format(Locale.US, HABIT_URI_FORMAT, getId());
|
||||
}
|
||||
|
||||
public synchronized boolean hasId()
|
||||
{
|
||||
return getId() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the habit has a reminder.
|
||||
*
|
||||
* @return true if habit has reminder, false otherwise
|
||||
*/
|
||||
public synchronized boolean hasReminder()
|
||||
{
|
||||
return data.reminder != null;
|
||||
}
|
||||
|
||||
public void invalidateNewerThan(Timestamp timestamp)
|
||||
{
|
||||
getScores().invalidateNewerThan(timestamp);
|
||||
getComputedEntries().invalidateNewerThan(timestamp);
|
||||
getStreaks().invalidateNewerThan(timestamp);
|
||||
}
|
||||
|
||||
public synchronized boolean isArchived()
|
||||
{
|
||||
return data.archived;
|
||||
}
|
||||
|
||||
public synchronized void setArchived(boolean archived)
|
||||
{
|
||||
data.archived = archived;
|
||||
}
|
||||
|
||||
public synchronized boolean isCompletedToday()
|
||||
{
|
||||
int todayCheckmark = getComputedEntries().getTodayValue();
|
||||
if (isNumerical())
|
||||
{
|
||||
if(getTargetType() == AT_LEAST)
|
||||
return todayCheckmark / 1000.0 >= data.targetValue;
|
||||
else
|
||||
return todayCheckmark / 1000.0 <= data.targetValue;
|
||||
}
|
||||
else return (todayCheckmark != NO && todayCheckmark != UNKNOWN);
|
||||
}
|
||||
|
||||
public synchronized boolean isNumerical()
|
||||
{
|
||||
return data.type == NUMBER_HABIT;
|
||||
}
|
||||
|
||||
public HabitData getData()
|
||||
{
|
||||
return new HabitData(data);
|
||||
}
|
||||
|
||||
public Integer getPosition()
|
||||
{
|
||||
return data.position;
|
||||
}
|
||||
|
||||
public void setPosition(int newPosition)
|
||||
{
|
||||
data.position = newPosition;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getQuestion()
|
||||
{
|
||||
return data.question;
|
||||
}
|
||||
|
||||
public void setQuestion(@NonNull String question)
|
||||
{
|
||||
data.question = question;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getUUID()
|
||||
{
|
||||
return data.uuid;
|
||||
}
|
||||
|
||||
public void setUUID(@NonNull String uuid)
|
||||
{
|
||||
data.uuid = uuid;
|
||||
}
|
||||
|
||||
public static final class HabitData
|
||||
{
|
||||
@NonNull
|
||||
public String name;
|
||||
|
||||
@NonNull
|
||||
public String description;
|
||||
|
||||
@NonNull
|
||||
public String question;
|
||||
|
||||
@NonNull
|
||||
public Frequency frequency;
|
||||
|
||||
public PaletteColor color;
|
||||
|
||||
public boolean archived;
|
||||
|
||||
public int targetType;
|
||||
|
||||
public double targetValue;
|
||||
|
||||
public int type;
|
||||
|
||||
public String uuid;
|
||||
|
||||
@NonNull
|
||||
public String unit;
|
||||
|
||||
@Nullable
|
||||
public Reminder reminder;
|
||||
|
||||
public int position;
|
||||
|
||||
public HabitData()
|
||||
{
|
||||
this.color = new PaletteColor(8);
|
||||
this.archived = false;
|
||||
this.frequency = new Frequency(3, 7);
|
||||
this.type = YES_NO_HABIT;
|
||||
this.name = "";
|
||||
this.description = "";
|
||||
this.question = "";
|
||||
this.targetType = AT_LEAST;
|
||||
this.targetValue = 100;
|
||||
this.unit = "";
|
||||
this.position = 0;
|
||||
this.uuid = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
|
||||
public HabitData(@NonNull HabitData model)
|
||||
{
|
||||
this.name = model.name;
|
||||
this.description = model.description;
|
||||
this.question = model.question;
|
||||
this.frequency = model.frequency;
|
||||
this.color = model.color;
|
||||
this.archived = model.archived;
|
||||
this.targetType = model.targetType;
|
||||
this.targetValue = model.targetValue;
|
||||
this.type = model.type;
|
||||
this.unit = model.unit;
|
||||
this.reminder = model.reminder;
|
||||
this.position = model.position;
|
||||
this.uuid = model.uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("name", name)
|
||||
.append("description", description)
|
||||
.append("frequency", frequency)
|
||||
.append("color", color)
|
||||
.append("archived", archived)
|
||||
.append("targetType", targetType)
|
||||
.append("targetValue", targetValue)
|
||||
.append("type", type)
|
||||
.append("unit", unit)
|
||||
.append("reminder", reminder)
|
||||
.append("position", position)
|
||||
.append("question", question)
|
||||
.append("uuid", uuid)
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
HabitData habitData = (HabitData) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.append(color, habitData.color)
|
||||
.append(archived, habitData.archived)
|
||||
.append(targetType, habitData.targetType)
|
||||
.append(targetValue, habitData.targetValue)
|
||||
.append(type, habitData.type)
|
||||
.append(name, habitData.name)
|
||||
.append(description, habitData.description)
|
||||
.append(frequency, habitData.frequency)
|
||||
.append(unit, habitData.unit)
|
||||
.append(reminder, habitData.reminder)
|
||||
.append(position, habitData.position)
|
||||
.append(question, habitData.question)
|
||||
.append(uuid, habitData.uuid)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(name)
|
||||
.append(description)
|
||||
.append(frequency)
|
||||
.append(color)
|
||||
.append(archived)
|
||||
.append(targetType)
|
||||
.append(targetValue)
|
||||
.append(type)
|
||||
.append(unit)
|
||||
.append(reminder)
|
||||
.append(position)
|
||||
.append(question)
|
||||
.append(uuid)
|
||||
.toHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("id", id)
|
||||
.append("data", data)
|
||||
.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.models
|
||||
|
||||
data class Habit(
|
||||
var color: PaletteColor = PaletteColor(8),
|
||||
var description: String = "",
|
||||
var frequency: Frequency = Frequency.DAILY,
|
||||
var id: Long? = null,
|
||||
var isArchived: Boolean = false,
|
||||
var name: String = "",
|
||||
var position: Int = 0,
|
||||
var question: String = "",
|
||||
var reminder: Reminder? = null,
|
||||
var targetType: Int = AT_LEAST,
|
||||
var targetValue: Double = 0.0,
|
||||
var type: Int = YES_NO_HABIT,
|
||||
var unit: String = "",
|
||||
var uuid: String? = null,
|
||||
val computedEntries: EntryList,
|
||||
val originalEntries: RepetitionList,
|
||||
val scores: ScoreList,
|
||||
val streaks: StreakList,
|
||||
) {
|
||||
var observable = ModelObservable()
|
||||
|
||||
val isNumerical: Boolean
|
||||
get() = type == NUMBER_HABIT
|
||||
|
||||
val uriString: String
|
||||
get() = "content://org.isoron.uhabits/habit/$id"
|
||||
|
||||
fun hasReminder(): Boolean = reminder != null
|
||||
|
||||
fun isCompletedToday(): Boolean = computedEntries.isCompletedToday
|
||||
|
||||
fun invalidateNewerThan(timestamp: Timestamp?) {
|
||||
scores.invalidateNewerThan(timestamp)
|
||||
computedEntries.invalidateNewerThan(timestamp)
|
||||
streaks.invalidateNewerThan(timestamp)
|
||||
}
|
||||
|
||||
fun copyFrom(other: Habit) {
|
||||
this.color = other.color
|
||||
this.description = other.description
|
||||
this.frequency = other.frequency
|
||||
// this.id should not be copied
|
||||
this.isArchived = other.isArchived
|
||||
this.name = other.name
|
||||
this.position = other.position
|
||||
this.question = other.question
|
||||
this.reminder = other.reminder
|
||||
this.targetType = other.targetType
|
||||
this.targetValue = other.targetValue
|
||||
this.type = other.type
|
||||
this.unit = other.unit
|
||||
this.uuid = other.uuid
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Habit) return false
|
||||
|
||||
if (color != other.color) return false
|
||||
if (description != other.description) return false
|
||||
if (frequency != other.frequency) return false
|
||||
if (id != other.id) return false
|
||||
if (isArchived != other.isArchived) return false
|
||||
if (name != other.name) return false
|
||||
if (position != other.position) return false
|
||||
if (question != other.question) return false
|
||||
if (reminder != other.reminder) return false
|
||||
if (targetType != other.targetType) return false
|
||||
if (targetValue != other.targetValue) return false
|
||||
if (type != other.type) return false
|
||||
if (unit != other.unit) return false
|
||||
if (uuid != other.uuid) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = color.hashCode()
|
||||
result = 31 * result + description.hashCode()
|
||||
result = 31 * result + frequency.hashCode()
|
||||
result = 31 * result + (id?.hashCode() ?: 0)
|
||||
result = 31 * result + isArchived.hashCode()
|
||||
result = 31 * result + name.hashCode()
|
||||
result = 31 * result + position
|
||||
result = 31 * result + question.hashCode()
|
||||
result = 31 * result + (reminder?.hashCode() ?: 0)
|
||||
result = 31 * result + targetType
|
||||
result = 31 * result + targetValue.hashCode()
|
||||
result = 31 * result + type
|
||||
result = 31 * result + unit.hashCode()
|
||||
result = 31 * result + (uuid?.hashCode() ?: 0)
|
||||
return result
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val AT_LEAST = 0
|
||||
const val AT_MOST = 1
|
||||
const val NUMBER_HABIT = 1
|
||||
const val YES_NO_HABIT = 0
|
||||
}
|
||||
}
|
@ -1,54 +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.core.models;
|
||||
|
||||
import org.isoron.uhabits.core.database.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
|
||||
/**
|
||||
* Interface implemented by factories that provide concrete implementations of
|
||||
* the core model classes.
|
||||
*/
|
||||
public interface ModelFactory
|
||||
{
|
||||
EntryList buildEntryList(Habit habit);
|
||||
|
||||
default Habit buildHabit()
|
||||
{
|
||||
return new Habit(this);
|
||||
}
|
||||
|
||||
default Habit buildHabit(Habit.HabitData data)
|
||||
{
|
||||
return new Habit(this, data);
|
||||
}
|
||||
|
||||
HabitList buildHabitList();
|
||||
|
||||
RepetitionList buildRepetitionList(Habit habit);
|
||||
|
||||
ScoreList buildScoreList(Habit habit);
|
||||
|
||||
StreakList buildStreakList(Habit habit);
|
||||
|
||||
Repository<HabitRecord> buildHabitListRepository();
|
||||
|
||||
Repository<RepetitionRecord> buildRepetitionListRepository();
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.models
|
||||
|
||||
import org.isoron.uhabits.core.database.*
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*
|
||||
|
||||
/**
|
||||
* Interface implemented by factories that provide concrete implementations of
|
||||
* the core model classes.
|
||||
*/
|
||||
interface ModelFactory {
|
||||
|
||||
fun buildHabit(): Habit {
|
||||
val computedEntries = buildEntryList()
|
||||
val originalEntries = buildRepetitionList()
|
||||
val scores = buildScoreList()
|
||||
val streaks = buildStreakList()
|
||||
val habit = Habit(
|
||||
computedEntries = computedEntries,
|
||||
originalEntries = originalEntries,
|
||||
scores = scores,
|
||||
streaks = streaks,
|
||||
)
|
||||
computedEntries.setHabit(habit)
|
||||
originalEntries.setHabit(habit)
|
||||
scores.setHabit(habit)
|
||||
streaks.setHabit(habit)
|
||||
return habit
|
||||
}
|
||||
|
||||
fun buildEntryList(): EntryList
|
||||
fun buildHabitList(): HabitList
|
||||
fun buildRepetitionList(): RepetitionList
|
||||
fun buildScoreList(): ScoreList
|
||||
fun buildStreakList(): StreakList
|
||||
fun buildHabitListRepository(): Repository<HabitRecord>
|
||||
fun buildRepetitionListRepository(): Repository<RepetitionRecord>
|
||||
}
|
@ -1,69 +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.core.models.memory;
|
||||
|
||||
import org.isoron.uhabits.core.database.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
|
||||
public class MemoryModelFactory implements ModelFactory
|
||||
{
|
||||
@Override
|
||||
public EntryList buildEntryList(Habit habit)
|
||||
{
|
||||
return new EntryList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HabitList buildHabitList()
|
||||
{
|
||||
return new MemoryHabitList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RepetitionList buildRepetitionList(Habit habit)
|
||||
{
|
||||
return new RepetitionList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScoreList buildScoreList(Habit habit)
|
||||
{
|
||||
return new MemoryScoreList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreakList buildStreakList(Habit habit)
|
||||
{
|
||||
return new MemoryStreakList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository<HabitRecord> buildHabitListRepository()
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository<RepetitionRecord> buildRepetitionListRepository()
|
||||
{
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.models.memory
|
||||
|
||||
import org.isoron.uhabits.core.models.*
|
||||
|
||||
class MemoryModelFactory : ModelFactory {
|
||||
override fun buildEntryList() = EntryList()
|
||||
override fun buildHabitList() = MemoryHabitList()
|
||||
override fun buildRepetitionList() = RepetitionList()
|
||||
override fun buildScoreList() = MemoryScoreList()
|
||||
override fun buildStreakList() = MemoryStreakList()
|
||||
override fun buildHabitListRepository() = throw NotImplementedError()
|
||||
override fun buildRepetitionListRepository() = throw NotImplementedError()
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.models.sqlite;
|
||||
|
||||
import org.isoron.uhabits.core.database.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.models.memory.*;
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
/**
|
||||
* Factory that provides models backed by an SQLite database.
|
||||
*/
|
||||
public class SQLModelFactory implements ModelFactory
|
||||
{
|
||||
public final Database db;
|
||||
|
||||
@Inject
|
||||
public SQLModelFactory(Database db)
|
||||
{
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntryList buildEntryList(Habit habit)
|
||||
{
|
||||
return new EntryList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HabitList buildHabitList()
|
||||
{
|
||||
return new SQLiteHabitList(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RepetitionList buildRepetitionList(Habit habit)
|
||||
{
|
||||
return new SQLiteRepetitionList(habit, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScoreList buildScoreList(Habit habit)
|
||||
{
|
||||
return new MemoryScoreList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreakList buildStreakList(Habit habit)
|
||||
{
|
||||
return new MemoryStreakList(habit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository<HabitRecord> buildHabitListRepository()
|
||||
{
|
||||
return new Repository<>(HabitRecord.class, db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Repository<RepetitionRecord> buildRepetitionListRepository()
|
||||
{
|
||||
return new Repository<>(RepetitionRecord.class, db);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.models.sqlite
|
||||
|
||||
import org.isoron.uhabits.core.database.*
|
||||
import org.isoron.uhabits.core.models.*
|
||||
import org.isoron.uhabits.core.models.memory.*
|
||||
import org.isoron.uhabits.core.models.sqlite.records.*
|
||||
import javax.inject.*
|
||||
|
||||
/**
|
||||
* Factory that provides models backed by an SQLite database.
|
||||
*/
|
||||
class SQLModelFactory
|
||||
@Inject constructor(
|
||||
val database: Database,
|
||||
) : ModelFactory {
|
||||
|
||||
override fun buildEntryList() = EntryList()
|
||||
override fun buildHabitList() = SQLiteHabitList(this)
|
||||
override fun buildRepetitionList() = SQLiteRepetitionList(this)
|
||||
override fun buildScoreList() = MemoryScoreList()
|
||||
override fun buildStreakList() = MemoryStreakList()
|
||||
|
||||
override fun buildHabitListRepository() =
|
||||
Repository(HabitRecord::class.java, database)
|
||||
|
||||
override fun buildRepetitionListRepository() =
|
||||
Repository(RepetitionRecord::class.java, database)
|
||||
}
|
Loading…
Reference in new issue