diff --git a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java index 3882311..f8a7d15 100644 --- a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java +++ b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; +import androidx.room.Update; import com.stormtales.notevault.data.entities.Song; import java.util.List; @@ -13,6 +14,9 @@ public interface SongDao { @Insert void insert(Song song); - @Query("SELECT * FROM song") + @Query("SELECT * FROM song WHERE syncStatus != 1") List getAllSongs(); + + @Update + void update(Song song); } diff --git a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java index 39a35d5..1d7f984 100644 --- a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java +++ b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java @@ -37,4 +37,10 @@ public class SongRepository { mainHandler.post(()-> callback.onResult(songs)); }); } + + public void updateSong(Song song) { + Executors.newSingleThreadExecutor().execute(() -> { + songDao.update(song); + }); + } } diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java b/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java index 940afe0..db13d89 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java @@ -27,6 +27,7 @@ import com.stormtales.notevault.databinding.FragmentHomeBinding; import com.stormtales.notevault.ui.songeditor.SongEditorDialog; import java.util.ArrayList; +import java.util.List; public class HomeFragment extends Fragment { @@ -83,7 +84,7 @@ public class HomeFragment extends Fragment { }); // RecyclerView einrichten - songAdapter = new SongAdapter(new ArrayList<>(), new SongEventClickListener()); + songAdapter = new SongAdapter(new ArrayList<>(), this::onEditSong, this::onDeleteSong); recyclerView.setAdapter(songAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL); @@ -110,15 +111,11 @@ public class HomeFragment extends Fragment { 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); } } \ No newline at end of file diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java b/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java index 43bc5ce..6a3b78b 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java +++ b/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java @@ -8,6 +8,7 @@ import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.repositories.SongRepository; +import com.stormtales.notevault.data.sync.SyncStatus; import kotlinx.coroutines.CoroutineScope; import java.util.ArrayList; @@ -51,4 +52,15 @@ public class HomeViewModel extends ViewModel { public void loadAllSongs() { songRepository.getAllSongs(songs->allSongs.setValue(songs)); } + + public void deleteSong(Song song) { + song.setSyncStatus(SyncStatus.DELETED); + songRepository.updateSong(song); + + List currentSongs = allSongs.getValue(); + if(currentSongs != null) { + currentSongs.remove(song); + allSongs.setValue(currentSongs); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java b/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java index 2e7487e..5b797df 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java +++ b/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java @@ -18,11 +18,13 @@ import java.util.List; public class SongAdapter extends RecyclerView.Adapter { private List songList; - private OnSongEventClickListener onSongEventClickListener; + private OnEditSongListener onEditSongListener; + private OnDeleteSongListener onDeleteSongListener; - public SongAdapter(List songList, OnSongEventClickListener onSongEventClickListener) { + public SongAdapter(List songList, OnEditSongListener onEditSongListener, OnDeleteSongListener onDeleteSongListener) { this.songList = songList; - this.onSongEventClickListener = onSongEventClickListener; + this.onEditSongListener = onEditSongListener; + this.onDeleteSongListener = onDeleteSongListener; } public void updateData(List songs) { @@ -64,8 +66,8 @@ public class SongAdapter extends RecyclerView.Adapter onSongEventClickListener.onEditSong(song)); - holder.deleteButton.setOnClickListener(v -> onSongEventClickListener.onDeleteSong(song)); + holder.editButton.setOnClickListener(v -> onEditSongListener.onEditSong(song)); + holder.deleteButton.setOnClickListener(v -> onDeleteSongListener.onDeleteSong(song)); } @Override @@ -73,8 +75,11 @@ public class SongAdapter extends RecyclerView.Adapter