From 14c047dcb20fb99aa23f1ae5378d085f7ca7cc89 Mon Sep 17 00:00:00 2001 From: Harshith Goka Date: Sun, 16 Sep 2018 21:23:02 +0530 Subject: [PATCH] add thumbnail_url,img; fix formats size bug causing random crash; unique history --- app/build.gradle | 3 +++ .../me/harshithgoka/youtubedl/Extractor.java | 5 ++++- .../harshithgoka/youtubedl/MainActivity.java | 15 +++++++++++--- .../me/harshithgoka/youtubedl/VideoInfo.java | 20 ++++++++++++++++++- .../youtubedl/VideoInfoAdapter.java | 10 ++++++++++ app/src/main/res/layout/video_info_item.xml | 19 +++++++++++++++--- build.gradle | 2 +- 7 files changed, 65 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b92f563..441b9eb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,9 @@ dependencies { implementation 'com.github.tony19:named-regexp:0.2.5' implementation 'com.google.code.gson:gson:2.8.5' + + implementation 'com.github.bumptech.glide:glide:4.8.0' + annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' } repositories { mavenCentral() diff --git a/app/src/main/java/me/harshithgoka/youtubedl/Extractor.java b/app/src/main/java/me/harshithgoka/youtubedl/Extractor.java index 83b31bd..464adb3 100644 --- a/app/src/main/java/me/harshithgoka/youtubedl/Extractor.java +++ b/app/src/main/java/me/harshithgoka/youtubedl/Extractor.java @@ -12,7 +12,9 @@ import java.io.IOException; import java.net.URLDecoder; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -242,6 +244,7 @@ public VideoInfo getFormats(String you_url) { length = args.getString("length_seconds"); view_count = args.getString("view_count"); author = args.getString("author"); + thumbnail_url = args.optString("thumbnail_url"); String[] fmts_enc = fmts.split(","); List formats = new ArrayList<>(); @@ -313,7 +316,7 @@ else if (params.contains("s")) { formats.add(f); } - VideoInfo videoInfo = new VideoInfo(video_id, title, length, view_count, author, thumbnail_url, formats); + VideoInfo videoInfo = new VideoInfo(video_id, title, length, view_count, author, thumbnail_url, new Timestamp(new Date().getTime()), formats); return videoInfo; } catch (IllegalStateException e) { diff --git a/app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java b/app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java index 7a36eb2..89b069b 100644 --- a/app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java +++ b/app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java @@ -313,8 +313,9 @@ public void hideLoading() { void loadVideoInfo(VideoInfo videoInfo) { Log.d("II", "Loading videoInfo"); + int numRemoved = formats.size(); formats.clear(); - formatAdapter.notifyItemRangeRemoved(0, videoInfo.formats.size()); + formatAdapter.notifyItemRangeRemoved(0, numRemoved); formats.addAll(videoInfo.formats); formatAdapter.notifyItemRangeInserted(0, videoInfo.formats.size()); videoTitle.setText(videoInfo.title); @@ -358,8 +359,16 @@ protected void onPostExecute(VideoInfo videoInfo) { List formats = videoInfo.formats; if (formats.size() > 0) { - history.add(0, videoInfo); - viAdapter.notifyItemInserted(0); + int index; + if ((index = history.indexOf(videoInfo)) != -1) { + history.remove(index); + history.add(0, videoInfo); + viAdapter.notifyItemMoved(index, 0); + } + else { + history.add(0, videoInfo); + viAdapter.notifyItemInserted(0); + } loadVideoInfo(videoInfo); String finalurl = formats.get(0).url; diff --git a/app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java b/app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java index c89f9cc..613d6a0 100644 --- a/app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java +++ b/app/src/main/java/me/harshithgoka/youtubedl/VideoInfo.java @@ -3,7 +3,10 @@ import android.os.Parcel; import android.os.Parcelable; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; import java.util.List; import java.util.Locale; @@ -15,9 +18,10 @@ public class VideoInfo { public int length; public int view_count; public List formats; + public Timestamp last_updated; - VideoInfo(String id, String title, String length, String view_count, String author, String thumbnail_url, List formats) { + VideoInfo(String id, String title, String length, String view_count, String author, String thumbnail_url, Timestamp last_updated, List formats) { this.video_id = id; this.title = title; this.author = author; @@ -40,9 +44,23 @@ public class VideoInfo { this.thumbnail_url = thumbnail_url; this.formats = new ArrayList<>(); this.formats.addAll(formats); + + this.last_updated = last_updated; } public String getUrl() { return String.format(Locale.UK, "https://www.youtube.com/watch?v=%s", video_id); } + + public boolean isEquivalent(VideoInfo v1) { + return video_id.equals(v1.video_id); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof VideoInfo) { + return this.isEquivalent((VideoInfo) obj); + } + return false; + } } diff --git a/app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java b/app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java index aba1fe0..051715e 100644 --- a/app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java +++ b/app/src/main/java/me/harshithgoka/youtubedl/VideoInfoAdapter.java @@ -4,16 +4,20 @@ import android.content.ClipboardManager; import android.content.Context; import android.net.Uri; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; +import com.bumptech.glide.Glide; + import java.util.List; import java.util.Locale; import androidx.annotation.NonNull; +import androidx.appcompat.widget.AppCompatImageView; import androidx.recyclerview.widget.RecyclerView; public class VideoInfoAdapter extends RecyclerView.Adapter { @@ -41,6 +45,10 @@ public void onBindViewHolder(@NonNull VideoInfoViewHolder holder, int position) holder.title.setText(videoInfo.title); holder.videoId.setText(videoInfo.video_id); holder.author.setText(videoInfo.author); + + Glide.with(context) + .load(videoInfo.thumbnail_url) + .into(holder.thumbnail); } @Override @@ -54,12 +62,14 @@ public int getItemCount() { class VideoInfoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView title, videoId, author; View copyLink; + AppCompatImageView thumbnail; public VideoInfoViewHolder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.video_title); videoId = itemView.findViewById(R.id.videoId); copyLink = itemView.findViewById(R.id.copyLink); author = itemView.findViewById(R.id.author); + thumbnail = itemView.findViewById(R.id.thumb); copyLink.setOnClickListener(this); itemView.setOnClickListener(this); } diff --git a/app/src/main/res/layout/video_info_item.xml b/app/src/main/res/layout/video_info_item.xml index 71ad110..b2451b6 100644 --- a/app/src/main/res/layout/video_info_item.xml +++ b/app/src/main/res/layout/video_info_item.xml @@ -25,9 +25,9 @@ android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text="Title" - android:textAppearance="@style/TextAppearance.AppCompat.Headline" + android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Title" app:layout_constraintEnd_toStartOf="@+id/copyLink" - app:layout_constraintStart_toStartOf="parent" + app:layout_constraintStart_toEndOf="@+id/thumb" app:layout_constraintTop_toTopOf="parent" /> + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index b53121f..c151711 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.0-rc02' + classpath 'com.android.tools.build:gradle:3.2.0-rc03' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files