admin
2021-06-05 ddff7888bf7e754d12fb5fc85a58f3012f456490
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
package com.tejia.lijin.app.ui.recommend;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;
 
import com.bumptech.glide.Glide;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.contract.SearchFgContract;
import com.tejia.lijin.app.entity.HotSearch;
import com.tejia.lijin.app.presenter.SearchFgPresenter;
import com.tejia.lijin.app.ui.dialog.OrderAppealHistoryDeleteHintDialog;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.util.Constant;
import com.tejia.lijin.app.util.FlowLayout;
import com.tejia.lijin.app.util.GlideRoundTransform;
import com.tejia.lijin.app.util.RecordImageTextVideo;
import com.tejia.lijin.app.util.umengCustomEvent.SearchCustomEvent;
import com.tejia.lijin.app.view.BaseFragment;
import com.umeng.analytics.MobclickAgent;
import com.wpc.library.content.ConnectivityChangeHelper;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.cache.DiskLruCache;
import com.wpc.library.util.common.StorageUtils;
import com.wpc.library.util.common.StringUtils;
 
import org.json.JSONObject;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
/**
 * Created by weikou2015 on 2017/2/21.
 * 搜索
 */
 
public class FragmentSearch extends BaseFragment<SearchFgContract.SearchView, SearchFgPresenter> implements SearchFgContract.SearchView, View.OnClickListener {
    private FlowLayout fl_hot_search, fl_search_history;
    private ConnectivityChangeHelper mChangeHelper;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture,
            ll_search_history_hint;
    private ScrollView sv_search;
    private DiskLruCache cache;
    private RelativeLayout ll_guidevideo;//优惠券视频教程
    private RelativeLayout ll_guidevideo2;//优惠券视频教程 更多
    private ImageView ll_guidevideoimg;//优惠券视频 封面图
 
    private Bundle mBundle;//获取Activity传递过来的参数
 
    @Override
    protected int initFragmentView() {
        return R.layout.fragment_search;
    }
 
    @Override
    protected void initFragmentChildView(View contentView) {
        try {
            cache = DiskLruCache.open(
                    new File(StorageUtils.getCacheDirectory(getContext())
                            .toString(), "http"), getVersionNum(getContext()),
                    1, 1024 * 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取Activity传递过来的参数
        mBundle = getArguments();
        ll_guidevideo = contentView.findViewById(R.id.ll_guidevideo);
        ll_guidevideo2 = contentView.findViewById(R.id.ll_guidevideo2);
        ll_guidevideoimg = contentView.findViewById(R.id.ll_guidevideoimg);
        fl_hot_search = contentView.findViewById(R.id.fl_hot_search);
        fl_search_history = contentView.findViewById(R.id.fl_search_history);
        ll_no_net = contentView.findViewById(R.id.ll_no_net);
        ll_no_data = contentView.findViewById(R.id.ll_no_data);
        ll_request_failture = contentView.findViewById(R.id.ll_request_failture);
        contentView.findViewById(R.id.tv_net_setting).setOnClickListener(this);
        contentView.findViewById(R.id.tv_refresh).setOnClickListener(this);
        sv_search = contentView.findViewById(R.id.sv_search);
        ll_search_history_hint = contentView.findViewById(R.id.ll_search_history_hint);
        contentView.findViewById(R.id.iv_delete_history).setOnClickListener(this);
        ll_guidevideo2.setOnClickListener(this);
        ll_guidevideoimg.setOnClickListener(this);
 
        mPresenter.initCacheData(cache, fl_hot_search, mBundle.getInt("id"), hList);//热门搜索缓存
        setSearchHistoryData();
 
        mChangeHelper = new ConnectivityChangeHelper(getContext(),
                new ConnectivityChangeHelper.OnConnectivityChangeListener() {
 
                    @Override
                    public void onNetworkUnAvailable() {
                        if (mList.size() == 0)
                            requestState(3);
                    }
 
                    @Override
                    public void onNetworkAvailable() {
                        requestState(0);
                        if (hList.size() == 0) {
                            fl_hot_search.removeAllViews();
                            mPresenter.getHotSearch(mBundle.getInt("id"));//热门搜索
                        }
                    }
 
                });
 
        //编辑完之后点击软键盘上的回车键
        //        setHotData();
 
        //改变 shape 背景色
        GradientDrawable p = (GradientDrawable) ll_guidevideoimg.getBackground();
        p.setColor(Color.parseColor("#EBEBEB"));
        //获取 图文视频地址
        RecordImageTextVideo textVideo = new RecordImageTextVideo(getContext());
        videoPicture = textVideo.getvideoPicture();
        videoUrl = textVideo.getvideoUrl();
        videoMoreUrl = textVideo.getvideoMoreUrl();
        txtImgUtl = textVideo.gettxtImgUtl();
        if (videoPicture == null || videoPicture.length() == 0 || videoPicture.equals(" ")) {//视频图片为空 隐藏视频
            ll_guidevideo.setVisibility(View.GONE);
        } else {
            Glide.with(this).load(videoPicture)
                    .transform(new GlideRoundTransform(getContext(), 8))
                    .into(ll_guidevideoimg);
        }
    }
 
 
    private void setSearchHistoryData() {
        //设置历史搜索数据
        mPresenter.fillFlowData(mList, fl_search_history, new SearchFgPresenter.ISearchItemClick() {
            @Override
            public void onClick(String text, int position) {
                mPresenter.addSearchHistory(text, mBundle.getInt("id"));
                SearchCustomEvent.searchHistory(getContext());
                mPresenter.addSearchResultActivity(text, mBundle.getInt("id"));
            }
        });
    }
 
    @Override
    protected void initFragmentData(Bundle savedInstanceState) {
 
    }
 
    /**
     * 初始化presenter
     *
     * @return
     */
    @Override
    protected SearchFgPresenter createPresenter() {
        return new SearchFgPresenter(getContext());
    }
 
 
    private String videoPicture;//视频封面图
    private String videoUrl;//点击封面图跳转的链接
    private String videoMoreUrl;//点击更多视频跳转的链接
    private String txtImgUtl;//图文更多跳转的链接
 
    List<String> mList = new ArrayList<>();
 
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && (getActivity()) != null) {
            //TODO now it's visible to user对用户可见
            mPresenter.setHistoricalRecords(mBundle.getInt("id"), mList, ll_search_history_hint, null, ll_guidevideo, videoPicture);
            setSearchHistoryData();
            Random random = new Random();
            String key = getActivity().getIntent().getStringExtra("key");
//            if (mBundle.getInt("id") != 1) {
            if (StringUtils.isEmpty(key) && hList.size() > 0) {
                ((SearchActivity) getActivity()).et_search.setHint(" " + hList.get(random.nextInt(hList.size())).getName());
            } else {
                ((SearchActivity) getActivity()).et_search.setHint(" " + key);
            }
//            }
        } else {
            //TODO now it's invisible to user对用户不可见
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
        MobclickAgent.onPageStart("搜索页");
        mPresenter.setHistoricalRecords(mBundle.getInt("id"), mList, ll_search_history_hint, null, ll_guidevideo, videoPicture);//显示历史记录
        setSearchHistoryData();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
        MobclickAgent.onPageEnd("搜索页");
    }
 
 
    private List<HotSearch> hList = new ArrayList<>();
 
 
    /**
     * .热门搜索请求成功
     *
     * @param list
     */
    @Override
    public void onSearchSuccess(List<HotSearch> list, JSONObject object) throws IOException {
//        if (mBundle.getInt("id") == 1) {
//            Log.e("eee", "list: " + list.size());
//        }
        if (list != null) {
            hList.clear();
            fl_hot_search.removeAllViews();
            hList.addAll(list);
            if (mBundle.getInt("id") == 1) {
                Random random = new Random();
                String key = getActivity().getIntent().getStringExtra("key");
//                Log.e("eee", "setUserVisibleHint: " + hList.size());
                if (StringUtils.isEmpty(key) && hList.size() > 0) {
                    ((SearchActivity) getActivity()).et_search.setHint(" " + hList.get(random.nextInt(hList.size())).getName());
                } else {
                    ((SearchActivity) getActivity()).et_search.setHint(" " + key);
                }
//                Log.e("eee", "onSearchSuccess: " + hList.size());
            }
            requestState(hList.size() == 0 ? 1 : 0);
            mPresenter.SuccessfulRequest(list, mBundle.getInt("id"), fl_hot_search, cache, object);//请求成功
        } else {
            if (hList.size() == 0)
                requestState(2);
        }
    }
 
    /**
     * 热门搜索请求失败
     *
     * @param errorInfo
     */
    @Override
    public void onSearchFail(String errorInfo) {
        if (hList.size() == 0) {
            if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                requestState(3);
            } else {
                requestState(2);
            }
        } else {
            if (NetUtils.getNetworkState(getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                Toast.makeText(getContext(), "网络未连接,请检测网络设置", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
            }
        }
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_delete_history:
//                clearHistory();
                OrderAppealHistoryDeleteHintDialog.Builder builder = new OrderAppealHistoryDeleteHintDialog.Builder(getContext());
                builder.setMessage("是否要清空搜索记录?").setNegativeButton("不了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        SharedPreferences sp;
                        if (mBundle.getInt("id") == Constant.GOODS_TYPE_TB) {
                            sp = getContext().getSharedPreferences("searchHistory", Context.MODE_PRIVATE);
                        } else if (mBundle.getInt("id") == Constant.GOODS_TYPE_JD) {
                            sp = getContext().getSharedPreferences("searchHistory_jd", Context.MODE_PRIVATE);
                        } else if (mBundle.getInt("id") == Constant.GOODS_TYPE_PDD) {
                            sp = getContext().getSharedPreferences("searchHistory_pdd", Context.MODE_PRIVATE);
                        } else if (mBundle.getInt("id") == Constant.GOODS_TYPE_VIP) {
                            sp = getContext().getSharedPreferences("searchHistory_vip", Context.MODE_PRIVATE);
                        } else if (mBundle.getInt("id") == Constant.GOODS_TYPE_SUNING) {
                            sp = getContext().getSharedPreferences("searchHistory_suning", Context.MODE_PRIVATE);
                        } else {
                            sp = getContext().getSharedPreferences("searchHistory", Context.MODE_PRIVATE);
                        }
                        SharedPreferences.Editor editor = sp.edit();
                        editor.clear();
                        editor.apply();
                        mList.clear();
                        setSearchHistoryData();
                        ll_search_history_hint.setVisibility(View.GONE);
                        if (videoPicture == null || videoPicture.length() == 0 || videoPicture.equals(" ")) {//视频图片为空 隐藏视频
                            ll_guidevideo.setVisibility(View.GONE);
                        } else {
                            ll_guidevideo.setVisibility(View.VISIBLE);
                        }
                    }
                }).create().show();
                break;
            case R.id.tv_net_setting:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.tv_refresh:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
//                requestState(0);
//                setHistory();
//                setHotData();
                break;
 
            case R.id.ll_guidevideo2://视频更多
                Intent intent2 = new Intent(getContext(), ShareBrowserActivity.class);
                intent2.putExtra("url", videoMoreUrl);
                intent2.putExtra("title", "新手攻略");
                startActivity(intent2);
                break;
            case R.id.ll_guidevideoimg://视频播放
                Intent intent3 = new Intent(getContext(), ShareBrowserActivity.class);
                intent3.putExtra("url", videoUrl);
                intent3.putExtra("title", "新手攻略");
                startActivity(intent3);
                break;
            default:
                break;
        }
    }
 
    /**
     * 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
     *
     * @param state
     */
    private void requestState(int state) {
        sv_search.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
        ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
        ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
    }
 
 
    private int getVersionNum(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 1;
        }
    }
}