@@ -8,6 +8,7 @@ package it.niedermann.owncloud.notes.edit
88
99import android.annotation.SuppressLint
1010import android.net.http.SslError
11+ import android.os.Build
1112import android.os.Bundle
1213import android.util.Log
1314import android.view.LayoutInflater
@@ -48,7 +49,9 @@ import it.niedermann.owncloud.notes.shared.util.rx.DisposableSet
4849import okio.IOException
4950import java.util.concurrent.TimeUnit
5051
51- class NoteDirectEditFragment : BaseNoteFragment (), Branded {
52+ class NoteDirectEditFragment :
53+ BaseNoteFragment (),
54+ Branded {
5255 private var _binding : FragmentNoteDirectEditBinding ? = null
5356 private val binding: FragmentNoteDirectEditBinding ?
5457 get() = _binding
@@ -69,9 +72,7 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
6972 // for hiding / showing the fab
7073 private var scrollStart: Int = 0
7174
72- public override fun getScrollView (): ScrollView ? {
73- return null
74- }
75+ public override fun getScrollView (): ScrollView ? = null
7576
7677 override fun scrollToY (y : Int ) {
7778 // do nothing
@@ -137,14 +138,16 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
137138
138139 override fun onResume () {
139140 super .onResume()
140- val timeoutDisposable = Single .just(Unit )
141- .delay(LOAD_TIMEOUT_SECONDS , TimeUnit .SECONDS )
142- .map {
143- if (binding?.noteWebview?.isVisible == false ) {
144- Log .w(TAG , " Editor not loaded after $LOAD_TIMEOUT_SECONDS seconds" )
145- handleLoadError()
146- }
147- }.subscribe()
141+ val timeoutDisposable =
142+ Single
143+ .just(Unit )
144+ .delay(LOAD_TIMEOUT_SECONDS , TimeUnit .SECONDS )
145+ .map {
146+ if (binding?.noteWebview?.isVisible == false ) {
147+ Log .w(TAG , " Editor not loaded after $LOAD_TIMEOUT_SECONDS seconds" )
148+ handleLoadError()
149+ }
150+ }.subscribe()
148151 disposables.add(timeoutDisposable)
149152 }
150153
@@ -156,7 +159,12 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
156159 }
157160
158161 Log .d(TAG , " onNoteLoaded() called" )
159- val newNoteParam = arguments?.getSerializable(PARAM_NEWNOTE ) as Note ?
162+ val newNoteParam =
163+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .TIRAMISU ) {
164+ arguments?.getSerializable(PARAM_NEWNOTE ) as Note ?
165+ } else {
166+ arguments?.getSerializable(PARAM_NEWNOTE , Note ::class .java)
167+ }
160168 if (newNoteParam != null || note.remoteId == null ) {
161169 createAndLoadNote(note)
162170 } else {
@@ -166,31 +174,30 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
166174
167175 private fun createAndLoadNote (newNote : Note ) {
168176 Log .d(TAG , " createAndLoadNote() called" )
169- val noteCreateDisposable = Single
170- .fromCallable {
171- try {
172- val response = notesApi.createNote(newNote).execute()
173- response.body()
174- } catch (e: IOException ) {
175- Log_OC .w(TAG , " Cant able to create a note: $e " )
176- null
177- }
178- }
179- .flatMap { createdNote ->
180- createdNote?.let {
181- repo.updateRemoteId(newNote.id, it.remoteId)
182- Single .fromCallable { repo.getNoteById(newNote.id) }
183- }
184- }
185- .subscribeOn(Schedulers .io())
186- .observeOn(AndroidSchedulers .mainThread())
187- .subscribe({ createdNote ->
188- loadNoteInWebView(createdNote)
189- }, { throwable ->
190- note = null
191- handleLoadError()
192- Log .e(TAG , " createAndLoadNote:" , throwable)
193- })
177+ val noteCreateDisposable =
178+ Single
179+ .fromCallable {
180+ try {
181+ val response = notesApi.createNote(newNote).execute()
182+ response.body()
183+ } catch (e: IOException ) {
184+ Log_OC .w(TAG , " Cant able to create a note: $e " )
185+ null
186+ }
187+ }.flatMap { createdNote ->
188+ createdNote?.let {
189+ repo.updateRemoteId(newNote.id, it.remoteId)
190+ Single .fromCallable { repo.getNoteById(newNote.id) }
191+ }
192+ }.subscribeOn(Schedulers .io())
193+ .observeOn(AndroidSchedulers .mainThread())
194+ .subscribe({ createdNote ->
195+ loadNoteInWebView(createdNote)
196+ }, { throwable ->
197+ note = null
198+ handleLoadError()
199+ Log .e(TAG , " createAndLoadNote:" , throwable)
200+ })
194201 disposables.add(noteCreateDisposable)
195202 }
196203
@@ -199,29 +206,33 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
199206
200207 context?.let { context ->
201208 val repository = DirectEditingRepository .getInstance(context.applicationContext)
202- val urlDisposable = repository.getDirectEditingUrl(account, note)
203- .observeOn(AndroidSchedulers .mainThread()).subscribe({ url ->
204- url?.let {
205- if (BuildConfig .DEBUG ) {
206- Log .d(TAG , " loadNoteInWebView: url = $url " )
209+ val urlDisposable =
210+ repository
211+ .getDirectEditingUrl(account, note)
212+ .observeOn(AndroidSchedulers .mainThread())
213+ .subscribe({ url ->
214+ url?.let {
215+ if (BuildConfig .DEBUG ) {
216+ Log .d(TAG , " loadNoteInWebView: url = $url " )
217+ }
218+ binding?.noteWebview?.loadUrl(url)
207219 }
208- binding?.noteWebview?.loadUrl(url)
209- }
210- }, { throwable ->
211- handleLoadError()
212- Log .e(TAG , " loadNoteInWebView:" , throwable)
213- })
220+ }, { throwable ->
221+ handleLoadError()
222+ Log .e(TAG , " loadNoteInWebView:" , throwable)
223+ })
214224 disposables.add(urlDisposable)
215225 }
216226 }
217227
218228 private fun handleLoadError () {
219229 binding?.run {
220- val snackbar = BrandedSnackbar .make(
221- plainEditingFab,
222- getString(R .string.direct_editing_error),
223- Snackbar .LENGTH_INDEFINITE ,
224- )
230+ val snackbar =
231+ BrandedSnackbar .make(
232+ plainEditingFab,
233+ getString(R .string.direct_editing_error),
234+ Snackbar .LENGTH_INDEFINITE ,
235+ )
225236
226237 if (note != null ) {
227238 snackbar.setAction(R .string.switch_to_plain_editing) {
@@ -278,31 +289,32 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
278289 JS_INTERFACE_NAME ,
279290 )
280291
281- binding?.noteWebview?.webViewClient = object : WebViewClient () {
282- override fun onReceivedError (
283- view : WebView ? ,
284- request : WebResourceRequest ? ,
285- error : WebResourceError ? ,
286- ) {
287- super .onReceivedError(view, request, error)
288- if (request?.isForMainFrame == true ) {
289- handleLoadError()
292+ binding?.noteWebview?.webViewClient =
293+ object : WebViewClient () {
294+ override fun onReceivedError (
295+ view : WebView ? ,
296+ request : WebResourceRequest ? ,
297+ error : WebResourceError ? ,
298+ ) {
299+ super .onReceivedError(view, request, error)
300+ if (request?.isForMainFrame == true ) {
301+ handleLoadError()
302+ }
290303 }
291- }
292304
293- @SuppressLint(" WebViewClientOnReceivedSslError" ) // only for debug mode
294- override fun onReceivedSslError (
295- view : WebView ? ,
296- handler : SslErrorHandler ? ,
297- error : SslError ? ,
298- ) {
299- if (BuildConfig .DEBUG ) {
300- handler?.proceed()
301- } else {
302- super .onReceivedSslError(view, handler, error)
305+ @SuppressLint(" WebViewClientOnReceivedSslError" ) // only for debug mode
306+ override fun onReceivedSslError (
307+ view : WebView ? ,
308+ handler : SslErrorHandler ? ,
309+ error : SslError ? ,
310+ ) {
311+ if (BuildConfig .DEBUG ) {
312+ handler?.proceed()
313+ } else {
314+ super .onReceivedSslError(view, handler, error)
315+ }
303316 }
304317 }
305- }
306318 }
307319
308320 /* *
@@ -333,7 +345,9 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
333345 }
334346 }
335347
336- private class DirectEditingMobileInterface (val noteDirectEditFragment : NoteDirectEditFragment ) {
348+ private class DirectEditingMobileInterface (
349+ val noteDirectEditFragment : NoteDirectEditFragment ,
350+ ) {
337351 @JavascriptInterface
338352 fun close () {
339353 noteDirectEditFragment.close()
@@ -367,20 +381,26 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
367381 }
368382
369383 toggleLoadingUI(true )
370- val updateDisposable = Single .just(note.remoteId)
371- .map { remoteId ->
372- val newNote = notesApi.getNote(remoteId).singleOrError().blockingGet().response
373- val localAccount = repo.getAccountByName(account.name)
374- repo.updateNoteAndSync(localAccount, note, newNote.content, newNote.title, null )
375- }
376- .subscribeOn(Schedulers .io())
377- .observeOn(AndroidSchedulers .mainThread())
378- .subscribe({
379- listener?.changeMode(NoteFragmentListener .Mode .EDIT , true )
380- }, { throwable ->
381- Log .e(TAG , " changeToEditMode: " , throwable)
382- listener?.changeMode(NoteFragmentListener .Mode .EDIT , true )
383- })
384+ val updateDisposable =
385+ Single
386+ .just(note.remoteId)
387+ .map { remoteId ->
388+ val newNote =
389+ notesApi
390+ .getNote(remoteId)
391+ .singleOrError()
392+ .blockingGet()
393+ .response
394+ val localAccount = repo.getAccountByName(account.name)
395+ repo.updateNoteAndSync(localAccount, note, newNote.content, newNote.title, null )
396+ }.subscribeOn(Schedulers .io())
397+ .observeOn(AndroidSchedulers .mainThread())
398+ .subscribe({
399+ listener?.changeMode(NoteFragmentListener .Mode .EDIT , true )
400+ }, { throwable ->
401+ Log .e(TAG , " changeToEditMode: " , throwable)
402+ listener?.changeMode(NoteFragmentListener .Mode .EDIT , true )
403+ })
384404 disposables.add(updateDisposable)
385405 }
386406
@@ -410,7 +430,8 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
410430 private const val JS_RESULT_OK = " ok"
411431
412432 // language=js
413- private val JS_CLOSE = """
433+ private val JS_CLOSE =
434+ """
414435 (function () {
415436 var closeIcons = document.getElementsByClassName("icon-close");
416437 if (closeIcons.length > 0) {
@@ -420,14 +441,18 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
420441 }
421442 return "$JS_RESULT_OK ";
422443 })();
423- """ .trimIndent()
444+ """ .trimIndent()
424445
425446 @JvmStatic
426- fun newInstance (accountId : Long , noteId : Long ): BaseNoteFragment {
427- val bundle = Bundle ().apply {
428- putLong(PARAM_NOTE_ID , noteId)
429- putLong(PARAM_ACCOUNT_ID , accountId)
430- }
447+ fun newInstance (
448+ accountId : Long ,
449+ noteId : Long ,
450+ ): BaseNoteFragment {
451+ val bundle =
452+ Bundle ().apply {
453+ putLong(PARAM_NOTE_ID , noteId)
454+ putLong(PARAM_ACCOUNT_ID , accountId)
455+ }
431456
432457 return NoteDirectEditFragment ().apply {
433458 arguments = bundle
@@ -436,9 +461,10 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
436461
437462 @JvmStatic
438463 fun newInstanceWithNewNote (newNote : Note ? ): BaseNoteFragment {
439- val bundle = Bundle ().apply {
440- putSerializable(PARAM_NEWNOTE , newNote)
441- }
464+ val bundle =
465+ Bundle ().apply {
466+ putSerializable(PARAM_NEWNOTE , newNote)
467+ }
442468
443469 return NoteDirectEditFragment ().apply {
444470 arguments = bundle
0 commit comments