22
33import android .animation .AnimatorSet ;
44import android .animation .ValueAnimator ;
5+ import android .content .Context ;
56import android .content .Intent ;
7+ import android .graphics .Bitmap ;
8+ import android .graphics .BitmapFactory ;
9+ import android .graphics .Canvas ;
10+ import android .graphics .drawable .Drawable ;
11+ import android .os .AsyncTask ;
12+ import android .os .Build ;
613import android .os .Bundle ;
14+ import android .os .StrictMode ;
715import android .util .Log ;
816import android .view .LayoutInflater ;
917import android .view .View ;
1220
1321import androidx .annotation .NonNull ;
1422import androidx .annotation .Nullable ;
23+ import androidx .core .content .ContextCompat ;
24+ import androidx .core .graphics .drawable .DrawableCompat ;
1525import androidx .fragment .app .Fragment ;
1626import androidx .recyclerview .widget .LinearLayoutManager ;
1727import androidx .recyclerview .widget .RecyclerView ;
28+ import androidx .swiperefreshlayout .widget .SwipeRefreshLayout ;
1829
1930import com .supun .streamix .ui .videoCard .IRecyclerView ;
2031import com .supun .streamix .ui .videoCard .VC_RecycleViewAdapter ;
2132import com .supun .streamix .ui .videoCard .VideoCardModel ;
2233
34+ import java .io .InputStream ;
35+ import java .net .URL ;
2336import java .util .ArrayList ;
2437import java .util .List ;
2538
2639import retrofit2 .Call ;
2740import retrofit2 .Callback ;
2841import retrofit2 .Response ;
2942
30- public class HomeFragment extends Fragment implements IRecyclerView {
43+ public class HomeFragment extends Fragment implements IRecyclerView , SwipeRefreshLayout . OnRefreshListener {
3144
32- private static final ArrayList <VideoCardModel > videoCardModels = new ArrayList <>();
45+ private final ArrayList <VideoCardModel > videoCardModels = new ArrayList <>();
3346 private MainActivity mainActivity ;
47+ private VC_RecycleViewAdapter adapter ;
48+
49+ private SwipeRefreshLayout swipeRefreshLayout ;
50+
51+ @ Override
52+ public void onCreate (@ Nullable Bundle savedInstanceState ) {
53+ super .onCreate (savedInstanceState );
54+ }
3455
3556 @ Nullable
3657 @ Override
3758 public View onCreateView (@ NonNull LayoutInflater inflater , @ Nullable ViewGroup container , @ Nullable Bundle savedInstanceState ) {
3859
3960 mainActivity = (MainActivity ) getActivity ();
4061
62+ // Avoid network op on main thread for loading thumbnails - Reduce UX
63+ StrictMode .ThreadPolicy policy = new StrictMode .ThreadPolicy .Builder ().permitAll ().build ();
64+ StrictMode .setThreadPolicy (policy );
65+
4166
4267 View view = inflater .inflate (R .layout .fragment_home , container , false );
4368
69+ swipeRefreshLayout = view .findViewById (R .id .swipe_container );
70+ swipeRefreshLayout .setOnRefreshListener (this );
71+ swipeRefreshLayout .setColorSchemeResources (
72+ R .color .red_900 ,
73+ R .color .yellow_100 ,
74+ R .color .black ,
75+ R .color .purple_700
76+ );
77+
78+ swipeRefreshLayout .post (() -> {
79+ swipeRefreshLayout .setRefreshing (true );
80+ setupVideoCardModels ();
81+
82+ Log .i ("zz" , String .valueOf (videoCardModels .size ()));
83+ });
84+
4485 RecyclerView recyclerView = view .findViewById (R .id .recycler_view_home );
45- setupVideoCardModels ();
46- Log .i ("zz" , String .valueOf (videoCardModels .size ()));
47- VC_RecycleViewAdapter adapter = new VC_RecycleViewAdapter (getContext (), videoCardModels , this );
86+ adapter = new VC_RecycleViewAdapter (getContext (), videoCardModels , this );
4887
4988 recyclerView .setLayoutManager (new LinearLayoutManager (getActivity ()));
5089 recyclerView .setAdapter (adapter );
@@ -93,10 +132,15 @@ else if(dy < -10){
93132 private void setupVideoCardModels (){
94133
95134 // This is to prevent duplicating items when orientation change
96- // videoCardModels.clear();
135+
136+ swipeRefreshLayout .setRefreshing (true );
137+
138+ videoCardModels .clear ();
139+ adapter .clear ();
140+
97141 ArrayList <String > ids = new ArrayList <>();
98142
99- String Tag = "vhagar " ;
143+ String Tag = "ASYNC " ;
100144 Log .i (Tag , "Starting" );
101145 Call <List <Video >> call = RetrofitClient .getInstance ().getAPI ().getVideos ();
102146 Log .i (Tag , "call " );
@@ -112,13 +156,35 @@ public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
112156 Log .i (Tag , "xxxx" );
113157 if (!ids .contains (videoList .get (i ).getID ())){
114158 ids .add (videoList .get (i ).getID ());
115- videoCardModels .add (new VideoCardModel (R .drawable .thumbnail_1 , videoList .get (i ).getTitle (),
159+
160+ Bitmap thumbnail ;
161+
162+ try {
163+ Log .i ("THUMBNAIL_LINK" , BuildConfig .FILE_SYSTEM_URL + "thumbnails/" + videoList .get (i ).getID () + ".jpg" );
164+ URL in = new URL (BuildConfig .FILE_SYSTEM_URL + "thumbnails/" + videoList .get (i ).getID () + ".jpg" );
165+
166+
167+ thumbnail = BitmapFactory .decodeStream (in .openConnection ().getInputStream ());
168+ } catch (Exception e ){
169+ Log .i ("THUMBNAIL" , e .toString ());
170+ thumbnail = getBitmapFromVectorDrawable (getContext (), R .drawable .no_thumbnail_available );
171+ }
172+
173+
174+
175+ videoCardModels .add (new VideoCardModel (
176+ thumbnail ,
177+ videoList .get (i ).getTitle (),
116178 videoList .get (i ).getDescription (),
117179 BuildConfig .FILE_SYSTEM_URL +videoList .get (i ).getID ()+"/" +videoList .get (i ).getID ()+"_out.mpd" ));
180+
181+ adapter .notifyItemInserted (videoCardModels .size () - 1 );
118182 }
119183
120184 }
121185 Log .i ("qqq" , String .valueOf (videoCardModels .size ()));
186+
187+ swipeRefreshLayout .setRefreshing (false );
122188 }
123189
124190 @ Override
@@ -129,53 +195,24 @@ public void onFailure(Call<List<Video>> call, Throwable t) {
129195 }
130196
131197 });
132- // These are the dummy data used to display
133- // String[] titles = {"Video 1", "Video 2", "Video 3", "Video 4", "Video 5", "Video 1", "Video 2", "Video 3", "Video 4", "Video 5"};
134- // String[] descriptions = {
135- // "This is a description for video 1",
136- // "This is a description for video 2",
137- // "This is a description for video 3",
138- // "This is a description for video 4",
139- // "This is a description for video 5",
140- // "This is a description for video 1",
141- // "This is a description for video 2",
142- // "This is a description for video 3",
143- // "This is a description for video 4",
144- // "This is a description for video 5"
145- // };
146-
147- int [] vectorImages = {
148- R .drawable .thumbnail_1 ,
149- R .drawable .thumbnail_2 ,
150- R .drawable .thumbnail_3 ,
151- R .drawable .no_thumbnail_available ,
152- R .drawable .no_thumbnail_available ,
153- R .drawable .no_thumbnail_available ,
154- R .drawable .no_thumbnail_available ,
155- R .drawable .no_thumbnail_available ,
156- R .drawable .no_thumbnail_available ,
157- R .drawable .no_thumbnail_available
158- };
159-
160- // String[] associatedUrls= {
161- // "http://192.168.1.15:8080/a/a_out.mpd",
162- // "http://192.168.1.15:8080/c/c_out.mpd",
163- // "http://192.168.1.15:8080/b/videofull1_out.mpd",
164- // "https://content.jwplatform.com/manifests/yp34SRmf.m3u8",
165- // "https://dash.akamaized.net/dash264/TestCasesIOP33/adapatationSetSwitching/5/manifest.mpd",
166- // "https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
167- // "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4",
168- // "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4",
169- // "https://samplelib.com/lib/preview/mp4/sample-30s.mp4",
170- // "https://samplelib.com/lib/preview/mp4/sample-30s.mp4"
171- // };
172-
173- // for(int i = 0; i < titles.size(); i++){
174- // videoCardModels.add(new VideoCardModel(vectorImages[0], titles.get(i), descriptions.get(i), associatedUrls.get(i)));
175- // }
176198
177199 }
178200
201+ public static Bitmap getBitmapFromVectorDrawable (Context context , int drawableId ) {
202+ Drawable drawable = ContextCompat .getDrawable (context , drawableId );
203+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
204+ drawable = (DrawableCompat .wrap (drawable )).mutate ();
205+ }
206+
207+ Bitmap bitmap = Bitmap .createBitmap (drawable .getIntrinsicWidth (),
208+ drawable .getIntrinsicHeight (), Bitmap .Config .ARGB_8888 );
209+ Canvas canvas = new Canvas (bitmap );
210+ drawable .setBounds (0 , 0 , canvas .getWidth (), canvas .getHeight ());
211+ drawable .draw (canvas );
212+
213+ return bitmap ;
214+ }
215+
179216 @ Override
180217 public void onCardClick (int position ) {
181218 Intent intent = new Intent (getActivity (), PlayerActivity .class );
@@ -214,4 +251,9 @@ public static void slideView(View view,
214251 animationSet .start ();
215252 }
216253
254+ @ Override
255+ public void onRefresh () {
256+ setupVideoCardModels ();
257+ Log .i ("SWIPE_REFRESH" , "Swipe refresh executed" );
258+ }
217259}
0 commit comments