Sync archive/unarchive commands

pull/286/head
Alinson S. Xavier 10 years ago
parent 1fcfb9b22e
commit 41d9e2f0f5

@ -97,12 +97,13 @@ abstract public class BaseActivity extends AppCompatActivity implements Thread.U
{
BaseActivity.this.onPostExecuteCommand(command, refreshKey);
BackupManager.dataChanged("org.isoron.uhabits");
if(shouldBroadcast) sync.postCommand(command);
if(shouldBroadcast)
{
sync.postCommand(command);
showToast(command.getExecuteStringId());
}
}
}.execute();
showToast(command.getExecuteStringId());
}
public void onPostExecuteCommand(Command command, Long refreshKey)

@ -27,7 +27,7 @@ import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import org.isoron.uhabits.commands.Command;
import org.isoron.uhabits.commands.ToggleRepetitionCommand;
import org.isoron.uhabits.commands.CommandParser;
import org.isoron.uhabits.helpers.DatabaseHelper;
import org.json.JSONException;
import org.json.JSONObject;
@ -129,23 +129,21 @@ public class SyncManager
{
Log.d("SyncManager", String.format("Received command: %s", arg.toString()));
JSONObject root = new JSONObject(arg.toString());
if(root.getString("command").equals("ToggleRepetition"))
{
Command received = ToggleRepetitionCommand.fromJSON(root);
if(received == null) throw new RuntimeException("received is null");
for(Command pending : outbox)
Command received = CommandParser.fromJSON(root);
if(received == null) throw new RuntimeException("received is null");
for(Command pending : outbox)
{
if(pending.getId().equals(received.getId()))
{
if(pending.getId().equals(received.getId()))
{
outbox.remove(pending);
Log.d("SyncManager", "Received command discarded");
return;
}
outbox.remove(pending);
Log.d("SyncManager", "Received command discarded");
return;
}
activity.executeCommand(received, null, false);
Log.d("SyncManager", "Received command executed");
}
activity.executeCommand(received, null, false);
Log.d("SyncManager", "Received command executed");
}
}

@ -19,9 +19,15 @@
package org.isoron.uhabits.commands;
import android.support.annotation.Nullable;
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 ArchiveHabitsCommand extends Command
@ -29,6 +35,12 @@ public class ArchiveHabitsCommand extends Command
private List<Habit> habits;
public ArchiveHabitsCommand(String id, List<Habit> habits)
{
super(id);
this.habits = habits;
}
public ArchiveHabitsCommand(List<Habit> habits)
{
this.habits = habits;
@ -55,4 +67,33 @@ public class ArchiveHabitsCommand extends Command
{
return R.string.toast_habit_unarchived;
}
@Nullable
@Override
public JSONObject toJSON()
{
try
{
JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data");
root.put("command", "ArchiveHabits");
data.put("habits", CommandParser.habitsToJSON(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("habits");
LinkedList<Habit> habits = CommandParser.habitsFromJSON(habitIds);
return new ArchiveHabitsCommand(id, habits);
}
}

@ -19,11 +19,16 @@
package org.isoron.uhabits.commands;
import android.support.annotation.Nullable;
import android.support.annotation.NonNull;
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.List;
public abstract class Command
{
private final String id;
@ -52,8 +57,21 @@ public abstract class Command
return null;
}
@Nullable
public JSONObject toJSON() { return null; }
public JSONObject toJSON()
{
try
{
JSONObject root = new JSONObject();
JSONObject data = new JSONObject();
root.put("id", getId());
root.put("data", data);
return root;
}
catch (JSONException e)
{
throw new RuntimeException(e.getMessage());
}
}
public String getId()
{

@ -0,0 +1,75 @@
/*
* 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.commands;
import android.support.annotation.NonNull;
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 CommandParser
{
public static Command fromJSON(JSONObject json) throws JSONException
{
switch(json.getString("command"))
{
case "ToggleRepetition":
return ToggleRepetitionCommand.fromJSON(json);
case "ArchiveHabits":
return ArchiveHabitsCommand.fromJSON(json);
case "UnarchiveHabits":
return UnarchiveHabitsCommand.fromJSON(json);
}
return null;
}
@NonNull
public static LinkedList<Habit> habitsFromJSON(JSONArray habitIds) throws JSONException
{
LinkedList<Habit> habits = new LinkedList<>();
for (int i = 0; i < habitIds.length(); i++)
{
Long hId = habitIds.getLong(i);
Habit h = Habit.get(hId);
if(h == null) continue;
habits.add(h);
}
return habits;
}
@NonNull
protected static JSONArray habitsToJSON(List<Habit> habits)
{
JSONArray habitIds = new JSONArray();
for(Habit h : habits) habitIds.put(h.getId());
return habitIds;
}
}

@ -56,19 +56,16 @@ public class ToggleRepetitionCommand extends Command
execute();
}
@Nullable
@Override
public JSONObject toJSON()
{
try
{
JSONObject root = new JSONObject();
JSONObject data = new JSONObject();
root.put("id", getId());
JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data");
root.put("command", "ToggleRepetition");
data.put("habit", habit.getId());
data.put("timestamp", timestamp);
root.put("data", data);
return root;
}
catch (JSONException e)

@ -21,7 +21,11 @@ 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 UnarchiveHabitsCommand extends Command
@ -29,6 +33,12 @@ public class UnarchiveHabitsCommand extends Command
private List<Habit> habits;
public UnarchiveHabitsCommand(String id, List<Habit> habits)
{
super(id);
this.habits = habits;
}
public UnarchiveHabitsCommand(List<Habit> habits)
{
this.habits = habits;
@ -55,4 +65,32 @@ public class UnarchiveHabitsCommand extends Command
{
return R.string.toast_habit_archived;
}
@Override
public JSONObject toJSON()
{
try
{
JSONObject root = super.toJSON();
JSONObject data = root.getJSONObject("data");
root.put("command", "UnarchiveHabits");
data.put("habits", CommandParser.habitsToJSON(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("habits");
LinkedList<Habit> habits = CommandParser.habitsFromJSON(habitIds);
return new UnarchiveHabitsCommand(id, habits);
}
}
Loading…
Cancel
Save