mirror of https://github.com/deniscerri/ytdlnis
fixed some things in download card
parent
2371ab734d
commit
19eab5e054
@ -1,214 +0,0 @@
|
||||
package com.deniscerri.ytdlnis.adapter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorMatrix;
|
||||
import android.graphics.ColorMatrixColorFilter;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.deniscerri.ytdlnis.R;
|
||||
import com.deniscerri.ytdlnis.database.models.HistoryItem;
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
import com.google.android.material.card.MaterialCardView;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class HistoryAdapter extends ListAdapter<HistoryItem, HistoryAdapter.ViewHolder> {
|
||||
private ArrayList<Integer> checkedItems;
|
||||
private final OnItemClickListener onItemClickListener;
|
||||
private Activity activity;
|
||||
|
||||
public HistoryAdapter(OnItemClickListener onItemClickListener, Activity activity) {
|
||||
super(DIFF_CALLBACK);
|
||||
this.checkedItems = new ArrayList<>();
|
||||
this.onItemClickListener = onItemClickListener;
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
private static final DiffUtil.ItemCallback<HistoryItem> DIFF_CALLBACK = new DiffUtil.ItemCallback<HistoryItem>() {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull HistoryItem oldItem, @NonNull HistoryItem newItem) {
|
||||
return oldItem.getId() == newItem.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull HistoryItem oldItem, @NonNull HistoryItem newItem) {
|
||||
return oldItem.getTime() == newItem.getTime();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
return super.getItemViewType(position);
|
||||
}
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private MaterialCardView cardView;
|
||||
|
||||
public ViewHolder(@NonNull View itemView, OnItemClickListener onItemClickListener) {
|
||||
super(itemView);
|
||||
cardView = itemView.findViewById(R.id.downloads_card_view);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View cardView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.downloads_card, parent, false);
|
||||
|
||||
return new HistoryAdapter.ViewHolder(cardView, onItemClickListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
HistoryItem video = getItem(position);
|
||||
MaterialCardView card = holder.cardView;
|
||||
// THUMBNAIL ----------------------------------
|
||||
ImageView thumbnail = card.findViewById(R.id.downloads_image_view);
|
||||
String imageURL= video.getThumb();
|
||||
|
||||
Handler uiHandler = new Handler(Looper.getMainLooper());
|
||||
if (!imageURL.isEmpty()){
|
||||
uiHandler.post(() -> Picasso.get().load(imageURL).into(thumbnail));
|
||||
}else {
|
||||
uiHandler.post(() -> Picasso.get().load(R.color.black).into(thumbnail));
|
||||
}
|
||||
thumbnail.setColorFilter(Color.argb(95, 0, 0, 0));
|
||||
|
||||
// TITLE ----------------------------------
|
||||
TextView videoTitle = card.findViewById(R.id.downloads_title);
|
||||
String title = video.getTitle();
|
||||
|
||||
if(title.length() > 100){
|
||||
title = title.substring(0, 40) + "...";
|
||||
}
|
||||
videoTitle.setText(title);
|
||||
|
||||
// Bottom Info ----------------------------------
|
||||
TextView bottomInfo = card.findViewById(R.id.downloads_info_bottom);
|
||||
String info = video.getAuthor();
|
||||
if (!video.getDuration().isEmpty()){
|
||||
if (!video.getAuthor().isEmpty()) info += " • ";
|
||||
info += video.getDuration();
|
||||
}
|
||||
bottomInfo.setText(info);
|
||||
|
||||
// TIME DOWNLOADED ----------------------------------
|
||||
TextView datetime = card.findViewById(R.id.downloads_info_time);
|
||||
long time = video.getTime();
|
||||
String downloadedTime;
|
||||
if (time == 0){
|
||||
downloadedTime = activity.getString(R.string.currently_downloading) + " " + video.getType();
|
||||
}else{
|
||||
Calendar cal = Calendar.getInstance();
|
||||
Date date = new Date(time*1000L);
|
||||
cal.setTime(date);
|
||||
int day = cal.get(Calendar.DAY_OF_MONTH);
|
||||
String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
|
||||
int year = cal.get(Calendar.YEAR);
|
||||
|
||||
DateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
|
||||
String timeString = formatter.format(date);
|
||||
downloadedTime = day + " " + month + " " + year + " - " + timeString;
|
||||
}
|
||||
datetime.setText(downloadedTime);
|
||||
|
||||
// BUTTON ----------------------------------
|
||||
LinearLayout buttonLayout = card.findViewById(R.id.downloads_download_button_layout);
|
||||
MaterialButton btn = buttonLayout.findViewById(R.id.downloads_download_button_type);
|
||||
|
||||
boolean filePresent = true;
|
||||
|
||||
//IS IN THE FILE SYSTEM?
|
||||
String path = video.getDownloadPath();
|
||||
File file = new File(path);
|
||||
if(!file.exists() && !path.isEmpty()){
|
||||
filePresent = false;
|
||||
thumbnail.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(){{setSaturation(0f);}}));
|
||||
thumbnail.setAlpha(0.7f);
|
||||
}
|
||||
|
||||
if(!video.getType().isEmpty()){
|
||||
if(video.getType().equals("audio")){
|
||||
if (filePresent) btn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded));
|
||||
else btn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music));
|
||||
}else{
|
||||
if (filePresent) btn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded));
|
||||
else btn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video));
|
||||
}
|
||||
}
|
||||
|
||||
if (btn.hasOnClickListeners()) btn.setOnClickListener(null);
|
||||
|
||||
if(checkedItems.contains(position)){
|
||||
card.setChecked(true);
|
||||
card.setStrokeWidth(5);
|
||||
}else{
|
||||
card.setChecked(false);
|
||||
card.setStrokeWidth(0);
|
||||
}
|
||||
|
||||
boolean finalFilePresent = filePresent;
|
||||
card.setOnLongClickListener(view -> {
|
||||
checkCard(card, position);
|
||||
return true;
|
||||
});
|
||||
card.setOnClickListener(view -> {
|
||||
if(checkedItems.size() > 0){
|
||||
checkCard(card, video.getId());
|
||||
}else{
|
||||
onItemClickListener.onCardClick(video.getId(), finalFilePresent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void checkCard(MaterialCardView card, int videoID){
|
||||
if(card.isChecked()){
|
||||
card.setStrokeWidth(0);
|
||||
checkedItems.remove(videoID);
|
||||
}else{
|
||||
card.setStrokeWidth(5);
|
||||
checkedItems.add(videoID);
|
||||
}
|
||||
card.setChecked(!card.isChecked());
|
||||
onItemClickListener.onCardSelect(videoID, card.isChecked());
|
||||
}
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onCardClick(int position, boolean isPresent);
|
||||
void onCardSelect(int position, boolean isChecked);
|
||||
void onButtonClick(int position);
|
||||
}
|
||||
|
||||
public void clearCheckedVideos(){
|
||||
int size = checkedItems.size();
|
||||
for (int i = 0; i < size; i++){
|
||||
int position = checkedItems.get(i);
|
||||
notifyItemChanged(position);
|
||||
}
|
||||
checkedItems.clear();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,189 @@
|
||||
package com.deniscerri.ytdlnis.adapter
|
||||
|
||||
import android.app.Activity
|
||||
import android.graphics.Color
|
||||
import android.graphics.ColorMatrix
|
||||
import android.graphics.ColorMatrixColorFilter
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.AsyncDifferConfig
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.deniscerri.ytdlnis.R
|
||||
import com.deniscerri.ytdlnis.database.models.HistoryItem
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
import com.squareup.picasso.Picasso
|
||||
import java.io.File
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class HistoryAdapter(onItemClickListener: OnItemClickListener, activity: Activity) : ListAdapter<HistoryItem?, HistoryAdapter.ViewHolder>(AsyncDifferConfig.Builder(DIFF_CALLBACK).build()) {
|
||||
private val checkedItems: ArrayList<Int>
|
||||
private val onItemClickListener: OnItemClickListener
|
||||
private val activity: Activity
|
||||
|
||||
init {
|
||||
checkedItems = ArrayList()
|
||||
this.onItemClickListener = onItemClickListener
|
||||
this.activity = activity
|
||||
}
|
||||
|
||||
class ViewHolder(itemView: View, onItemClickListener: OnItemClickListener?) : RecyclerView.ViewHolder(itemView) {
|
||||
val cardView: MaterialCardView
|
||||
|
||||
init {
|
||||
cardView = itemView.findViewById(R.id.downloads_card_view)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val cardView = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.downloads_card, parent, false)
|
||||
return ViewHolder(cardView, onItemClickListener)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val video = getItem(position)
|
||||
val card = holder.cardView
|
||||
// THUMBNAIL ----------------------------------
|
||||
val thumbnail = card.findViewById<ImageView>(R.id.downloads_image_view)
|
||||
val imageURL = video!!.thumb
|
||||
val uiHandler = Handler(Looper.getMainLooper())
|
||||
if (imageURL.isNotEmpty()) {
|
||||
uiHandler.post { Picasso.get().load(imageURL).into(thumbnail) }
|
||||
} else {
|
||||
uiHandler.post { Picasso.get().load(R.color.black).into(thumbnail) }
|
||||
}
|
||||
thumbnail.setColorFilter(Color.argb(95, 0, 0, 0))
|
||||
|
||||
// TITLE ----------------------------------
|
||||
val videoTitle = card.findViewById<TextView>(R.id.downloads_title)
|
||||
var title = video.title
|
||||
if (title.length > 100) {
|
||||
title = title.substring(0, 40) + "..."
|
||||
}
|
||||
videoTitle.text = title
|
||||
|
||||
// Bottom Info ----------------------------------
|
||||
val bottomInfo = card.findViewById<TextView>(R.id.downloads_info_bottom)
|
||||
var info = video.author
|
||||
if (video.duration.isNotEmpty()) {
|
||||
if (video.author.isNotEmpty()) info += " • "
|
||||
info += video.duration
|
||||
}
|
||||
bottomInfo.text = info
|
||||
|
||||
// TIME DOWNLOADED ----------------------------------
|
||||
val datetime = card.findViewById<TextView>(R.id.downloads_info_time)
|
||||
val time = video.time
|
||||
val downloadedTime: String
|
||||
if (time == 0L) {
|
||||
downloadedTime = activity.getString(R.string.currently_downloading) + " " + video.type
|
||||
} else {
|
||||
val cal = Calendar.getInstance()
|
||||
val date = Date(time * 1000L)
|
||||
cal.time = date
|
||||
val day = cal[Calendar.DAY_OF_MONTH]
|
||||
val month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())
|
||||
val year = cal[Calendar.YEAR]
|
||||
val formatter: DateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
|
||||
val timeString = formatter.format(date)
|
||||
downloadedTime = "$day $month $year - $timeString"
|
||||
}
|
||||
datetime.text = downloadedTime
|
||||
|
||||
// BUTTON ----------------------------------
|
||||
val buttonLayout = card.findViewById<LinearLayout>(R.id.downloads_download_button_layout)
|
||||
val btn = buttonLayout.findViewById<MaterialButton>(R.id.downloads_download_button_type)
|
||||
var filePresent = true
|
||||
|
||||
//IS IN THE FILE SYSTEM?
|
||||
val path = video.downloadPath
|
||||
val file = File(path)
|
||||
if (!file.exists() && path.isNotEmpty()) {
|
||||
filePresent = false
|
||||
thumbnail.colorFilter = ColorMatrixColorFilter(object : ColorMatrix() {
|
||||
init {
|
||||
setSaturation(0f)
|
||||
}
|
||||
})
|
||||
thumbnail.alpha = 0.7f
|
||||
}
|
||||
if (video.type.isNotEmpty()) {
|
||||
if (video.type == "audio") {
|
||||
if (filePresent) btn.icon = ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded) else btn.icon = ContextCompat.getDrawable(activity, R.drawable.ic_music)
|
||||
} else {
|
||||
if (filePresent) btn.icon = ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded) else btn.icon = ContextCompat.getDrawable(activity, R.drawable.ic_video)
|
||||
}
|
||||
}
|
||||
if (btn.hasOnClickListeners()) btn.setOnClickListener(null)
|
||||
if (checkedItems.contains(position)) {
|
||||
card.isChecked = true
|
||||
card.strokeWidth = 5
|
||||
} else {
|
||||
card.isChecked = false
|
||||
card.strokeWidth = 0
|
||||
}
|
||||
val finalFilePresent = filePresent
|
||||
card.setOnLongClickListener {
|
||||
checkCard(card, position)
|
||||
true
|
||||
}
|
||||
card.setOnClickListener {
|
||||
if (checkedItems.size > 0) {
|
||||
checkCard(card, video.id!!)
|
||||
} else {
|
||||
onItemClickListener.onCardClick(video.id!!, finalFilePresent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkCard(card: MaterialCardView, videoID: Int) {
|
||||
if (card.isChecked) {
|
||||
card.strokeWidth = 0
|
||||
checkedItems.removeAt(videoID)
|
||||
} else {
|
||||
card.strokeWidth = 5
|
||||
checkedItems.add(videoID)
|
||||
}
|
||||
card.isChecked = !card.isChecked
|
||||
onItemClickListener.onCardSelect(videoID, card.isChecked)
|
||||
}
|
||||
|
||||
interface OnItemClickListener {
|
||||
fun onCardClick(position: Int, isPresent: Boolean)
|
||||
fun onCardSelect(position: Int, isChecked: Boolean)
|
||||
fun onButtonClick(position: Int)
|
||||
}
|
||||
|
||||
fun clearCheckedVideos() {
|
||||
val size = checkedItems.size
|
||||
for (i in 0 until size) {
|
||||
val position = checkedItems[i]
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
checkedItems.clear()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DIFF_CALLBACK: DiffUtil.ItemCallback<HistoryItem> = object : DiffUtil.ItemCallback<HistoryItem>() {
|
||||
override fun areItemsTheSame(oldItem: HistoryItem, newItem: HistoryItem): Boolean {
|
||||
return oldItem.id === newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: HistoryItem, newItem: HistoryItem): Boolean {
|
||||
return oldItem.time == newItem.time
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,212 +0,0 @@
|
||||
package com.deniscerri.ytdlnis.adapter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Color;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.deniscerri.ytdlnis.R;
|
||||
import com.deniscerri.ytdlnis.database.models.ResultItem;
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
import com.google.android.material.card.MaterialCardView;
|
||||
import com.google.android.material.progressindicator.LinearProgressIndicator;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class HomeAdapter extends ListAdapter<ResultItem, HomeAdapter.ViewHolder> {
|
||||
private final ArrayList<Integer> checkedVideos;
|
||||
private final OnItemClickListener onItemClickListener;
|
||||
private Activity activity;
|
||||
|
||||
public HomeAdapter(HomeAdapter.OnItemClickListener onItemClickListener, Activity activity){
|
||||
super(DIFF_CALLBACK);
|
||||
this.checkedVideos = new ArrayList<>();
|
||||
this.onItemClickListener = onItemClickListener;
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
private static final DiffUtil.ItemCallback<ResultItem> DIFF_CALLBACK = new DiffUtil.ItemCallback<ResultItem>() {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull ResultItem oldItem, @NonNull ResultItem newItem) {
|
||||
return oldItem.getId() == newItem.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull ResultItem oldItem, @NonNull ResultItem newItem) {
|
||||
return oldItem.getUrl().equals(newItem.getUrl());
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
return super.getItemViewType(position);
|
||||
}
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private MaterialCardView cardView;
|
||||
|
||||
public ViewHolder(@NonNull View itemView, OnItemClickListener onItemClickListener) {
|
||||
super(itemView);
|
||||
cardView = itemView.findViewById(R.id.result_card_view);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View cardView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.result_card, parent, false);
|
||||
return new HomeAdapter.ViewHolder(cardView, onItemClickListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
ResultItem video = getItem(position);
|
||||
MaterialCardView card = holder.cardView;
|
||||
// THUMBNAIL ----------------------------------
|
||||
ImageView thumbnail = card.findViewById(R.id.result_image_view);
|
||||
String imageURL= video.getThumb();
|
||||
if (!imageURL.isEmpty()){
|
||||
Handler uiHandler = new Handler(Looper.getMainLooper());
|
||||
uiHandler.post(() -> Picasso.get().load(imageURL).into(thumbnail));
|
||||
thumbnail.setColorFilter(Color.argb(70, 0, 0, 0));
|
||||
}else {
|
||||
Handler uiHandler = new Handler(Looper.getMainLooper());
|
||||
uiHandler.post(() -> Picasso.get().load(R.color.black).into(thumbnail));
|
||||
thumbnail.setColorFilter(Color.argb(70, 0, 0, 0));
|
||||
}
|
||||
|
||||
// TITLE ----------------------------------
|
||||
TextView videoTitle = card.findViewById(R.id.result_title);
|
||||
String title = video.getTitle();
|
||||
|
||||
if(title.length() > 100){
|
||||
title = title.substring(0, 40) + "...";
|
||||
}
|
||||
videoTitle.setText(title);
|
||||
|
||||
// Bottom Info ----------------------------------
|
||||
|
||||
TextView author = card.findViewById(R.id.author);
|
||||
author.setText(video.getAuthor());
|
||||
|
||||
TextView duration = card.findViewById(R.id.duration);
|
||||
if (!video.getDuration().isEmpty()){
|
||||
duration.setText(video.getDuration());
|
||||
}
|
||||
|
||||
// BUTTONS ----------------------------------
|
||||
int videoID = video.getId();
|
||||
|
||||
LinearLayout buttonLayout = card.findViewById(R.id.download_button_layout);
|
||||
|
||||
MaterialButton musicBtn = buttonLayout.findViewById(R.id.download_music);
|
||||
musicBtn.setTag(videoID + "##audio");
|
||||
musicBtn.setTag(R.id.cancelDownload, "false");
|
||||
musicBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "audio"));
|
||||
|
||||
MaterialButton videoBtn = buttonLayout.findViewById(R.id.download_video);
|
||||
videoBtn.setTag(videoID + "##video");
|
||||
videoBtn.setTag(R.id.cancelDownload, "false");
|
||||
videoBtn.setOnClickListener(view -> onItemClickListener.onButtonClick(position, "video"));
|
||||
|
||||
|
||||
// PROGRESS BAR ----------------------------------------------------
|
||||
|
||||
LinearProgressIndicator progressBar = card.findViewById(R.id.download_progress);
|
||||
progressBar.setTag(videoID + "##progress");
|
||||
progressBar.setProgress(0);
|
||||
progressBar.setIndeterminate(true);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
|
||||
// if (video.isDownloading()){
|
||||
// progressBar.setVisibility(View.VISIBLE);
|
||||
// }else {
|
||||
// progressBar.setProgress(0);
|
||||
// progressBar.setIndeterminate(true);
|
||||
// progressBar.setVisibility(View.GONE);
|
||||
// }
|
||||
//
|
||||
// if (video.isDownloadingAudio()) {
|
||||
// musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
|
||||
// musicBtn.setTag(R.id.cancelDownload, "true");
|
||||
// }else{
|
||||
// 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.isDownloadingVideo()){
|
||||
// videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
|
||||
// videoBtn.setTag(R.id.cancelDownload, "true");
|
||||
// }else{
|
||||
// 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(videoID)){
|
||||
card.setChecked(true);
|
||||
card.setStrokeWidth(5);
|
||||
}else{
|
||||
card.setChecked(false);
|
||||
card.setStrokeWidth(0);
|
||||
}
|
||||
|
||||
card.setTag(videoID + "##card");
|
||||
card.setOnLongClickListener(view -> {
|
||||
checkCard(card, videoID);
|
||||
return true;
|
||||
});
|
||||
card.setOnClickListener(view -> {
|
||||
if(checkedVideos.size() > 0){
|
||||
checkCard(card, videoID);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkCard(MaterialCardView card, int videoID){
|
||||
if(card.isChecked()){
|
||||
card.setStrokeWidth(0);
|
||||
checkedVideos.remove(Integer.valueOf(videoID));
|
||||
}else{
|
||||
card.setStrokeWidth(5);
|
||||
checkedVideos.add(videoID);
|
||||
}
|
||||
card.setChecked(!card.isChecked());
|
||||
onItemClickListener.onCardClick(videoID, card.isChecked());
|
||||
}
|
||||
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onButtonClick(int videoID, String type);
|
||||
void onCardClick(int videoID, boolean add);
|
||||
}
|
||||
|
||||
public void clearCheckedVideos(){
|
||||
int size = checkedVideos.size();
|
||||
for (int i = 0; i < size; i++){
|
||||
int position = checkedVideos.get(i);
|
||||
notifyItemChanged(position);
|
||||
}
|
||||
checkedVideos.clear();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,186 @@
|
||||
package com.deniscerri.ytdlnis.adapter
|
||||
|
||||
import android.app.Activity
|
||||
import android.graphics.Color
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.AsyncDifferConfig
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.deniscerri.ytdlnis.R
|
||||
import com.deniscerri.ytdlnis.database.models.ResultItem
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
import com.google.android.material.progressindicator.LinearProgressIndicator
|
||||
import com.squareup.picasso.Picasso
|
||||
|
||||
class HomeAdapter(onItemClickListener: OnItemClickListener, activity: Activity) : ListAdapter<ResultItem?, HomeAdapter.ViewHolder>(AsyncDifferConfig.Builder(DIFF_CALLBACK).build()) {
|
||||
private val checkedVideos: ArrayList<String>
|
||||
private val onItemClickListener: OnItemClickListener
|
||||
private val activity: Activity
|
||||
|
||||
init {
|
||||
checkedVideos = ArrayList()
|
||||
this.onItemClickListener = onItemClickListener
|
||||
this.activity = activity
|
||||
}
|
||||
|
||||
class ViewHolder(itemView: View, onItemClickListener: OnItemClickListener?) : RecyclerView.ViewHolder(itemView) {
|
||||
val cardView: MaterialCardView
|
||||
|
||||
init {
|
||||
cardView = itemView.findViewById(R.id.result_card_view)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val cardView = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.result_card, parent, false)
|
||||
return ViewHolder(cardView, onItemClickListener)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val video = getItem(position)
|
||||
val card = holder.cardView
|
||||
// THUMBNAIL ----------------------------------
|
||||
val thumbnail = card.findViewById<ImageView>(R.id.result_image_view)
|
||||
val imageURL = video!!.thumb
|
||||
if (imageURL.isNotEmpty()) {
|
||||
val uiHandler = Handler(Looper.getMainLooper())
|
||||
uiHandler.post { Picasso.get().load(imageURL).into(thumbnail) }
|
||||
thumbnail.setColorFilter(Color.argb(70, 0, 0, 0))
|
||||
} else {
|
||||
val uiHandler = Handler(Looper.getMainLooper())
|
||||
uiHandler.post { Picasso.get().load(R.color.black).into(thumbnail) }
|
||||
thumbnail.setColorFilter(Color.argb(70, 0, 0, 0))
|
||||
}
|
||||
|
||||
// TITLE ----------------------------------
|
||||
val videoTitle = card.findViewById<TextView>(R.id.result_title)
|
||||
var title = video.title
|
||||
if (title.length > 100) {
|
||||
title = title.substring(0, 40) + "..."
|
||||
}
|
||||
videoTitle.text = title
|
||||
|
||||
// Bottom Info ----------------------------------
|
||||
val author = card.findViewById<TextView>(R.id.author)
|
||||
author.text = video.author
|
||||
val duration = card.findViewById<TextView>(R.id.duration)
|
||||
if (video.duration.isNotEmpty()) {
|
||||
duration.text = video.duration
|
||||
}
|
||||
|
||||
// BUTTONS ----------------------------------
|
||||
val videoURL = video.url
|
||||
val buttonLayout = card.findViewById<LinearLayout>(R.id.download_button_layout)
|
||||
val musicBtn = buttonLayout.findViewById<MaterialButton>(R.id.download_music)
|
||||
musicBtn.tag = "$videoURL##audio"
|
||||
musicBtn.setTag(R.id.cancelDownload, "false")
|
||||
musicBtn.setOnClickListener { onItemClickListener.onButtonClick(videoURL, "audio") }
|
||||
val videoBtn = buttonLayout.findViewById<MaterialButton>(R.id.download_video)
|
||||
videoBtn.tag = "$videoURL##video"
|
||||
videoBtn.setTag(R.id.cancelDownload, "false")
|
||||
videoBtn.setOnClickListener { onItemClickListener.onButtonClick(videoURL, "video") }
|
||||
|
||||
|
||||
// PROGRESS BAR ----------------------------------------------------
|
||||
val progressBar = card.findViewById<LinearProgressIndicator>(R.id.download_progress)
|
||||
progressBar.tag = "$videoURL##progress"
|
||||
progressBar.progress = 0
|
||||
progressBar.isIndeterminate = true
|
||||
progressBar.visibility = View.GONE
|
||||
|
||||
// if (video.isDownloading()){
|
||||
// progressBar.setVisibility(View.VISIBLE);
|
||||
// }else {
|
||||
// progressBar.setProgress(0);
|
||||
// progressBar.setIndeterminate(true);
|
||||
// progressBar.setVisibility(View.GONE);
|
||||
// }
|
||||
//
|
||||
// if (video.isDownloadingAudio()) {
|
||||
// musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
|
||||
// musicBtn.setTag(R.id.cancelDownload, "true");
|
||||
// }else{
|
||||
// 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.isDownloadingVideo()){
|
||||
// videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
|
||||
// videoBtn.setTag(R.id.cancelDownload, "true");
|
||||
// }else{
|
||||
// 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(videoURL)) {
|
||||
card.isChecked = true
|
||||
card.strokeWidth = 5
|
||||
} else {
|
||||
card.isChecked = false
|
||||
card.strokeWidth = 0
|
||||
}
|
||||
card.tag = "$videoURL##card"
|
||||
card.setOnLongClickListener {
|
||||
checkCard(card, videoURL)
|
||||
true
|
||||
}
|
||||
card.setOnClickListener {
|
||||
if (checkedVideos.size > 0) {
|
||||
checkCard(card, videoURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkCard(card: MaterialCardView, videoURL: String) {
|
||||
if (card.isChecked) {
|
||||
card.strokeWidth = 0
|
||||
checkedVideos.remove(videoURL)
|
||||
} else {
|
||||
card.strokeWidth = 5
|
||||
checkedVideos.add(videoURL)
|
||||
}
|
||||
card.isChecked = !card.isChecked
|
||||
onItemClickListener.onCardClick(videoURL, card.isChecked)
|
||||
}
|
||||
|
||||
interface OnItemClickListener {
|
||||
fun onButtonClick(videoURL: String, type: String?)
|
||||
fun onCardClick(videoURL: String, add: Boolean)
|
||||
}
|
||||
|
||||
fun clearCheckedVideos() {
|
||||
// val size = checkedVideos.size
|
||||
// for (i in 0 until size) {
|
||||
// val position = checkedVideos[i]
|
||||
// notifyItemChanged(position)
|
||||
// }
|
||||
// checkedVideos.clear()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DIFF_CALLBACK: DiffUtil.ItemCallback<ResultItem> = object : DiffUtil.ItemCallback<ResultItem>() {
|
||||
override fun areItemsTheSame(oldItem: ResultItem, newItem: ResultItem): Boolean {
|
||||
return oldItem.id === newItem.id
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: ResultItem, newItem: ResultItem): Boolean {
|
||||
return oldItem.url == newItem.url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue