Add search

This commit is contained in:
Antonio Cappiello 2020-11-21 13:54:49 +01:00
parent 6eff64e7e1
commit 8c889f7a38
39 changed files with 999 additions and 93 deletions

View file

@ -0,0 +1,67 @@
package com.cappielloantonio.play.repository;
import android.app.Application;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.RecentSearchDao;
import com.cappielloantonio.play.model.RecentSearch;
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) {
InsertThreadSafe insert = new InsertThreadSafe(recentSearchDao, recentSearch);
Thread thread = new Thread(insert);
thread.start();
}
public void deleteAll() {
DeleteAllThreadSafe delete = new DeleteAllThreadSafe(recentSearchDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
private RecentSearch recentSearch;
public InsertThreadSafe(RecentSearchDao recentSearchDao, RecentSearch recentSearch) {
this.recentSearchDao = recentSearchDao;
this.recentSearch = recentSearch;
}
@Override
public void run() {
recentSearchDao.insert(recentSearch);
}
}
private static class DeleteAllThreadSafe implements Runnable {
private RecentSearchDao recentSearchDao;
public DeleteAllThreadSafe(RecentSearchDao recentSearchDao) {
this.recentSearchDao = recentSearchDao;
}
@Override
public void run() {
recentSearchDao.deleteAll();
}
}
}

View file

@ -14,17 +14,23 @@ import java.util.List;
public class SongRepository {
private SongDao songDao;
private LiveData<List<Song>> listLiveSongs;
private LiveData<List<Song>> searchListLiveSongs;
public SongRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
songDao = database.songDao();
listLiveSongs = songDao.getAll();
}
public LiveData<List<Song>> getListLiveSongs() {
listLiveSongs = songDao.getAll();
return listLiveSongs;
}
public LiveData<List<Song>> searchListLiveSongs(String title) {
searchListLiveSongs = songDao.searchSong(title);
return searchListLiveSongs;
}
public boolean exist(Song song) {
boolean exist = false;
@ -35,8 +41,7 @@ public class SongRepository {
try {
thread.join();
exist = existThread.exist();
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}