Skip to content
Open
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 @@ -110,7 +110,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
}

requireActivity().runOnUiThread(() -> {
onNoteLoaded(note);
Log.v("createAndLoadNote", "oldNote: id: " + note.getId() + " remote: " + note.getRemoteId());
Note updatedNote = repo.getNoteById$(note.getId()).getValue();
if (updatedNote != null) {
note = updatedNote;
}
Log.v("createAndLoadNote", "updatedNote: id: " + note.getId() + " remote: " + note.getRemoteId());
onNoteLoaded(note); // TODO update note
requireActivity().invalidateOptionsMenu();

if (listener != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import it.niedermann.owncloud.notes.shared.model.ApiVersion
import it.niedermann.owncloud.notes.shared.model.ISyncCallback
import it.niedermann.owncloud.notes.shared.util.ExtendedFabUtil
import it.niedermann.owncloud.notes.shared.util.rx.DisposableSet
import okio.IOException
import java.util.concurrent.TimeUnit

class NoteDirectEditFragment : BaseNoteFragment(), Branded {
Expand Down Expand Up @@ -155,29 +154,56 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
return
}

Log.d(TAG, "onNoteLoaded() called")
Log.d("createAndLoadNote", "onNoteLoaded() called")
val newNoteParam = arguments?.getSerializable(PARAM_NEWNOTE) as Note?
if (newNoteParam != null || note.remoteId == null) {
createAndLoadNote(note)
} else {
loadNoteInWebView(note)
Log.v(
"createAndLoadNote",
"on note loaded: newNote null: ${newNoteParam == null}; remoteId: ${note.remoteId}"
)

if (repo.isSyncPossible) {
val acc = repo.getAccountByName(account.name)
repo.scheduleSync(acc, false)

// todo wait until sync is done, how?
repo.syncStatus.observe(this) { state: Boolean ->
if (!state) {
// update note
val updatedNote = repo.getNoteById(note.id)

Log.v(
"createAndLoadNote",
"on note loaded: updatedNote null : ${newNoteParam == null}; remoteId: ${note.remoteId}"
)

//if (updatedNote != null || updatedNote?.remoteId == null) {
// createAndLoadNote(note)
//} else {
loadNoteInWebView(updatedNote)
//}
}
}
Log.v("createAndLoadNote", "sync started")
}
}


// TODO test with a real, 'slow" server!
private fun createAndLoadNote(newNote: Note) {
Log.d(TAG, "createAndLoadNote() called")
Log.d("createAndLoadNote", "createAndLoadNote() called with internal id: ${newNote.id}")
val noteCreateDisposable = Single
.fromCallable {
try {
val response = notesApi.createNote(newNote).execute()
response.body()
} catch (e: IOException) {
Log_OC.w(TAG, "Cant able to create a note: $e")
} catch (e: Exception) {
Log_OC.w("createAndLoadNote", "Cant able to create a note: $e")
null
}
}
.flatMap { createdNote ->
createdNote?.let {
createdNote.let {
Log_OC.d("createAndLoadNote", "created note on server: ${it.remoteId}")
repo.updateRemoteId(newNote.id, it.remoteId)
Single.fromCallable { repo.getNoteById(newNote.id) }
}
Expand All @@ -189,7 +215,7 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
}, { throwable ->
note = null
handleLoadError()
Log.e(TAG, "createAndLoadNote:", throwable)
Log.e("createAndLoadNote", "createAndLoadNote:", throwable)
})
disposables.add(noteCreateDisposable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
@SuppressWarnings("UnusedReturnValue")
public class NotesRepository {

private static final String TAG = NotesRepository.class.getSimpleName();
private static final String TAG = "createAndLoadNote";

private static NotesRepository instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void run() {
* Push local changes: for each locally created/edited/deleted Note, use NotesClient in order to push the changed to the server.
*/
private boolean pushLocalChanges() {
Log.d(TAG, "pushLocalChanges()");
Log.d("createAndLoadNote", "pushLocalChanges()");

boolean success = true;
final var notes = repo.getLocalModifiedNotes(localAccount.getId());
Expand All @@ -135,7 +135,7 @@ private boolean pushLocalChanges() {
throw new Exception("Server returned null after editing \"" + note.getTitle() + "\" (#" + note.getId() + ")");
}
} else if (editResponse.code() == HTTP_NOT_FOUND) {
Log.v(TAG, " ...Note does no longer exist on server → recreate");
Log.v("createAndLoadNote", " ...Note does no longer exist on server → recreate");
final var createResponse = notesAPI.createNote(note).execute();
if (createResponse.isSuccessful()) {
remoteNote = createResponse.body();
Expand All @@ -150,16 +150,22 @@ private boolean pushLocalChanges() {
throw new Exception(editResponse.message());
}
} else {
Log.v(TAG, " ...Note does not have a remoteId yet → create");
Log.v("createAndLoadNote", " ...Note does not have a remoteId yet → create");
final var createResponse = notesAPI.createNote(note).execute();
if (createResponse.isSuccessful()) {
remoteNote = createResponse.body();
if (remoteNote == null) {
Log.e(TAG, " ...Tried to create \"" + note.getTitle() + "\" (#" + note.getId() + ") but the server response was null.");
Log.e("createAndLoadNote", " ...Tried to create \"" + note.getTitle() + "\" (#" + note.getId() + ") but the server response was null.");
throw new Exception("Server returned null after creating \"" + note.getTitle() + "\" (#" + note.getId() + ")");

}
Log.v("createAndLoadNote", "remoteNote remoteId: " + remoteNote.getRemoteId());
Log.v("createAndLoadNote", "internal id: " + note.getId() + " remote: " + note.getRemoteId());
repo.updateRemoteId(note.getId(), remoteNote.getRemoteId());
Note updatedNote = repo.getNoteById(note.getId());
Log.v("createAndLoadNote", "internal id: " + updatedNote.getId() + " remote: " + updatedNote.getRemoteId());
} else {
Log.v("createAndLoadNote", "create failed: " + createResponse.message());
throw new Exception(createResponse.message());
}
}
Expand Down
Loading