admin
2021-12-22 0a1336cd2b95126d66d6f3126cb48a446cdbfad1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.hanju.video.app.ui.main;
 
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hanju.video.app.entity.recommend.HomeSpecial;
import com.hanju.lib.library.content.ConnectivityChangeHelper;
import com.hanju.lib.library.content.ConnectivityChangeHelper.OnConnectivityChangeListener;
import com.hanju.lib.library.util.cache.DiskLruCache;
import com.hanju.lib.library.util.common.DimenUtils;
import com.hanju.lib.library.util.common.StorageUtils;
import com.hanju.lib.library.util.common.StringUtils;
import com.hanju.video.app.util.JsonUtil;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.hanju.video.app.util.http.BasicTextHttpResponseHandler;
import com.hanju.video.app.util.http.HttpApiUtil;
import com.hanju.video.app.R;
import com.hanju.video.app.ui.MyRetainViewFragment;
import com.hanju.video.app.ui.category.MVideosActivity;
import com.hanju.video.app.ui.recommend.SearchActivity;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
 
/**
 * 分类
 */
public class CategoryFragment extends MyRetainViewFragment implements
        OnClickListener {
    private TextView tv_no_network;
 
    FrameLayout fl_ad;
 
    private SwipeRefreshLayout ptsv_container;
 
    private RecyclerView rv_category;
 
    private DiskLruCache cache;
 
    private ConnectivityChangeHelper mChangeHelper;
 
    public CategoryFragment() {
        super(CategoryFragment.class.getName());
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            cache = DiskLruCache.open(
                    new File(StorageUtils.getCacheDirectory(getContext())
                            .toString(), "http"), getVersionNum(getContext()),
                    1, 1024 * 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public int getContentResource() {
        return R.layout.category_fragment;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        initView(contentView);
 
 
        ptsv_container
                .setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        getCategories();
                    }
                });
 
        ptsv_container.post(new Runnable() {
 
            @Override
            public void run() {
                if (cache != null) {
                    {
                        DiskLruCache.Snapshot snapshot = null;
                        try {
                            snapshot = cache.get(getKey("getCategoriesChannel"));
                            if (snapshot != null) {
 
                                List<HomeSpecial> categories =  JsonUtil.videoGson.fromJson(
                                        snapshot.getString(0),
                                        new TypeToken<List<HomeSpecial>>() {
                                        }.getType());
                                rv_category
                                        .setAdapter(new CategoryAdapter(
                                                categories));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (snapshot != null) {
                                snapshot.close();
                            }
                        }
                    }
                }
            }
        });
        ptsv_container.postDelayed(new Runnable() {
 
            @Override
            public void run() {
                getCategories();
            }
        }, 200);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
    private String getKey(String method) {
        return new Md5FileNameGenerator().generate(method);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_go_search: {
                Intent intent = new Intent(tv_no_network.getContext(), SearchActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.tv_no_network: {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }
 
 
    private void getCategories() {
        HttpApiUtil.getSearchVideoType(tv_no_network.getContext(), "hanju-video-types",
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            Gson gson = new GsonBuilder()
                                    .create();
 
                            List<HomeSpecial> categories = gson.fromJson(jsonObject.getJSONObject("Data")
                                    .getJSONArray("list").toString(), new TypeToken<List<HomeSpecial>>() {
                            }.getType());
 
                            rv_category.setAdapter(new CategoryAdapter(
                                    categories));
                            DiskLruCache.Editor editor = cache
                                    .edit(getKey("getCategoriesChannel"));
                            editor.set(0, jsonObject.getJSONObject("Data")
                                    .getJSONArray("list").toString());
                            editor.commit();
                        }
                    }
 
                    @Override
                    public void onFinish() {
                        ptsv_container.setRefreshing(false);
                    }
                });
    }
 
    private void initView(View contentView) {
        tv_no_network = contentView.findViewById(R.id.tv_no_network);
        fl_ad = contentView.findViewById(R.id.fl_ad);
        tv_no_network.setOnClickListener(this);
 
        mChangeHelper = new ConnectivityChangeHelper(tv_no_network.getContext(),
                new OnConnectivityChangeListener() {
 
                    @Override
                    public void onNetworkUnAvailable() {
                        tv_no_network.setVisibility(View.VISIBLE);
                    }
 
                    @Override
                    public void onWiFiAvailable() {
                        tv_no_network.setVisibility(View.GONE);
                    }
 
                    @Override
                    public void onMobileAvailable() {
                        tv_no_network.setVisibility(View.GONE);
                    }
                });
 
        ptsv_container = contentView
                .findViewById(R.id.ptsv_container);
        rv_category = contentView
                .findViewById(R.id.rv_category);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 6);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position < 4) {
                    return 3;
                } else {
                    return 2;
                }
            }
        });
        rv_category.setLayoutManager(layoutManager);
        rv_category.addItemDecoration(itemDecoration);
    }
 
    RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }
 
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
        }
 
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int index = parent.getChildAdapterPosition(view);
            int total = parent.getAdapter().getItemCount();
            int minWidth = DimenUtils.dipToPixels(2, view.getContext());
            if (index < 4) {
                if (index % 2 == 0) {
                    outRect.left = 0;
                    outRect.right = 3 * minWidth;
                } else {
                    outRect.right = 0;
                    outRect.left = 3 * minWidth;
                }
                if (index > 1) {
                    outRect.top = 3 * minWidth;
                }
 
            } else {
                int minStart = index - 4;
                if (minStart % 3 == 0) {
                    outRect.left = 0;
                    outRect.right = 4 * minWidth;
                } else if (minStart % 3 == 1) {
                    outRect.right = 2 * minWidth;
                    outRect.left = 2 * minWidth;
                } else if (minStart % 3 == 2) {
                    outRect.right = 0;
                    outRect.left = 4 * minWidth;
                }
                outRect.top = 3 * minWidth;
            }
            outRect.bottom = 3 * minWidth;
        }
    };
 
 
    public static int getVersionNum(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return pi.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return 1;
        }
    }
 
    class CategoryAdapter extends RecyclerView.Adapter {
        private List<HomeSpecial> videoTypeList;
 
        public CategoryAdapter(List<HomeSpecial> videoTypeList) {
            this.videoTypeList = videoTypeList;
        }
 
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, null, true));
        }
 
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            final HomeSpecial videoType = videoTypeList.get(position);
            vh.tv_name.setText(videoType.getName());
            Glide.with(vh.iv_picture.getContext()).load(videoType.getIcon()).transition(withCrossFade()).into(vh.iv_picture);
            vh.view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (videoType == null || StringUtils.isEmpty(videoType.getId()))
                        return;
                    Intent intent = new Intent(getContext(), MVideosActivity.class);// category包中的mvideosActivity.class
                    intent.putExtra("special", videoType);
                    intent.putExtra("type_name", videoType.getName());
                    v.getContext().startActivity(intent);
                }
            });
        }
 
        @Override
        public int getItemCount() {
            return videoTypeList.size();
        }
 
        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView iv_picture;
            TextView tv_name;
            View view;
 
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                view = itemView;
                iv_picture = itemView.findViewById(R.id.iv_picture);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }
}