package com.weikou.beibeivideo.ui.main;
|
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.view.View;
|
import android.widget.Toast;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.BeibeiVideoApplication;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.vo.CouponVideoInfo;
|
import com.weikou.beibeivideo.entity.vo.GoldCornTask;
|
import com.weikou.beibeivideo.entity.vo.SignInInfo;
|
import com.weikou.beibeivideo.ui.MyRetainViewFragment;
|
import com.weikou.beibeivideo.ui.goldcorn.GoldCornAdapter;
|
import com.weikou.beibeivideo.ui.login.LoginActivity;
|
import com.weikou.beibeivideo.util.JumpActivityUtil;
|
import com.weikou.beibeivideo.util.UserUtil;
|
import com.weikou.beibeivideo.util.ui.ToastUtil;
|
import com.weikou.beibeivideo.widget.MySwipeRefreshLayout;
|
|
import org.apache.http.Header;
|
import org.json.JSONArray;
|
import org.json.JSONObject;
|
|
import java.lang.reflect.Type;
|
import java.util.List;
|
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
/**
|
* 影视豆
|
*/
|
public class GoldCornFragment extends MyRetainViewFragment implements GoldCornAdapter.IGoldCornActionListener {
|
private int mCurrentPage = 1;
|
private RecyclerView rv_goldcorn;
|
private MySwipeRefreshLayout srl_goldcorn;
|
private GoldCornAdapter mGoldCornAdapter;
|
|
private List<CouponVideoInfo> videoList;
|
private List<GoldCornTask> taskList;
|
private String couponExchangeUrl;
|
private SignInInfo signInInfo;
|
|
|
public GoldCornFragment() {
|
super();
|
setName(GoldCornFragment.class.getName());
|
}
|
|
public static GoldCornFragment newInstance() {
|
return new GoldCornFragment();
|
}
|
|
@Override
|
public int getContentResource() {
|
return R.layout.fragment_goldcorn;
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
if (refreshTask) {
|
refreshTask = false;
|
getTask();
|
}
|
if (refreshSignInfo) {
|
refreshSignInfo = false;
|
getSignInInfo();
|
}
|
|
//清除登录状态
|
if (signInInfo != null && mGoldCornAdapter != null) {
|
mGoldCornAdapter.notifyItemChanged(0);
|
}
|
|
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
public void onCreateView(View contentView, Bundle savedInstanceState) {
|
rv_goldcorn = contentView.findViewById(R.id.rv_goldcorn);
|
srl_goldcorn = contentView.findViewById(R.id.srl_goldcorn);
|
first = true;
|
srl_goldcorn.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
@Override
|
public void onRefresh() {
|
first = true;
|
requestData();
|
}
|
});
|
requestData();
|
}
|
|
@Override
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
super.onViewCreated(view, savedInstanceState);
|
if (savedInstanceState != null)
|
signInInfo = (SignInInfo) savedInstanceState.getSerializable("signInfo");
|
}
|
|
@Override
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
if (savedInstanceState != null)
|
signInInfo = (SignInInfo) savedInstanceState.getSerializable("signInfo");
|
}
|
|
private void initData() {
|
mGoldCornAdapter = new GoldCornAdapter(getActivity(), videoList, taskList, couponExchangeUrl, signInInfo, this);
|
rv_goldcorn.setAdapter(mGoldCornAdapter);
|
rv_goldcorn.setLayoutManager(new LinearLayoutManager(getContext()));
|
}
|
|
private boolean first;
|
|
private void requestData() {
|
getSignInInfo();
|
}
|
|
/**
|
* 获取签到信息
|
*/
|
private void getSignInInfo() {
|
srl_goldcorn.setRefreshing(true);
|
BeibeiVideoAPI.getSignInInfo(BeibeiVideoApplication.application, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, org.json.JSONObject jsonObject) throws Exception {
|
|
if (jsonObject.optBoolean("IsPost")) {
|
org.json.JSONObject data = jsonObject.optJSONObject("Data");
|
signInInfo = new Gson().fromJson(data.toString(), SignInInfo.class);
|
if (mGoldCornAdapter != null) {
|
mGoldCornAdapter.setSignInfo(signInInfo);
|
if (mGoldCornAdapter.getItemCount() > 0)
|
mGoldCornAdapter.notifyItemChanged(0);
|
}
|
if (first) {
|
getCouponVideoList();
|
} else {
|
srl_goldcorn.setRefreshing(false);
|
}
|
} else {
|
Toast.makeText(BeibeiVideoApplication.application, jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
srl_goldcorn.setRefreshing(false);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
srl_goldcorn.setRefreshing(false);
|
}
|
});
|
|
}
|
|
|
private void getCouponVideoList() {
|
|
BeibeiVideoAPI.getGoldCornCouponVideoList(BeibeiVideoApplication.application, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, org.json.JSONObject jsonObject) throws Exception {
|
|
if (jsonObject.optBoolean("IsPost")) {
|
org.json.JSONObject data = jsonObject.optJSONObject("Data");
|
couponExchangeUrl = data.optString("exchangeUrl");
|
JSONArray list = data.optJSONArray("list");
|
Type type = new TypeToken<List<CouponVideoInfo>>() {
|
}.getType();
|
videoList = new Gson().fromJson(list.toString(), type);
|
if (first) {
|
getTask();
|
} else {
|
srl_goldcorn.setRefreshing(false);
|
}
|
} else {
|
Toast.makeText(BeibeiVideoApplication.application, jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
srl_goldcorn.setRefreshing(false);
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
srl_goldcorn.setRefreshing(false);
|
}
|
});
|
|
}
|
|
|
private void getTask() {
|
BeibeiVideoAPI.getGoldCornTaskList(BeibeiVideoApplication.application, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, org.json.JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
org.json.JSONObject data = jsonObject.optJSONObject("Data");
|
JSONArray array = data.optJSONArray("list");
|
Type type = new TypeToken<List<GoldCornTask>>() {
|
}.getType();
|
List<GoldCornTask> list = new Gson().fromJson(array.toString(), type);
|
if (taskList == null)
|
taskList = list;
|
else {
|
taskList.clear();
|
taskList.addAll(list);
|
}
|
if (first) {
|
first = false;
|
initData();
|
} else {
|
if (mGoldCornAdapter.getItemCount() > 0)
|
mGoldCornAdapter.notifyItemChanged(mGoldCornAdapter.getItemCount() - 1);
|
}
|
} else {
|
Toast.makeText(BeibeiVideoApplication.application, jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
srl_goldcorn.setRefreshing(false);
|
}
|
});
|
|
}
|
|
|
private void signIn() {
|
BeibeiVideoAPI.signIn(BeibeiVideoApplication.application, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, org.json.JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
org.json.JSONObject data = jsonObject.optJSONObject("Data");
|
ToastUtil.showGetGoldGornToast(getContext(), "+" + data.optString("goldCorn"));
|
getSignInInfo();
|
} else {
|
Toast.makeText(BeibeiVideoApplication.application, jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
|
}
|
|
|
private boolean refreshTask = false;
|
private boolean refreshSignInfo = false;
|
|
@Override
|
public void onClick(int p, GoldCornTask task) {
|
BeibeiVideoAPI.canDoGoldCornTask(getContext(), task.getSourceCode(), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
JumpActivityUtil.jumpPage(getActivity(), task.getJumpDetail(), JumpActivityUtil.getParamsJSON(task.getParams()));
|
refreshTask = true;
|
refreshSignInfo = true;
|
} else {
|
Toast.makeText(getContext(), jsonObject.optString("Error"), Toast.LENGTH_SHORT).show();
|
}
|
}
|
});
|
|
}
|
|
@Override
|
public void onSignIn() {
|
if (!UserUtil.isLogin(BeibeiVideoApplication.application)) {
|
startActivityForResult(new Intent(BeibeiVideoApplication.application, LoginActivity.class).putExtra("result", true), 20001);
|
return;
|
}
|
signIn();
|
}
|
|
@Override
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
if (requestCode == 20001) {
|
getSignInInfo();
|
}
|
}
|
|
public static class GoldCornExpireInfo {
|
public String goldcorn;
|
public long expireTime;
|
}
|
|
@Override
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
outState.putSerializable("signInfo", signInInfo);
|
super.onSaveInstanceState(outState);
|
}
|
|
}
|