mirror of https://github.com/tasks/tasks
Added indentation action and unit test
parent
3b6cf82d8d
commit
927ba61e5a
@ -0,0 +1,57 @@
|
|||||||
|
package com.todoroo.astrid.gtasks;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.timsu.astrid.R;
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.andlib.service.ContextManager;
|
||||||
|
import com.todoroo.andlib.service.DependencyInjectionService;
|
||||||
|
import com.todoroo.astrid.api.AstridApiConstants;
|
||||||
|
import com.todoroo.astrid.core.PluginServices;
|
||||||
|
import com.todoroo.astrid.data.Metadata;
|
||||||
|
|
||||||
|
abstract public class GtasksIndentAction extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@Autowired private GtasksMetadataService gtasksMetadataService;
|
||||||
|
|
||||||
|
abstract int getDelta();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
ContextManager.setContext(context);
|
||||||
|
DependencyInjectionService.getInstance().inject(this);
|
||||||
|
|
||||||
|
long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, -1);
|
||||||
|
if(taskId == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Metadata metadata = gtasksMetadataService.getTaskMetadata(taskId);
|
||||||
|
if(metadata == null)
|
||||||
|
metadata = GtasksMetadata.createEmptyMetadata();
|
||||||
|
|
||||||
|
int newIndent = Math.max(0, metadata.getValue(GtasksMetadata.INDENTATION) + getDelta());
|
||||||
|
metadata.setValue(GtasksMetadata.INDENTATION, newIndent);
|
||||||
|
PluginServices.getMetadataService().save(metadata);
|
||||||
|
|
||||||
|
Toast.makeText(context, context.getString(R.string.gtasks_indent_toast, newIndent),
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GtasksIncreaseIndentAction extends GtasksIndentAction {
|
||||||
|
@Override
|
||||||
|
public int getDelta() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GtasksDecreaseIndentAction extends GtasksIndentAction {
|
||||||
|
@Override
|
||||||
|
public int getDelta() {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
package com.todoroo.astrid.helper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
|
|
||||||
|
import com.todoroo.astrid.api.AstridApiConstants;
|
||||||
|
|
||||||
|
public class TaskListContextMenuExtensionLoader {
|
||||||
|
|
||||||
|
public static class ContextMenuItem {
|
||||||
|
public CharSequence title;
|
||||||
|
public Intent intent;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ContextMenuItem[] contextMenuItemCache = new ContextMenuItem[0];
|
||||||
|
|
||||||
|
public void loadContextMenuIntents(Context context) {
|
||||||
|
Intent queryIntent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
|
||||||
|
PackageManager pm = context.getPackageManager();
|
||||||
|
List<ResolveInfo> resolveInfoList = pm.queryBroadcastReceivers(queryIntent,
|
||||||
|
PackageManager.GET_META_DATA);
|
||||||
|
|
||||||
|
int length = resolveInfoList.size();
|
||||||
|
contextMenuItemCache = new ContextMenuItem[length];
|
||||||
|
for(int i = 0; i < length; i++) {
|
||||||
|
ResolveInfo resolveInfo = resolveInfoList.get(i);
|
||||||
|
|
||||||
|
ContextMenuItem item = new ContextMenuItem();
|
||||||
|
|
||||||
|
item.intent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
|
||||||
|
item.intent.setClassName(resolveInfo.activityInfo.packageName,
|
||||||
|
resolveInfo.activityInfo.name);
|
||||||
|
item.title = resolveInfo.loadLabel(pm);
|
||||||
|
contextMenuItemCache[i] = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadInNewThread(final Context context) {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
loadContextMenuIntents(context);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContextMenuItem[] getList() {
|
||||||
|
return contextMenuItemCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,100 @@
|
|||||||
|
package com.todoroo.astrid.gtasks;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.service.Autowired;
|
||||||
|
import com.todoroo.astrid.api.AstridApiConstants;
|
||||||
|
import com.todoroo.astrid.core.PluginServices;
|
||||||
|
import com.todoroo.astrid.data.Metadata;
|
||||||
|
import com.todoroo.astrid.data.Task;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksIndentAction.GtasksDecreaseIndentAction;
|
||||||
|
import com.todoroo.astrid.gtasks.GtasksIndentAction.GtasksIncreaseIndentAction;
|
||||||
|
import com.todoroo.astrid.test.DatabaseTestCase;
|
||||||
|
|
||||||
|
public class GtasksIndentActionTest extends DatabaseTestCase {
|
||||||
|
|
||||||
|
@Autowired private GtasksMetadataService gtasksMetadataService;
|
||||||
|
|
||||||
|
private Task task;
|
||||||
|
|
||||||
|
public void testIndentWithoutMetadata() {
|
||||||
|
givenTask(taskWithoutMetadata());
|
||||||
|
|
||||||
|
whenTrigger(new GtasksIncreaseIndentAction());
|
||||||
|
|
||||||
|
thenExpectIndentationLevel(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIndentWithMetadata() {
|
||||||
|
givenTask(taskWithMetadata(0));
|
||||||
|
|
||||||
|
whenTrigger(new GtasksIncreaseIndentAction());
|
||||||
|
|
||||||
|
thenExpectIndentationLevel(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDeindentWithMetadata() {
|
||||||
|
givenTask(taskWithMetadata(1));
|
||||||
|
|
||||||
|
whenTrigger(new GtasksDecreaseIndentAction());
|
||||||
|
|
||||||
|
thenExpectIndentationLevel(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDeindentWithoutMetadata() {
|
||||||
|
givenTask(taskWithoutMetadata());
|
||||||
|
|
||||||
|
whenTrigger(new GtasksDecreaseIndentAction());
|
||||||
|
|
||||||
|
thenExpectIndentationLevel(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDeindentWhenAlreadyZero() {
|
||||||
|
givenTask(taskWithMetadata(0));
|
||||||
|
|
||||||
|
whenTrigger(new GtasksDecreaseIndentAction());
|
||||||
|
|
||||||
|
thenExpectIndentationLevel(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- helpers
|
||||||
|
|
||||||
|
private Task taskWithMetadata(int indentation) {
|
||||||
|
Task task = new Task();
|
||||||
|
PluginServices.getTaskService().save(task);
|
||||||
|
Metadata metadata = GtasksMetadata.createEmptyMetadata();
|
||||||
|
metadata.setValue(GtasksMetadata.INDENTATION, indentation);
|
||||||
|
metadata.setValue(Metadata.TASK, task.getId());
|
||||||
|
PluginServices.getMetadataService().save(metadata);
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void thenExpectIndentationLevel(int expected) {
|
||||||
|
Metadata metadata = gtasksMetadataService.getTaskMetadata(task.getId());
|
||||||
|
assertNotNull(metadata);
|
||||||
|
int indentation = metadata.getValue(GtasksMetadata.INDENTATION);
|
||||||
|
assertTrue("indentation: " + indentation,
|
||||||
|
indentation == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void whenTrigger(BroadcastReceiver action) {
|
||||||
|
Intent intent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
|
||||||
|
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
|
||||||
|
action.onReceive(getContext(), intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void givenTask(Task taskToTest) {
|
||||||
|
task = taskToTest;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Task taskWithoutMetadata() {
|
||||||
|
Task task = new Task();
|
||||||
|
PluginServices.getTaskService().save(task);
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue