tempus/app/src/main/java/com/cappielloantonio/play/viewmodel/SearchViewModel.java

110 lines
3.7 KiB
Java
Raw Normal View History

2020-11-21 13:54:49 +01:00
package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
2020-11-22 19:11:38 +01:00
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
2021-04-20 11:53:51 +02:00
import com.cappielloantonio.play.model.Genre;
2020-11-21 13:54:49 +01:00
import com.cappielloantonio.play.model.RecentSearch;
import com.cappielloantonio.play.model.Song;
2020-11-22 19:11:38 +01:00
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
2021-04-20 11:53:51 +02:00
import com.cappielloantonio.play.repository.GenreRepository;
2020-11-21 13:54:49 +01:00
import com.cappielloantonio.play.repository.RecentSearchRepository;
import com.cappielloantonio.play.repository.SongRepository;
2020-11-25 15:12:07 +01:00
import java.util.ArrayList;
import java.util.LinkedHashSet;
2020-11-21 13:54:49 +01:00
import java.util.List;
public class SearchViewModel extends AndroidViewModel {
2020-11-25 15:12:07 +01:00
private static final String TAG = "SearchViewModel";
2021-04-20 11:53:51 +02:00
private String query = "";
2020-11-21 13:54:49 +01:00
private SongRepository songRepository;
2020-11-22 19:11:38 +01:00
private AlbumRepository albumRepository;
private ArtistRepository artistRepository;
2021-04-20 11:53:51 +02:00
private GenreRepository genreRepository;
2020-11-21 13:54:49 +01:00
private RecentSearchRepository recentSearchRepository;
private LiveData<List<Song>> searchSong;
2020-11-22 19:11:38 +01:00
private LiveData<List<Album>> searchAlbum;
private LiveData<List<Artist>> searchArtist;
2021-04-20 11:53:51 +02:00
private LiveData<List<Genre>> searchGenre;
2020-11-21 13:54:49 +01:00
public SearchViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository(application);
2020-11-22 19:11:38 +01:00
albumRepository = new AlbumRepository(application);
artistRepository = new ArtistRepository(application);
2021-04-20 11:53:51 +02:00
genreRepository = new GenreRepository(application);
2020-11-21 13:54:49 +01:00
recentSearchRepository = new RecentSearchRepository(application);
}
2021-04-20 11:53:51 +02:00
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
if(!query.isEmpty()) {
insertNewSearch(query);
}
}
2020-11-21 13:54:49 +01:00
public LiveData<List<Song>> searchSong(String title) {
2020-11-21 18:41:35 +01:00
searchSong = songRepository.searchListLiveSong(title);
2020-11-21 13:54:49 +01:00
return searchSong;
}
2020-11-22 19:11:38 +01:00
public LiveData<List<Album>> searchAlbum(String name) {
searchAlbum = albumRepository.searchListLiveAlbum(name);
return searchAlbum;
}
public LiveData<List<Artist>> searchArtist(String name) {
searchArtist = artistRepository.searchListLiveArtist(name);
return searchArtist;
2020-11-21 13:54:49 +01:00
}
2021-04-20 11:53:51 +02:00
public LiveData<List<Genre>> searchGenre(String name) {
searchGenre = genreRepository.searchListLiveGenre(name);
return searchGenre;
2020-11-21 13:54:49 +01:00
}
public void insertNewSearch(String search) {
recentSearchRepository.insert(new RecentSearch(search));
}
2021-04-20 11:53:51 +02:00
public void deleteRecentSearch(String search) {
recentSearchRepository.delete(new RecentSearch(search));
2020-11-21 13:54:49 +01:00
}
2020-11-25 15:12:07 +01:00
public List<String> getSearchSuggestion(String query) {
ArrayList<String> suggestions = new ArrayList<>();
suggestions.addAll(songRepository.getSearchSuggestion(query));
suggestions.addAll(albumRepository.getSearchSuggestion(query));
suggestions.addAll(artistRepository.getSearchSuggestion(query));
2021-04-20 11:53:51 +02:00
suggestions.addAll(genreRepository.getSearchSuggestion(query));
2020-11-25 15:12:07 +01:00
LinkedHashSet<String> hashSet = new LinkedHashSet<>(suggestions);
ArrayList<String> suggestionsWithoutDuplicates = new ArrayList<>(hashSet);
return suggestionsWithoutDuplicates;
2020-11-25 15:12:07 +01:00
}
2021-04-20 11:53:51 +02:00
public List<String> getRecentSearchSuggestion() {
ArrayList<String> suggestions = new ArrayList<>();
suggestions.addAll(recentSearchRepository.getRecentSearchSuggestion());
return suggestions;
}
2020-11-21 13:54:49 +01:00
}