mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Sync remaining commands
This commit is contained in:
@@ -63,7 +63,12 @@ abstract public class BaseActivity extends AppCompatActivity implements Thread.U
|
||||
|
||||
androidExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
sync = new SyncManager(this);
|
||||
}
|
||||
|
||||
@@ -99,7 +104,7 @@ abstract public class BaseActivity extends AppCompatActivity implements Thread.U
|
||||
BackupManager.dataChanged("org.isoron.uhabits");
|
||||
if(shouldBroadcast)
|
||||
{
|
||||
sync.postCommand(command);
|
||||
if(sync != null) sync.postCommand(command);
|
||||
showToast(command.getExecuteStringId());
|
||||
}
|
||||
}
|
||||
@@ -191,10 +196,12 @@ abstract public class BaseActivity extends AppCompatActivity implements Thread.U
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy()
|
||||
protected void onPause()
|
||||
{
|
||||
sync.close();
|
||||
super.onDestroy();
|
||||
sync = null;
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void dismissNotifications(Context context)
|
||||
|
||||
@@ -42,7 +42,7 @@ public class SyncManager
|
||||
public static final String SYNC_SERVER_URL = "http://10.0.2.2:4000";
|
||||
|
||||
private static String GROUP_KEY = "sEBY3poXHFH7EyB43V2JoQUNEtBjMgdD";
|
||||
private static String CLIENT_KEY;
|
||||
private static String CLIENT_ID;
|
||||
|
||||
@NonNull
|
||||
private Socket socket;
|
||||
@@ -53,7 +53,7 @@ public class SyncManager
|
||||
{
|
||||
this.activity = activity;
|
||||
outbox = new LinkedList<>();
|
||||
CLIENT_KEY = DatabaseHelper.getRandomId();
|
||||
CLIENT_ID = DatabaseHelper.getRandomId();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -80,6 +80,7 @@ public class SyncManager
|
||||
|
||||
public void close()
|
||||
{
|
||||
socket.off();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
@@ -98,7 +99,7 @@ public class SyncManager
|
||||
{
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("group_key", GROUP_KEY);
|
||||
json.put("client_key", CLIENT_KEY);
|
||||
json.put("client_id", CLIENT_ID);
|
||||
json.put("version", BuildConfig.VERSION_NAME);
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class ArchiveHabitsCommand extends Command
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "ArchiveHabits");
|
||||
data.put("habits", CommandParser.habitsToJSON(habits));
|
||||
data.put("ids", CommandParser.habitListToJSON(habits));
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
@@ -91,9 +91,9 @@ public class ArchiveHabitsCommand extends Command
|
||||
{
|
||||
String id = json.getString("id");
|
||||
JSONObject data = (JSONObject) json.get("data");
|
||||
JSONArray habitIds = data.getJSONArray("habits");
|
||||
JSONArray habitIds = data.getJSONArray("ids");
|
||||
|
||||
LinkedList<Habit> habits = CommandParser.habitsFromJSON(habitIds);
|
||||
LinkedList<Habit> habits = CommandParser.habitListFromJSON(habitIds);
|
||||
return new ArchiveHabitsCommand(id, habits);
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,15 @@
|
||||
|
||||
package org.isoron.uhabits.commands;
|
||||
|
||||
import com.activeandroid.ActiveAndroid;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ChangeHabitColorCommand extends Command
|
||||
@@ -34,7 +36,18 @@ public class ChangeHabitColorCommand extends Command
|
||||
List<Integer> originalColors;
|
||||
Integer newColor;
|
||||
|
||||
public ChangeHabitColorCommand(String id, List<Habit> habits, Integer newColor)
|
||||
{
|
||||
super(id);
|
||||
init(habits, newColor);
|
||||
}
|
||||
|
||||
public ChangeHabitColorCommand(List<Habit> habits, Integer newColor)
|
||||
{
|
||||
init(habits, newColor);
|
||||
}
|
||||
|
||||
private void init(List<Habit> habits, Integer newColor)
|
||||
{
|
||||
this.habits = habits;
|
||||
this.newColor = newColor;
|
||||
@@ -77,4 +90,33 @@ public class ChangeHabitColorCommand extends Command
|
||||
{
|
||||
return R.string.toast_habit_changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject toJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "ChangeHabitColor");
|
||||
data.put("ids", CommandParser.habitListToJSON(habits));
|
||||
data.put("color", newColor);
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Command fromJSON(JSONObject json) throws JSONException
|
||||
{
|
||||
String id = json.getString("id");
|
||||
JSONObject data = (JSONObject) json.get("data");
|
||||
JSONArray habitIds = data.getJSONArray("ids");
|
||||
int newColor = data.getInt("color");
|
||||
|
||||
LinkedList<Habit> habits = CommandParser.habitListFromJSON(habitIds);
|
||||
return new ChangeHabitColorCommand(id, habits, newColor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,13 +43,30 @@ public class CommandParser
|
||||
|
||||
case "UnarchiveHabits":
|
||||
return UnarchiveHabitsCommand.fromJSON(json);
|
||||
|
||||
case "ChangeHabitColor":
|
||||
return ChangeHabitColorCommand.fromJSON(json);
|
||||
|
||||
case "CreateHabit":
|
||||
return CreateHabitCommand.fromJSON(json);
|
||||
|
||||
case "DeleteHabits":
|
||||
return DeleteHabitsCommand.fromJSON(json);
|
||||
|
||||
case "EditHabit":
|
||||
return EditHabitCommand.fromJSON(json);
|
||||
|
||||
// TODO: Implement this
|
||||
// case "ReorderHabit":
|
||||
// return ReorderHabitCommand.fromJSON(json);
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static LinkedList<Habit> habitsFromJSON(JSONArray habitIds) throws JSONException
|
||||
public static LinkedList<Habit> habitListFromJSON(JSONArray habitIds) throws JSONException
|
||||
{
|
||||
LinkedList<Habit> habits = new LinkedList<>();
|
||||
|
||||
@@ -66,7 +83,7 @@ public class CommandParser
|
||||
}
|
||||
|
||||
@NonNull
|
||||
protected static JSONArray habitsToJSON(List<Habit> habits)
|
||||
protected static JSONArray habitListToJSON(List<Habit> habits)
|
||||
{
|
||||
JSONArray habitIds = new JSONArray();
|
||||
for(Habit h : habits) habitIds.put(h.getId());
|
||||
|
||||
@@ -19,14 +19,25 @@
|
||||
|
||||
package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class CreateHabitCommand extends Command
|
||||
{
|
||||
private Habit model;
|
||||
private Long savedId;
|
||||
|
||||
public CreateHabitCommand(String id, Habit model, Long savedId)
|
||||
{
|
||||
super(id);
|
||||
this.model = model;
|
||||
this.savedId = savedId;
|
||||
}
|
||||
|
||||
public CreateHabitCommand(Habit model)
|
||||
{
|
||||
this.model = model;
|
||||
@@ -68,4 +79,32 @@ public class CreateHabitCommand extends Command
|
||||
return R.string.toast_habit_deleted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject toJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "CreateHabit");
|
||||
data.put("habit", model.toJSON());
|
||||
data.put("id", savedId);
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Command fromJSON(JSONObject root) throws JSONException
|
||||
{
|
||||
String commandId = root.getString("id");
|
||||
JSONObject data = (JSONObject) root.get("data");
|
||||
Habit model = Habit.fromJSON(data.getJSONObject("habit"));
|
||||
Long savedId = data.getLong("id");
|
||||
|
||||
return new CreateHabitCommand(commandId, model, savedId);
|
||||
}
|
||||
}
|
||||
@@ -21,13 +21,23 @@ package org.isoron.uhabits.commands;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class DeleteHabitsCommand extends Command
|
||||
{
|
||||
private List<Habit> habits;
|
||||
|
||||
public DeleteHabitsCommand(String id, List<Habit> habits)
|
||||
{
|
||||
super(id);
|
||||
this.habits = habits;
|
||||
}
|
||||
|
||||
public DeleteHabitsCommand(List<Habit> habits)
|
||||
{
|
||||
this.habits = habits;
|
||||
@@ -57,4 +67,31 @@ public class DeleteHabitsCommand extends Command
|
||||
{
|
||||
return R.string.toast_habit_restored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject toJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "DeleteHabits");
|
||||
data.put("ids", CommandParser.habitListToJSON(habits));
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Command fromJSON(JSONObject json) throws JSONException
|
||||
{
|
||||
String id = json.getString("id");
|
||||
JSONObject data = (JSONObject) json.get("data");
|
||||
JSONArray habitIds = data.getJSONArray("ids");
|
||||
|
||||
LinkedList<Habit> habits = CommandParser.habitListFromJSON(habitIds);
|
||||
return new DeleteHabitsCommand(id, habits);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,12 @@
|
||||
|
||||
package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class EditHabitCommand extends Command
|
||||
{
|
||||
@@ -29,7 +33,18 @@ public class EditHabitCommand extends Command
|
||||
private long savedId;
|
||||
private boolean hasIntervalChanged;
|
||||
|
||||
public EditHabitCommand(String id, Habit original, Habit modified)
|
||||
{
|
||||
super(id);
|
||||
init(original, modified);
|
||||
}
|
||||
|
||||
public EditHabitCommand(Habit original, Habit modified)
|
||||
{
|
||||
init(original, modified);
|
||||
}
|
||||
|
||||
private void init(Habit original, Habit modified)
|
||||
{
|
||||
this.savedId = original.getId();
|
||||
this.modified = new Habit(modified);
|
||||
@@ -81,4 +96,35 @@ public class EditHabitCommand extends Command
|
||||
{
|
||||
return R.string.toast_habit_changed_back;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject toJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "EditHabit");
|
||||
data.put("id", savedId);
|
||||
data.put("params", modified.toJSON());
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Command fromJSON(JSONObject root) throws JSONException
|
||||
{
|
||||
String commandId = root.getString("id");
|
||||
JSONObject data = (JSONObject) root.get("data");
|
||||
Habit original = Habit.get(data.getLong("id"));
|
||||
if(original == null) return null;
|
||||
|
||||
Habit modified = Habit.fromJSON(data.getJSONObject("params"));
|
||||
|
||||
return new EditHabitCommand(commandId, original, modified);
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class UnarchiveHabitsCommand extends Command
|
||||
JSONObject root = super.toJSON();
|
||||
JSONObject data = root.getJSONObject("data");
|
||||
root.put("command", "UnarchiveHabits");
|
||||
data.put("habits", CommandParser.habitsToJSON(habits));
|
||||
data.put("ids", CommandParser.habitListToJSON(habits));
|
||||
return root;
|
||||
}
|
||||
catch (JSONException e)
|
||||
@@ -87,9 +87,9 @@ public class UnarchiveHabitsCommand extends Command
|
||||
{
|
||||
String id = json.getString("id");
|
||||
JSONObject data = (JSONObject) json.get("data");
|
||||
JSONArray habitIds = data.getJSONArray("habits");
|
||||
JSONArray habitIds = data.getJSONArray("ids");
|
||||
|
||||
LinkedList<Habit> habits = CommandParser.habitsFromJSON(habitIds);
|
||||
LinkedList<Habit> habits = CommandParser.habitListFromJSON(habitIds);
|
||||
return new UnarchiveHabitsCommand(id, habits);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ import com.opencsv.CSVWriter;
|
||||
|
||||
import org.isoron.uhabits.helpers.ColorHelper;
|
||||
import org.isoron.uhabits.helpers.DateHelper;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
@@ -510,4 +512,46 @@ public class Habit extends Model
|
||||
|
||||
csv.close();
|
||||
}
|
||||
|
||||
public JSONObject toJSON()
|
||||
{
|
||||
try
|
||||
{
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("name", name);
|
||||
json.put("description", description);
|
||||
json.put("freqNum", freqNum);
|
||||
json.put("freqDen", freqDen);
|
||||
json.put("color", color);
|
||||
json.put("position", position);
|
||||
json.put("reminderHour", reminderHour);
|
||||
json.put("reminderMin", reminderMin);
|
||||
json.put("reminderDays", reminderDays);
|
||||
json.put("archived", archived);
|
||||
return json;
|
||||
}
|
||||
catch(JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Habit fromJSON(JSONObject json) throws JSONException
|
||||
{
|
||||
Habit habit = new Habit();
|
||||
habit.name = json.getString("name");
|
||||
habit.description = json.getString("description");
|
||||
habit.freqNum = json.getInt("freqNum");
|
||||
habit.freqDen = json.getInt("freqDen");
|
||||
habit.color = json.getInt("color");
|
||||
habit.position = json.getInt("position");
|
||||
habit.archived = json.getInt("archived");
|
||||
if(json.has("reminderHour"))
|
||||
{
|
||||
habit.reminderHour = json.getInt("reminderHour");
|
||||
habit.reminderMin = json.getInt("reminderMin");
|
||||
habit.reminderDays = json.getInt("reminderDays");
|
||||
}
|
||||
return habit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user