mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Move commands to uhabits-core
This commit is contained in:
@@ -90,6 +90,9 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull
|
||||
private Preferences prefs;
|
||||
|
||||
@NonNull
|
||||
private final CommandParser commandParser;
|
||||
|
||||
@Inject
|
||||
public ListHabitsScreen(@NonNull BaseActivity activity,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@@ -99,9 +102,11 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||
@NonNull ColorPickerDialogFactory colorPickerFactory,
|
||||
@NonNull EditHabitDialogFactory editHabitDialogFactory,
|
||||
@NonNull Preferences prefs)
|
||||
@NonNull Preferences prefs,
|
||||
@NonNull CommandParser commandParser)
|
||||
{
|
||||
super(activity);
|
||||
this.commandParser = commandParser;
|
||||
setRootView(rootView);
|
||||
this.prefs = prefs;
|
||||
this.colorPickerFactory = colorPickerFactory;
|
||||
@@ -122,7 +127,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
if(command.isRemote()) return;
|
||||
showMessage(command.getExecuteStringId());
|
||||
showMessage(commandParser.getExecuteString(command));
|
||||
}
|
||||
|
||||
public void onDettached()
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.support.annotation.*;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.json.*;
|
||||
|
||||
@@ -42,6 +43,30 @@ public class CommandParser
|
||||
this.modelFactory = modelFactory;
|
||||
}
|
||||
|
||||
@StringRes
|
||||
public Integer getExecuteString(@NonNull Command command)
|
||||
{
|
||||
if(command instanceof ArchiveHabitsCommand)
|
||||
return R.string.toast_habit_archived;
|
||||
|
||||
if(command instanceof ChangeHabitColorCommand)
|
||||
return R.string.toast_habit_changed;
|
||||
|
||||
if(command instanceof CreateHabitCommand)
|
||||
return R.string.toast_habit_created;
|
||||
|
||||
if(command instanceof DeleteHabitsCommand)
|
||||
return R.string.toast_habit_deleted;
|
||||
|
||||
if(command instanceof EditHabitCommand)
|
||||
return R.string.toast_habit_changed;
|
||||
|
||||
if(command instanceof UnarchiveHabitsCommand)
|
||||
return R.string.toast_habit_unarchived;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Command parse(@NonNull JSONObject json) throws JSONException
|
||||
{
|
||||
|
||||
@@ -108,13 +108,25 @@ public class SyncManager implements CommandRunner.Listener
|
||||
connect(context, serverURL);
|
||||
}
|
||||
|
||||
private JSONObject toJSONObject(String json)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new JSONObject(json);
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@NonNull Command command,
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
if (command.isRemote()) return;
|
||||
|
||||
JSONObject msg = command.toJson();
|
||||
JSONObject msg = toJSONObject(command.toJson());
|
||||
Long now = new Date().getTime();
|
||||
Event e = new Event(command.getId(), now, msg.toString());
|
||||
e.save();
|
||||
|
||||
@@ -30,9 +30,7 @@ import org.isoron.uhabits.models.sqlite.records.*;
|
||||
import org.isoron.uhabits.sync.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class DatabaseUtils
|
||||
{
|
||||
@@ -70,11 +68,6 @@ public abstract class DatabaseUtils
|
||||
return databaseFilename;
|
||||
}
|
||||
|
||||
public static String getRandomId()
|
||||
{
|
||||
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void initializeActiveAndroid(Context context)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities;
|
||||
package org.isoron.androidbase.activities;
|
||||
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
@@ -27,8 +27,8 @@ import android.support.v7.widget.Toolbar;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.androidbase.activities.*;
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
@@ -17,7 +17,7 @@
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities;
|
||||
package org.isoron.androidbase.activities;
|
||||
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
@@ -25,7 +25,6 @@ import android.support.v7.view.ActionMode;
|
||||
import android.support.v7.widget.*;
|
||||
import android.view.*;
|
||||
|
||||
import org.isoron.androidbase.activities.*;
|
||||
import org.isoron.uhabits.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
@@ -38,6 +37,7 @@ import static android.view.View.*;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.hamcrest.core.IsEqual.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.robolectric.Robolectric.*;
|
||||
@@ -71,6 +71,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
|
||||
private Preferences prefs;
|
||||
|
||||
private CommandParser commandParser;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
@@ -86,10 +88,11 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
|
||||
dialogFactory = mock(EditHabitDialogFactory.class);
|
||||
prefs = mock(Preferences.class);
|
||||
commandParser = mock(CommandParser.class);
|
||||
|
||||
screen = spy(new ListHabitsScreen(activity, commandRunner, rootView,
|
||||
intentFactory, themeSwitcher, confirmDeleteDialogFactory,
|
||||
colorPickerDialogFactory, dialogFactory, prefs));
|
||||
colorPickerDialogFactory, dialogFactory, prefs, commandParser));
|
||||
|
||||
doNothing().when(screen).showMessage(anyInt());
|
||||
|
||||
@@ -122,7 +125,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
public void testOnCommand()
|
||||
{
|
||||
Command c = mock(Command.class);
|
||||
when(c.getExecuteStringId()).thenReturn(R.string.toast_habit_deleted);
|
||||
when(commandParser.getExecuteString(c)).thenReturn(
|
||||
R.string.toast_habit_deleted);
|
||||
screen.onCommandExecuted(c, null);
|
||||
verify(screen).showMessage(R.string.toast_habit_deleted);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ dependencies {
|
||||
implementation 'com.google.auto.factory:auto-factory:1.0-beta3'
|
||||
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
||||
implementation 'org.apache.commons:commons-lang3:3.5'
|
||||
implementation 'com.google.code.gson:gson:2.7'
|
||||
|
||||
implementation ('com.opencsv:opencsv:3.9') {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
|
||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -50,18 +49,6 @@ public class ArchiveHabitsCommand extends Command
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_archived;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_unarchived;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -61,18 +60,6 @@ public class ChangeHabitColorCommand extends Command
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_changed;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
@@ -24,7 +24,6 @@ import android.support.annotation.*;
|
||||
import com.google.gson.*;
|
||||
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.json.*;
|
||||
|
||||
/**
|
||||
* A Command represents a desired set of changes that should be performed on the
|
||||
@@ -43,7 +42,7 @@ public abstract class Command
|
||||
|
||||
public Command()
|
||||
{
|
||||
id = DatabaseUtils.getRandomId();
|
||||
id = StringUtils.getRandomId();
|
||||
isRemote = false;
|
||||
}
|
||||
|
||||
@@ -55,11 +54,6 @@ public abstract class Command
|
||||
|
||||
public abstract void execute();
|
||||
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
@@ -70,11 +64,6 @@ public abstract class Command
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isRemote()
|
||||
{
|
||||
return isRemote;
|
||||
@@ -86,17 +75,9 @@ public abstract class Command
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public JSONObject toJson()
|
||||
public String toJson()
|
||||
{
|
||||
try
|
||||
{
|
||||
String json = new GsonBuilder().create().toJson(toRecord());
|
||||
return new JSONObject(json);
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return new GsonBuilder().create().toJson(toRecord());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -23,7 +23,6 @@ import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
/**
|
||||
@@ -62,18 +61,6 @@ public class CreateHabitCommand extends Command
|
||||
savedId = savedHabit.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_created;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_deleted;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -52,23 +51,11 @@ public class DeleteHabitsCommand extends Command
|
||||
habitList.remove(h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_deleted;
|
||||
}
|
||||
|
||||
public List<Habit> getSelected()
|
||||
{
|
||||
return Collections.unmodifiableList(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_restored;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
@@ -23,7 +23,6 @@ import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
/**
|
||||
@@ -77,18 +76,6 @@ public class EditHabitCommand extends Command
|
||||
copyAttributes(this.modified);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_changed_back;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.commands;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -51,18 +50,6 @@ public class UnarchiveHabitsCommand extends Command
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getExecuteStringId()
|
||||
{
|
||||
return R.string.toast_habit_unarchived;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getUndoStringId()
|
||||
{
|
||||
return R.string.toast_habit_archived;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.utils;
|
||||
|
||||
import java.math.*;
|
||||
import java.util.*;
|
||||
|
||||
public class StringUtils
|
||||
{
|
||||
public static String getRandomId()
|
||||
{
|
||||
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user