|
|
@@ -1,50 +1,60 @@
|
|
|
package com.sheep.gamegroup.view.activity;
|
|
|
|
|
|
-import android.os.Bundle;
|
|
|
import android.support.v4.app.Fragment;
|
|
|
-import android.support.v4.app.FragmentTransaction;
|
|
|
import android.support.v7.widget.AppCompatAutoCompleteTextView;
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
import android.text.Editable;
|
|
|
import android.text.TextUtils;
|
|
|
import android.text.TextWatcher;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
-import android.view.ViewGroup;
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
-import android.widget.AdapterView;
|
|
|
import android.widget.ImageView;
|
|
|
-import android.widget.ListView;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
-import com.sheep.gamegroup.absBase.BaseActivity;
|
|
|
-import com.sheep.gamegroup.model.entity.SearchAppRecord;
|
|
|
+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.ISearch;
|
|
|
+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.Applications;
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+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.util.ApiUtil;
|
|
|
+import com.sheep.gamegroup.util.DataUtil;
|
|
|
import com.sheep.gamegroup.util.ListUtil;
|
|
|
-import com.sheep.gamegroup.util.MyDbManager;
|
|
|
import com.sheep.gamegroup.util.ViewUtil;
|
|
|
-import com.sheep.gamegroup.view.adapter.ArrayAdapter;
|
|
|
-import com.sheep.gamegroup.view.fragment.FgtSearchApp;
|
|
|
+import com.sheep.gamegroup.view.adapter.AdpTitleInfoList;
|
|
|
+import com.sheep.gamegroup.view.fragment.FgtSearchAppOrTask;
|
|
|
import com.sheep.jiuyan.samllsheep.R;
|
|
|
import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
|
|
|
+import org.afinal.simplecache.ApiKey;
|
|
|
+
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.OnClick;
|
|
|
+import rx.android.schedulers.AndroidSchedulers;
|
|
|
+import rx.schedulers.Schedulers;
|
|
|
|
|
|
/**
|
|
|
* Created by realicing on 2018/9/13.
|
|
|
* realicing@sina.com
|
|
|
*/
|
|
|
-public class ActSearchAppOrTask extends BaseActivity {
|
|
|
+public class ActSearchAppOrTask extends BaseContainerActivity {
|
|
|
@BindView(R.id.title_search_input)
|
|
|
AppCompatAutoCompleteTextView title_search_input;
|
|
|
@BindView(R.id.search_app_list)
|
|
|
- ListView search_app_list;
|
|
|
+ RecyclerView recyclerView;
|
|
|
@BindView(R.id.frame_container)
|
|
|
View frame_container;
|
|
|
@BindView(R.id.title_search_commit)
|
|
|
@@ -52,69 +62,25 @@ public class ActSearchAppOrTask extends BaseActivity {
|
|
|
|
|
|
@Override
|
|
|
protected int getLayoutId() {
|
|
|
- return R.layout.act_search_app;
|
|
|
+ return R.layout.act_search_app_or_task;
|
|
|
}
|
|
|
|
|
|
- private FgtSearchApp fgtSearchApp;
|
|
|
|
|
|
@Override
|
|
|
- public void initView() {
|
|
|
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
|
|
- Bundle bundle = new Bundle();
|
|
|
- Fragment fragment = getSupportFragmentManager().findFragmentByTag("tag");
|
|
|
- if (fragment == null) {
|
|
|
- fragment = new FgtSearchApp();
|
|
|
- fragment.setArguments(bundle);
|
|
|
- transaction.add(R.id.frame_container, fragment, "tag");
|
|
|
- transaction.commitAllowingStateLoss();
|
|
|
- } else {
|
|
|
- fragment.setArguments(bundle);
|
|
|
- transaction.replace(R.id.frame_container, fragment);
|
|
|
- transaction.commitAllowingStateLoss();
|
|
|
- }
|
|
|
- if (fgtSearchApp == null && fragment instanceof FgtSearchApp)
|
|
|
- fgtSearchApp = (FgtSearchApp) fragment;
|
|
|
- inputAdapter = new ArrayAdapter<SearchAppRecord>(SheepApp.getInstance(), R.layout.item_serach_record, searchRecordList){
|
|
|
- @Override
|
|
|
- public boolean convert(int position, View view, ViewGroup parent, final SearchAppRecord item) {
|
|
|
- TextView item_search_record_name = (TextView)view.findViewById(R.id.item_search_record_name);
|
|
|
- ImageView item_search_record_x = (ImageView)view.findViewById(R.id.item_search_record_x);
|
|
|
- ViewUtil.setText(item_search_record_name, item.getInput());
|
|
|
- item_search_record_x.setOnClickListener(new View.OnClickListener() {
|
|
|
- @Override
|
|
|
- public void onClick(View view) {
|
|
|
- searchRecordMap.remove(item.getInput());
|
|
|
- allSearchRecordList.remove(item);
|
|
|
- searchRecordList.clear();
|
|
|
- ListUtil.addAll(searchRecordList, allSearchRecordList, 10);
|
|
|
- inputAdapter.notifyDataSetChanged();
|
|
|
- MyDbManager.getInstance().hideSearchApp(item);
|
|
|
- if(searchRecordList.isEmpty()){
|
|
|
- frame_container.setVisibility(View.VISIBLE);
|
|
|
- search_app_list.setVisibility(View.INVISIBLE);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- return true;
|
|
|
- }
|
|
|
- };
|
|
|
- search_app_list.setAdapter(inputAdapter);
|
|
|
+ protected Fragment initFragment() {
|
|
|
+ return new FgtSearchAppOrTask();
|
|
|
}
|
|
|
|
|
|
- private ArrayAdapter<SearchAppRecord> inputAdapter;
|
|
|
+ private List<TitleInfoList> titleInfoListList = new ArrayList<>();
|
|
|
|
|
|
@Override
|
|
|
public void initListener() {
|
|
|
- search_app_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
- @Override
|
|
|
- public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
|
|
|
- SearchAppRecord item = ListUtil.getItem(searchRecordList, position);
|
|
|
- if(item != null){
|
|
|
- title_search_input.setText(item.getInput());
|
|
|
- title_search_input.setSelection(item.getInput().length());
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
+ recyclerView.setHasFixedSize(true);
|
|
|
+ recyclerView.setNestedScrollingEnabled(false);
|
|
|
+ recyclerView.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
|
|
|
+ AdpTitleInfoList adpTitleInfoList = new AdpTitleInfoList(titleInfoListList);
|
|
|
+ adpTitleInfoList.bindToRecyclerView(recyclerView);
|
|
|
+
|
|
|
title_search_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
@Override
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
@@ -137,8 +103,8 @@ public class ActSearchAppOrTask extends BaseActivity {
|
|
|
@Override
|
|
|
public void afterTextChanged(Editable editable) {
|
|
|
title_search_input.removeCallbacks(autoSearchRunnable);
|
|
|
- if(editable.toString().isEmpty() && !searchRecordList.isEmpty()){//输入框内容为空且有搜索记录时显示搜索记录
|
|
|
- search_app_list.setVisibility(View.VISIBLE);
|
|
|
+ if (editable.toString().isEmpty() && !isTitleInfoListEmpty()) {//输入框内容为空且有搜索记录时显示搜索记录
|
|
|
+ recyclerView.setVisibility(View.VISIBLE);
|
|
|
frame_container.setVisibility(View.INVISIBLE);
|
|
|
} else {
|
|
|
title_search_input.postDelayed(autoSearchRunnable, 1000L);//1秒后自动搜索
|
|
|
@@ -150,36 +116,120 @@ public class ActSearchAppOrTask extends BaseActivity {
|
|
|
private Runnable autoSearchRunnable = new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- if(title_search_commit != null)
|
|
|
+ if (title_search_commit != null)
|
|
|
title_search_commit.performClick();
|
|
|
}
|
|
|
};
|
|
|
- private Comparator<SearchAppRecord> comparator = new Comparator<SearchAppRecord>() {
|
|
|
- @Override
|
|
|
- public int compare(SearchAppRecord item1, SearchAppRecord item2) {
|
|
|
- return item2.getCount() - item1.getCount();
|
|
|
- }
|
|
|
- };
|
|
|
+
|
|
|
+ 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> topSearchStatisticsList = new ArrayList<>();
|
|
|
@Override
|
|
|
public void initData() {
|
|
|
+ //尝试获取缓存数据
|
|
|
+ List<TopSearchStatistics> newList = DataUtil.getInstance().getCacheList(ApiKey.top_search_statistics, TopSearchStatistics.class);
|
|
|
+ if (!ListUtil.isEmpty(newList)) {
|
|
|
+ topSearchStatisticsList.addAll(newList);
|
|
|
+ addListData("热门推荐", topSearchStatisticsList, SORT_TOP_SEARCH);
|
|
|
+ }
|
|
|
+
|
|
|
//初始化搜索历史
|
|
|
- List<SearchAppRecord> newSgrList = MyDbManager.getInstance().getAllSearchAppRecord();
|
|
|
- ListUtil.addAll(allSearchRecordList, newSgrList);
|
|
|
- Collections.sort(allSearchRecordList, comparator);
|
|
|
- ListUtil.addAll(searchRecordList, allSearchRecordList, 10);
|
|
|
- for (SearchAppRecord item : allSearchRecordList) {
|
|
|
- searchRecordMap.put(item.getInput(), item);
|
|
|
+ List<SearchRecord> newSgrList = DDProviderHelper.getInstance().getSearchRecordList(SheepApp.getInstance(), MAX_SHOW_SEARCH_RECORD_COUNT);
|
|
|
+ if (!ListUtil.isEmpty(newSgrList)) {
|
|
|
+ searchRecordList.addAll(newSgrList);
|
|
|
+ addListData("搜索历史", searchRecordList, SORT_SEARCH_RECORD);
|
|
|
}
|
|
|
- inputAdapter.notifyDataSetChanged();
|
|
|
- if(searchRecordList.isEmpty()) {
|
|
|
+ notifyDataSetChangedTitleInfoList();
|
|
|
+ SheepApp.getInstance().getNetComponent().getApiService().getTopSearchStatistics()
|
|
|
+ .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);
|
|
|
+ topSearchStatisticsList.clear();
|
|
|
+ topSearchStatisticsList.addAll(newList);
|
|
|
+ if(ListUtil.getItem(titleInfoListList, new ListUtil.CallBack<TitleInfoList, Boolean>() {
|
|
|
+ @Override
|
|
|
+ public Boolean call(TitleInfoList titleInfoList) {
|
|
|
+ return titleInfoList.getSort() == SORT_TOP_SEARCH;
|
|
|
+ }
|
|
|
+ }) == null){
|
|
|
+ addListData("热门推荐", topSearchStatisticsList, SORT_TOP_SEARCH);
|
|
|
+ }
|
|
|
+ notifyDataSetChangedTitleInfoList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ //排序与刷新
|
|
|
+ private void notifyDataSetChangedTitleInfoList() {
|
|
|
+ //排序
|
|
|
+ Collections.sort(titleInfoListList);
|
|
|
+ //刷新
|
|
|
+ recyclerView.getAdapter().notifyDataSetChanged();
|
|
|
+ if (isTitleInfoListEmpty()) {
|
|
|
frame_container.setVisibility(View.VISIBLE);
|
|
|
- search_app_list.setVisibility(View.INVISIBLE);
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
} else {
|
|
|
frame_container.setVisibility(View.INVISIBLE);
|
|
|
- search_app_list.setVisibility(View.VISIBLE);
|
|
|
+ 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(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ list.remove(item);
|
|
|
+ recyclerView.getAdapter().notifyDataSetChanged();
|
|
|
+ DDProviderHelper.getInstance().deleteSearchRecord(SheepApp.getInstance(), (SearchRecord) item);
|
|
|
+ if (isTitleInfoListEmpty()) {
|
|
|
+ frame_container.setVisibility(View.VISIBLE);
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ ViewUtil.setVisibility(item_search_record_x, false);
|
|
|
+ }
|
|
|
+ //点击一条搜索历史
|
|
|
+ view.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ title_search_input.setText(item.getInput());
|
|
|
+ title_search_input.setSelection(item.getInput().length());
|
|
|
+ if(item instanceof IHomePageSearch)
|
|
|
+ ApiUtil.postTopSearchStatisticsClickTopSearch((IHomePageSearch) item);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).setSort(sort));
|
|
|
+ }
|
|
|
+
|
|
|
+ //搜索历史中没有数据且没有获取到服务器的热门搜索列表
|
|
|
+ private boolean isTitleInfoListEmpty() {
|
|
|
+ return searchRecordList.isEmpty() && topSearchStatisticsList.isEmpty() ;
|
|
|
+ }
|
|
|
+
|
|
|
@OnClick({R.id.title_search_commit, R.id.title_img_back})
|
|
|
public void onViewClicked(View view) {
|
|
|
switch (view.getId()) {
|
|
|
@@ -192,30 +242,24 @@ public class ActSearchAppOrTask extends BaseActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- List<SearchAppRecord> searchRecordList = new ArrayList<>();
|
|
|
- List<SearchAppRecord> allSearchRecordList = new ArrayList<>();
|
|
|
- Map<String, SearchAppRecord> searchRecordMap = new HashMap<>();
|
|
|
+ List<ISearchRecord> searchRecordList = new ArrayList<>();
|
|
|
|
|
|
//尝试搜索游戏
|
|
|
private void toSearchApp() {
|
|
|
- search_app_list.setVisibility(View.INVISIBLE);
|
|
|
+ recyclerView.setVisibility(View.INVISIBLE);
|
|
|
frame_container.setVisibility(View.VISIBLE);
|
|
|
String inputText = title_search_input.getText().toString();
|
|
|
if (!TextUtils.isEmpty(inputText)) {
|
|
|
//保存搜索到数据库
|
|
|
- SearchAppRecord searchRecord = searchRecordMap.get(inputText);
|
|
|
- if(searchRecord == null) {
|
|
|
- searchRecord = new SearchAppRecord();
|
|
|
- searchRecord.setInput(inputText);
|
|
|
- searchRecordList.add(searchRecord);
|
|
|
- allSearchRecordList.add(searchRecord);
|
|
|
- searchRecordMap.put(inputText, searchRecord);
|
|
|
- }
|
|
|
- searchRecord.setTime(System.currentTimeMillis());
|
|
|
- searchRecord.addCount();
|
|
|
- MyDbManager.getInstance().saveOrUpdate(searchRecord);
|
|
|
- inputAdapter.notifyDataSetChanged();
|
|
|
+ SearchRecord searchRecord = new SearchRecord();
|
|
|
+ searchRecord.setInput(inputText);
|
|
|
+ searchRecord.setLast_time(System.currentTimeMillis());
|
|
|
+ searchRecord.setCount(1);
|
|
|
+ searchRecordList.add(searchRecord);
|
|
|
+ DDProviderHelper.getInstance().addOrUpdateSearchRecord(SheepApp.getInstance(), searchRecord, null);
|
|
|
+ recyclerView.getAdapter().notifyDataSetChanged();
|
|
|
}
|
|
|
- fgtSearchApp.toSearch(inputText);
|
|
|
+ if (fragment instanceof ISearch)
|
|
|
+ ((ISearch) fragment).toSearch(inputText);
|
|
|
}
|
|
|
}
|