@@ -98,54 +98,82 @@ public void onAttach(@NonNull Context context) {
9898 @ Override
9999 public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
100100 super .onViewCreated (view , savedInstanceState );
101- executor .submit (() -> {
101+ setHasOptionsMenu (true );
102+
103+ executor .execute (() -> {
102104 try {
103- final var ssoAccount = SingleAccountHelper .getCurrentSingleSignOnAccount (requireContext ().getApplicationContext ());
104- this .localAccount = repo .getAccountByName (ssoAccount .name );
105-
106- if (savedInstanceState == null ) {
107- final long id = requireArguments ().getLong (PARAM_NOTE_ID );
108- if (id > 0 ) {
109- final long accountId = requireArguments ().getLong (PARAM_ACCOUNT_ID );
110- if (accountId > 0 ) {
111- /* Switch account if account id has been provided */
112- this .localAccount = repo .getAccountById (accountId );
113- SingleAccountHelper .commitCurrentAccount (requireContext ().getApplicationContext (), localAccount .getAccountName ());
114- }
115- isNew = false ;
116- note = originalNote = repo .getNoteById (id );
117- requireActivity ().runOnUiThread (() -> onNoteLoaded (note ));
118- requireActivity ().invalidateOptionsMenu ();
119- } else {
120- final var paramNote = (Note ) requireArguments ().getSerializable (PARAM_NEWNOTE );
121- final var content = requireArguments ().getString (PARAM_CONTENT );
122- if (paramNote == null ) {
123- if (content == null ) {
124- throw new IllegalArgumentException (PARAM_NOTE_ID + " is not given, argument " + PARAM_NEWNOTE + " is missing and " + PARAM_CONTENT + " is missing." );
125- } else {
126- note = new Note (-1 , null , Calendar .getInstance (), NoteUtil .generateNoteTitle (content ), content , getString (R .string .category_readonly ), false , null , DBStatus .VOID , -1 , "" , 0 , false , false );
127- requireActivity ().runOnUiThread (() -> onNoteLoaded (note ));
128- requireActivity ().invalidateOptionsMenu ();
129- }
130- } else {
131- paramNote .setStatus (DBStatus .LOCAL_EDITED );
132- note = repo .addNote (localAccount .getId (), paramNote );
133- originalNote = null ;
134- requireActivity ().runOnUiThread (() -> onNoteLoaded (note ));
135- requireActivity ().invalidateOptionsMenu ();
136- }
137- }
105+ initializeAccount ();
106+
107+ if (savedInstanceState != null ) {
108+ loadFromSavedState (savedInstanceState );
138109 } else {
139- note = (Note ) savedInstanceState .getSerializable (SAVEDKEY_NOTE );
140- originalNote = (Note ) savedInstanceState .getSerializable (SAVEDKEY_ORIGINAL_NOTE );
141- requireActivity ().runOnUiThread (() -> onNoteLoaded (note ));
142- requireActivity ().invalidateOptionsMenu ();
110+ loadFromArguments ();
143111 }
112+
113+ requireActivity ().runOnUiThread (() -> {
114+ onNoteLoaded (note );
115+ requireActivity ().invalidateOptionsMenu ();
116+
117+ if (listener != null ) {
118+ listener .onNoteUpdated (note );
119+ }
120+ });
144121 } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e ) {
145122 Log_OC .e (TAG , e .getLocalizedMessage ());
146123 }
147124 });
148- setHasOptionsMenu (true );
125+ }
126+
127+ private void initializeAccount () throws NextcloudFilesAppAccountNotFoundException , NoCurrentAccountSelectedException {
128+ final var ssoAccount = SingleAccountHelper .getCurrentSingleSignOnAccount (requireContext ().getApplicationContext ());
129+ this .localAccount = repo .getAccountByName (ssoAccount .name );
130+ }
131+
132+ private void loadFromSavedState (Bundle savedInstanceState ) {
133+ note = (Note ) savedInstanceState .getSerializable (SAVEDKEY_NOTE );
134+ originalNote = (Note ) savedInstanceState .getSerializable (SAVEDKEY_ORIGINAL_NOTE );
135+ }
136+
137+ private void loadFromArguments () {
138+ final long noteId = requireArguments ().getLong (PARAM_NOTE_ID );
139+
140+ if (noteId > 0 ) {
141+ loadExistingNote (noteId );
142+ } else {
143+ createNewNote ();
144+ }
145+ }
146+
147+ private void loadExistingNote (long noteId ) {
148+ // Switch account if provided
149+ final long accountId = requireArguments ().getLong (PARAM_ACCOUNT_ID );
150+ if (accountId > 0 ) {
151+ this .localAccount = repo .getAccountById (accountId );
152+ SingleAccountHelper .commitCurrentAccount (requireContext ().getApplicationContext (), localAccount .getAccountName ());
153+ }
154+
155+ isNew = false ;
156+ note = originalNote = repo .getNoteById (noteId );
157+ }
158+
159+ private void createNewNote () {
160+ final var paramNote = (Note ) requireArguments ().getSerializable (PARAM_NEWNOTE );
161+ final var content = requireArguments ().getString (PARAM_CONTENT );
162+
163+ if (paramNote != null ) {
164+ // Create from provided note
165+ paramNote .setStatus (DBStatus .LOCAL_EDITED );
166+ note = repo .addNote (localAccount .getId (), paramNote );
167+ originalNote = null ;
168+ } else if (content != null ) {
169+ // Create from content string
170+ note = new Note (-1 , null , Calendar .getInstance (),
171+ NoteUtil .generateNoteTitle (content ), content ,
172+ getString (R .string .category_readonly ), false , null ,
173+ DBStatus .VOID , -1 , "" , 0 , false , false );
174+ } else {
175+ throw new IllegalArgumentException ("Missing required parameters: noteId, newNote, or content" );
176+ }
149177 }
150178
151179 @ Nullable
0 commit comments