Add UUID to habits

pull/699/head
Alinson S. Xavier 5 years ago
parent 659c528744
commit 68ccf37fd5

@ -97,9 +97,9 @@ class ListHabitsScreen
commandRunner.removeListener(this)
}
override fun onCommandExecuted(command: Command, refreshKey: Long?) {
if (command.isRemote) return
showMessage(getExecuteString(command))
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
if (command != null)
showMessage(getExecuteString(command))
}
override fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {

@ -19,6 +19,8 @@
package org.isoron.uhabits.tasks;
import android.util.*;
import androidx.annotation.NonNull;
import com.google.auto.factory.*;
@ -83,7 +85,7 @@ public class ImportDataTask implements Task
catch (Exception e)
{
result = FAILED;
e.printStackTrace();
Log.e("ImportDataTask", "Import failed", e);
}
modelFactory.db.endTransaction();

@ -45,7 +45,7 @@ class WidgetUpdater
private var lastUpdated = 0L
override fun onCommandExecuted(command: Command, refreshKey: Long?) {
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
updateWidgets(refreshKey)
}

@ -22,5 +22,5 @@ package org.isoron.uhabits.core;
public class Config
{
public static final String DATABASE_FILENAME = "uhabits.db";
public static int DATABASE_VERSION = 23;
public static int DATABASE_VERSION = 24;
}

@ -38,18 +38,14 @@ public abstract class Command
{
private String id;
private boolean isRemote;
public Command()
{
id = StringUtils.getRandomId();
isRemote = false;
}
public Command(String id)
{
this.id = id;
isRemote = false;
}
public abstract void execute();
@ -64,16 +60,6 @@ public abstract class Command
this.id = id;
}
public boolean isRemote()
{
return isRemote;
}
public void setRemote(boolean remote)
{
isRemote = remote;
}
@NonNull
public String toJson()
{

@ -66,12 +66,17 @@ public class CommandRunner
@Override
public void onPostExecute()
{
for (Listener l : listeners)
l.onCommandExecuted(command, refreshKey);
notifyListeners(command, refreshKey);
}
});
}
public void notifyListeners(Command command, Long refreshKey)
{
for (Listener l : listeners)
l.onCommandExecuted(command, refreshKey);
}
public void removeListener(Listener l)
{
listeners.remove(l);
@ -83,7 +88,7 @@ public class CommandRunner
*/
public interface Listener
{
void onCommandExecuted(@NonNull Command command,
void onCommandExecuted(@Nullable Command command,
@Nullable Long refreshKey);
}
}

@ -101,35 +101,42 @@ public class LoopDBImporter extends AbstractImporter
habitsRepository = new Repository<>(HabitRecord.class, db);
repsRepository = new Repository<>(RepetitionRecord.class, db);
for (HabitRecord habitRecord : habitsRepository.findAll(
"order by position"))
List<HabitRecord> records = habitsRepository.findAll("order by position");
for (HabitRecord habitRecord : records)
{
Habit habit = habitList.getById(habitRecord.id);
List<RepetitionRecord> reps =
repsRepository.findAll("where habit = ?",
habitRecord.id.toString());
Habit habit = habitList.getByUUID(habitRecord.uuid);
if (habit == null)
{
habit = modelFactory.buildHabit();
habitRecord.id = null;
habitRecord.copyTo(habit);
runner.execute(new CreateHabitCommand(modelFactory, habitList, habit), null);
new CreateHabitCommand(modelFactory, habitList, habit).execute();
}
else
{
Habit modified = modelFactory.buildHabit();
habitRecord.id = habit.id;
habitRecord.copyTo(modified);
if (!modified.getData().equals(habit.getData()))
runner.execute(new EditHabitCommand(modelFactory, habitList, habit, modified), null);
new EditHabitCommand(modelFactory, habitList, habit, modified).execute();
}
List<RepetitionRecord> reps =
repsRepository.findAll("where habit = ?",
habitRecord.id.toString());
// Reload saved version of the habit
habit = habitList.getByUUID(habitRecord.uuid);
for (RepetitionRecord r : reps)
{
Timestamp t = new Timestamp(r.timestamp);
Repetition rep = habit.getRepetitions().getByTimestamp(t);
if(rep == null || rep.getValue() != r.value)
runner.execute(new CreateRepetitionCommand(habitList, habit, t, r.value), habit.id);
new CreateRepetitionCommand(habitList, habit, t, r.value).execute();
}
}
runner.notifyListeners(null, null);
}
}

@ -356,14 +356,27 @@ public class Habit
}
@NonNull
public String getQuestion() {
public String getQuestion()
{
return data.question;
}
public void setQuestion(@NonNull String 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
@ -388,6 +401,8 @@ public class Habit
public int type;
public String uuid;
@NonNull
public String unit;
@ -409,6 +424,7 @@ public class Habit
this.targetValue = 100;
this.unit = "";
this.position = 0;
this.uuid = UUID.randomUUID().toString().replace("-", "");
}
public HabitData(@NonNull HabitData model)
@ -425,6 +441,7 @@ public class Habit
this.unit = model.unit;
this.reminder = model.reminder;
this.position = model.position;
this.uuid = model.uuid;
}
@Override
@ -443,6 +460,7 @@ public class Habit
.append("reminder", reminder)
.append("position", position)
.append("question", question)
.append("uuid", uuid)
.toString();
}
@ -468,6 +486,7 @@ public class Habit
.append(reminder, habitData.reminder)
.append(position, habitData.position)
.append(question, habitData.question)
.append(uuid, habitData.uuid)
.isEquals();
}
@ -487,6 +506,7 @@ public class Habit
.append(reminder)
.append(position)
.append(question)
.append(uuid)
.toHashCode();
}
}

@ -83,6 +83,15 @@ public abstract class HabitList implements Iterable<Habit>
@Nullable
public abstract Habit getById(long id);
/**
* Returns the habit with specified UUID.
*
* @param uuid the UUID of the habit
* @return the habit, or null if none exist
*/
@Nullable
public abstract Habit getByUUID(String uuid);
/**
* Returns the habit that occupies a certain position.
*

@ -94,6 +94,13 @@ public class MemoryHabitList extends HabitList
return null;
}
@Override
public synchronized Habit getByUUID(String uuid)
{
for (Habit h : list) if (h.getUUID().equals(uuid)) return h;
return null;
}
@NonNull
@Override
public synchronized Habit getByPosition(int position)

@ -98,6 +98,14 @@ public class SQLiteHabitList extends HabitList
return list.getById(id);
}
@Override
@Nullable
public synchronized Habit getByUUID(String uuid)
{
loadRecords();
return list.getByUUID(uuid);
}
@Override
@NonNull
public synchronized Habit getByPosition(int position)

@ -81,6 +81,9 @@ public class HabitRecord
@Column
public Long id;
@Column
public String uuid;
public void copyFrom(Habit model)
{
this.id = model.getId();
@ -95,6 +98,7 @@ public class HabitRecord
this.unit = model.getUnit();
this.position = model.getPosition();
this.question = model.getQuestion();
this.uuid = model.getUUID();
Frequency freq = model.getFrequency();
this.freqNum = freq.getNumerator();
@ -126,6 +130,7 @@ public class HabitRecord
habit.setTargetValue(this.targetValue);
habit.setUnit(this.unit);
habit.setPosition(this.position);
habit.setUUID(this.uuid);
if (reminderHour != null && reminderMin != null)
{

@ -56,7 +56,7 @@ public class ReminderScheduler implements CommandRunner.Listener
}
@Override
public synchronized void onCommandExecuted(@NonNull Command command,
public synchronized void onCommandExecuted(@Nullable Command command,
@Nullable Long refreshKey)
{
if (command instanceof CreateRepetitionCommand) return;

@ -73,7 +73,7 @@ public class NotificationTray
}
@Override
public void onCommandExecuted(@NonNull Command command,
public void onCommandExecuted(@Nullable Command command,
@Nullable Long refreshKey)
{
if (command instanceof CreateRepetitionCommand)

@ -0,0 +1,2 @@
alter table habits add column uuid text;
update habits set uuid = lower(hex(randomblob(16) || id));

@ -127,7 +127,7 @@ public class BaseUnitTest
DriverManager.getConnection("jdbc:sqlite::memory:"));
db.execute("pragma user_version=8;");
MigrationHelper helper = new MigrationHelper(db);
helper.migrateTo(23);
helper.migrateTo(Config.DATABASE_VERSION);
return db;
}
catch (SQLException e)

@ -132,7 +132,7 @@ public class ImportTest extends BaseUnitTest
assertTrue(file.canRead());
GenericImporter importer = new GenericImporter(habitList,
new LoopDBImporter(habitList, modelFactory, databaseOpener),
new LoopDBImporter(habitList, modelFactory, databaseOpener, commandRunner),
new RewireDBImporter(habitList, modelFactory, databaseOpener),
new TickmateDBImporter(habitList, modelFactory, databaseOpener),
new HabitBullCSVImporter(habitList, modelFactory));

@ -148,6 +148,7 @@ public class HabitTest extends BaseUnitTest
public void testToString() throws Exception
{
Habit h = modelFactory.buildHabit();
h.setUUID("nnnn");
h.setReminder(new Reminder(22, 30, WeekdayList.EVERY_DAY));
String expected = "{id: <null>, data: {name: , description: ," +
" frequency: {numerator: 3, denominator: 7}," +
@ -155,7 +156,7 @@ public class HabitTest extends BaseUnitTest
" targetValue: 100.0, type: 0, unit: ," +
" reminder: {hour: 22, minute: 30," +
" days: {weekdays: [true,true,true,true,true,true,true]}}," +
" position: 0, question: }}";
" position: 0, question: , uuid: nnnn}}";
assertThat(h.toString(), equalTo(expected));
}

Loading…
Cancel
Save