Android Open Source - commande-godo Todos Data Source






From Project

Back to project page commande-godo.

License

The source code is released under:

MIT License

If you think the Android project commande-godo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.ualberta.commande.android.commande_godo.data;
/*w w  w .j a va  2s.c om*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

public class TodosDataSource {
  
  private Context context;
  public List<TodoItem> todos;
  
  public TodosDataSource(Context context) {
    this.context = context;
    this.todos = this.getTodos();
  }
  
  public List<TodoItem> getTodos() {
    List<TodoItem> todos;
    try {
          todos = this.loadTodos();
    } catch (Exception e) {
      // TODO: handle exception
      Log.i("TODOS", e.getMessage());
      todos = this.getEmptyList();
    }
    
    return todos;
  }
  
  private List<TodoItem> getEmptyList() {
    List<TodoItem> todos = new ArrayList<TodoItem>();
    return todos;
  }
  
  private List<TodoItem> loadTodos() throws IOException {
    
    FileInputStream fis = this.context.openFileInput("todos");
    List<TodoItem> todoList = TodoJsonReader.readJsonStream(fis);
    fis.close();
    return todoList;
  }
  
  public boolean update(TodoItem updatedTodo) {
    for (TodoItem originalTodo : this.todos) {
      if ( originalTodo.getKey().equals(updatedTodo.getKey()) ) {
        originalTodo = updatedTodo;
        this.saveTodos();
        return true;
      }
    }
    
    return false;
  }
  
  public boolean remove(TodoItem todo) {
    this.todos.remove(todo);
    this.saveTodos();
    return true;
  }
  
  public boolean saveTodos() {
    try {
          this.writeTodos(this.todos);
    } catch (Exception e) {
      // TODO: handle exception
      Log.i("TODOS", e.getMessage());
    }
    return true;
  }
  
  private boolean writeTodos(List<TodoItem> todos) throws IOException {
    
    // http://www.lynda.com/Android-tutorials/Creating-reading-JSON-data-files/112584/121170-4.html, Sept 14, 2014
    FileOutputStream fos = this.context.openFileOutput("todos", Context.MODE_PRIVATE);
    TodoJsonWriter.writeJsonStream(fos, todos);
    fos.close();
    return true;
  }
  
  public int getStat(String statCode) {
    
    int count = 0;
    
    if (statCode.equals("tic")) {
      for (TodoItem todo : todos) {
        if (todo.isCompleted()) {
          count ++;
        }
      }
      return count;
    } 
    
    if (statCode.equals("tiu")) {
      for (TodoItem todo : todos) {
        if (!todo.isCompleted()) {
          count ++;
        }
      }
      return count;
    } 

    if (statCode.equals("tia")) {
      for (TodoItem todo : todos) {
        if (todo.isArchived()) {
          count ++;
        }
      }
      return count;
    } 

    if (statCode.equals("aic")) {
      for (TodoItem todo : todos) {
        if (todo.isCompleted() && todo.isArchived()) {
          count ++;
        }
      }
      return count;
    } 

    if (statCode.equals("aiu")) {
      for (TodoItem todo : todos) {
        if (!todo.isCompleted() && todo.isArchived()) {
          count ++;
        }
      }
      return count;
    } 
    
    return -1;
  }
  
}




Java Source Code List

ca.ualberta.commande.android.commande_godo.MainActivity.java
ca.ualberta.commande.android.commande_godo.NewTodoActivity.java
ca.ualberta.commande.android.commande_godo.SummaryActivity.java
ca.ualberta.commande.android.commande_godo.TodoAdapter.java
ca.ualberta.commande.android.commande_godo.TodoEmailer.java
ca.ualberta.commande.android.commande_godo.data.TodoItem.java
ca.ualberta.commande.android.commande_godo.data.TodoJsonReader.java
ca.ualberta.commande.android.commande_godo.data.TodoJsonWriter.java
ca.ualberta.commande.android.commande_godo.data.TodosDataSource.java