Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
public class EditNoteActivity extends AppCompatActivity {

public static final String PARAM_NOTE = "note";
public static final String PARAM_ORIGINAL_NOTE = "original_note";
public static final String PARAM_NOTE_POSITION = "note_position";

private static final String LOG_TAG = "EditNote/SAVE";
private static final long DELAY = 2000; // in ms
private static final long DELAY_AFTER_SYNC = 5000; // in ms

private EditText content = null;
private DBNote note = null;
private DBNote note, originalNote;
private int notePosition = 0;
private Timer timer, timerNextSync;
private boolean saveActive = false;
Expand All @@ -44,11 +45,12 @@ protected void onCreate(final Bundle savedInstanceState) {
setContentView(R.layout.activity_edit);
if (savedInstanceState == null) {
Log.d(getClass().getSimpleName(), "Starting from Intent");
note = (DBNote) getIntent().getSerializableExtra(PARAM_NOTE);
note = originalNote = (DBNote) getIntent().getSerializableExtra(PARAM_NOTE);
notePosition = getIntent().getIntExtra(PARAM_NOTE_POSITION, 0);
} else {
Log.d(getClass().getSimpleName(), "Starting from SavedState");
note = (DBNote) savedInstanceState.getSerializable(PARAM_NOTE);
originalNote = (DBNote) savedInstanceState.getSerializable(PARAM_ORIGINAL_NOTE);
notePosition = savedInstanceState.getInt(PARAM_NOTE_POSITION);
}
content = (EditText) findViewById(R.id.editContent);
Expand Down Expand Up @@ -102,13 +104,14 @@ public void run() {
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putSerializable(PARAM_NOTE, note);
outState.putSerializable(PARAM_ORIGINAL_NOTE, originalNote);
outState.putInt(PARAM_NOTE_POSITION, notePosition);
super.onSaveInstanceState(outState);
}

@Override
public void onBackPressed() {
saveAndClose();
saveAndClose(true);
}

/**
Expand All @@ -126,13 +129,17 @@ public boolean onCreateOptionsMenu(Menu menu) {
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
NoteSQLiteOpenHelper db;
switch (item.getItemId()) {
case android.R.id.home:
saveAndClose();
saveAndClose(true);
return true;
case R.id.menu_cancel:
Log.d(LOG_TAG, "CANCEL: changed: "+note);
Log.d(LOG_TAG, "CANCEL: original: "+originalNote);
note = db.updateNoteAndSync(originalNote, null, null);
saveAndClose(false);
return true;
case R.id.menu_delete:
db = new NoteSQLiteOpenHelper(this);
db.deleteNoteAndSync(note.getId());
Intent data = new Intent();
data.putExtra(PARAM_NOTE_POSITION, notePosition);
Expand Down Expand Up @@ -168,7 +175,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
/**
* Saves all changes and closes the Activity
*/
private void saveAndClose() {
private void saveAndClose(boolean save) {
content.setEnabled(false);
if(timer!=null) {
timer.cancel();
Expand All @@ -178,7 +185,12 @@ private void saveAndClose() {
timerNextSync.cancel();
timerNextSync = null;
}
saveData(null);
if(save) {
Log.d(LOG_TAG, "saveAndClose with SAVE");
saveData(null);
} else {
Log.d(LOG_TAG, "saveAndClose WITHOUT save");
}
Intent data = new Intent();
data.setAction(Intent.ACTION_VIEW);
data.putExtra(PARAM_NOTE, note);
Expand Down Expand Up @@ -261,6 +273,7 @@ public void run() {
* @param callback
*/
private void saveData(ICallback callback) {
Log.d(LOG_TAG, "saveData()");
note = db.updateNoteAndSync(note, getContent(), callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,18 @@ public List<DBNote> getLocalModifiedNotes() {
* The title is derived from the new content automatically, and modified date as well as DBStatus are updated, too -- if the content differs to the state in the database.
*
* @param oldNote Note to be changed
* @param newContent New content
* @param newContent New content. If this is <code>null</code>, then <code>oldNote</code> is saved again (useful for undoing changes).
* @param callback When the synchronization is finished, this callback will be invoked (optional).
* @return changed note if differs from database, otherwise the old note.
*/
public DBNote updateNoteAndSync(DBNote oldNote, String newContent, ICallback callback) {
debugPrintFullDB();
DBNote newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNoteTitle(newContent), newContent, DBStatus.LOCAL_EDITED);
DBNote newNote;
if(newContent==null) {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), DBStatus.LOCAL_EDITED);
} else {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNoteTitle(newContent), newContent, DBStatus.LOCAL_EDITED);
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(key_status, newNote.getStatus().getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ private void pushLocalChanges() {
// if note is not new, try to edit it.
if (note.getRemoteId()>0) {
Log.d(getClass().getSimpleName(), " ...try to edit");
remoteNote = client.editNote(note.getRemoteId(), note.getContent());
remoteNote = client.editNote(note);
}
// However, the note may be deleted on the server meanwhile; or was never synchronized -> (re)create
// Please note, thas dbHelper.updateNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
if (remoteNote == null) {
Log.d(getClass().getSimpleName(), " ...Note does not exist on server -> (re)create");
remoteNote = client.createNote(note.getContent());
remoteNote = client.createNote(note);
dbHelper.updateNote(note.getId(), remoteNote, note);
} else {
dbHelper.updateNote(note.getId(), remoteNote, note);
Expand Down
132 changes: 38 additions & 94 deletions app/src/main/java/it/niedermann/owncloud/notes/util/NotesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,130 +41,74 @@ public NotesClient(String url, String username, String password) {
this.password = password;
}

public List<OwnCloudNote> getNotes() throws JSONException,
IOException {
List<OwnCloudNote> notesList = new ArrayList<>();
JSONArray notes = new JSONArray(requestServer("notes", METHOD_GET, null));
private OwnCloudNote getNoteFromJSON(JSONObject json) throws JSONException {
long noteId = 0;
String noteTitle = "";
String noteContent = "";
Calendar noteModified = null;
JSONObject currentItem;
for (int i = 0; i < notes.length(); i++) {
currentItem = notes.getJSONObject(i);
if (!json.isNull(key_id)) {
noteId = json.getLong(key_id);
}
if (!json.isNull(key_title)) {
noteTitle = json.getString(key_title);
}
if (!json.isNull(key_content)) {
noteContent = json.getString(key_content);
}
if (!json.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance();
noteModified
.setTimeInMillis(json.getLong(key_modified) * 1000);
}
return new OwnCloudNote(noteId, noteModified, noteTitle, noteContent);
}

if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong(key_id);
}
if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString(key_title);
}
if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString(key_content);
}
if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance();
noteModified
.setTimeInMillis(currentItem.getLong(key_modified) * 1000);
}
notesList
.add(new OwnCloudNote(noteId, noteModified, noteTitle, noteContent));
public List<OwnCloudNote> getNotes() throws JSONException, IOException {
List<OwnCloudNote> notesList = new ArrayList<>();
JSONArray notes = new JSONArray(requestServer("notes", METHOD_GET, null));
for (int i = 0; i < notes.length(); i++) {
JSONObject json = notes.getJSONObject(i);
notesList.add(getNoteFromJSON(json));
}
return notesList;
}

/**
* Fetches a Note by ID from Server
* TODO Maybe fetch only id, title and modified from server until a note has been opened?
*
* @param id long - ID of the wanted note
* @return Requested Note
* @throws JSONException
* @throws IOException
*/
@SuppressWarnings("unused")
public OwnCloudNote getNoteById(long id) throws
JSONException, IOException {
long noteId = 0;
String noteTitle = "";
String noteContent = "";
Calendar noteModified = null;
JSONObject currentItem = new JSONObject(
requestServer("notes/" + id, METHOD_GET, null));
public OwnCloudNote getNoteById(long id) throws JSONException, IOException {
JSONObject json = new JSONObject(requestServer("notes/" + id, METHOD_GET, null));
return getNoteFromJSON(json);
}

if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong(key_id);
}
if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString(key_title);
}
if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString(key_content);
}
if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance();
noteModified
.setTimeInMillis(currentItem.getLong(key_modified) * 1000);
}
return new OwnCloudNote(noteId, noteModified, noteTitle, noteContent);
private OwnCloudNote putNote(OwnCloudNote note, String path) throws JSONException, IOException {
JSONObject paramObject = new JSONObject();
paramObject.accumulate(key_content, note.getContent());
paramObject.accumulate(key_modified, note.getModified().getTimeInMillis()/1000);
JSONObject json = new JSONObject(requestServer(path, METHOD_PUT, paramObject));
return getNoteFromJSON(json);
}

/**
* Creates a Note on the Server
*
* @param content String - Content of the new Note
* @param note {@link OwnCloudNote} - the new Note
* @return Created Note including generated Title, ID and lastModified-Date
* @throws JSONException
* @throws IOException
*/
public OwnCloudNote createNote(String content) throws
JSONException, IOException {
long noteId = 0;
String noteTitle = "";
String noteContent = "";
Calendar noteModified = null;

JSONObject paramObject = new JSONObject();
paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer("notes", METHOD_POST,
paramObject));

if (!currentItem.isNull(key_id)) {
noteId = currentItem.getLong(key_id);
}
if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString(key_title);
}
if (!currentItem.isNull(key_content)) {
noteContent = currentItem.getString(key_content);
}
if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance();
noteModified
.setTimeInMillis(currentItem.getLong(key_modified) * 1000);
}
return new OwnCloudNote(noteId, noteModified, noteTitle, noteContent);
public OwnCloudNote createNote(OwnCloudNote note) throws JSONException, IOException {
return putNote(note, "notes");
}

public OwnCloudNote editNote(long noteId, String content)
throws JSONException, IOException {
String noteTitle = "";
Calendar noteModified = null;

JSONObject paramObject = new JSONObject();
paramObject.accumulate(key_content, content);
JSONObject currentItem = new JSONObject(requestServer(
"notes/" + noteId, METHOD_PUT, paramObject));

if (!currentItem.isNull(key_title)) {
noteTitle = currentItem.getString(key_title);
}
if (!currentItem.isNull(key_modified)) {
noteModified = GregorianCalendar.getInstance();
noteModified
.setTimeInMillis(currentItem.getLong(key_modified) * 1000);
}
return new OwnCloudNote(noteId, noteModified, noteTitle, content);
public OwnCloudNote editNote(OwnCloudNote note) throws JSONException, IOException {
return putNote(note, "notes/" + note.getRemoteId());
}

public void deleteNote(long noteId) throws
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/menu/menu_note_list_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
android:orderInCategory="110"
app:showAsAction="never"
android:title="@string/menu_copy"/-->
<item
android:id="@+id/menu_cancel"
android:icon="@drawable/ic_action_cancel"
android:orderInCategory="110"
android:title="@string/menu_cancel"
app:showAsAction="never" />
<item
android:id="@+id/menu_delete"
android:icon="@drawable/ic_action_delete"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="menu_delete">Löschen</string>
<string name="menu_copy">Kopieren</string>
<string name="menu_edit">Bearbeiten</string>
<string name="menu_cancel">Abbrechen</string>
<string name="menu_preview">Vorschau</string>
<string name="menu_share">Teilen</string>
<string name="menu_about">Über</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<string name="menu_delete">Delete</string>
<string name="menu_copy">Copy</string>
<string name="menu_edit">Edit</string>
<string name="menu_cancel">Cancel</string>
<string name="menu_preview">Preview</string>
<string name="menu_share">Share</string>
<string name="menu_about">About</string>
Expand Down