Skip to content

Commit

Permalink
Creates remote todo.txt if it does not exist
Browse files Browse the repository at this point in the history
Closes todotxt#46

Improves message when creating remote todo.txt
Saves local tasks when sync-ing after changing path, if no todo.txt remotely.
Still doesn't sync on startup (except first run)
  • Loading branch information
tormodh authored and ginatrapani committed Jan 19, 2011
1 parent 4a62e36 commit 3c0ead2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
26 changes: 18 additions & 8 deletions src/com/todotxt/todotxttouch/DropboxFetchAsyncTask.java
Expand Up @@ -53,16 +53,19 @@ protected void onPreExecute() {
@Override
protected Boolean doInBackground(Void... params) {
try {
DropboxAPI api = ((TodoApplication) m_act.getApplication()).getAPI();
m_remoteFile = api.getFileStream(Constants.DROPBOX_MODUS, m_app.getRemoteFileAndPath(), null);

if ( m_remoteFile.isError() ) {
if ( 404 == m_remoteFile.httpCode ) {
api.putFile(Constants.DROPBOX_MODUS, m_app.getRemotePath(), Constants.TODOFILE);
DropboxAPI api = ((TodoApplication) m_act.getApplication())
.getAPI();
m_remoteFile = api.getFileStream(Constants.DROPBOX_MODUS,
m_app.getRemoteFileAndPath(), null);

if (m_remoteFile.isError()) {
if (404 == m_remoteFile.httpCode) {
api.putFile(Constants.DROPBOX_MODUS, m_app.getRemotePath(),
Constants.TODOFILE);
}
return false;
}

m_act.m_tasks = TodoUtil.loadTasksFromStream(m_remoteFile.is);
TodoUtil.writeToFile(m_act.m_tasks, Constants.TODOFILE);
return true;
Expand All @@ -78,7 +81,14 @@ protected void onPostExecute(Boolean result) {
m_act.setFilteredTasks(false);
Log.d(TodoTxtTouch.TAG, "populateFromUrl size=" + m_act.m_tasks.size());
if (!result) {
Util.showToastLong(m_act, "Sync failed: " + (null==m_remoteFile?"Null remote file":m_remoteFile.httpReason));
if (null != m_remoteFile && 404 == m_remoteFile.httpCode) {
Util.showToastLong(m_act,
"Added: " + m_app.getRemoteFileAndPath());
} else {
Util.showToastLong(m_act, "Sync failed: "
+ (null == m_remoteFile ? "Null remote file"
: m_remoteFile.httpReason));
}
} else {
Util.showToastShort(m_act, m_act.m_tasks.size() + " items");
}
Expand Down
17 changes: 8 additions & 9 deletions src/com/todotxt/todotxttouch/TodoApplication.java
Expand Up @@ -76,13 +76,14 @@ private boolean authenticate() {
return true;
}
clearKeys();
m_loggedIn = false;
return false;
}

private void clearKeys() {
// TODO Auto-generated method stub

Editor editor = m_prefs.edit();
editor.clear();
editor.commit();
m_loggedIn = false;
}

private String[] getAuthKeys() {
Expand All @@ -99,11 +100,8 @@ public void onTerminate() {

public void unlinkDropbox() {
Log.i(TAG, "Clearing current user data!");
Editor editor = m_prefs.edit();
editor.clear();
editor.commit();
getAPI().deauthenticate();
m_loggedIn = false;
clearKeys();
Constants.TODOFILE.delete();
Constants.TODOFILETMP.delete();
}
Expand Down Expand Up @@ -135,9 +133,10 @@ public void setConfig(Config authenticate) {
}

public String getRemotePath() {
return m_prefs.getString("todotxtpath", getResources().getString(R.string.TODOTXTPATH_defaultPath));
return m_prefs.getString("todotxtpath",
getResources().getString(R.string.TODOTXTPATH_defaultPath));
}

public String getRemoteFileAndPath() {
return getRemotePath() + "/" + Constants.REMOTE_FILE;
}
Expand Down
26 changes: 18 additions & 8 deletions src/com/todotxt/todotxttouch/TodoTxtTouch.java
Expand Up @@ -121,18 +121,28 @@ public void onCreate(Bundle savedInstanceState) {

getListView().setOnCreateContextMenuListener(this);

initializeTasks();
}

private void initializeTasks() {
boolean firstrun = m_app.m_prefs.getBoolean(Constants.PREF_FIRSTRUN,
true);

if (firstrun) {
Log.i(TAG, "Initializing app");
Editor editor = m_app.m_prefs.edit();
editor.putBoolean(Constants.PREF_FIRSTRUN, false);
editor.commit();
if (getAPI().isAuthenticated()) {
populateFromExternal();
} else {
login();
try {
if (!Constants.TODOFILE.exists()) {
Util.createParentDirectory(Constants.TODOFILE);
Constants.TODOFILE.createNewFile();
}
} catch (Exception e) {
Log.e(TAG, "Error creating local files", e);
}

populateFromExternal();
} else {
populateFromFile();
}
Expand Down Expand Up @@ -442,9 +452,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
m_search = data.getStringExtra(Constants.EXTRA_SEARCH);
setFilteredTasks(false);
}
} else if ( requestCode == REQUEST_PREFERENCES) {
if ( resultCode == Preferences.RESULT_SYNC_LIST) {
populateFromExternal();
} else if (requestCode == REQUEST_PREFERENCES) {
if (resultCode == Preferences.RESULT_SYNC_LIST) {
initializeTasks();
}
}
}
Expand Down Expand Up @@ -619,7 +629,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
default:
holder.taskprio.setTextColor(res.getColor(R.color.black));
}
// hide line numbers unless show preference is checked
// hide line numbers unless show preference is checked
if (!m_app.m_prefs.getBoolean("showlinenumberspref", false)) {
holder.taskid.setTextColor(res.getColor(R.color.white));
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/todotxt/todotxttouch/TodoUtil.java
Expand Up @@ -77,7 +77,7 @@ public static ArrayList<Task> loadTasksFromFile() throws IOException {
BufferedReader in = null;
if (!Constants.TODOFILE.exists()) {
Log.w(TAG, Constants.TODOFILE.getAbsolutePath()
+ " does not exists!");
+ " does not exist!");
} else {
InputStream is = new FileInputStream(Constants.TODOFILE);
try {
Expand All @@ -102,7 +102,7 @@ public static ArrayList<Task> loadTasksFromFile() throws IOException {
public static void writeToFile(List<Task> tasks, File file) {
try {
if (!Util.isDeviceWritable()) {
throw new IOException("Device Not Writable!");
throw new IOException("Device is not writable!");
}
Util.createParentDirectory(file);
FileWriter fw = new FileWriter(file);
Expand Down

0 comments on commit 3c0ead2

Please sign in to comment.