diff --git a/.gitignore b/.gitignore index aa724b7..0f99422 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ .externalNativeBuild .cxx local.properties +/music_database +/music_database-shm +/music_database-wal 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 0f2fe3b..3882311 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 @@ -1,5 +1,6 @@ package com.stormtales.notevault.data.dao; +import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; diff --git a/app/src/main/java/com/stormtales/notevault/data/entities/Song.java b/app/src/main/java/com/stormtales/notevault/data/entities/Song.java index 4fb6039..51f67f4 100644 --- a/app/src/main/java/com/stormtales/notevault/data/entities/Song.java +++ b/app/src/main/java/com/stormtales/notevault/data/entities/Song.java @@ -29,6 +29,7 @@ public class Song { this.composer = composer; this.genre = genre; this.year = year; + this.syncStatus = SyncStatus.CREATED; } public 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 9081498..39a35d5 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 @@ -2,11 +2,15 @@ package com.stormtales.notevault.data.repositories; import android.app.Application; import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import androidx.lifecycle.LiveData; import com.stormtales.notevault.data.MusicDatabase; import com.stormtales.notevault.data.dao.SongDao; import com.stormtales.notevault.data.entities.Song; import java.util.List; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SongRepository { @@ -21,7 +25,16 @@ public class SongRepository { }); } - public List getAllSongs() { - return songDao.getAllSongs(); + public interface LoadHomeViewModelCallback { + void onResult(T result); + } + + public void getAllSongs(LoadHomeViewModelCallback> callback) { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + executorService.execute(() -> { + List songs = songDao.getAllSongs(); + Handler mainHandler = new Handler(Looper.getMainLooper()); + mainHandler.post(()-> callback.onResult(songs)); + }); } } 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 9c2c37a..940afe0 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 @@ -38,9 +38,8 @@ public class HomeFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - SongRepository songRepository = new SongRepository(this.getContext()); homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class); - homeViewModel. + homeViewModel.setSongRepository(new SongRepository(this.getContext())); binding = FragmentHomeBinding.inflate(inflater, container, false); View root = binding.getRoot(); 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 f8f98ef..43bc5ce 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 @@ -19,16 +19,13 @@ public class HomeViewModel extends ViewModel { private SongRepository songRepository; public HomeViewModel() { - /*songRepository = new SongRepository(application); - List requestedSongs = songRepository.getAllSongs();*/ List currentSongs = new ArrayList<>(); - currentSongs.add(new Song("Test", "Some Test Composer", "Genre", 2022)); this.allSongs = new MutableLiveData<>(currentSongs); /*this.allSongs.setValue(songRepository.getAllSongs());*/ } public void addSong(Song song) { - //songRepository.insert(song); + songRepository.insert(song); List currentSongs = allSongs.getValue(); if(currentSongs == null) { currentSongs = new ArrayList<>(); @@ -45,4 +42,13 @@ public class HomeViewModel extends ViewModel { public LiveData> getAllSongsLive() { return allSongs; } + + public void setSongRepository(SongRepository songRepository) { + this.songRepository = songRepository; + loadAllSongs(); + } + + public void loadAllSongs() { + songRepository.getAllSongs(songs->allSongs.setValue(songs)); + } } \ No newline at end of file