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 com.video.base.ui.subview.MyRefreshLayout;
|
|
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 MyRefreshLayout 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);
|
}
|
}
|
}
|
}
|