finished download states on home fragment

handled download states through the recyclerview so that they are correct even if the app is killed and reopened
This commit is contained in:
Denis Çerri 2022-10-05 01:46:58 +02:00
parent b93ed7309e
commit e86b44ee51
No known key found for this signature in database
GPG key ID: 3F50F14A8E7F7A13
12 changed files with 216 additions and 98 deletions

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="192.168.1.131:46883" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-10-04T21:20:22.834747800Z" />
</component>
</project>

View file

@ -156,12 +156,13 @@ public class DownloaderService extends Service {
} }
} }
public void cancelDownload(){ public void cancelDownload(boolean cancelAll){
try{ try{
YoutubeDL.getInstance().destroyProcessById(downloadProcessID); YoutubeDL.getInstance().destroyProcessById(downloadProcessID);
compositeDisposable.clear(); compositeDisposable.clear();
//stopForeground(true); //stopForeground(true);
onDownloadEnd(); if (cancelAll) onDownloadCancel();
else onDownloadEnd();
}catch(Exception err){ }catch(Exception err){
Log.e(TAG, err.getMessage()); Log.e(TAG, err.getMessage());
} }
@ -170,7 +171,7 @@ public class DownloaderService extends Service {
public void removeItemFromDownloadQueue(Video video){ public void removeItemFromDownloadQueue(Video video){
//if its the same video with the same download type as the current downloading one //if its the same video with the same download type as the current downloading one
if (downloadInfo.getVideo().getURL().equals(video.getURL()) && downloadInfo.getVideo().getDownloadedType().equals(video.getDownloadedType())){ if (downloadInfo.getVideo().getURL().equals(video.getURL()) && downloadInfo.getVideo().getDownloadedType().equals(video.getDownloadedType())){
cancelDownload(); cancelDownload(false);
downloadQueue.pop(); downloadQueue.pop();
downloadInfo.setDownloadQueue(downloadQueue); downloadInfo.setDownloadQueue(downloadQueue);
startDownload(downloadQueue); startDownload(downloadQueue);
@ -194,6 +195,19 @@ public class DownloaderService extends Service {
} }
} }
private void onDownloadCancel(){
try{
for (Activity activity: activities.keySet()){
activity.runOnUiThread(() -> {
IDownloaderListener callback = activities.get(activity);
callback.onDownloadCancel(downloadInfo);
});
}
}catch (Exception e){
e.printStackTrace();
}
}
private void onDownloadEnd(){ private void onDownloadEnd(){
try{ try{
for (Activity activity: activities.keySet()){ for (Activity activity: activities.keySet()){

View file

@ -186,7 +186,7 @@ public class MainActivity extends AppCompatActivity{
public void cancelDownloadService(){ public void cancelDownloadService(){
if(!isDownloadServiceRunning) return; if(!isDownloadServiceRunning) return;
iDownloaderService.cancelDownload(); iDownloaderService.cancelDownload(true);
stopDownloadService(); stopDownloadService();
} }

View file

@ -94,28 +94,42 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
MaterialButton musicBtn = buttonLayout.findViewById(R.id.download_music); MaterialButton musicBtn = buttonLayout.findViewById(R.id.download_music);
musicBtn.setTag(videoID + "##audio"); musicBtn.setTag(videoID + "##audio");
musicBtn.setTag(R.id.cancelDownload, "false");
musicBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "audio")); musicBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "audio"));
MaterialButton videoBtn = buttonLayout.findViewById(R.id.download_video); MaterialButton videoBtn = buttonLayout.findViewById(R.id.download_video);
videoBtn.setTag(videoID + "##video"); videoBtn.setTag(videoID + "##video");
videoBtn.setTag(R.id.cancelDownload, "false");
videoBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "video")); videoBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "video"));
// PROGRESS BAR ---------------------------------------------------- // PROGRESS BAR ----------------------------------------------------
LinearProgressIndicator progressBar = card.findViewById(R.id.download_progress); LinearProgressIndicator progressBar = card.findViewById(R.id.download_progress);
progressBar.setVisibility(View.GONE);
progressBar.setTag(videoID + "##progress"); progressBar.setTag(videoID + "##progress");
if(video.isAudioDownloaded() == 1){ if (video.isDownloading()){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded)); progressBar.setVisibility(View.VISIBLE);
}else{ if (video.isDownloadingAudio()){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music)); musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
} musicBtn.setTag(R.id.cancelDownload, "true");
if(video.isVideoDownloaded() == 1){ }else if (video.isDownloadingVideo()){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded)); videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
}else{ videoBtn.setTag(R.id.cancelDownload, "true");
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video)); }
}else {
progressBar.setVisibility(View.GONE);
progressBar.setIndeterminate(true);
if(video.isAudioDownloaded() == 1){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded));
}else{
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music));
}
if(video.isVideoDownloaded() == 1){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded));
}else{
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video));
}
} }
if(checkedVideos.contains(position)){ if(checkedVideos.contains(position)){
@ -167,6 +181,10 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
notifyItemRangeInserted(size, videoList.size()); notifyItemRangeInserted(size, videoList.size());
} }
public void updateVideoListItem(Video v, int position){
videoList.set(position, v);
}
public void clearCheckedVideos(){ public void clearCheckedVideos(){
int size = checkedVideos.size(); int size = checkedVideos.size();
for (int i = 0; i < size; i++){ for (int i = 0; i < size; i++){

View file

@ -10,7 +10,7 @@ import java.util.ArrayList;
public class DBManager extends SQLiteOpenHelper { public class DBManager extends SQLiteOpenHelper {
public static final String db_name = "ytdlnis_db"; public static final String db_name = "ytdlnis_db";
public static final int db_version = 7; public static final int db_version = 9;
public static final String results_table_name = "results"; public static final String results_table_name = "results";
public static final String history_table_name = "history"; public static final String history_table_name = "history";
public static final String id = "id"; public static final String id = "id";
@ -27,6 +27,8 @@ public class DBManager extends SQLiteOpenHelper {
public static final String isPlaylistItem = "isPlaylistItem"; public static final String isPlaylistItem = "isPlaylistItem";
public static final String website = "website"; public static final String website = "website";
public static final String downloadPath = "downloadPath"; public static final String downloadPath = "downloadPath";
public static final String downloadingAudio = "downloadingAudio";
public static final String downloadingVideo = "downloadingVideo";
public DBManager(Context context){ public DBManager(Context context){
@ -46,7 +48,9 @@ public class DBManager extends SQLiteOpenHelper {
+ downloadedAudio + " INTEGER," + downloadedAudio + " INTEGER,"
+ downloadedVideo + " INTEGER," + downloadedVideo + " INTEGER,"
+ isPlaylistItem + " INTENGER," + isPlaylistItem + " INTENGER,"
+ website + " TEXT)"; + website + " TEXT,"
+ downloadingAudio + " INTEGER,"
+ downloadingVideo + " INTEGER)";
sqLiteDatabase.execSQL(query); sqLiteDatabase.execSQL(query);
@ -113,7 +117,7 @@ public class DBManager extends SQLiteOpenHelper {
if(cursor.moveToFirst()){ if(cursor.moveToFirst()){
do { do {
// on below line we are adding the data from cursor to our array list. // on below line we are adding the data from cursor to our array list.
list.add(new Video(cursor.getString(1), //id list.add(new Video(cursor.getString(1), //videoId
cursor.getString(2), //url cursor.getString(2), //url
cursor.getString(3), //title cursor.getString(3), //title
cursor.getString(4), //author cursor.getString(4), //author
@ -122,7 +126,9 @@ public class DBManager extends SQLiteOpenHelper {
cursor.getInt(7), //downloadedAudio cursor.getInt(7), //downloadedAudio
cursor.getInt(8), //downloadedVideo cursor.getInt(8), //downloadedVideo
cursor.getInt(9), //isPlaylistItem cursor.getInt(9), //isPlaylistItem
cursor.getString(10))); //website cursor.getString(10), //website
cursor.getInt(11), //isDownloadingAudio
cursor.getInt(12))); //isDownloadingVideo
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
@ -173,6 +179,8 @@ public class DBManager extends SQLiteOpenHelper {
values.put(downloadedVideo, v.isVideoDownloaded()); values.put(downloadedVideo, v.isVideoDownloaded());
values.put(isPlaylistItem, v.getIsPlaylistItem()); values.put(isPlaylistItem, v.getIsPlaylistItem());
values.put(website, v.getWebsite()); values.put(website, v.getWebsite());
values.put(downloadingAudio, 0);
values.put(downloadingVideo, 0);
db.insert(results_table_name, null, values); db.insert(results_table_name, null, values);
} }
@ -198,22 +206,34 @@ public class DBManager extends SQLiteOpenHelper {
db.close(); db.close();
} }
public void updateDownloadStatusOnResult(String id, String type){ public void updateDownloadStatusOnResult(String id, String type, boolean downloaded){
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
switch (type){ switch (type){
case "audio": case "audio":
values.put(downloadedAudio, 1); if (downloaded) values.put(downloadedAudio, 1);
values.put(downloadingAudio, 0);
break; break;
case "video": case "video":
values.put(downloadedVideo, 1); if (downloaded) values.put(downloadedVideo, 1);
values.put(downloadingVideo, 0);
break; break;
} }
db.update(results_table_name, values, "videoId = ?", new String[]{id}); db.update(results_table_name, values, "videoId = ?", new String[]{id});
} }
public void updateDownloadingStatusOnResult(String id, String type, boolean downloading){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
type = (type.equals("audio")) ? downloadingAudio : downloadingVideo;
int value = (downloading) ? 1 : 0;
values.put(type, value);
db.update(results_table_name, values, "videoId = ?", new String[]{id});
}
public int checkDownloaded(String url, String downloadType){ public int checkDownloaded(String url, String downloadType){
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + history_table_name + " WHERE url='" + Cursor cursor = db.rawQuery("SELECT * FROM " + history_table_name + " WHERE url='" +

View file

@ -18,10 +18,12 @@ public class Video implements Parcelable {
private int isPlaylistItem; private int isPlaylistItem;
private String downloadPath; private String downloadPath;
private String website; private String website;
private boolean downloadingAudio;
private boolean downloadingVideo;
// RESULTS OBJECT // RESULTS OBJECT
public Video(String videoId, String url, String title, String author, String duration, String thumb, public Video(String videoId, String url, String title, String author, String duration, String thumb,
int downloadedAudio, int downloadedVideo, int isPlaylistItem, String website) { int downloadedAudio, int downloadedVideo, int isPlaylistItem, String website, int downloadingAudio, int downloadingVideo) {
this.videoId = videoId; this.videoId = videoId;
this.url = url; this.url = url;
this.title = title; this.title = title;
@ -32,6 +34,8 @@ public class Video implements Parcelable {
this.downloadedAudio = downloadedAudio; this.downloadedAudio = downloadedAudio;
this.isPlaylistItem = isPlaylistItem; this.isPlaylistItem = isPlaylistItem;
this.website = website; this.website = website;
this.downloadingAudio = intToBoolean(downloadingAudio);
this.downloadingVideo = intToBoolean(downloadingVideo);
} }
//HISTORY OBJECT //HISTORY OBJECT
@ -49,6 +53,9 @@ public class Video implements Parcelable {
this.website = website; this.website = website;
} }
private boolean intToBoolean(int i){
return i == 1;
}
protected Video(Parcel in) { protected Video(Parcel in) {
id = in.readInt(); id = in.readInt();
@ -199,6 +206,26 @@ public class Video implements Parcelable {
this.downloadPath = downloadPath; this.downloadPath = downloadPath;
} }
public boolean isDownloadingAudio() {
return downloadingAudio;
}
public void setDownloadingAudio(boolean downloadingAudio) {
this.downloadingAudio = downloadingAudio;
}
public boolean isDownloadingVideo() {
return downloadingVideo;
}
public void setDownloadingVideo(boolean downloadingVideo) {
this.downloadingVideo = downloadingVideo;
}
public boolean isDownloading() {
return this.downloadingAudio || this.downloadingVideo;
}
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;
@ -239,7 +266,8 @@ public class Video implements Parcelable {
", isPlaylistItem=" + isPlaylistItem + ", isPlaylistItem=" + isPlaylistItem +
", downloadPath='" + downloadPath + '\'' + ", downloadPath='" + downloadPath + '\'' +
", website='" + website + '\'' + ", website='" + website + '\'' +
", downloadingAudio=" + downloadingAudio +
", downloadingVideo=" + downloadingVideo +
'}'; '}';
} }
} }

View file

@ -91,6 +91,9 @@ public class CustomCommandActivity extends AppCompatActivity {
swapFabs(); swapFabs();
} }
@Override
public void onDownloadCancel(DownloadInfo downloadInfo) {}
public void onDownloadServiceEnd() { public void onDownloadServiceEnd() {
stopDownloadService(); stopDownloadService();
@ -160,7 +163,7 @@ public class CustomCommandActivity extends AppCompatActivity {
public void cancelDownloadService(){ public void cancelDownloadService(){
if(!isDownloadServiceRunning) return; if(!isDownloadServiceRunning) return;
iDownloaderService.cancelDownload(); iDownloaderService.cancelDownload(false);
stopDownloadService(); stopDownloadService();
} }

View file

@ -94,6 +94,11 @@ public class HistoryFragment extends Fragment implements HistoryRecyclerViewAdap
}catch(Exception ignored){} }catch(Exception ignored){}
} }
@Override
public void onDownloadCancel(DownloadInfo downloadInfo) {
}
public void onDownloadServiceEnd() {} public void onDownloadServiceEnd() {}
}; };

View file

@ -86,18 +86,16 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
downloadInfo = info; downloadInfo = info;
try{ try{
if(downloadInfo != null){ if(downloadInfo != null){
String id = downloadInfo.getVideo().getVideoId(); Video video = downloadInfo.getVideo();
String type = downloadInfo.getVideo().getDownloadedType(); String id = video.getVideoId();
video = findVideo(id);
recyclerView.smoothScrollToPosition(resultObjects.indexOf(findVideo(id))); int index = resultObjects.indexOf(video);
MaterialButton clickedButton = recyclerView.findViewWithTag(id +"##"+type); recyclerView.smoothScrollToPosition(index);
progressBar = recyclerView.findViewWithTag(id + "##progress"); updateDownloadingStatusOnResult(video, video.getDownloadedType(), true);
progressBar.setVisibility(View.VISIBLE); homeRecyclerViewAdapter.notifyItemChanged(index);
downloading = true; downloading = true;
progressBar = fragmentView.findViewWithTag(downloadInfo.getVideo().getVideoId()+"##progress");
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(true); topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(true);
clickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
clickedButton.setTag(R.id.cancelDownload, "true");
} }
}catch(Exception ignored){} }catch(Exception ignored){}
} }
@ -110,14 +108,8 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
int progress = downloadInfo.getProgress(); int progress = downloadInfo.getProgress();
if (progress > 0) { if (progress > 0) {
progressBar = fragmentView.findViewWithTag(downloadInfo.getVideo().getVideoId()+"##progress"); progressBar = fragmentView.findViewWithTag(downloadInfo.getVideo().getVideoId()+"##progress");
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgressCompat(progress, true); progressBar.setProgressCompat(progress, true);
} }
String id = downloadInfo.getVideo().getVideoId();
String type = downloadInfo.getVideo().getDownloadedType();
MaterialButton clickedButton = recyclerView.findViewWithTag(id + "##"+type);
clickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
clickedButton.setTag(R.id.cancelDownload, "true");
}catch(Exception ignored){} }catch(Exception ignored){}
}); });
} }
@ -125,46 +117,31 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
public void onDownloadError(DownloadInfo info){ public void onDownloadError(DownloadInfo info){
downloadInfo = info; downloadInfo = info;
try{ try{
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
downloading = false;
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false);
Video item = downloadInfo.getVideo(); Video item = downloadInfo.getVideo();
String id = item.getVideoId(); String id = item.getVideoId();
String type = item.getDownloadedType(); String type = item.getDownloadedType();
MaterialButton theClickedButton = recyclerView.findViewWithTag(id + "##"+type); updateDownloadStatusOnResult(item, type, false);
if (theClickedButton != null) {
if (type.equals("audio")) { item = findVideo(id);
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded)); if (type.equals("audio")) item.setDownloadingAudio(false);
} else { else if (type.equals("video")) item.setDownloadingVideo(false);
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded)); homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(item));
}
theClickedButton.setTag(R.id.cancelDownload, "false"); downloading = false;
} topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false);
}catch(Exception ignored){} }catch(Exception ignored){}
} }
public void onDownloadEnd(DownloadInfo info) { public void onDownloadEnd(DownloadInfo info) {
downloadInfo = info; downloadInfo = info;
try{ try{
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
Video item = downloadInfo.getVideo(); Video item = downloadInfo.getVideo();
String id = item.getVideoId(); String id = item.getVideoId();
String type = item.getDownloadedType(); String type = item.getDownloadedType();
MaterialButton theClickedButton = recyclerView.findViewWithTag(id + "##"+type); item = findVideo(id);
updateDownloadingStatusOnResult(item, type, false);
if (theClickedButton != null) { homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(item));
if (type.equals("audio")) {
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded));
} else {
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded));
}
theClickedButton.setTag(R.id.cancelDownload, "false");
}
downloading = false; downloading = false;
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false); topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false);
@ -187,11 +164,31 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
for (int i = 0; i < files.size(); i++) paths[i] = files.get(i).getAbsolutePath(); for (int i = 0; i < files.size(); i++) paths[i] = files.get(i).getAbsolutePath();
MediaScannerConnection.scanFile(context, paths, null, null); MediaScannerConnection.scanFile(context, paths, null, null);
addToHistory(item, new Date(), paths); addToHistory(item, new Date(), paths);
updateDownloadStatusOnResult(item, type); updateDownloadStatusOnResult(item, type, true);
mainActivity.updateHistoryFragment(); mainActivity.updateHistoryFragment();
}catch(Exception ignored){} }catch(Exception ignored){}
} }
@Override
public void onDownloadCancel(DownloadInfo info) {
downloadInfo = info;
try{
while (!info.getDownloadQueue().isEmpty()){
Video item = downloadInfo.getDownloadQueue().pop();
String id = item.getVideoId();
String type = item.getDownloadedType();
item = findVideo(id);
updateDownloadingStatusOnResult(item, type, false);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(item));
}
downloading = false;
topAppBar.getMenu().findItem(R.id.cancel_download).setVisible(false);
}catch(Exception ignored){}
}
public void onDownloadServiceEnd() { public void onDownloadServiceEnd() {
mainActivity.stopDownloadService(); mainActivity.stopDownloadService();
@ -277,10 +274,12 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
try { try {
infoUtil = new InfoUtil(context); infoUtil = new InfoUtil(context);
resultObjects = infoUtil.getTrending(context); resultObjects = infoUtil.getTrending(context);
dbManager.addToResults(resultObjects);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
} }
dbManager.close();
if (resultObjects != null) { if (resultObjects != null) {
uiHandler.post(this::scrollToTop); uiHandler.post(this::scrollToTop);
if (resultObjects.size() > 1 && resultObjects.get(1).getIsPlaylistItem() == 1) { if (resultObjects.size() > 1 && resultObjects.get(1).getIsPlaylistItem() == 1) {
@ -288,7 +287,6 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
} }
} }
uiHandler.post(() -> { uiHandler.post(() -> {
homeRecyclerViewAdapter.setVideoList(resultObjects, true); homeRecyclerViewAdapter.setVideoList(resultObjects, true);
shimmerCards.stopShimmer(); shimmerCards.stopShimmer();
@ -327,6 +325,8 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
searchView.setInputType(InputType.TYPE_TEXT_VARIATION_URI); searchView.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
searchView.setQueryHint(getString(R.string.search_hint)); searchView.setQueryHint(getString(R.string.search_hint));
dbManager = new DBManager(context);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
@ -349,6 +349,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
int itemId = m.getItemId(); int itemId = m.getItemId();
if(itemId == R.id.delete_results){ if(itemId == R.id.delete_results){
dbManager.clearResults(); dbManager.clearResults();
dbManager.close();
selectedObjects = new ArrayList<>(); selectedObjects = new ArrayList<>();
downloadAllFab.setVisibility(View.GONE); downloadAllFab.setVisibility(View.GONE);
downloadFabs.setVisibility(View.GONE); downloadFabs.setVisibility(View.GONE);
@ -357,21 +358,14 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
try{ try{
mainActivity.cancelDownloadService(); mainActivity.cancelDownloadService();
topAppBar.getMenu().findItem(itemId).setVisible(false); topAppBar.getMenu().findItem(itemId).setVisible(false);
for (int i = 0; i < downloadInfo.getDownloadQueue().size(); i++){
Video vid = downloadInfo.getDownloadQueue().get(i);
String type = vid.getDownloadedType();
updateDownloadingStatusOnResult(vid, type, false);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(vid));
}
downloadQueue = new ArrayList<>(); downloadQueue = new ArrayList<>();
downloading = false; downloading = false;
String id = progressBar.getTag().toString().split("##progress")[0];
progressBar.setVisibility(View.GONE);
String type = findVideo(id).getDownloadedType();
MaterialButton theClickedButton = recyclerView.findViewWithTag(id + "##"+type);
if (theClickedButton != null) {
if (type.equals("audio")) {
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_stopped));
} else {
theClickedButton.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_stopped));
}
}
}catch(Exception ignored){} }catch(Exception ignored){}
} }
return true; return true;
@ -431,6 +425,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
} }
dbManager.clearResults(); dbManager.clearResults();
dbManager.addToResults(resultObjects); dbManager.addToResults(resultObjects);
dbManager.close();
new Handler(Looper.getMainLooper()).post(() -> { new Handler(Looper.getMainLooper()).post(() -> {
homeRecyclerViewAdapter.setVideoList(resultObjects, true); homeRecyclerViewAdapter.setVideoList(resultObjects, true);
shimmerCards.stopShimmer(); shimmerCards.stopShimmer();
@ -472,7 +467,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
dbManager.clearResults(); dbManager.clearResults();
dbManager.addToResults(resultObjects); dbManager.addToResults(resultObjects);
dbManager.close();
new Handler(Looper.getMainLooper()).post(() -> { new Handler(Looper.getMainLooper()).post(() -> {
homeRecyclerViewAdapter.setVideoList(resultObjects, true); homeRecyclerViewAdapter.setVideoList(resultObjects, true);
shimmerCards.stopShimmer(); shimmerCards.stopShimmer();
@ -497,6 +492,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
try { try {
String nextPageToken = ""; String nextPageToken = "";
dbManager.clearResults(); dbManager.clearResults();
dbManager.close();
resultObjects.clear(); resultObjects.clear();
homeRecyclerViewAdapter.setVideoList(new ArrayList<>(), true); homeRecyclerViewAdapter.setVideoList(new ArrayList<>(), true);
do { do {
@ -510,6 +506,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
shimmerCards.setVisibility(View.GONE); shimmerCards.setVisibility(View.GONE);
}); });
dbManager.addToResults(tmp_vids); dbManager.addToResults(tmp_vids);
dbManager.close();
if (tmp_token.isEmpty()) break; if (tmp_token.isEmpty()) break;
if (tmp_token.equals(nextPageToken)) break; if (tmp_token.equals(nextPageToken)) break;
nextPageToken = tmp_token; nextPageToken = tmp_token;
@ -545,6 +542,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
resultObjects.addAll(video); resultObjects.addAll(video);
dbManager.clearResults(); dbManager.clearResults();
dbManager.addToResults(resultObjects); dbManager.addToResults(resultObjects);
dbManager.close();
} }
new Handler(Looper.getMainLooper()).post(() -> { new Handler(Looper.getMainLooper()).post(() -> {
@ -591,16 +589,15 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
try { try {
if (btn.getTag(R.id.cancelDownload).equals("true")){ if (btn.getTag(R.id.cancelDownload).equals("true")){
mainActivity.removeItemFromDownloadQueue(vid); mainActivity.removeItemFromDownloadQueue(vid);
int icon = (type.equals("audio")) ? R.drawable.ic_music : R.drawable.ic_video; updateDownloadingStatusOnResult(vid, type, false);
btn.setIcon(ContextCompat.getDrawable(activity, icon)); homeRecyclerViewAdapter.notifyItemChanged(position);
btn.setTag(R.id.cancelDownload, "false");
return; return;
} }
}catch (Exception ignored){} }catch (Exception ignored){}
} }
downloadQueue.add(vid); downloadQueue.add(vid);
btn.setTag(R.id.cancelDownload, "true"); updateDownloadingStatusOnResult(vid, type, true);
btn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel)); homeRecyclerViewAdapter.notifyItemChanged(position);
if (isStoragePermissionGranted()){ if (isStoragePermissionGranted()){
mainActivity.startDownloadService(downloadQueue, listener); mainActivity.startDownloadService(downloadQueue, listener);
downloadQueue.clear(); downloadQueue.clear();
@ -643,22 +640,35 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
video.setDownloadedTime(downloadedTime); video.setDownloadedTime(downloadedTime);
video.setDownloadPath(path); video.setDownloadPath(path);
dbManager.addToHistory(video); dbManager.addToHistory(video);
dbManager.close();
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
} }
public void updateDownloadStatusOnResult(Video v, String type) { public void updateDownloadStatusOnResult(Video v, String type, boolean downloaded) {
if (v != null) { if (v != null) {
dbManager = new DBManager(context); dbManager = new DBManager(context);
try { try {
dbManager.updateDownloadStatusOnResult(v.getVideoId(), type); dbManager.updateDownloadStatusOnResult(v.getVideoId(), type, downloaded);
dbManager.close();
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
} }
public void updateDownloadingStatusOnResult(Video v, String type, boolean downloading) {
if (v != null) {
if (type.equals("audio")) v.setDownloadingAudio(downloading);
else if (type.equals("video")) v.setDownloadingVideo(downloading);
homeRecyclerViewAdapter.updateVideoListItem(v, resultObjects.indexOf(v));
dbManager = new DBManager(context);
try {
dbManager.updateDownloadingStatusOnResult(v.getVideoId(), type, downloading);
dbManager.close();
} catch (Exception ignored) {}
}
}
@Override @Override
public void onCardClick(int position, boolean add) { public void onCardClick(int position, boolean add) {
@ -693,6 +703,8 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
for (int i = 0; i < selectedObjects.size(); i++) { for (int i = 0; i < selectedObjects.size(); i++) {
Video vid = findVideo(selectedObjects.get(i).getVideoId()); Video vid = findVideo(selectedObjects.get(i).getVideoId());
vid.setDownloadedType(buttonData[1]); vid.setDownloadedType(buttonData[1]);
updateDownloadingStatusOnResult(vid, buttonData[1], true);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(vid));
downloadQueue.add(vid); downloadQueue.add(vid);
} }
selectedObjects = new ArrayList<>(); selectedObjects = new ArrayList<>();
@ -765,13 +777,11 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
for (int i = start; i < end; i++){ for (int i = start; i < end; i++){
Video vid = findVideo(resultObjects.get(i).getVideoId()); Video vid = findVideo(resultObjects.get(i).getVideoId());
vid.setDownloadedType(type); vid.setDownloadedType(type);
updateDownloadingStatusOnResult(vid, type, true);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(vid));
downloadQueue.add(vid); downloadQueue.add(vid);
} }
if (downloading) {
Toast.makeText(context, R.string.added_to_queue, Toast.LENGTH_LONG).show();
return;
}
if(isStoragePermissionGranted()){ if(isStoragePermissionGranted()){
mainActivity.startDownloadService(downloadQueue, listener); mainActivity.startDownloadService(downloadQueue, listener);
downloadQueue.clear(); downloadQueue.clear();

View file

@ -5,5 +5,6 @@ public interface IDownloaderListener {
void onDownloadProgress(DownloadInfo downloadInfo); void onDownloadProgress(DownloadInfo downloadInfo);
void onDownloadError(DownloadInfo downloadInfo); void onDownloadError(DownloadInfo downloadInfo);
void onDownloadEnd(DownloadInfo downloadInfo); void onDownloadEnd(DownloadInfo downloadInfo);
void onDownloadCancel(DownloadInfo downloadInfo);
void onDownloadServiceEnd(); void onDownloadServiceEnd();
} }

View file

@ -9,6 +9,6 @@ public interface IDownloaderService {
void addActivity(Activity activity, IDownloaderListener callback); void addActivity(Activity activity, IDownloaderListener callback);
void removeActivity(Activity activity); void removeActivity(Activity activity);
void updateQueue(ArrayList<Video> queue); void updateQueue(ArrayList<Video> queue);
void cancelDownload(); void cancelDownload(boolean cancelAll);
void removeItemFromDownloadQueue(Video video); void removeItemFromDownloadQueue(Video video);
} }

View file

@ -193,7 +193,7 @@ public class InfoUtil {
int downloadedVideo = dbManager.checkDownloaded(url, "video"); int downloadedVideo = dbManager.checkDownloaded(url, "video");
int isPLaylist = 0; int isPLaylist = 0;
video = new Video(id, url, title, author, duration, thumb, downloadedAudio, downloadedVideo, isPLaylist, "youtube"); video = new Video(id, url, title, author, duration, thumb, downloadedAudio, downloadedVideo, isPLaylist, "youtube", 0, 0);
}catch(Exception e){ }catch(Exception e){
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@ -319,7 +319,9 @@ public class InfoUtil {
dbManager.checkDownloaded(url, "audio"), dbManager.checkDownloaded(url, "audio"),
dbManager.checkDownloaded(url, "video"), dbManager.checkDownloaded(url, "video"),
isPlaylist, isPlaylist,
website) website,
0,
0)
); );
} }
}catch(Exception e){ }catch(Exception e){