Android Open Source - EverToDo Task Bag Impl






From Project

Back to project page EverToDo.

License

The source code is released under:

GNU General Public License

If you think the Android project EverToDo 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

/**
 */*from  w  w  w.  ja va  2 s.  co m*/
 * Todo.txt Touch/src/com/todotxt/todotxttouch/task/TaskBagImpl.java
 *
 * Copyright (c) 2011 Tim Barlotta
 *
 * LICENSE:
 *
 * This file is part of Todo.txt Touch, an Android app for managing your todo.txt file (http://todotxt.com).
 *
 * Todo.txt Touch 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 2 of the License, or (at your option) any
 * later version.
 *
 * Todo.txt Touch 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 Todo.txt Touch.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * @author Tim Barlotta <tim[at]barlotta[dot]net>
 * @author Tormod Haugen
 * @copyright 2011 Tim Barlotta
 */
package com.todotxt.todotxttouch.task;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.SharedPreferences;
import android.util.Log;

import com.todotxt.todotxttouch.remote.RemoteClientManager;
import com.todotxt.todotxttouch.util.TaskIo;

/**
 * Implementation of the TaskBag interface
 * 
 * @author Tim Barlotta
 */
class TaskBagImpl implements TaskBag {
  private static final String TAG = TaskBagImpl.class.getSimpleName();
  private Preferences preferences;
  private final LocalTaskRepository localRepository;
  private final RemoteClientManager remoteClientManager;
  private ArrayList<Task> tasks = new ArrayList<Task>();

  public TaskBagImpl(Preferences preferences,
      LocalTaskRepository localRepository,
      RemoteClientManager remoteClientManager) {
    this.preferences = preferences;
    this.localRepository = localRepository;
    this.remoteClientManager = remoteClientManager;
  }

  public void updatePreferences(TaskBagImpl.Preferences preferences) {
    this.preferences = preferences;
  }

  @Override
  public void reload() {
    localRepository.init();
    this.tasks = localRepository.load();
  }

  @Override
  public int size() {
    return tasks.size();
  }

  @Override
  public List<Task> getTasks() {
    return getTasks(null, null);
  }

  @Override
  public List<Task> getTasks(Filter<Task> filter, Comparator<Task> comparator) {
    ArrayList<Task> localTasks = new ArrayList<Task>();
    if (filter != null) {
      for (Task t : tasks) {
        if (filter.apply(t)) {
          localTasks.add(t);
        }
      }
    } else {
      localTasks.addAll(tasks);
    }

    if (comparator == null) {
      comparator = Sort.PRIORITY_DESC.getComparator();
    }

    Collections.sort(localTasks, comparator);

    return localTasks;
  }

  @Override
  public void addAsTask(String input) {
    try {
      reload();
      Task task = new Task(tasks.size(), input,
          (preferences.isPrependDateEnabled() ? new Date() : null));
      tasks.add(task);
      localRepository.store(tasks);
    } catch (Exception e) {
      throw new TaskPersistException("An error occurred while adding {"
          + input + "}", e);
    }
  }

  @Override
  public void update(Task task) {
    try {
      reload();
      Task found = TaskBagImpl.find(tasks, task);
      if (found != null) {
        task.copyInto(found);
        Log.i(TAG, "copied into found {" + found + "}");
        localRepository.store(tasks);
      } else {
        throw new TaskPersistException("Task not found, not updated");
      }
    } catch (Exception e) {
      throw new TaskPersistException(
          "An error occurred while updating Task {" + task + "}", e);
    }
  }

  @Override
  public void delete(Task task) {
    try {
      reload();
      Task found = TaskBagImpl.find(tasks, task);
      if (found != null) {
        tasks.remove(found);
        localRepository.store(tasks);
      } else {
        throw new TaskPersistException("Task not found, not deleted");
      }
    } catch (Exception e) {
      throw new TaskPersistException(
          "An error occurred while deleting Task {" + task + "}", e);
    }
  }

  /* REMOTE APIS */
  @Override
  public void pushToRemote() {
    pushToRemote(false);
  }

  @Override
  public void pushToRemote(boolean overridePreference) {
    if (!this.preferences.isWorkOfflineEnabled() || overridePreference) {
      remoteClientManager.getRemoteClient().pushTodo(
          LocalFileTaskRepository.TODO_TXT_FILE);
    }
  }

  @Override
  public void pullFromRemote() {
    pullFromRemote(false);
  }

  @Override
  public void pullFromRemote(boolean overridePreference) {
    try {
      if (!this.preferences.isWorkOfflineEnabled() || overridePreference) {
        File remoteFile = remoteClientManager.getRemoteClient()
            .pullTodo();
        if (remoteFile != null && remoteFile.exists()) {
          ArrayList<Task> remoteTasks = TaskIo
              .loadTasksFromFile(remoteFile);
          localRepository.store(remoteTasks);
          reload();
        }
      }
    } catch (IOException e) {
      throw new TaskPersistException(
          "Error loading tasks from remote file", e);
    }
  }

  @Override
  public ArrayList<Priority> getPriorities() {
    // TODO cache this after reloads?
    Set<Priority> res = new HashSet<Priority>();
    for (Task item : tasks) {
      res.add(item.getPriority());
    }
    ArrayList<Priority> ret = new ArrayList<Priority>(res);
    Collections.sort(ret);
    return ret;
  }

  @Override
  public ArrayList<String> getContexts() {
    // TODO cache this after reloads?
    Set<String> res = new HashSet<String>();
    for (Task item : tasks) {
      res.addAll(item.getContexts());
    }
    ArrayList<String> ret = new ArrayList<String>(res);
    Collections.sort(ret);
    return ret;
  }

  @Override
  public ArrayList<String> getProjects() {
    // TODO cache this after reloads?
    Set<String> res = new HashSet<String>();
    for (Task item : tasks) {
      res.addAll(item.getProjects());
    }
    ArrayList<String> ret = new ArrayList<String>(res);
    Collections.sort(ret);
    return ret;
  }

  private static Task find(List<Task> tasks, Task task) {
    for (Task task2 : tasks) {
      if (task2.getText().equals(task.getOriginalText())
          && task2.getPriority() == task.getOriginalPriority()) {
        return task2;
      }
    }
    return null;
  }

  public static class Preferences {
    private final SharedPreferences sharedPreferences;

    public Preferences(SharedPreferences sharedPreferences) {
      this.sharedPreferences = sharedPreferences;
    }

    public boolean isUseWindowsLineBreaksEnabled() {
      return sharedPreferences.getBoolean("linebreakspref", false);
    }

    public boolean isPrependDateEnabled() {
      return sharedPreferences.getBoolean("todotxtprependdate", false);
    }

    public boolean isWorkOfflineEnabled() {
      return sharedPreferences.getBoolean("workofflinepref", false);
    }
  }
}




Java Source Code List

com.todotxt.todotxttouch.AddTask.java
com.todotxt.todotxttouch.Constants.java
com.todotxt.todotxttouch.Filter.java
com.todotxt.todotxttouch.HelpActivity.java
com.todotxt.todotxttouch.LoginScreen.java
com.todotxt.todotxttouch.Preferences.java
com.todotxt.todotxttouch.TodoApplication.java
com.todotxt.todotxttouch.TodoException.java
com.todotxt.todotxttouch.TodoTxtTouch.java
com.todotxt.todotxttouch.remote.Client.java
com.todotxt.todotxttouch.remote.DropboxFileRemoteException.java
com.todotxt.todotxttouch.remote.DropboxLoginAsyncTask.java
com.todotxt.todotxttouch.remote.DropboxRemoteClient.java
com.todotxt.todotxttouch.remote.LocalClient.java
com.todotxt.todotxttouch.remote.RemoteClientManager.java
com.todotxt.todotxttouch.remote.RemoteClient.java
com.todotxt.todotxttouch.remote.RemoteException.java
com.todotxt.todotxttouch.remote.RemoteLoginTask.java
com.todotxt.todotxttouch.task.AndFilter.java
com.todotxt.todotxttouch.task.ByContextFilter.java
com.todotxt.todotxttouch.task.ByPriorityFilter.java
com.todotxt.todotxttouch.task.ByProjectFilter.java
com.todotxt.todotxttouch.task.ByTextFilter.java
com.todotxt.todotxttouch.task.ContextParser.java
com.todotxt.todotxttouch.task.FilterFactory.java
com.todotxt.todotxttouch.task.Filter.java
com.todotxt.todotxttouch.task.LocalFileTaskRepository.java
com.todotxt.todotxttouch.task.LocalTaskRepository.java
com.todotxt.todotxttouch.task.OrFilter.java
com.todotxt.todotxttouch.task.PriorityTextSplitter.java
com.todotxt.todotxttouch.task.Priority.java
com.todotxt.todotxttouch.task.ProjectParser.java
com.todotxt.todotxttouch.task.Sort.java
com.todotxt.todotxttouch.task.TaskBagFactory.java
com.todotxt.todotxttouch.task.TaskBagImpl.java
com.todotxt.todotxttouch.task.TaskBag.java
com.todotxt.todotxttouch.task.TaskPersistException.java
com.todotxt.todotxttouch.task.Task.java
com.todotxt.todotxttouch.task.TextSplitter.java
com.todotxt.todotxttouch.util.CursorPositionCalculator.java
com.todotxt.todotxttouch.util.RelativeDate.java
com.todotxt.todotxttouch.util.Strings.java
com.todotxt.todotxttouch.util.TaskIo.java
com.todotxt.todotxttouch.util.Util.java