Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Missing Note NPE Checks #2585

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
@Override
public void onResume() {
super.onResume();

if (note == null) {
Log.e(TAG, "note is null, onResume");
return;
}

if (listener == null) {
Log.e(TAG, "listener is null, onResume");
return;
}

listener.onNoteUpdated(note);
}

Expand Down Expand Up @@ -203,6 +214,11 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}

private void prepareFavoriteOption(MenuItem item) {
if (note == null) {
Log.e(TAG, "note is null, prepareFavoriteOption");
return;
}

item.setIcon(note.getFavorite() ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_border_white_24dp);
item.setChecked(note.getFavorite());

Expand Down Expand Up @@ -301,6 +317,11 @@ protected boolean shouldShowToolbar() {
}

public void onCloseNote() {
if (note == null) {
Log.e(TAG, "note is null, onCloseNote");
return;
}

if (!titleModified && originalNote == null && getContent().isEmpty()) {
repo.deleteNoteAndSync(localAccount, note.getId());
}
Expand All @@ -313,23 +334,29 @@ public void onCloseNote() {
*/
protected void saveNote(@Nullable ISyncCallback callback) {
Log.d(TAG, "saveData()");
if (note != null) {
final var newContent = getContent();
if (note.getContent().equals(newContent)) {
if (note.getScrollY() != originalScrollY) {
Log.v(TAG, "... only saving new scroll state, since content did not change");
repo.updateScrollY(note.getId(), note.getScrollY());
} else {
Log.v(TAG, "... not saving, since nothing has changed");
}
if (note == null) {
Log.e(TAG, "note is null, saveNote");
return;
}

if (listener == null) {
Log.e(TAG, "listener is null, saveNote");
return;
}

final var newContent = getContent();
if (note.getContent().equals(newContent)) {
if (note.getScrollY() != originalScrollY) {
Log.v(TAG, "... only saving new scroll state, since content did not change");
repo.updateScrollY(note.getId(), note.getScrollY());
} else {
// FIXME requires database queries on main thread!
note = repo.updateNoteAndSync(localAccount, note, newContent, null, callback);
listener.onNoteUpdated(note);
requireActivity().invalidateOptionsMenu();
Log.v(TAG, "... not saving, since nothing has changed");
}
} else {
Log.e(TAG, "note is null");
// FIXME requires database queries on main thread!
note = repo.updateNoteAndSync(localAccount, note, newContent, null, callback);
listener.onNoteUpdated(note);
requireActivity().invalidateOptionsMenu();
}
}

Expand Down Expand Up @@ -368,13 +395,33 @@ public void showEditTitleDialog() {

@Override
public void onCategoryChosen(String category) {
if (note == null) {
Log.e(TAG, "note is null, onCategoryChosen");
return;
}

if (listener == null) {
Log.e(TAG, "listener is null, onCategoryChosen");
return;
}

repo.setCategory(localAccount, note.getId(), category);
note.setCategory(category);
listener.onNoteUpdated(note);
}

@Override
public void onTitleEdited(String newTitle) {
if (note == null) {
Log.e(TAG, "note is null, onTitleEdited");
return;
}

if (listener == null) {
Log.e(TAG, "listener is null, onTitleEdited");
return;
}

titleModified = true;
note.setTitle(newTitle);
executor.submit(() -> {
Expand All @@ -384,6 +431,16 @@ public void onTitleEdited(String newTitle) {
}

public void moveNote(Account account) {
if (note == null) {
Log.e(TAG, "note is null, moveNote");
return;
}

if (listener == null) {
Log.e(TAG, "listener is null, moveNote");
return;
}

final var moveLiveData = repo.moveNoteToAnotherAccount(account, note);
moveLiveData.observe(this, (v) -> moveLiveData.removeObservers(this));
listener.close();
Expand Down
Loading