admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
package com.weikou.beibeivideo.util;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.util.common.StringUtils;
import com.weikou.beibeivideo.entity.VideoDetailInfo;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
 
public class VideoUtil {
 
    public final static List<VideoDetailInfo> videoEpisodeList = new ArrayList<>();
 
    /**
     * 保存临时剧集
     *
     * @param context
     * @param episodeList
     * @param append
     */
    public static void saveVideoEpisodeList(Context context, List<VideoDetailInfo> episodeList, boolean append) {
        if (episodeList == null)
            episodeList = new ArrayList<>();
        if (!append)
            videoEpisodeList.clear();
        videoEpisodeList.addAll(episodeList);
        saveTempVideoDetailList(context, videoEpisodeList);
    }
 
    /**
     * 清除临时剧集
     *
     * @param context
     */
    public static void clearVideoEpisodeList(Context context) {
        videoEpisodeList.clear();
        saveTempVideoDetailList(context, new ArrayList<>());
    }
 
 
    public static String getWatchCountShortName(String watchCount) {
        DecimalFormat df = new DecimalFormat("###.0");
        try {
            return (StringUtils.isBlank(watchCount) ? "0" : (Integer.parseInt(watchCount)) / 10000 > 0 ? df.format(Integer.parseInt(watchCount) / 10000f) + "万" : watchCount);
        } catch (Exception e) {
        }
        return "";
    }
 
 
    private static void saveTempVideoDetailList(Context context, List<VideoDetailInfo> detailInfos) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("videoTempInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
 
      Gson gson=  new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 
        editor.putString("videoDetailInfoList", gson.toJson(detailInfos));
        editor.commit();
    }
 
 
    /**
     * 获取临时剧集
     *
     * @param context
     * @return
     */
    private static List<VideoDetailInfo> getTempVideoDetailList(Context context) {
        Gson gson=  new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
        SharedPreferences sharedPreferences = context.getSharedPreferences("videoTempInfo", Context.MODE_PRIVATE);
        String content = sharedPreferences.getString("videoDetailInfoList", "");
        if (!StringUtils.isEmpty(content)) {
            return gson.fromJson(content, new TypeToken<List<VideoDetailInfo>>() {
            }.getType());
        }
        return new ArrayList<>();
    }
 
    /**
     * 从缓存中加载
     *
     * @param context
     */
    public static void loadVideoEpisodeListCache(Context context) {
        List<VideoDetailInfo> detailList = getTempVideoDetailList(context);
        VideoUtil.videoEpisodeList.clear();
        if (detailList != null && detailList.size() > 0)
            VideoUtil.videoEpisodeList.addAll(detailList);
    }
 
 
}