|
|
@@ -0,0 +1,241 @@
|
|
|
+package com.sheep.gamegroup.module.game.activity;
|
|
|
+
|
|
|
+import android.support.v4.app.Fragment;
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.view.inputmethod.EditorInfo;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
|
|
|
+import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
|
|
|
+import com.sheep.gamegroup.absBase.BaseContainerActivity;
|
|
|
+import com.sheep.gamegroup.absBase.IHomePageSearch;
|
|
|
+import com.sheep.gamegroup.absBase.ISearchRecord;
|
|
|
+import com.sheep.gamegroup.greendao.DDProviderHelper;
|
|
|
+import com.sheep.gamegroup.greendao.download.SearchRecord;
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+import com.sheep.gamegroup.model.entity.GameEntity;
|
|
|
+import com.sheep.gamegroup.model.entity.TitleInfoList;
|
|
|
+import com.sheep.gamegroup.model.entity.TopSearchStatistics;
|
|
|
+import com.sheep.gamegroup.model.util.SheepSubscriber;
|
|
|
+import com.sheep.gamegroup.module.game.fragment.FgtSearchGameGroup;
|
|
|
+import com.sheep.gamegroup.util.ApiUtil;
|
|
|
+import com.sheep.gamegroup.util.DataUtil;
|
|
|
+import com.sheep.gamegroup.util.ListUtil;
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
+import com.sheep.gamegroup.view.adapter.AdpTitleInfoList;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
|
|
|
+
|
|
|
+import org.afinal.simplecache.ApiKey;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2019/1/21.
|
|
|
+ * realicing@sina.com
|
|
|
+ * 小绵羊3.4.10新增 -- 搜索游戏组
|
|
|
+ */
|
|
|
+public class ActSearchGameGroup extends BaseContainerActivity {
|
|
|
+ @BindView(R.id.search_app_list)
|
|
|
+ RecyclerView recyclerView;
|
|
|
+ @BindView(R.id.frame_container)
|
|
|
+ View frame_container;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int getLayoutId() {
|
|
|
+ return R.layout.act_search_app_or_task;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FgtSearchGameGroup fgtSearchGameGroup;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initView() {
|
|
|
+ super.initView();
|
|
|
+ if (fgtSearchGameGroup == null && fragment instanceof FgtSearchGameGroup)
|
|
|
+ fgtSearchGameGroup = (FgtSearchGameGroup) fragment;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Fragment initFragment() {
|
|
|
+ return new FgtSearchGameGroup();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<TitleInfoList> titleInfoListList = new ArrayList<>();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initListener() {
|
|
|
+ TitleBarUtils.getInstance()
|
|
|
+ .setShowOrHide(this, true)
|
|
|
+ .setTitleFinish(this)
|
|
|
+ .setSearchBox(this, getString(R.string.search_game_name),
|
|
|
+ (v, actionId, event) -> {
|
|
|
+ if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
|
+ toSearchApp();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+ (text) -> {
|
|
|
+ frame_container.removeCallbacks(autoSearchRunnable);
|
|
|
+ if (text.isEmpty() && !isTitleInfoListEmpty()) {//输入框内容为空且有搜索记录时显示搜索记录
|
|
|
+ recyclerView.setVisibility(View.VISIBLE);
|
|
|
+ frame_container.setVisibility(View.INVISIBLE);
|
|
|
+ } else {
|
|
|
+ long delay = textByClick ? 1L : 1000L;
|
|
|
+ frame_container.postDelayed(autoSearchRunnable, delay);//1秒后自动搜索
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .setRightBtn(this, "搜索", 0,
|
|
|
+ v -> toSearchApp());
|
|
|
+
|
|
|
+ recyclerView.setHasFixedSize(true);
|
|
|
+ recyclerView.setNestedScrollingEnabled(false);
|
|
|
+ recyclerView.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
|
|
|
+ AdpTitleInfoList adpTitleInfoList = new AdpTitleInfoList(titleInfoListList);
|
|
|
+ adpTitleInfoList.bindToRecyclerView(recyclerView);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Runnable autoSearchRunnable = this::toSearchApp;
|
|
|
+
|
|
|
+ public static final int MAX_SHOW_SEARCH_RECORD_COUNT = 10;//最多展示的搜索历史的个数
|
|
|
+ public static final int SORT_TOP_SEARCH = 1;//热门推荐 的排序
|
|
|
+ public static final int SORT_SEARCH_RECORD = 2;//搜索历史 的排序
|
|
|
+ List<ISearchRecord> topSearchList = new ArrayList<>();
|
|
|
+
|
|
|
+ public static final String TOP_SEARCH_NAME = "热门搜索";
|
|
|
+ @Override
|
|
|
+ public void initData() {
|
|
|
+ //尝试获取缓存数据
|
|
|
+ List<GameEntity> newList = DataUtil.getInstance().getCacheList(ApiKey.getFindUserLike, GameEntity.class);
|
|
|
+ if (!ListUtil.isEmpty(newList)) {
|
|
|
+ topSearchList.addAll(newList);
|
|
|
+ addListData(TOP_SEARCH_NAME, topSearchList, SORT_TOP_SEARCH);
|
|
|
+ }
|
|
|
+
|
|
|
+ //初始化搜索历史
|
|
|
+ List<SearchRecord> newSgrList = DDProviderHelper.getInstance().getSearchRecordList(MAX_SHOW_SEARCH_RECORD_COUNT);
|
|
|
+ if (!ListUtil.isEmpty(newSgrList)) {
|
|
|
+ searchRecordList.addAll(newSgrList);
|
|
|
+ addListData("搜索历史", searchRecordList, SORT_SEARCH_RECORD);
|
|
|
+ }
|
|
|
+ notifyDataSetChangedTitleInfoList();
|
|
|
+ SheepApp.getInstance().getNetComponent().getApiService().getFindUserLike()
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
+ @Override
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
+ boolean isNewData = DataUtil.getInstance().isNewData(ApiKey.top_search_statistics);
|
|
|
+ if (isNewData) {
|
|
|
+ List<TopSearchStatistics> newList = baseMessage.getDatas(TopSearchStatistics.class);
|
|
|
+ topSearchList.clear();
|
|
|
+ topSearchList.addAll(newList);
|
|
|
+ if (ListUtil.getItem(titleInfoListList, titleInfoList -> titleInfoList.getSort() == SORT_TOP_SEARCH) == null) {
|
|
|
+ addListData(TOP_SEARCH_NAME, topSearchList, SORT_TOP_SEARCH);
|
|
|
+ }
|
|
|
+ notifyDataSetChangedTitleInfoList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //排序与刷新
|
|
|
+ private void notifyDataSetChangedTitleInfoList() {
|
|
|
+ //排序
|
|
|
+ Collections.sort(titleInfoListList);
|
|
|
+ //刷新
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
+ if (isTitleInfoListEmpty()) {
|
|
|
+ frame_container.setVisibility(View.VISIBLE);
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
+ } else {
|
|
|
+ frame_container.setVisibility(View.INVISIBLE);
|
|
|
+ recyclerView.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加列表数据到总的列表中
|
|
|
+ private void addListData(String title, final List<ISearchRecord> list, int sort) {
|
|
|
+ titleInfoListList.add(new TitleInfoList(title, new RecyclerViewAdapter<ISearchRecord>(SheepApp.getInstance(), R.layout.item_i_serach_record, list) {
|
|
|
+ @Override
|
|
|
+ public void convert(ViewHolder viewHolder, final ISearchRecord item, int position) {
|
|
|
+ View view = viewHolder.itemView;
|
|
|
+ TextView item_search_record_name = view.findViewById(R.id.item_search_record_name);
|
|
|
+ ImageView item_search_record_x = view.findViewById(R.id.item_search_record_x);
|
|
|
+ ViewUtil.setText(item_search_record_name, item.getInput());
|
|
|
+ if (item instanceof SearchRecord) {
|
|
|
+ //点击一条搜索历史后面的xx
|
|
|
+ item_search_record_x.setOnClickListener(view12 -> {
|
|
|
+ list.remove(item);
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
+ DDProviderHelper.getInstance().deleteSearchRecord((SearchRecord) item);
|
|
|
+ if (isTitleInfoListEmpty()) {
|
|
|
+ frame_container.setVisibility(View.VISIBLE);
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ ViewUtil.setVisibility(item_search_record_x, false);
|
|
|
+ }
|
|
|
+ //点击一条搜索历史
|
|
|
+ view.setOnClickListener(view1 -> {
|
|
|
+ textByClick = true;
|
|
|
+ TitleBarUtils.getInstance().setSearchText(ActSearchGameGroup.this, item.getInput());
|
|
|
+ if (item instanceof IHomePageSearch)
|
|
|
+ ApiUtil.postTopSearchStatisticsClickTopSearch((IHomePageSearch) item);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).setSort(sort));
|
|
|
+ }
|
|
|
+ private boolean textByClick = false;//通过点击搜索历史或者热门推荐来搜索,应该立即执行
|
|
|
+
|
|
|
+ //搜索历史中没有数据且没有获取到服务器的热门搜索列表
|
|
|
+ private boolean isTitleInfoListEmpty() {
|
|
|
+ return searchRecordList.isEmpty() && topSearchList.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ISearchRecord> searchRecordList = new ArrayList<>();
|
|
|
+
|
|
|
+ //尝试搜索游戏
|
|
|
+ private void toSearchApp() {
|
|
|
+ textByClick = false;//重置
|
|
|
+ if(fgtSearchGameGroup == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(fgtSearchGameGroup.isLoadingData()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
+ frame_container.setVisibility(View.VISIBLE);
|
|
|
+ String inputText = TitleBarUtils.getInstance().getSearchText(this);
|
|
|
+ if (!TextUtils.isEmpty(inputText)) {
|
|
|
+ //保存搜索到数据库
|
|
|
+ SearchRecord searchRecord = new SearchRecord();
|
|
|
+ searchRecord.setInput(inputText);
|
|
|
+ searchRecord.setLast_time(System.currentTimeMillis());
|
|
|
+ searchRecord.setCount(1);
|
|
|
+ DDProviderHelper.getInstance().addOrUpdateSearchRecord(searchRecord, item -> {
|
|
|
+ if(item == null) {
|
|
|
+ searchRecordList.add(searchRecord);
|
|
|
+ ViewUtil.notifyDataSetChanged(recyclerView);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ fgtSearchGameGroup.toSearch(inputText);
|
|
|
+ }
|
|
|
+}
|