Skip to content

Commit

Permalink
add npe checks
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Feb 19, 2025
1 parent b1e1ef9 commit ed79387
Showing 1 changed file with 71 additions and 14 deletions.
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

0 comments on commit ed79387

Please sign in to comment.