Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchHistoryAdapter.java « search « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40843a69d3ed666ffb910510ca94ddd59c02ee85 (plain)
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
package com.mapswithme.maps.search;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.maps.widget.SearchToolbarController;
import com.mapswithme.util.Graphics;

class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder>
{
  private static final int TYPE_ITEM = 0;
  private static final int TYPE_CLEAR = 1;
  private static final int TYPE_MY_POSITION = 2;

  private final SearchToolbarController mSearchToolbarController;
  private final boolean mShowMyPosition;

  public static class ViewHolder extends RecyclerView.ViewHolder
  {
    private final TextView mText;

    public ViewHolder(View itemView)
    {
      super(itemView);
      mText = (TextView) itemView;
      Graphics.tint(mText);
    }
  }

  public SearchHistoryAdapter(SearchToolbarController searchToolbarController)
  {
    SearchRecents.refresh();
    mSearchToolbarController = searchToolbarController;
    mShowMyPosition = (RoutingController.get().isWaitingPoiPick() &&
                       LocationHelper.INSTANCE.getMyPosition() != null);
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type)
  {
    final ViewHolder res;

    switch (type)
    {
      case TYPE_ITEM:
        res = new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_search_recent, viewGroup, false));
        res.mText.setOnClickListener(new View.OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
            mSearchToolbarController.setQuery(res.mText.getText());
          }
        });
        break;

      case TYPE_CLEAR:
        res = new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_search_clear_history, viewGroup, false));
        res.mText.setOnClickListener(new View.OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
            SearchRecents.clear();
            notifyDataSetChanged();
          }
        });
        break;

      case TYPE_MY_POSITION:
        res = new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_search_my_position, viewGroup, false));
        res.mText.setOnClickListener(new View.OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
            RoutingController.get().onPoiSelected(LocationHelper.INSTANCE.getMyPosition());
            mSearchToolbarController.onUpClick();
          }
        });
        break;

      default:
        throw new IllegalArgumentException("Unsupported ViewHolder type given");
    }

    Graphics.tint(res.mText);
    return res;
  }

  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int position)
  {
    if (getItemViewType(position) == TYPE_ITEM)
    {
      if (mShowMyPosition)
        position--;

      viewHolder.mText.setText(SearchRecents.get(position));
    }
  }

  @Override
  public int getItemCount()
  {
    int res = SearchRecents.getSize();
    if (res > 0)
      res++;

    if (mShowMyPosition)
      res++;

    return res;
  }

  @Override
  public int getItemViewType(int position)
  {
    if (mShowMyPosition)
    {
      if (position == 0)
        return TYPE_MY_POSITION;

      position--;
    }

    return (position < SearchRecents.getSize() ? TYPE_ITEM : TYPE_CLEAR);
  }
}