@@ -45,7 +45,6 @@ import it.niedermann.owncloud.notes.shared.model.ApiVersion
4545import it.niedermann.owncloud.notes.shared.model.ISyncCallback
4646import it.niedermann.owncloud.notes.shared.util.ExtendedFabUtil
4747import it.niedermann.owncloud.notes.shared.util.rx.DisposableSet
48- import okio.IOException
4948import java.util.concurrent.TimeUnit
5049
5150class NoteDirectEditFragment : BaseNoteFragment (), Branded {
@@ -155,29 +154,56 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
155154 return
156155 }
157156
158- Log .d(TAG , " onNoteLoaded() called" )
157+ Log .d(" createAndLoadNote " , " onNoteLoaded() called" )
159158 val newNoteParam = arguments?.getSerializable(PARAM_NEWNOTE ) as Note ?
160- if (newNoteParam != null || note.remoteId == null ) {
161- createAndLoadNote(note)
162- } else {
163- loadNoteInWebView(note)
159+ Log .v(
160+ " createAndLoadNote" ,
161+ " on note loaded: newNote null: ${newNoteParam == null } ; remoteId: ${note.remoteId} "
162+ )
163+
164+ if (repo.isSyncPossible) {
165+ val acc = repo.getAccountByName(account.name)
166+ repo.scheduleSync(acc, false )
167+
168+ // todo wait until sync is done, how?
169+ repo.syncStatus.observe(this ) { state: Boolean ->
170+ if (! state) {
171+ // update note
172+ val updatedNote = repo.getNoteById(note.id)
173+
174+ Log .v(
175+ " createAndLoadNote" ,
176+ " on note loaded: updatedNote null : ${newNoteParam == null } ; remoteId: ${note.remoteId} "
177+ )
178+
179+ // if (updatedNote != null || updatedNote?.remoteId == null) {
180+ // createAndLoadNote(note)
181+ // } else {
182+ loadNoteInWebView(updatedNote)
183+ // }
184+ }
185+ }
186+ Log .v(" createAndLoadNote" , " sync started" )
164187 }
165188 }
166189
190+
191+ // TODO test with a real, 'slow" server!
167192 private fun createAndLoadNote (newNote : Note ) {
168- Log .d(TAG , " createAndLoadNote() called" )
193+ Log .d(" createAndLoadNote " , " createAndLoadNote() called with internal id: ${newNote.id} " )
169194 val noteCreateDisposable = Single
170195 .fromCallable {
171196 try {
172197 val response = notesApi.createNote(newNote).execute()
173198 response.body()
174- } catch (e: IOException ) {
175- Log_OC .w(TAG , " Cant able to create a note: $e " )
199+ } catch (e: Exception ) {
200+ Log_OC .w(" createAndLoadNote " , " Cant able to create a note: $e " )
176201 null
177202 }
178203 }
179204 .flatMap { createdNote ->
180- createdNote?.let {
205+ createdNote.let {
206+ Log_OC .d(" createAndLoadNote" , " created note on server: ${it.remoteId} " )
181207 repo.updateRemoteId(newNote.id, it.remoteId)
182208 Single .fromCallable { repo.getNoteById(newNote.id) }
183209 }
@@ -189,7 +215,7 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
189215 }, { throwable ->
190216 note = null
191217 handleLoadError()
192- Log .e(TAG , " createAndLoadNote:" , throwable)
218+ Log .e(" createAndLoadNote " , " createAndLoadNote:" , throwable)
193219 })
194220 disposables.add(noteCreateDisposable)
195221 }
0 commit comments