|
2 | 2 |
|
3 | 3 | import android.animation.AnimatorSet; |
4 | 4 | import android.animation.ValueAnimator; |
| 5 | +import android.content.Context; |
5 | 6 | import 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; |
6 | 13 | import android.os.Bundle; |
| 14 | +import android.os.StrictMode; |
7 | 15 | import android.util.Log; |
8 | 16 | import android.view.LayoutInflater; |
9 | 17 | import android.view.View; |
|
12 | 20 |
|
13 | 21 | import androidx.annotation.NonNull; |
14 | 22 | import androidx.annotation.Nullable; |
| 23 | +import androidx.core.content.ContextCompat; |
| 24 | +import androidx.core.graphics.drawable.DrawableCompat; |
15 | 25 | import androidx.fragment.app.Fragment; |
16 | 26 | import androidx.recyclerview.widget.LinearLayoutManager; |
17 | 27 | import androidx.recyclerview.widget.RecyclerView; |
| 28 | +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; |
18 | 29 |
|
19 | 30 | import com.supun.streamix.ui.videoCard.IRecyclerView; |
20 | 31 | import com.supun.streamix.ui.videoCard.VC_RecycleViewAdapter; |
21 | 32 | import com.supun.streamix.ui.videoCard.VideoCardModel; |
22 | 33 |
|
| 34 | +import java.io.InputStream; |
| 35 | +import java.net.URL; |
23 | 36 | import java.util.ArrayList; |
| 37 | +import java.util.List; |
24 | 38 |
|
25 | | -public class HomeFragment extends Fragment implements IRecyclerView { |
| 39 | +import retrofit2.Call; |
| 40 | +import retrofit2.Callback; |
| 41 | +import retrofit2.Response; |
26 | 42 |
|
27 | | - private static final ArrayList<VideoCardModel> videoCardModels = new ArrayList<>(); |
| 43 | +public class HomeFragment extends Fragment implements IRecyclerView, SwipeRefreshLayout.OnRefreshListener { |
| 44 | + |
| 45 | + private final ArrayList<VideoCardModel> videoCardModels = new ArrayList<>(); |
28 | 46 | 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 | + } |
29 | 55 |
|
30 | 56 | @Nullable |
31 | 57 | @Override |
32 | 58 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
33 | 59 |
|
34 | 60 | mainActivity = (MainActivity) getActivity(); |
35 | 61 |
|
| 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 | + |
36 | 66 |
|
37 | 67 | View view = inflater.inflate(R.layout.fragment_home, container, false); |
38 | 68 |
|
| 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 | + |
39 | 85 | RecyclerView recyclerView = view.findViewById(R.id.recycler_view_home); |
40 | | - setupVideoCardModels(); |
41 | | - VC_RecycleViewAdapter adapter = new VC_RecycleViewAdapter(getContext(), videoCardModels, this); |
| 86 | + adapter = new VC_RecycleViewAdapter(getContext(), videoCardModels, this); |
42 | 87 |
|
43 | 88 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); |
44 | 89 | recyclerView.setAdapter(adapter); |
@@ -87,52 +132,85 @@ else if(dy < -10){ |
87 | 132 | private void setupVideoCardModels(){ |
88 | 133 |
|
89 | 134 | // This is to prevent duplicating items when orientation change |
| 135 | + |
| 136 | + swipeRefreshLayout.setRefreshing(true); |
| 137 | + |
90 | 138 | videoCardModels.clear(); |
| 139 | + adapter.clear(); |
| 140 | + |
| 141 | + ArrayList<String> ids = new ArrayList<>(); |
| 142 | + |
| 143 | + String Tag = "ASYNC"; |
| 144 | + Log.i(Tag, "Starting"); |
| 145 | + Call<List<Video>> call = RetrofitClient.getInstance().getAPI().getVideos(); |
| 146 | + Log.i(Tag, "call "); |
| 147 | + call.enqueue(new Callback<List<Video>>() { |
| 148 | + @Override |
| 149 | + public void onResponse(Call<List<Video>> call, Response<List<Video>> response) { |
| 150 | + Log.i(Tag, "Got data"); |
| 151 | + List<Video> videoList = response.body(); |
| 152 | + Log.i("size", String.valueOf(videoList.size())); |
| 153 | + Log.i(Tag, videoList.get(0).getTitle()); |
| 154 | + Log.i(Tag, videoList.get(0).getDescription()); |
| 155 | + for (int i = 0; i < videoList.size(); i++) { |
| 156 | + Log.i(Tag, "xxxx"); |
| 157 | + if (!ids.contains(videoList.get(i).getID())){ |
| 158 | + ids.add(videoList.get(i).getID()); |
| 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 | + |
91 | 173 |
|
92 | | - // These are the dummy data used to display |
93 | | - String[] titles = {"Video 1", "Video 2", "Video 3", "Video 4", "Video 5", "Video 1", "Video 2", "Video 3", "Video 4", "Video 5"}; |
94 | | - String[] descriptions = { |
95 | | - "This is a description for video 1", |
96 | | - "This is a description for video 2", |
97 | | - "This is a description for video 3", |
98 | | - "This is a description for video 4", |
99 | | - "This is a description for video 5", |
100 | | - "This is a description for video 1", |
101 | | - "This is a description for video 2", |
102 | | - "This is a description for video 3", |
103 | | - "This is a description for video 4", |
104 | | - "This is a description for video 5" |
105 | | - }; |
106 | | - |
107 | | - int[] vectorImages = { |
108 | | - R.drawable.thumbnail_1, |
109 | | - R.drawable.thumbnail_2, |
110 | | - R.drawable.thumbnail_3, |
111 | | - R.drawable.no_thumbnail_available, |
112 | | - R.drawable.no_thumbnail_available, |
113 | | - R.drawable.no_thumbnail_available, |
114 | | - R.drawable.no_thumbnail_available, |
115 | | - R.drawable.no_thumbnail_available, |
116 | | - R.drawable.no_thumbnail_available, |
117 | | - R.drawable.no_thumbnail_available |
118 | | - }; |
119 | | - |
120 | | - String[] associatedUrls= { |
121 | | - "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", |
122 | | - "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4", |
123 | | - "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4", |
124 | | - "https://samplelib.com/lib/preview/mp4/sample-30s.mp4", |
125 | | - "https://samplelib.com/lib/preview/mp4/sample-30s.mp4", |
126 | | - "https://samplelib.com/lib/preview/mp4/sample-5s.mp4", |
127 | | - "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4", |
128 | | - "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4", |
129 | | - "https://samplelib.com/lib/preview/mp4/sample-30s.mp4", |
130 | | - "https://samplelib.com/lib/preview/mp4/sample-30s.mp4" |
131 | | - }; |
132 | | - |
133 | | - for(int i = 0; i < titles.length; i++){ |
134 | | - videoCardModels.add(new VideoCardModel(vectorImages[i], titles[i], descriptions[i], associatedUrls[i])); |
| 174 | + |
| 175 | + videoCardModels.add(new VideoCardModel( |
| 176 | + thumbnail, |
| 177 | + videoList.get(i).getTitle(), |
| 178 | + videoList.get(i).getDescription(), |
| 179 | + BuildConfig.FILE_SYSTEM_URL+videoList.get(i).getID()+"/"+videoList.get(i).getID()+"_out.mpd")); |
| 180 | + |
| 181 | + adapter.notifyItemInserted(videoCardModels.size() - 1); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + Log.i("qqq", String.valueOf(videoCardModels.size())); |
| 186 | + |
| 187 | + swipeRefreshLayout.setRefreshing(false); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void onFailure(Call<List<Video>> call, Throwable t) { |
| 192 | + |
| 193 | + Log.e(Tag, "Mission Faileddddd"); |
| 194 | +// Toast.makeText(getApplicationContext(), "An error has occured", Toast.LENGTH_LONG).show(); |
| 195 | + } |
| 196 | + |
| 197 | + }); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 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(); |
135 | 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; |
136 | 214 | } |
137 | 215 |
|
138 | 216 | @Override |
@@ -173,4 +251,9 @@ public static void slideView(View view, |
173 | 251 | animationSet.start(); |
174 | 252 | } |
175 | 253 |
|
| 254 | + @Override |
| 255 | + public void onRefresh() { |
| 256 | + setupVideoCardModels(); |
| 257 | + Log.i("SWIPE_REFRESH", "Swipe refresh executed"); |
| 258 | + } |
176 | 259 | } |
0 commit comments