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

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

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Pair;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.statistics.Statistics;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import static com.mapswithme.maps.search.HotelsFilter.Op.OP_EQ;

public class PriceFilterView extends LinearLayout implements View.OnClickListener
{
  public static final int UNDEFINED = -1;
  static final int LOW = 1;
  static final int MEDIUM = 2;
  static final int HIGH = 3;

  @Retention(RetentionPolicy.SOURCE)
  @IntDef({ UNDEFINED, LOW, MEDIUM, HIGH })
  public @interface PriceDef
  {
  }

  private static class Item
  {
    @NonNull
    final View mFrame;
    @NonNull
    final TextView mTitle;
    boolean mSelected;

    private Item(@NonNull View frame, @NonNull TextView title)
    {
      mFrame = frame;
      mTitle = title;
    }

    void select(boolean select)
    {
      mSelected = select;
      update();
    }

    void update()
    {
      @DrawableRes
      int background = UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.filterPropertyBackground);
      @ColorRes
      int titleColor =
          mSelected ? UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.accentButtonTextColor)
                 : UiUtils.getStyledResourceId(mFrame.getContext(), android.R.attr.textColorPrimary);
      if (!mSelected)
        mFrame.setBackgroundResource(background);
      else
        mFrame.setBackgroundColor(ContextCompat.getColor(mFrame.getContext(),
                                                         UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.colorAccent)));
      mTitle.setTextColor(ContextCompat.getColor(mFrame.getContext(), titleColor));
    }
  }

  @Nullable
  private HotelsFilter mFilter;

  @NonNull
  private final SparseArray<Item> mItems = new SparseArray<>();

  public PriceFilterView(Context context)
  {
    this(context, null, 0);
  }

  public PriceFilterView(Context context, AttributeSet attrs)
  {
    this(context, attrs, 0);
  }

  public PriceFilterView(Context context, AttributeSet attrs, int defStyleAttr)
  {
    super(context, attrs, defStyleAttr);
    init(context);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public PriceFilterView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
  {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
  }

  private void init(Context context)
  {
    setOrientation(HORIZONTAL);
    LayoutInflater.from(context).inflate(R.layout.price_filter, this, true);
  }

  @Override
  protected void onFinishInflate()
  {
    View low = findViewById(R.id.low);
    low.setOnClickListener(this);
    mItems.append(R.id.low, new Item(low, (TextView) findViewById(R.id.low_title)));
    View medium = findViewById(R.id.medium);
    medium.setOnClickListener(this);
    mItems.append(R.id.medium, new Item(medium, (TextView) findViewById(R.id.medium_title)));
    View high = findViewById(R.id.high);
    high.setOnClickListener(this);
    mItems.append(R.id.high, new Item(high, (TextView) findViewById(R.id.high_title)));
  }

  public void update(@Nullable HotelsFilter filter)
  {
    mFilter = filter;

    deselectAll();
    if (mFilter != null)
      updateRecursive(mFilter);
  }

  private void updateRecursive(@NonNull HotelsFilter filter)
  {
    if (filter instanceof HotelsFilter.PriceRateFilter)
    {
      HotelsFilter.PriceRateFilter price = (HotelsFilter.PriceRateFilter) filter;
      selectByValue(price.mValue);
    }
    else if (filter instanceof HotelsFilter.Or)
    {
      HotelsFilter.Or or = (HotelsFilter.Or) filter;
      updateRecursive(or.mLhs);
      updateRecursive(or.mRhs);
    }
    else
    {
      throw new AssertionError("Wrong hotels filter type");
    }
  }

  private void deselectAll()
  {
    for (int i = 0; i < mItems.size(); ++i)
    {
      Item item = mItems.valueAt(i);
      item.select(false);
    }
  }

  private void selectByValue(@PriceDef int value)
  {
    switch (value)
    {
      case LOW:
        select(R.id.low, true);
        break;
      case MEDIUM:
        select(R.id.medium, true);
        break;
      case HIGH:
        select(R.id.high, true);
        break;
    }
  }

  private void select(@IdRes int id, boolean force)
  {
    for (int i = 0; i < mItems.size(); ++i)
    {
      int key = mItems.keyAt(i);
      Item item = mItems.valueAt(i);
      if (key == id)
      {
        item.select(force || !item.mSelected);
        return;
      }
    }
  }

  @Override
  public void onClick(View v)
  {
    switch (v.getId())
    {
      case R.id.low:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.PRICE_CATEGORY,
                                                        String.valueOf(LOW)));
        break;
      case R.id.medium:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.PRICE_CATEGORY,
                                                        String.valueOf(MEDIUM)));
        break;
      case R.id.high:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.PRICE_CATEGORY,
                                                        String.valueOf(HIGH)));
        break;
    }

    select(v.getId(), false);
  }

  public void updateFilter()
  {
    List<HotelsFilter.PriceRateFilter> filters = new ArrayList<>();
    for (int i = 0; i < mItems.size(); ++i)
    {
      int key = mItems.keyAt(i);
      Item item = mItems.valueAt(i);
      if (item.mSelected)
      {
        @PriceDef
        int value = LOW;
        switch (key)
        {
          case R.id.low:
            value = LOW;
            break;
          case R.id.medium:
            value = MEDIUM;
            break;
          case R.id.high:
            value = HIGH;
            break;
        }
        filters.add(new HotelsFilter.PriceRateFilter(OP_EQ, value));
      }
    }

    if (filters.size() > 3)
      throw new AssertionError("Wrong filters count");
    mFilter = null;
    for (HotelsFilter filter : filters)
    {
      if (mFilter == null)
        mFilter = filter;
      else
        mFilter = new HotelsFilter.Or(mFilter, filter);
    }
  }

  @Nullable
  public HotelsFilter getFilter()
  {
    return mFilter;
  }
}