nextNoteVault #23
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -13,3 +13,6 @@
 | 
			
		||||
.externalNativeBuild
 | 
			
		||||
.cxx
 | 
			
		||||
local.properties
 | 
			
		||||
/music_database
 | 
			
		||||
/music_database-shm
 | 
			
		||||
/music_database-wal
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -29,6 +29,7 @@ public class Song {
 | 
			
		||||
        this.composer = composer;
 | 
			
		||||
        this.genre = genre;
 | 
			
		||||
        this.year = year;
 | 
			
		||||
        this.syncStatus = SyncStatus.CREATED;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Song() {
 | 
			
		||||
 | 
			
		||||
@ -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<Song> getAllSongs() {
 | 
			
		||||
        return songDao.getAllSongs();
 | 
			
		||||
    public interface LoadHomeViewModelCallback<T> {
 | 
			
		||||
        void onResult(T result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void getAllSongs(LoadHomeViewModelCallback<List<Song>> callback) {
 | 
			
		||||
        ExecutorService executorService = Executors.newSingleThreadExecutor();
 | 
			
		||||
        executorService.execute(() -> {
 | 
			
		||||
            List<Song> songs = songDao.getAllSongs();
 | 
			
		||||
            Handler mainHandler = new Handler(Looper.getMainLooper());
 | 
			
		||||
            mainHandler.post(()-> callback.onResult(songs));
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
 | 
			
		||||
@ -19,16 +19,13 @@ public class HomeViewModel extends ViewModel {
 | 
			
		||||
    private SongRepository songRepository;
 | 
			
		||||
 | 
			
		||||
    public HomeViewModel() {
 | 
			
		||||
        /*songRepository = new SongRepository(application);
 | 
			
		||||
        List<Song> requestedSongs = songRepository.getAllSongs();*/
 | 
			
		||||
        List<Song> 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<Song> currentSongs = allSongs.getValue();
 | 
			
		||||
        if(currentSongs == null) {
 | 
			
		||||
            currentSongs = new ArrayList<>();
 | 
			
		||||
@ -45,4 +42,13 @@ public class HomeViewModel extends ViewModel {
 | 
			
		||||
    public LiveData<List<Song>> getAllSongsLive() {
 | 
			
		||||
        return allSongs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setSongRepository(SongRepository songRepository) {
 | 
			
		||||
        this.songRepository = songRepository;
 | 
			
		||||
        loadAllSongs();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void loadAllSongs() {
 | 
			
		||||
        songRepository.getAllSongs(songs->allSongs.setValue(songs));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user