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

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

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.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 static com.mapswithme.maps.search.HotelsFilter.Op.OP_GE;

public class RatingFilterView extends LinearLayout implements View.OnClickListener
{
  static final float GOOD = 7.0f;
  static final float VERY_GOOD = 8.0f;
  static final float EXCELLENT = 9.0f;

  private static class Item
  {
    @NonNull
    final View mFrame;
    @NonNull
    final TextView mTitle;
    @Nullable
    final TextView mSubtitle;

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

    public void select(boolean select)
    {
      @DrawableRes
      int background = UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.filterPropertyBackground);
      @ColorRes
      int titleColor =
          select ? UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.accentButtonTextColor)
                 : UiUtils.getStyledResourceId(mFrame.getContext(), android.R.attr.textColorPrimary);
      @ColorRes
      int subtitleColor =
          select ? UiUtils.getStyledResourceId(mFrame.getContext(), android.R.attr.textColorSecondaryInverse)
                 : UiUtils.getStyledResourceId(mFrame.getContext(), android.R.attr.textColorSecondary);
      if (!select)
        mFrame.setBackgroundResource(background);
      else
        mFrame.setBackgroundColor(ContextCompat.getColor(mFrame.getContext(),
            UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.colorAccent)));
      mTitle.setTextColor(ContextCompat.getColor(mFrame.getContext(), titleColor));
      if (mSubtitle != null)
        mSubtitle.setTextColor(ContextCompat.getColor(mFrame.getContext(), subtitleColor));
    }
  }

  @Nullable
  private HotelsFilter.RatingFilter mFilter;

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

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

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

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

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public RatingFilterView(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.rating_filter, this, true);
  }

  @Override
  protected void onFinishInflate()
  {
    View any = findViewById(R.id.any);
    any.setOnClickListener(this);
    mItems.append(R.id.any, new Item(any, findViewById(R.id.any_title), null));
    View good = findViewById(R.id.good);
    good.setOnClickListener(this);
    mItems.append(R.id.good, new Item(good, findViewById(R.id.good_title),
                                      findViewById(R.id.good_subtitle)));
    View veryGood = findViewById(R.id.very_good);
    veryGood.setOnClickListener(this);
    mItems.append(R.id.very_good, new Item(veryGood, findViewById(R.id.very_good_title),
                                           findViewById(R.id.very_good_subtitle)));
    View excellent = findViewById(R.id.excellent);
    excellent.setOnClickListener(this);
    mItems.append(R.id.excellent, new Item(excellent, findViewById(R.id.excellent_title),
                                           findViewById(R.id.excellent_subtitle)));
  }

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

    if (mFilter == null)
      select(R.id.any);
    else if (mFilter.mValue == GOOD)
      select(R.id.good);
    else if (mFilter.mValue == VERY_GOOD)
      select(R.id.very_good);
    else if (mFilter.mValue == EXCELLENT)
      select(R.id.excellent);
  }

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

  @Override
  public void onClick(View v)
  {
    switch (v.getId())
    {
      case R.id.any:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.RATING,
                                                        Statistics.ParamValue.ANY));
        update(null);
        break;
      case R.id.good:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.RATING,
                                                        String.valueOf(GOOD)));
        update(new HotelsFilter.RatingFilter(OP_GE, GOOD));
        break;
      case R.id.very_good:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.RATING,
                                                        String.valueOf(VERY_GOOD)));
        update(new HotelsFilter.RatingFilter(OP_GE, VERY_GOOD));
        break;
      case R.id.excellent:
        Statistics.INSTANCE.trackFilterClick(Statistics.EventParam.HOTEL,
                                             new Pair<>(Statistics.EventParam.RATING,
                                                        String.valueOf(EXCELLENT)));
        update(new HotelsFilter.RatingFilter(OP_GE, EXCELLENT));
        break;
    }
  }

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