mirror of
https://github.com/CappielloAntonio/tempo.git
synced 2026-02-01 23:13:36 +00:00
Improve search
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
package com.cappielloantonio.play.repository;
|
||||
|
||||
import android.app.Application;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.cappielloantonio.play.database.AppDatabase;
|
||||
import com.cappielloantonio.play.database.dao.AlbumDao;
|
||||
import com.cappielloantonio.play.database.dao.SongDao;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.paulrybitskyi.persistentsearchview.adapters.model.SuggestionItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AlbumRepository {
|
||||
private static final String TAG = "AlbumRepository";
|
||||
|
||||
private AlbumDao albumDao;
|
||||
private LiveData<List<Album>> listLiveAlbums;
|
||||
private LiveData<List<Album>> artistListLiveAlbums;
|
||||
@@ -46,6 +51,23 @@ public class AlbumRepository {
|
||||
return searchListLiveAlbum;
|
||||
}
|
||||
|
||||
public List<String> getSearchSuggestion(String query) {
|
||||
List<String> suggestions = new ArrayList<>();
|
||||
|
||||
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(albumDao, query, 5);
|
||||
Thread thread = new Thread(suggestionsThread);
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
suggestions = suggestionsThread.getSuggestions();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
public boolean exist(Album album) {
|
||||
boolean exist = false;
|
||||
|
||||
@@ -145,4 +167,26 @@ public class AlbumRepository {
|
||||
albumDao.delete(album);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SearchSuggestionsThreadSafe implements Runnable {
|
||||
private AlbumDao albumDao;
|
||||
private String query;
|
||||
private int number;
|
||||
private List<String> suggestions = new ArrayList<>();
|
||||
|
||||
public SearchSuggestionsThreadSafe(AlbumDao albumDao, String query, int number) {
|
||||
this.albumDao = albumDao;
|
||||
this.query = query;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
suggestions = albumDao.searchSuggestions(query, number);
|
||||
}
|
||||
|
||||
public List<String> getSuggestions() {
|
||||
return suggestions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,14 @@ import android.app.Application;
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.cappielloantonio.play.database.AppDatabase;
|
||||
import com.cappielloantonio.play.database.dao.AlbumDao;
|
||||
import com.cappielloantonio.play.database.dao.ArtistDao;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ArtistRepository {
|
||||
@@ -40,6 +42,23 @@ public class ArtistRepository {
|
||||
return searchListLiveArtist;
|
||||
}
|
||||
|
||||
public List<String> getSearchSuggestion(String query) {
|
||||
List<String> suggestions = new ArrayList<>();
|
||||
|
||||
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(artistDao, query, 5);
|
||||
Thread thread = new Thread(suggestionsThread);
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
suggestions = suggestionsThread.getSuggestions();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
public boolean exist(Artist artist) {
|
||||
boolean exist = false;
|
||||
|
||||
@@ -140,4 +159,26 @@ public class ArtistRepository {
|
||||
artistDao.delete(artist);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SearchSuggestionsThreadSafe implements Runnable {
|
||||
private ArtistDao artistDao;
|
||||
private String query;
|
||||
private int number;
|
||||
private List<String> suggestions = new ArrayList<>();
|
||||
|
||||
public SearchSuggestionsThreadSafe(ArtistDao artistDao, String query, int number) {
|
||||
this.artistDao = artistDao;
|
||||
this.query = query;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
suggestions = artistDao.searchSuggestions(query, number);
|
||||
}
|
||||
|
||||
public List<String> getSuggestions() {
|
||||
return suggestions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ import android.app.Application;
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.cappielloantonio.play.database.AppDatabase;
|
||||
import com.cappielloantonio.play.database.dao.AlbumDao;
|
||||
import com.cappielloantonio.play.database.dao.SongDao;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class SongRepository {
|
||||
@@ -74,6 +76,23 @@ public class SongRepository {
|
||||
return listLiveFilteredSongs;
|
||||
}
|
||||
|
||||
public List<String> getSearchSuggestion(String query) {
|
||||
List<String> suggestions = new ArrayList<>();
|
||||
|
||||
SearchSuggestionsThreadSafe suggestionsThread = new SearchSuggestionsThreadSafe(songDao, query, 5);
|
||||
Thread thread = new Thread(suggestionsThread);
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
suggestions = suggestionsThread.getSuggestions();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
public boolean exist(Song song) {
|
||||
boolean exist = false;
|
||||
|
||||
@@ -233,4 +252,26 @@ public class SongRepository {
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SearchSuggestionsThreadSafe implements Runnable {
|
||||
private SongDao songDao;
|
||||
private String query;
|
||||
private int number;
|
||||
private List<String> suggestions = new ArrayList<>();
|
||||
|
||||
public SearchSuggestionsThreadSafe(SongDao songDao, String query, int number) {
|
||||
this.songDao = songDao;
|
||||
this.query = query;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
suggestions = songDao.searchSuggestions(query, number);
|
||||
}
|
||||
|
||||
public List<String> getSuggestions() {
|
||||
return suggestions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user