Implemented download service

This commit is contained in:
CappielloAntonio 2021-07-29 17:12:55 +02:00
parent f09d3b774d
commit 0e41cc20bd
9 changed files with 332 additions and 10 deletions

View file

@ -0,0 +1,122 @@
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.DownloadDao;
import com.cappielloantonio.play.database.dao.QueueDao;
import com.cappielloantonio.play.model.Download;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.QueueUtil;
import java.util.ArrayList;
import java.util.List;
public class DownloadRepository {
private static final String TAG = "QueueRepository";
private DownloadDao downloadDao;
private LiveData<List<Download>> listLiveDownload;
private LiveData<List<Download>> listLiveDownloadSample;
public DownloadRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
downloadDao = database.downloadDao();
}
public LiveData<List<Download>> getLiveDownload() {
listLiveDownload = downloadDao.getAll();
return listLiveDownload;
}
public LiveData<List<Download>> getLiveDownloadSample(int size) {
listLiveDownloadSample = downloadDao.getSample(size);
return listLiveDownloadSample;
}
public void insert(Download download) {
InsertThreadSafe insert = new InsertThreadSafe(downloadDao, download);
Thread thread = new Thread(insert);
thread.start();
}
private static class InsertThreadSafe implements Runnable {
private DownloadDao downloadDao;
private Download download;
public InsertThreadSafe(DownloadDao downloadDao, Download download) {
this.downloadDao = downloadDao;
this.download = download;
}
@Override
public void run() {
downloadDao.insert(download);
}
}
public void insertAll(List<Download> downloads) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(downloadDao, downloads);
Thread thread = new Thread(insertAll);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private DownloadDao downloadDao;
private List<Download> downloads;
public InsertAllThreadSafe(DownloadDao downloadDao, List<Download> downloads) {
this.downloadDao = downloadDao;
this.downloads = downloads;
}
@Override
public void run() {
downloadDao.insertAll(downloads);
}
}
public void deleteAll() {
DeleteAllThreadSafe deleteAll = new DeleteAllThreadSafe(downloadDao);
Thread thread = new Thread(deleteAll);
thread.start();
}
private static class DeleteAllThreadSafe implements Runnable {
private DownloadDao downloadDao;
public DeleteAllThreadSafe(DownloadDao downloadDao) {
this.downloadDao = downloadDao;
}
@Override
public void run() {
downloadDao.deleteAll();
}
}
public void delete(Download download) {
DeleteThreadSafe delete = new DeleteThreadSafe(downloadDao, download);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteThreadSafe implements Runnable {
private DownloadDao downloadDao;
private Download download;
public DeleteThreadSafe(DownloadDao downloadDao, Download download) {
this.downloadDao = downloadDao;
this.download = download;
}
@Override
public void run() {
downloadDao.delete(download.getSongID());
}
}
}