Skip to content

Commit 8de4cb1

Browse files
committed
wip: error and conflict handling when switching edit modes
Signed-off-by: Álvaro Brey <[email protected]>
1 parent b374771 commit 8de4cb1

File tree

8 files changed

+212
-102
lines changed

8 files changed

+212
-102
lines changed

app/src/main/java/it/niedermann/owncloud/notes/NotesApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.app.Application;
66
import android.content.Context;
77
import android.util.Log;
8+
import android.webkit.WebView;
89

910
import androidx.appcompat.app.AppCompatDelegate;
1011
import androidx.preference.PreferenceManager;
@@ -29,6 +30,9 @@ public void onCreate() {
2930
lockedPreference = prefs.getBoolean(getString(R.string.pref_key_lock), false);
3031
isGridViewEnabled = getDefaultSharedPreferences(this).getBoolean(getString(R.string.pref_key_gridview), false);
3132
super.onCreate();
33+
if (BuildConfig.DEBUG) {
34+
WebView.setWebContentsDebuggingEnabled(true);
35+
}
3236
}
3337

3438
public static void setAppTheme(DarkModeSetting setting) {

app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
7171
private Note originalNote;
7272
private int originalScrollY;
7373
protected NotesRepository repo;
74+
@Nullable
7475
protected NoteFragmentListener listener;
7576
private boolean titleModified = false;
7677

@@ -106,6 +107,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
106107
}
107108
isNew = false;
108109
note = originalNote = repo.getNoteById(id);
110+
Log.d(TAG, "TESTING: retrieved note: " + note);
109111
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
110112
requireActivity().invalidateOptionsMenu();
111113
} else {
@@ -241,7 +243,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
241243
.show(requireActivity().getSupportFragmentManager(), BaseNoteFragment.class.getSimpleName()));
242244
return true;
243245
} else if (itemId == R.id.menu_share) {
244-
ShareUtil.openShareDialog(requireContext(), note.getTitle(), note.getContent());
246+
shareNote();
245247
return false;
246248
} else if (itemId == MENU_ID_PIN) {
247249
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -264,6 +266,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
264266
return super.onOptionsItemSelected(item);
265267
}
266268

269+
protected void shareNote() {
270+
ShareUtil.openShareDialog(requireContext(), note.getTitle(), note.getContent());
271+
}
272+
267273
@CallSuper
268274
protected void onNoteLoaded(Note note) {
269275
this.originalScrollY = note.getScrollY();
@@ -387,6 +393,6 @@ enum Mode {
387393

388394
void onNoteUpdated(Note note);
389395

390-
void changeMode(@NonNull Mode mode);
396+
void changeMode(@NonNull Mode mode, boolean reloadNote);
391397
}
392398
}

app/src/main/java/it/niedermann/owncloud/notes/edit/EditNoteActivity.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,40 @@ private void launchExistingNote(long accountId, long noteId) {
150150
launchExistingNote(accountId, noteId, null);
151151
}
152152

153+
private void launchExistingNote(long accountId, long noteId, @Nullable final String mode) {
154+
launchExistingNote(accountId, noteId, mode, false);
155+
}
153156

154157
/**
155158
* Starts a {@link NoteEditFragment} or {@link NotePreviewFragment} for an existing note.
156159
*
157-
* @param noteId ID of the existing note.
158-
* @param mode View-mode of the fragment (pref value or null). If null will be chosen based on
159-
* user preferences.
160+
* @param noteId ID of the existing note.
161+
* @param mode View-mode of the fragment (pref value or null). If null will be chosen based on
162+
* user preferences.
163+
* @param discardState If true, the state of the fragment will be discarded and a new fragment will be created
160164
*/
161-
private void launchExistingNote(long accountId, long noteId, @Nullable final String mode) {
165+
private void launchExistingNote(long accountId, long noteId, @Nullable final String mode, final boolean discardState) {
162166
// save state of the fragment in order to resume with the same note and originalNote
163-
Fragment.SavedState savedState = null;
164-
if (fragment != null) {
165-
savedState = getSupportFragmentManager().saveFragmentInstanceState(fragment);
166-
}
167-
fragment = getNoteFragment(accountId, noteId, mode);
167+
runOnUiThread(() -> {
168+
Fragment.SavedState savedState = null;
169+
if (fragment != null && !discardState) {
170+
savedState = getSupportFragmentManager().saveFragmentInstanceState(fragment);
171+
}
172+
fragment = getNoteFragment(accountId, noteId, mode);
168173

169-
if (savedState != null) {
170-
fragment.setInitialSavedState(savedState);
171-
}
172-
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_view, fragment).commit();
173-
if(!fragment.shouldShowToolbar()){
174-
binding.toolbar.setVisibility(View.GONE);
175-
}else {
176-
binding.toolbar.setVisibility(View.VISIBLE);
177-
}
174+
if (savedState != null) {
175+
fragment.setInitialSavedState(savedState);
176+
}
177+
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_view, fragment).commit();
178+
if (!fragment.shouldShowToolbar()) {
179+
binding.toolbar.setVisibility(View.GONE);
180+
} else {
181+
binding.toolbar.setVisibility(View.VISIBLE);
182+
}
183+
});
178184
}
179185

186+
180187
private String getPreferenceMode() {
181188
final var prefKeyNoteMode = getString(R.string.pref_key_note_mode);
182189
final var prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
@@ -289,10 +296,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
289296
close();
290297
return true;
291298
} else if (itemId == R.id.menu_preview) {
292-
changeMode(Mode.PREVIEW);
299+
changeMode(Mode.PREVIEW, false);
293300
return true;
294301
} else if (itemId == R.id.menu_edit) {
295-
changeMode(Mode.EDIT);
302+
changeMode(Mode.EDIT, false);
296303
return true;
297304
}
298305
return super.onOptionsItemSelected(item);
@@ -340,18 +347,18 @@ public void onNoteUpdated(Note note) {
340347
}
341348

342349
@Override
343-
public void changeMode(@NonNull Mode mode) {
350+
public void changeMode(@NonNull Mode mode, boolean reloadNote) {
344351
switch (mode) {
345352
case EDIT:
346-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit));
353+
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
347354
break;
348355
case PREVIEW:
349-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview));
356+
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
350357
break;
351358
case DIRECT_EDIT:
352359
// TODO deal with conflict when doing some edits and then changing to direct edit
353360
// TODO deal with note not created yet on server
354-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit));
361+
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
355362
break;
356363
default:
357364
throw new IllegalStateException("Unknown mode: " + mode);

0 commit comments

Comments
 (0)