Add UUID to habits

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

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

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

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

@ -22,5 +22,5 @@ package org.isoron.uhabits.core;
public class Config public class Config
{ {
public static final String DATABASE_FILENAME = "uhabits.db"; 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 String id;
private boolean isRemote;
public Command() public Command()
{ {
id = StringUtils.getRandomId(); id = StringUtils.getRandomId();
isRemote = false;
} }
public Command(String id) public Command(String id)
{ {
this.id = id; this.id = id;
isRemote = false;
} }
public abstract void execute(); public abstract void execute();
@ -64,16 +60,6 @@ public abstract class Command
this.id = id; this.id = id;
} }
public boolean isRemote()
{
return isRemote;
}
public void setRemote(boolean remote)
{
isRemote = remote;
}
@NonNull @NonNull
public String toJson() public String toJson()
{ {

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

@ -101,35 +101,42 @@ public class LoopDBImporter extends AbstractImporter
habitsRepository = new Repository<>(HabitRecord.class, db); habitsRepository = new Repository<>(HabitRecord.class, db);
repsRepository = new Repository<>(RepetitionRecord.class, db); repsRepository = new Repository<>(RepetitionRecord.class, db);
for (HabitRecord habitRecord : habitsRepository.findAll( List<HabitRecord> records = habitsRepository.findAll("order by position");
"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) if (habit == null)
{ {
habit = modelFactory.buildHabit(); habit = modelFactory.buildHabit();
habitRecord.id = null;
habitRecord.copyTo(habit); habitRecord.copyTo(habit);
runner.execute(new CreateHabitCommand(modelFactory, habitList, habit), null); new CreateHabitCommand(modelFactory, habitList, habit).execute();
} }
else else
{ {
Habit modified = modelFactory.buildHabit(); Habit modified = modelFactory.buildHabit();
habitRecord.id = habit.id;
habitRecord.copyTo(modified); habitRecord.copyTo(modified);
if (!modified.getData().equals(habit.getData())) 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 = // Reload saved version of the habit
repsRepository.findAll("where habit = ?", habit = habitList.getByUUID(habitRecord.uuid);
habitRecord.id.toString());
for (RepetitionRecord r : reps) for (RepetitionRecord r : reps)
{ {
Timestamp t = new Timestamp(r.timestamp); Timestamp t = new Timestamp(r.timestamp);
Repetition rep = habit.getRepetitions().getByTimestamp(t); Repetition rep = habit.getRepetitions().getByTimestamp(t);
if(rep == null || rep.getValue() != r.value) 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 @NonNull
public String getQuestion() { public String getQuestion()
{
return data.question; return data.question;
} }
public void setQuestion(@NonNull String question) { public void setQuestion(@NonNull String question)
{
data.question = 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 public static final class HabitData
{ {
@NonNull @NonNull
@ -388,6 +401,8 @@ public class Habit
public int type; public int type;
public String uuid;
@NonNull @NonNull
public String unit; public String unit;
@ -409,6 +424,7 @@ public class Habit
this.targetValue = 100; this.targetValue = 100;
this.unit = ""; this.unit = "";
this.position = 0; this.position = 0;
this.uuid = UUID.randomUUID().toString().replace("-", "");
} }
public HabitData(@NonNull HabitData model) public HabitData(@NonNull HabitData model)
@ -425,6 +441,7 @@ public class Habit
this.unit = model.unit; this.unit = model.unit;
this.reminder = model.reminder; this.reminder = model.reminder;
this.position = model.position; this.position = model.position;
this.uuid = model.uuid;
} }
@Override @Override
@ -443,6 +460,7 @@ public class Habit
.append("reminder", reminder) .append("reminder", reminder)
.append("position", position) .append("position", position)
.append("question", question) .append("question", question)
.append("uuid", uuid)
.toString(); .toString();
} }
@ -468,6 +486,7 @@ public class Habit
.append(reminder, habitData.reminder) .append(reminder, habitData.reminder)
.append(position, habitData.position) .append(position, habitData.position)
.append(question, habitData.question) .append(question, habitData.question)
.append(uuid, habitData.uuid)
.isEquals(); .isEquals();
} }
@ -487,6 +506,7 @@ public class Habit
.append(reminder) .append(reminder)
.append(position) .append(position)
.append(question) .append(question)
.append(uuid)
.toHashCode(); .toHashCode();
} }
} }

@ -83,6 +83,15 @@ public abstract class HabitList implements Iterable<Habit>
@Nullable @Nullable
public abstract Habit getById(long id); 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. * Returns the habit that occupies a certain position.
* *

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

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

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

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

@ -73,7 +73,7 @@ public class NotificationTray
} }
@Override @Override
public void onCommandExecuted(@NonNull Command command, public void onCommandExecuted(@Nullable Command command,
@Nullable Long refreshKey) @Nullable Long refreshKey)
{ {
if (command instanceof CreateRepetitionCommand) 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:")); DriverManager.getConnection("jdbc:sqlite::memory:"));
db.execute("pragma user_version=8;"); db.execute("pragma user_version=8;");
MigrationHelper helper = new MigrationHelper(db); MigrationHelper helper = new MigrationHelper(db);
helper.migrateTo(23); helper.migrateTo(Config.DATABASE_VERSION);
return db; return db;
} }
catch (SQLException e) catch (SQLException e)

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

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

Loading…
Cancel
Save