admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
371
372
373
374
375
376
377
378
379
package com.weikou.beibeivideo.ui.main;
 
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
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");
            String taskListStr = savedInstanceState.getString("taskList", null);
            Gson gson = new Gson();
            if (taskListStr != null) {
                Type type = new TypeToken<List<GoldCornTask>>() {
                }.getType();
                taskList = gson.fromJson(taskListStr, type);
            }
            String videoListStr = savedInstanceState.getString("videoList", null);
            if (videoListStr != null) {
                Type type = new TypeToken<List<CouponVideoInfo>>() {
                }.getType();
                videoList = gson.fromJson(videoListStr, type);
            }
 
 
        }
    }
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            signInInfo = (SignInInfo) savedInstanceState.getSerializable("signInfo");
            String taskListStr = savedInstanceState.getString("taskList", null);
            Gson gson = new Gson();
            if (taskListStr != null) {
                Type type = new TypeToken<List<GoldCornTask>>() {
                }.getType();
                taskList = gson.fromJson(taskListStr, type);
            }
            String videoListStr = savedInstanceState.getString("videoList", null);
            if (videoListStr != null) {
                Type type = new TypeToken<List<CouponVideoInfo>>() {
                }.getType();
                videoList = gson.fromJson(videoListStr, type);
            }
        }
    }
 
    private void initData() {
        mGoldCornAdapter = new GoldCornAdapter(getActivity(), videoList, taskList, couponExchangeUrl, signInInfo, this);
        rv_goldcorn.setAdapter(mGoldCornAdapter);
        rv_goldcorn.setLayoutManager(new XLinearLayoutManager(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);
            UserUtil.toLogin(BeibeiVideoApplication.application);
            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);
 
        if (taskList != null)
            outState.putString("taskList", new Gson().toJson(taskList));
 
        if (videoList != null)
            outState.putString("videoList", new Gson().toJson(videoList));
 
        super.onSaveInstanceState(outState);
    }
 
 
    public class XLinearLayoutManager extends LinearLayoutManager {
        public XLinearLayoutManager(Context context) {
            super(context);
        }
 
        public XLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
 
        public XLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
 
        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
 
    }
 
}