Search page renewed

This commit is contained in:
CappielloAntonio 2021-04-20 11:53:51 +02:00
parent a4dc5f643d
commit a7fd7688ab
13 changed files with 358 additions and 174 deletions

View file

@ -5,8 +5,10 @@ import android.app.Application;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.ArtistDao;
import com.cappielloantonio.play.database.dao.GenreDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Genre;
import java.util.ArrayList;
@ -17,6 +19,7 @@ public class GenreRepository {
private SongGenreCrossDao songGenreCrossDao;
private LiveData<List<Genre>> listLiveGenres;
private LiveData<List<Genre>> listLiveAlbumGenre;
private LiveData<List<Genre>> searchListLiveGenre;
public GenreRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
@ -110,4 +113,48 @@ public class GenreRepository {
genreDao.deleteAll();
}
}
public LiveData<List<Genre>> searchListLiveGenre(String name) {
searchListLiveGenre = genreDao.searchGenre(name);
return searchListLiveGenre;
}
public List<String> getSearchSuggestion(String query) {
List<String> suggestions = new ArrayList<>();
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(genreDao, query, 5);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
suggestions = suggestionsThread.getSuggestions();
} catch (InterruptedException e) {
e.printStackTrace();
}
return suggestions;
}
private static class SearchSuggestionsThreadSafe implements Runnable {
private GenreDao genreDao;
private String query;
private int number;
private List<String> suggestions = new ArrayList<>();
public SearchSuggestionsThreadSafe(GenreDao genreDao, String query, int number) {
this.genreDao = genreDao;
this.query = query;
this.number = number;
}
@Override
public void run() {
suggestions = genreDao.searchSuggestions(query, number);
}
public List<String> getSuggestions() {
return suggestions;
}
}
}

View file

@ -8,20 +8,15 @@ import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.RecentSearchDao;
import com.cappielloantonio.play.model.RecentSearch;
import java.util.ArrayList;
import java.util.List;
public class RecentSearchRepository {
private RecentSearchDao recentSearchDao;
private LiveData<List<RecentSearch>> listLiveRecentSearches;
public RecentSearchRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
recentSearchDao = database.recentSearchDao();
listLiveRecentSearches = recentSearchDao.getLast(3);
}
public LiveData<List<RecentSearch>> getListLiveRecentSearches() {
return listLiveRecentSearches;
}
public void insert(RecentSearch recentSearch) {
@ -30,6 +25,27 @@ public class RecentSearchRepository {
thread.start();
}
public void delete(RecentSearch recentSearch) {
DeleteThreadSafe delete = new DeleteThreadSafe(recentSearchDao, recentSearch);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private RecentSearch recentSearch;
public DeleteThreadSafe(RecentSearchDao recentSearchDao, RecentSearch recentSearch) {
this.recentSearchDao = recentSearchDao;
this.recentSearch = recentSearch;
}
@Override
public void run() {
recentSearchDao.delete(recentSearch);
}
}
private static class InsertThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private RecentSearch recentSearch;
@ -63,4 +79,41 @@ public class RecentSearchRepository {
recentSearchDao.deleteAll();
}
}
public List<String> getRecentSearchSuggestion() {
List<String> recent = new ArrayList<>();
RecentThreadSafe suggestionsThread = new RecentThreadSafe(recentSearchDao,10);
Thread thread = new Thread(suggestionsThread);
thread.start();
try {
thread.join();
recent = suggestionsThread.getRecent();
} catch (InterruptedException e) {
e.printStackTrace();
}
return recent;
}
private static class RecentThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private int limit;
private List<String> recent = new ArrayList<>();
public RecentThreadSafe(RecentSearchDao recentSearchDao, int limit) {
this.recentSearchDao = recentSearchDao;
this.limit = limit;
}
@Override
public void run() {
recent = recentSearchDao.getRecent(limit);
}
public List<String> getRecent() {
return recent;
}
}
}