Search code examples
javaandroidmvvmviewmodelandroid-livedata

Updating a note after it was just created


I am currently working on a note taking app that follows the MVVM architecture. there are 2 activities. Main with the list of notes and view mode which allows the user to view the note. If they click on the description or title it then allows edit mode where you can save changes and what not. When a new note is being created, it takes the user to the same activity to view/edit notes and it enables edit mode with empty title and description. The problem im having at the moment is that if the user creates a note and decides to edit it right after it was created, when the user comes back to the list of notes it has a bunch of notes each containing each update he made so the note is getting saved into a new note instead of updating the existing one.

Code for save note:

private void saveNote(){

        //updates existing note. Next Statement adds new note
        if(getIntent().hasExtra(EXTRA_ID)){
            mViewTitle.setText(mEditTitle.getText().toString());
            String timeStamp = Utility.getCurrentTimestamp();
            timeStamp = timeStamp.replace("-", " ");

            Note note = new Note(mTitle, mContent, timeStamp);
            note.setId(mID);
            mAddEditNoteViewModel.update(note);
            Toast.makeText(this, "Note Updated", Toast.LENGTH_SHORT).show();
        } else if(!getIntent().hasExtra(EXTRA_ID)){
            mViewTitle.setText(mEditTitle.getText().toString());
            String timeStamp = Utility.getCurrentTimestamp();
            timeStamp = timeStamp.replace("-", " ");

            Note note = new Note(mTitle, mContent, timeStamp);
            mAddEditNoteViewModel.insert(note);
            Toast.makeText(this, "New Note Created:", Toast.LENGTH_SHORT).show();
        }
    }

as you can see, The way i check if i need to update or create a new note is by checking if the intent has the data i need (in this case the ID). But when the user just created the note and didnt go back to the main activity, every time the newly created note gets updated without going back to main would call the else if statement thus creating a new note. I can't seem to find a way to get around this. I was thinking of only saving the new note AFTER the user goes back to the main activity but if the user creates a note, clicks on the checkmark icon (to save) and closes the app without going back to main, the note will not be saved. How can i approach this problem?


Solution

  • I was able to get the result i wanted by doing the following:

    in my repository, i return a long with the get method which returns the id being returned in the doInBackground method of the asynctask and unlike before (where the do in background was happening after the results from insert were delivered (null)) the result isnt delivered until doInBackground runs:

     public Long insert(Note note) {
            try {
                Long id = new InsertNoteAsyncTask(mNoteDao).execute(note).get();
                Log.d(TAG, "insert: CALLED - REPOSITORY - LONG: " + id);
                return id;
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    then the view model just gets and return the long

    public Long insert(Note note) {
        return mNoteRepository.insert(note);
    }
    

    and everything works as expected. Big thanks to @Tenfour04 for the help