Fix note creating

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky
2025-11-05 07:36:29 +01:00
parent 9dde22a72f
commit 7bde151210
4 changed files with 55 additions and 17 deletions

View File

@ -110,7 +110,13 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
}
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) {

View File

@ -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 {
@ -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) }
}
@ -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)
}

View File

@ -91,7 +91,7 @@ import retrofit2.Call;
@SuppressWarnings("UnusedReturnValue")
public class NotesRepository {
private static final String TAG = NotesRepository.class.getSimpleName();
private static final String TAG = "createAndLoadNote";
private static NotesRepository instance;

View File

@ -114,7 +114,7 @@ abstract class NotesServerSyncTask extends Thread {
* 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());
@ -135,7 +135,7 @@ abstract class NotesServerSyncTask extends Thread {
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();
@ -150,16 +150,22 @@ abstract class NotesServerSyncTask extends Thread {
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());
}
}