Search in sources :

Example 1 with ToDo

use of com.instructure.canvasapi.model.ToDo in project instructure-android by instructure.

the class TodoUnitTest method testToDo.

@Test
public void testToDo() {
    Gson gson = CanvasRestAdapter.getGSONParser();
    ToDo[] toDoList = gson.fromJson(todoJSON, ToDo[].class);
    assertNotNull(toDoList);
    for (ToDo toDo : toDoList) {
        assertNotNull(toDo);
        assertNotNull(toDo.getType());
        assertNotNull(toDo.getTitle());
        assertNotNull(toDo.getHtmlUrl());
        assertNotNull(toDo.getIgnore());
        assertNotNull(toDo.getIgnorePermanently());
        if (toDo.getAssignment() != null) {
            Assignment assignment = toDo.getAssignment();
            assertTrue(assignment.getId() > 0);
        }
    }
}
Also used : Assignment(com.instructure.canvasapi.model.Assignment) ToDo(com.instructure.canvasapi.model.ToDo) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 2 with ToDo

use of com.instructure.canvasapi.model.ToDo in project instructure-android by instructure.

the class ToDoAPI method mergeToDoUpcoming.

// ///////////////////////////////////////////////////////////////////////////
// Helper Methods
// ///////////////////////////////////////////////////////////////////////////
public static ArrayList<ToDo> mergeToDoUpcoming(ArrayList<ToDo> todos, ArrayList<ToDo> upcomingEvents) {
    if (todos == null && upcomingEvents == null) {
        return null;
    }
    if (todos == null) {
        todos = new ArrayList<ToDo>();
    }
    if (upcomingEvents == null) {
        upcomingEvents = new ArrayList<ToDo>();
    }
    // Add all Assignment ids from TODO
    HashMap<Long, Boolean> assignmentIds = new HashMap<Long, Boolean>();
    for (ToDo toDo : todos) {
        if (toDo.getAssignment() != null) {
            assignmentIds.put(toDo.getAssignment().getId(), true);
        }
    }
    // If the hashmap contains any assignment ids from Upcoming, it's a duplicate
    Iterator<ToDo> iterator = upcomingEvents.iterator();
    while (iterator.hasNext()) {
        ToDo current = iterator.next();
        Assignment assignment = current.getScheduleItem().getAssignment();
        if (assignment != null && assignmentIds.containsKey(assignment.getId())) {
            // We already have it in ToDo so remove the item.
            iterator.remove();
        }
    }
    int todoIndex = 0;
    int upcomingIndex = 0;
    ArrayList<ToDo> merged = new ArrayList<ToDo>();
    while (todoIndex < todos.size() || upcomingIndex < upcomingEvents.size()) {
        // We only have upcoming left.
        if (todoIndex >= todos.size()) {
            List<ToDo> subset = upcomingEvents.subList(upcomingIndex, upcomingEvents.size());
            for (ToDo upcomming : subset) {
                merged.add(upcomming);
            }
            return merged;
        }
        // We only have todo left.
        if (upcomingIndex >= upcomingEvents.size()) {
            List<ToDo> subset = todos.subList(todoIndex, todos.size());
            for (ToDo td : subset) {
                merged.add(td);
            }
            return merged;
        }
        // We need to determine which one comes sooner.
        Date toDoDate;
        if (todos.get(todoIndex).getAssignment() == null) {
            toDoDate = null;
        } else {
            toDoDate = todos.get(todoIndex).getAssignment().getDueDate();
        }
        Date upcomingDate = upcomingEvents.get(upcomingIndex).getScheduleItem().getStartDate();
        // handle null cases first
        if (toDoDate == null) {
            merged.add(upcomingEvents.get(upcomingIndex));
            upcomingIndex++;
        } else if (upcomingDate == null) {
            merged.add(todos.get(todoIndex));
            todoIndex++;
        } else if (toDoDate.before(upcomingDate)) {
            merged.add(todos.get(todoIndex));
            todoIndex++;
        } else {
            merged.add(upcomingEvents.get(upcomingIndex));
            upcomingIndex++;
        }
    }
    // Should never get here.
    return merged;
}
Also used : Assignment(com.instructure.canvasapi.model.Assignment) ToDo(com.instructure.canvasapi.model.ToDo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

Assignment (com.instructure.canvasapi.model.Assignment)2 ToDo (com.instructure.canvasapi.model.ToDo)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1