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.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.BaseAdapter;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.RelativeLayout;
|
import android.widget.ScrollView;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.bumptech.glide.Glide;
|
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 com.wpc.library.widget.MyListView;
|
import com.tejia.lijin.app.contract.SearchFgContract;
|
import com.tejia.lijin.app.presenter.SearchFgPresenter;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.util.Constant;
|
import com.tejia.lijin.app.view.BaseFragment;
|
import com.tejia.lijin.app.entity.HotSearch;
|
import com.tejia.lijin.app.ui.dialog.OrderAppealHistoryDeleteHintDialog;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
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 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;
|
private HistoryAdapter adapter;
|
private MyListView lv_history;
|
// private AutoCompleteTextView et_search;
|
private TextView tv_clear_history;
|
private ConnectivityChangeHelper mChangeHelper;
|
private LinearLayout ll_no_net, ll_no_data, ll_request_failture,
|
ll_search_history_hint;
|
private ScrollView sv_search;
|
// public String Searchtxt;//搜索关键词
|
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);
|
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);
|
lv_history = contentView.findViewById(R.id.lv_search_history);
|
ll_search_history_hint = contentView.findViewById(R.id.ll_search_history_hint);
|
tv_clear_history = contentView.findViewById(R.id.tv_clear_history);
|
tv_clear_history.setOnClickListener(this);
|
ll_guidevideo2.setOnClickListener(this);
|
ll_guidevideoimg.setOnClickListener(this);
|
|
mPresenter.initCacheData(cache, fl_hot_search, mBundle.getInt("id"), hList);//热门搜索缓存
|
adapter = new HistoryAdapter(mList);
|
lv_history.setAdapter(adapter);
|
|
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);
|
}
|
}
|
|
@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, adapter, ll_search_history_hint, null, ll_guidevideo, videoPicture);
|
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, adapter, ll_search_history_hint, null, ll_guidevideo, videoPicture);//显示历史记录
|
}
|
|
@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.tv_clear_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();
|
adapter.notifyDataSetChanged();
|
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;
|
}
|
}
|
|
|
/**
|
* 历史记录
|
*/
|
public class HistoryAdapter extends BaseAdapter {
|
List<String> mList;
|
|
public HistoryAdapter(List<String> list) {
|
mList = list;
|
}
|
|
@Override
|
public int getCount() {
|
return mList == null ? 0 : mList.size();
|
}
|
|
@Override
|
public Object getItem(int position) {
|
return mList.get(position);
|
}
|
|
@Override
|
public long getItemId(int position) {
|
return position;
|
}
|
|
@Override
|
public View getView(final int position, View view, ViewGroup parent) {
|
view = LayoutInflater.from(getContext()).inflate(R.layout.item_search_history, null);
|
final TextView tv_search_content = view.findViewById(R.id.tv_search_content);
|
final ImageView iv_search_content_close = view.findViewById(R.id.iv_search_content_close);
|
tv_search_content.setText(mList.get(position));
|
tv_search_content.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
mPresenter.addSearchHistory(tv_search_content.getText().toString(), mBundle.getInt("id"));
|
SearchCustomEvent.searchHistory(getContext());
|
mPresenter.addSearchResultActivity(tv_search_content.getText().toString(), mBundle.getInt("id"));
|
}
|
});
|
iv_search_content_close.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
mList.remove(position);
|
notifyDataSetChanged();
|
if (mList.size() == 0) {
|
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);
|
}
|
}
|
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.putInt("searchNums", mList.size());
|
for (int i = 0; i < mList.size(); i++) {
|
editor.putString("searchItem_" + (mList.size() - 1 - i), mList.get(mList.size() - 1 - i));
|
}
|
editor.apply();
|
}
|
});
|
return view;
|
}
|
}
|
}
|