Skip to content

Instantly share code, notes, and snippets.

@drapp
Created March 20, 2012 22:43
Show Gist options
  • Save drapp/2142062 to your computer and use it in GitHub Desktop.
Save drapp/2142062 to your computer and use it in GitHub Desktop.
TaskMob Snippets
public class JumbleOfStuff extends StackMobModel {
private Task favoriteTask;
private TaskList[] allTheTaskLists;
private Set<Task> taskSet;
}
StackMobModelQuery<Task> highPriorityTaskQuery = new StackMobModelQuery<Task>(Task.class).fieldIsGreaterThan("priority", "0");
highPriorityTaskQuery.send(new StackMobQueryCallback<Task>() {
@Override
public void success(List<Task> tasks) {
//handle success
}
@Override
public void failure(StackMobException e) {
//handle failure case
}
});
public class Task extends StackMobModel {
private String name;
private Date dueDate;
private int priority = 0;
private boolean done = false;
public Task(String name) {
super(Task.class);
this.name = name;
}
//Add whatever setters/getters/other functionality you want here
}
public class TaskActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
//...
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
Task clickedTask = adapter.getItem(pos);
CheckBox cb = (CheckBox) v.findViewById(R.id.task_done_checkbox);
cb.setChecked(!cb.isChecked());
clickedTask.setDone(cb.isChecked());
clickedTask.save();
}
});
}
}
public class TaskActivity extends ListActivity {
//...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Task newTask = // Get the Task from the form activity we launched
taskList.getTasks().add(newTask);
taskList.saveWithDepth(1);
adapter.notifyDataSetChanged();
}
}
blogPostTask.destroy();
// Create a new Task and save it to the server
Task blogPostTask = new Task("Write blog post");
blogPostTask.save();
//...
// Reload the task from the server. Useful if you think the
// data may have been changed since saving the object
blogPostTask.fetch(new StackMobCallback() {
@Override public void success(String responseBody) {
// The blogPostTask object is now filled in with data.
// You can ignore the responseBody argument.
}
@Override public void failure(StackMobException e) {
// handle failure case
}
});
public class TaskList extends StackMobModel {
private String name;
private List<Task> tasks = new ArrayList<Task>();
public TaskList(String name, List<Task> tasks) {
super(TaskList.class);
this.name = name;
this.tasks = tasks;
}
}
Task blogPostTask = new Task("Write blog post");
Task proofreadTask = new Task("Proofread");
Task[] tasks = new Task[]{blogPostTask, proofreadTask};
TaskList blogTasks = new TaskList("Blog Tasks", Arrays.asList(tasks));
blogTasks.saveWithDepth(1);
//This will reload the TaskList and both Task objects
blogTasks.fetchWithDepth(1);
public class TaskMob extends ListActivity {
//...
private StackMobModelQuery<TaskList> tasksQuery = new StackMobModelQuery<TaskList>(TaskList.class).expandDepthIs(1);
@Override
public void onCreate(Bundle savedInstanceState) {
//...
tasksQuery.send(new StackMobQueryCallback<TaskList>() {
@Override
public void success(List<TaskList> taskLists) {
initAdapter(taskLists);
}
@Override
public void failure(StackMobException e) {
//handle failure case
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment