admin
2021-10-21 7e2ac66d7c532a5725635fa3913789bb17c1e157
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
package com.mugua.mgvideo.ui.category.fragment;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.lcjian.library.RetainViewFragment;
import com.lcjian.library.util.cache.DiskLruCache;
import com.lcjian.library.util.common.StorageUtils;
import com.mugua.mgvideo.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.umeng.analytics.MobclickAgent;
import com.mugua.mgvideo.MGVideoAPI;
import com.mugua.mgvideo.ui.category.ChannelAdapter;
import com.mugua.mgvideo.ui.category.ChannelAdapterHot;
import com.yeshi.base.entity.video.VideoType;
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ScrollView;
import android.widget.TextView;
 
/**
 * 频道碎片
 */
public class ChannelFragment extends RetainViewFragment {
    private PullToRefreshScrollView channelScrollView;
    private GridView gv_channel, gv_channel_hot;
    private TextView tv_hotsort;
    private DiskLruCache cache;
    // 判断请求是否成功,默认成功,避免二次执行请求
    private boolean isSucceed = true;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            cache = DiskLruCache.open(
                    new File(StorageUtils.getCacheDirectory(getActivity())
                            .toString(), "http"), getVersionNum(getActivity()),
                    1, 1024 * 1024);
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /** 获取频道页数据 */
    private void getCategories() {
        if (getActivity() == null)
            return;
        SharedPreferences preferences = getActivity().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        MGVideoAPI.getClass(getActivity(), uid,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                            Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            getdatas(jsonObject.getJSONObject("Data")
                                    .getJSONArray("data").toString());
                            // cache.remove(getKey("getCategories"));
                            DiskLruCache.Editor editor = cache
                                    .edit(getKey("getCategories"));
 
                            editor.set(0, jsonObject.getJSONObject("Data")
                                    .getJSONArray("data").toString());
                            editor.commit();
                            isSucceed = true;
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            byte[] responseBytes, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseBytes,
                                throwable);
                        isSucceed = false;
                    }
 
                    @Override
                    public void onFinish() {
                        channelScrollView.onRefreshComplete();
                    }
                });
    }
 
    /**
     * 通过json String获取数据
     * 
     * @param jsonObject
     *            jsonObject.getJSONObject("Data").getJSONArray("data").toString
     *            ()
     * @throws Exception
     */
    protected void getdatas(String jsonObject) throws Exception {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(
                FieldNamingPolicy.UPPER_CAMEL_CASE).create();
        List<VideoType> categories = gson.fromJson(jsonObject,
                new TypeToken<List<VideoType>>() {
                }.getType());
        List<VideoType> mainTypes = new ArrayList<VideoType>();
        List<VideoType> hotTypes = new ArrayList<VideoType>();
        for (VideoType vt : categories) {
            if ("1".equalsIgnoreCase(vt.getMain())) {// 主分类
                mainTypes.add(vt);
            } else {// 热门分类
                hotTypes.add(vt);
            }
        }
        if (hotTypes.size() > 0) {
            gv_channel_hot.setVisibility(View.VISIBLE);
            tv_hotsort.setVisibility(View.INVISIBLE);
        } else {
            gv_channel_hot.setVisibility(View.GONE);
        }
        gv_channel.setAdapter(new ChannelAdapter(mainTypes, getActivity()));
        gv_channel_hot
                .setAdapter(new ChannelAdapterHot(hotTypes, getActivity()));
        // 显示热门分类
        if (tv_hotsort.getVisibility() == View.INVISIBLE) {
            tv_hotsort.setVisibility(View.VISIBLE);
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    private String getKey(String method) {
        return new Md5FileNameGenerator().generate(method);
    }
 
    /** 获取版本号 */
    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;
        }
    }
 
    @Override
    public int getContentResource() {
        return R.layout.category_channel;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        channelScrollView = (PullToRefreshScrollView) contentView
                .findViewById(R.id.channel_ptrsv);
        channelScrollView.setMode(Mode.PULL_FROM_START);
        channelScrollView
                .setOnRefreshListener(new OnRefreshListener<ScrollView>() {
                    @Override
                    public void onRefresh(
                            PullToRefreshBase<ScrollView> refreshView) {
                        // TODO 频道下拉刷新逻辑
                        getCategories();
                    }
                });
        tv_hotsort = (TextView) contentView
                .findViewById(R.id.category_channel_tv_hot);
        gv_channel = (GridView) contentView
                .findViewById(R.id.category_channel_gv_sort);
        gv_channel_hot = (GridView) contentView
                .findViewById(R.id.category_channel_gv_hotsort);
        if (cache != null) {
            DiskLruCache.Snapshot snapshot = null;
            try {
                snapshot = cache.get(getKey("getCategories"));
                if (snapshot != null) {
                    getdatas(snapshot.getString(0));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (snapshot != null) {
                    snapshot.close();
                }
            }
        }
        getCategories();
    }
 
    /** 刷新分类页 */
    public void refreshCategories() {
        if (!isSucceed) {// 之前请求失败时才执行
            getCategories();
        }
    }
}