admin
2021-08-19 6f07cf3ce7ccb37911f5c755b7833732fb8eee64
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package com.hanju.video.app.ui.main;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
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.BasicTextHttpResponseHandler;
import com.hanju.video.app.HttpApiUtil;
import com.hanju.video.app.db.HanJuSQLiteOpenHelper;
import com.hanju.video.app.db.WatchHistoryTable;
import com.hanju.video.app.entity.VideoInfo;
import com.hanju.video.app.ui.mine.AboutUsActivity;
import com.hanju.video.app.ui.mine.WatchHistoryMineAdapter;
import com.hanju.video.app.util.HanJuConstant;
import com.hanju.video.app.util.UserUtil;
import com.lcjian.library.content.ConnectivityChangeHelper;
import com.lcjian.library.content.ConnectivityChangeHelper.OnConnectivityChangeListener;
import com.lcjian.library.util.common.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.hanju.video.app.R;
import com.hanju.video.app.entity.NewComment;
import com.hanju.video.app.ui.MyRetainViewFragment;
import com.hanju.video.app.ui.common.FollowActivity;
import com.hanju.video.app.ui.login.LoginActivity;
import com.hanju.video.app.ui.login.PersonInfoActivity;
import com.hanju.video.app.ui.mine.BrowserActivity;
import com.hanju.video.app.ui.mine.DownloadActivity;
import com.hanju.video.app.ui.mine.MyFavouriteActivity;
import com.hanju.video.app.ui.mine.SettingsActivity;
import com.hanju.video.app.ui.mine.SystemMessageActivity;
import com.hanju.video.app.ui.mine.WatchHistoryActivity;
import com.hanju.video.app.ui.recommend.SearchActivity;
import com.hanju.video.app.util.GlideCircleTransform;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import de.greenrobot.event.EventBus;
 
 
public class MineFragment extends MyRetainViewFragment implements
        OnClickListener {
 
    public MineFragment() {
        super(MineFragment.class.getName());
    }
 
    private TextView tv_no_network;
 
    private ConnectivityChangeHelper mChangeHelper;
 
    private TextView tv_id;// 用户名
    private TextView tv_name;//昵称
    private ImageView iv_user_logo;// 用户头像
    private TextView tv_desc;//简介
    /**
     * 用于表示登录状态
     */
    public static boolean isLogin;
 
    private TextView tv_mine_favourite, tv_mine_follow, tv_mine_save;
 
 
    private boolean isHaveNewCom;// 是否存在新的评论消息
 
    private RecyclerView rv_watch_history;
 
    List<WatchHistoryMineAdapter.MineWatchHistoryVideoInfo> historyList = new ArrayList<>();
 
    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }
 
    @Override
    public int getContentResource() {
        return R.layout.mine_fragment;
    }
 
    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("flash back", "show");
        super.onSaveInstanceState(outState);
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
 
        tv_no_network = contentView.findViewById(R.id.tv_no_network);
        rv_watch_history = contentView.findViewById(R.id.rv_watch_history);
        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);
                    }
                });
 
        tv_name = contentView.findViewById(R.id.mine_tv_name);
        tv_id = contentView.findViewById(R.id.tv_mine_id);
        iv_user_logo = contentView.findViewById(R.id.mine_ci_head);
 
        contentView.findViewById(R.id.mine_rl_head).setOnClickListener(this);
 
        contentView.findViewById(R.id.ll_mine_favourite).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mine_follow).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mine_save).setOnClickListener(this);
        contentView.findViewById(R.id.tv_mine_watch_history).setOnClickListener(this);
 
        tv_mine_favourite = contentView.findViewById(R.id.tv_mine_favourite);
        tv_mine_follow = contentView.findViewById(R.id.tv_mine_follow);
        tv_mine_save = contentView.findViewById(R.id.tv_mine_save);
        tv_desc = contentView.findViewById(R.id.tv_desc);
 
        contentView.findViewById(R.id.ll_mine_about).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mine_setting).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mine_advice).setOnClickListener(this);
        getUserVideoDataCount();
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_recommend_watch_history: {
                startActivity(new Intent(tv_no_network.getContext(),
                        WatchHistoryActivity.class));
            }
            break;
            case R.id.iv_activity_main_download: {
                Toast.makeText(tv_no_network.getContext(), "暂时不支持下载!", Toast.LENGTH_LONG).show();
            }
            break;
            case R.id.ll_activity_main_search: {
                startActivity(new Intent(tv_no_network.getContext(),
                        SearchActivity.class));
            }
            break;
            case R.id.mine_rl_head:
                Intent intent = new Intent();
                if (isLogin) {// 登录后 逻辑
                    intent.setClass(tv_no_network.getContext(), PersonInfoActivity.class);
                } else {// 登录前
                    intent.setClass(tv_no_network.getContext(), LoginActivity.class);
                }
                startActivity(intent);
                break;
            case R.id.ll_mine_favourite:
                startActivity(new Intent(tv_no_network.getContext(), MyFavouriteActivity.class));
                break;
            case R.id.tv_mine_watch_history:
                startActivity(new Intent(tv_no_network.getContext(), WatchHistoryActivity.class));
                break;
            case R.id.ll_mine_save:
                startActivity(new Intent(tv_no_network.getContext(), DownloadActivity.class));
                break;
            case R.id.ll_mine_follow:
                if (UserUtil.getLoginUid(getContext()) == null) {
                    startActivity(new Intent(tv_no_network.getContext(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(tv_no_network.getContext(), FollowActivity.class));
                break;
 
            case R.id.ll_mine_about://关于我们
                Intent intent1 = new Intent(getContext(), AboutUsActivity.class);
                startActivity(intent1);
                break;
            case R.id.ll_mine_setting://设置
                startActivity(new Intent(tv_no_network.getContext(), SettingsActivity.class));
                break;
            case R.id.ll_mine_advice://建议
                startActivity(new Intent(getContext(), BrowserActivity.class).putExtra("title", "意见反馈").putExtra("url", "https://support.qq.com/embed/phone/343178/new-post/"));
                break;
        }
    }
 
    UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
 
        }
 
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(tv_no_network.getContext(), "分享成功!", Toast.LENGTH_LONG).show();
        }
 
        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            if (!UMShareAPI.get(tv_no_network.getContext()).isInstall(getActivity(), share_media)) {
                Toast.makeText(tv_no_network.getContext(), share_media == SHARE_MEDIA.QQ ? "没有安装QQ" : "没有安装微信", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(tv_no_network.getContext(), "分享出错!", Toast.LENGTH_LONG).show();
            }
        }
 
        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Toast.makeText(tv_no_network.getContext(), "分享取消!", Toast.LENGTH_LONG).show();
        }
    };
 
    private String getShareUrl() {
        SharedPreferences preferences = tv_no_network.getContext().getSharedPreferences("user",
                Context.MODE_PRIVATE);
        return preferences.getString("share_url", "http://yy.umgotv.com");
    }
 
    private String getShareContent() {
        SharedPreferences preferences = tv_no_network.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String shareContent = preferences.getString(
                "share_content",
                "牛逼的APP,什么样的新片,大片,电视剧都有---"
                        + getResources().getString(
                        R.string.app_name))
                + getShareUrl();
        return shareContent;
    }
 
    /**
     * 打开消息页面
     */
    private void openMsg() {
        EventBus.getDefault().post(new NewComment(true));
        SharedPreferences preferences = tv_no_network.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putBoolean("isCheckedCom", false);
 
        editor.commit();
        startActivity(new Intent(tv_no_network.getContext(),
                SystemMessageActivity.class));
    }
 
    /**
     * 初始化用户登录信息
     */
    private void initLoginInfo() {
        SharedPreferences sp_user = tv_no_network.getContext().getSharedPreferences("user",
                Context.MODE_PRIVATE);
        isLogin = !StringUtils.isEmpty(sp_user.getString("LoginUid", ""));// 获取登录状态
        if (isLogin) {
            String portrait = sp_user.getString("portrait", "");// 用户头像
            String name = sp_user.getString("name", "");// 用户名称
            try {
                Glide.with(getActivity().getApplicationContext()).load(portrait).placeholder(R.drawable.ic_portrait_default)
                        .transform(new GlideCircleTransform(getActivity()))
                        .into(iv_user_logo);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            tv_id.setVisibility(View.VISIBLE);
            tv_name.setText(name);
        } else {// 恢复登录前的状态
            iv_user_logo.setImageResource(R.drawable.img_head_portrait);// 默认头像
            tv_id.setVisibility(View.GONE);
            tv_name.setText("未登录");
        }
    }
 
    /**
     * shizh用户信息的展示
     */
 
    @Override
    public void onResume() {
        super.onResume();
        // 获取缓存中的消息状态
        SharedPreferences preferences = tv_no_network.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        isHaveNewCom = preferences.getBoolean("isCheckedCom", false);
        if (!isLogin) {
            initLoginInfo();
        }
        mChangeHelper.registerReceiver();
        MobclickAgent.onPageStart("主页面—我的");
 
        historyList.clear();
        List<WatchHistoryMineAdapter.MineWatchHistoryVideoInfo> list = new ArrayList<>();
        try {
            list = getWatchHistory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        historyList.addAll(list);
        if (rv_watch_history.getAdapter() == null) {
            WatchHistoryMineAdapter adapter = new WatchHistoryMineAdapter(getContext(), historyList);
            GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.HORIZONTAL, false);
            rv_watch_history.setLayoutManager(layoutManager);
            adapter.initRecyclerViewDisplayWidth(rv_watch_history);
            rv_watch_history.setAdapter(adapter);
        } else {
            rv_watch_history.getAdapter().notifyDataSetChanged();
        }
        String loginUid = UserUtil.getLoginUid(getContext());
        if (!StringUtils.isEmpty(loginUid)) {
            HttpApiUtil.getPersonInfo(getContext(), UserUtil.getUid(getContext()), UserUtil.getLoginUid(getContext()), new BasicTextHttpResponseHandler() {
                @Override
                public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                    if (jsonObject.optBoolean("IsPost")) {
                        JSONObject data = jsonObject.optJSONObject("Data");
                        Glide.with(getActivity()).load(HanJuConstant.addPreFix(data.optString("Portrait"))).transform(new GlideCircleTransform(getContext())).placeholder(R.drawable.ic_portrait_default).error(R.drawable.ic_portrait_default).into(iv_user_logo);
                        tv_name.setText(data.optString("Nickname"));
                        tv_id.setText("ID:" + data.optString("Id"));
                        String sign = data.optString("Sign");
                        if (!StringUtils.isEmpty(sign))
                            tv_desc.setText(sign);
                    } else {
                        String msg = jsonObject.optString("Error");
                        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
                        if (msg != null && msg.contains("注销")) {
                            Editor edit = getContext().getSharedPreferences("user",
                                    Context.MODE_PRIVATE).edit();
                            edit.putString("LoginUid", "");
                            edit.putBoolean("PushType", false);
                            edit.commit();
                            UserUtil.logout(getContext());
                            MineFragment.isLogin = false;// 改为未登录状态
                            initLoginInfo();
                        }
                    }
                }
            });
        }
 
    }
 
    private void getUserVideoDataCount() {
 
        HttpApiUtil.getUserVideoDataCount(getContext(), UserUtil.getUid(getContext()), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optBoolean("IsPost")) {
                    JSONObject data = jsonObject.optJSONObject("Data");
                    int collectionCount = data.optInt("collectionCount");
                    tv_mine_favourite.setText(collectionCount + "");
                    int attentionCount = data.optInt("attentionCount");
                    tv_mine_follow.setText(attentionCount + "");
                }
            }
        });
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
        MobclickAgent.onPageEnd("主页面—我的");
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        isLogin = false;
    }
 
    public static String getVersionName(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return pi.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }
 
    private List<WatchHistoryMineAdapter.MineWatchHistoryVideoInfo> getWatchHistory() throws Exception {
        List<WatchHistoryMineAdapter.MineWatchHistoryVideoInfo> videoInfoList = new ArrayList<>();
        Gson gson = new GsonBuilder().setFieldNamingPolicy(
                FieldNamingPolicy.UPPER_CAMEL_CASE).create();
        SQLiteDatabase database = new HanJuSQLiteOpenHelper(getContext()).getReadableDatabase();
        Cursor cursor = database.query(WatchHistoryTable.TABLE_NAME, new String[]{WatchHistoryTable._ID, WatchHistoryTable.VIDEO_ID, WatchHistoryTable.VIDEO_DETAIL_ID, WatchHistoryTable.VIDEO_DETAIL}, null, null, null, null, WatchHistoryTable.UPDATE_TIME + " DESC");
        cursor.moveToFirst();
        do {
            final Long id = cursor.getLong(cursor
                    .getColumnIndex(WatchHistoryTable._ID));
            final String videoId = cursor.getString(cursor
                    .getColumnIndex(WatchHistoryTable.VIDEO_ID));
            final String videoDetailId = cursor.getString(cursor
                    .getColumnIndex(WatchHistoryTable.VIDEO_DETAIL_ID));
            String videoInfoStr = cursor.getString(cursor
                    .getColumnIndexOrThrow(WatchHistoryTable.VIDEO_DETAIL));
            VideoInfo videoInfo = gson.fromJson(videoInfoStr, new TypeToken<VideoInfo>() {
            }.getType());
            String[] types = new String[]{"150", "151", "152", "153"};
            if (videoInfo.getVideoType() != null && Arrays.asList(types).contains(videoInfo.getVideoType().getId())) {
                videoInfoList.add(new WatchHistoryMineAdapter.MineWatchHistoryVideoInfo(true, videoInfo));
            } else {
                videoInfoList.add(new WatchHistoryMineAdapter.MineWatchHistoryVideoInfo(false, videoInfo));
            }
 
            if (videoInfoList.size() > 20)
                break;
        } while (cursor.moveToNext());
        cursor.close();
        return videoInfoList;
    }
 
}