nextNoteVault #23
@ -10,7 +10,7 @@ import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
@Entity
 | 
			
		||||
public class Song {
 | 
			
		||||
    @PrimaryKey
 | 
			
		||||
    @PrimaryKey(autoGenerate = true)
 | 
			
		||||
    private int localID;
 | 
			
		||||
 | 
			
		||||
    private String serverID;
 | 
			
		||||
 | 
			
		||||
@ -112,7 +112,10 @@ public class HomeFragment extends Fragment {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onEditSong(Song song) {
 | 
			
		||||
 | 
			
		||||
        SongEditorDialog songEditorDialog = new SongEditorDialog();
 | 
			
		||||
        songEditorDialog.setEditedSong(song);
 | 
			
		||||
        songEditorDialog.setHomeViewModel(homeViewModel);
 | 
			
		||||
        songEditorDialog.show(getParentFragmentManager(), "songEditorDialog");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void onDeleteSong(Song song) {
 | 
			
		||||
 | 
			
		||||
@ -63,4 +63,18 @@ public class HomeViewModel extends ViewModel {
 | 
			
		||||
            allSongs.setValue(currentSongs);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateSong(Song editedSong) {
 | 
			
		||||
        List<Song> currentSongs = allSongs.getValue();
 | 
			
		||||
        if(currentSongs != null) {
 | 
			
		||||
            for(int i = 0; i < currentSongs.size(); i++) {
 | 
			
		||||
                if(currentSongs.get(i).getLocalID() == editedSong.getLocalID()) {
 | 
			
		||||
                    currentSongs.set(i, editedSong);
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            songRepository.updateSong(editedSong);
 | 
			
		||||
            allSongs.setValue(currentSongs);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -17,6 +17,7 @@ import android.view.ViewGroup;
 | 
			
		||||
import androidx.lifecycle.ViewModelProvider;
 | 
			
		||||
import com.stormtales.notevault.R;
 | 
			
		||||
import com.stormtales.notevault.data.entities.Song;
 | 
			
		||||
import com.stormtales.notevault.data.sync.SyncStatus;
 | 
			
		||||
import com.stormtales.notevault.ui.home.HomeViewModel;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
 | 
			
		||||
@ -27,6 +28,8 @@ public class SongEditorDialog extends DialogFragment {
 | 
			
		||||
    private Uri[] noteSheetFiles;
 | 
			
		||||
    private HomeViewModel homeViewModel;
 | 
			
		||||
 | 
			
		||||
    private Song editedSong;
 | 
			
		||||
 | 
			
		||||
    public SongEditorDialog() {
 | 
			
		||||
        // Required empty public constructor
 | 
			
		||||
    }
 | 
			
		||||
@ -38,6 +41,8 @@ public class SongEditorDialog extends DialogFragment {
 | 
			
		||||
        LayoutInflater inflater = LayoutInflater.from(getContext());
 | 
			
		||||
        View dialogView = inflater.inflate(R.layout.fragment_song_editor_dialog, null);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        dialogView.findViewById(R.id.btnCancel).setOnClickListener(v-> onCancel());
 | 
			
		||||
        dialogView.findViewById(R.id.btnSave).setOnClickListener(v -> onSave());
 | 
			
		||||
 | 
			
		||||
@ -47,9 +52,22 @@ public class SongEditorDialog extends DialogFragment {
 | 
			
		||||
 | 
			
		||||
        dialog = builder.create();
 | 
			
		||||
        dialog.setCanceledOnTouchOutside(false);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        return dialog;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onStart() {
 | 
			
		||||
        super.onStart();
 | 
			
		||||
        if(editedSong != null) {
 | 
			
		||||
            ((EditText) dialog.findViewById(R.id.etTitle)).setText(editedSong.getTitle());
 | 
			
		||||
            ((EditText) dialog.findViewById(R.id.etComposer)).setText(editedSong.getComposer());
 | 
			
		||||
            ((EditText) dialog.findViewById(R.id.etGenre)).setText(editedSong.getGenre());
 | 
			
		||||
            ((EditText) dialog.findViewById(R.id.etYear)).setText(String.valueOf(editedSong.getYear()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void onCancel() {
 | 
			
		||||
        dialog.dismiss();
 | 
			
		||||
    }
 | 
			
		||||
@ -62,8 +80,17 @@ public class SongEditorDialog extends DialogFragment {
 | 
			
		||||
        int releaseYear = year_string.isBlank()? 0 : Integer.parseInt(year_string);
 | 
			
		||||
        String genre = ((EditText) dialog.findViewById(R.id.etGenre)).getText().toString();
 | 
			
		||||
 | 
			
		||||
        if(editedSong != null) {
 | 
			
		||||
            editedSong.setTitle(title);
 | 
			
		||||
            editedSong.setComposer(composer);
 | 
			
		||||
            editedSong.setYear(releaseYear);
 | 
			
		||||
            editedSong.setGenre(genre);
 | 
			
		||||
            editedSong.setSyncStatus(SyncStatus.MODIFIED);
 | 
			
		||||
            homeViewModel.updateSong(editedSong);
 | 
			
		||||
        } else {
 | 
			
		||||
            Song song = new Song(title, composer, genre, releaseYear);
 | 
			
		||||
            homeViewModel.addSong(song);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        dialog.dismiss();
 | 
			
		||||
@ -77,4 +104,8 @@ public class SongEditorDialog extends DialogFragment {
 | 
			
		||||
    public void setHomeViewModel(HomeViewModel homeViewModel) {
 | 
			
		||||
        this.homeViewModel = homeViewModel;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setEditedSong(Song editedSong) {
 | 
			
		||||
        this.editedSong = editedSong;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user