Delete Songs

This commit is contained in:
Fawkes100 2025-01-18 16:58:16 +01:00
parent ae85c7db5a
commit cf9b66b648
5 changed files with 40 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData;
import androidx.room.Dao; import androidx.room.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
import androidx.room.Update;
import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.entities.Song;
import java.util.List; import java.util.List;
@ -13,6 +14,9 @@ public interface SongDao {
@Insert @Insert
void insert(Song song); void insert(Song song);
@Query("SELECT * FROM song") @Query("SELECT * FROM song WHERE syncStatus != 1")
List<Song> getAllSongs(); List<Song> getAllSongs();
@Update
void update(Song song);
} }

View File

@ -37,4 +37,10 @@ public class SongRepository {
mainHandler.post(()-> callback.onResult(songs)); mainHandler.post(()-> callback.onResult(songs));
}); });
} }
public void updateSong(Song song) {
Executors.newSingleThreadExecutor().execute(() -> {
songDao.update(song);
});
}
} }

View File

@ -27,6 +27,7 @@ import com.stormtales.notevault.databinding.FragmentHomeBinding;
import com.stormtales.notevault.ui.songeditor.SongEditorDialog; import com.stormtales.notevault.ui.songeditor.SongEditorDialog;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
@ -83,7 +84,7 @@ public class HomeFragment extends Fragment {
}); });
// RecyclerView einrichten // RecyclerView einrichten
songAdapter = new SongAdapter(new ArrayList<>(), new SongEventClickListener()); songAdapter = new SongAdapter(new ArrayList<>(), this::onEditSong, this::onDeleteSong);
recyclerView.setAdapter(songAdapter); recyclerView.setAdapter(songAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL);
@ -110,15 +111,11 @@ public class HomeFragment extends Fragment {
songEditorDialog.show(getParentFragmentManager(), "songEditorDialog"); songEditorDialog.show(getParentFragmentManager(), "songEditorDialog");
} }
private static class SongEventClickListener implements SongAdapter.OnSongEventClickListener {
@Override
public void onEditSong(Song song) { public void onEditSong(Song song) {
} }
@Override
public void onDeleteSong(Song song) { public void onDeleteSong(Song song) {
this.homeViewModel.deleteSong(song);
}
} }
} }

View File

@ -8,6 +8,7 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.entities.Song;
import com.stormtales.notevault.data.repositories.SongRepository; import com.stormtales.notevault.data.repositories.SongRepository;
import com.stormtales.notevault.data.sync.SyncStatus;
import kotlinx.coroutines.CoroutineScope; import kotlinx.coroutines.CoroutineScope;
import java.util.ArrayList; import java.util.ArrayList;
@ -51,4 +52,15 @@ public class HomeViewModel extends ViewModel {
public void loadAllSongs() { public void loadAllSongs() {
songRepository.getAllSongs(songs->allSongs.setValue(songs)); songRepository.getAllSongs(songs->allSongs.setValue(songs));
} }
public void deleteSong(Song song) {
song.setSyncStatus(SyncStatus.DELETED);
songRepository.updateSong(song);
List<Song> currentSongs = allSongs.getValue();
if(currentSongs != null) {
currentSongs.remove(song);
allSongs.setValue(currentSongs);
}
}
} }

View File

@ -18,11 +18,13 @@ import java.util.List;
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder> { public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder> {
private List<Song> songList; private List<Song> songList;
private OnSongEventClickListener onSongEventClickListener; private OnEditSongListener onEditSongListener;
private OnDeleteSongListener onDeleteSongListener;
public SongAdapter(List<Song> songList, OnSongEventClickListener onSongEventClickListener) { public SongAdapter(List<Song> songList, OnEditSongListener onEditSongListener, OnDeleteSongListener onDeleteSongListener) {
this.songList = songList; this.songList = songList;
this.onSongEventClickListener = onSongEventClickListener; this.onEditSongListener = onEditSongListener;
this.onDeleteSongListener = onDeleteSongListener;
} }
public void updateData(List<Song> songs) { public void updateData(List<Song> songs) {
@ -64,8 +66,8 @@ public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder
holder.textComposition.setText(song.getComposer()); holder.textComposition.setText(song.getComposer());
holder.textTitle.setText(song.getTitle()); holder.textTitle.setText(song.getTitle());
holder.editButton.setOnClickListener(v -> onSongEventClickListener.onEditSong(song)); holder.editButton.setOnClickListener(v -> onEditSongListener.onEditSong(song));
holder.deleteButton.setOnClickListener(v -> onSongEventClickListener.onDeleteSong(song)); holder.deleteButton.setOnClickListener(v -> onDeleteSongListener.onDeleteSong(song));
} }
@Override @Override
@ -73,8 +75,11 @@ public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongViewHolder
return songList.size(); return songList.size();
} }
public interface OnSongEventClickListener { public interface OnEditSongListener {
void onEditSong(Song song); void onEditSong(Song song);
}
public interface OnDeleteSongListener {
void onDeleteSong(Song song); void onDeleteSong(Song song);
} }
} }