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

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

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
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.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

class HotelsTypeAdapter extends RecyclerView.Adapter<HotelsTypeAdapter.HotelsTypeViewHolder>
{
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = HotelsTypeAdapter.class.getName();

  @NonNull
  private static final String SEARCH_HOTEL_FILTER = MwmApplication
      .get().getString(R.string.search_hotel_filter);
  @NonNull
  private static final HotelsFilter.HotelType[] TYPES = SearchEngine.nativeGetHotelTypes();

  @Nullable
  private final OnTypeSelectedListener mListener;
  @NonNull
  private final List<Item> mItems;

  HotelsTypeAdapter(@Nullable OnTypeSelectedListener listener)
  {
    mListener = listener;
    mItems = new ArrayList<>();
    for (HotelsFilter.HotelType type : TYPES)
      mItems.add(new Item(type));
  }

  @Override
  public HotelsTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    return new HotelsTypeViewHolder(inflater.inflate(R.layout.item_tag, parent, false), mItems,
                                    mListener);
  }

  @Override
  public void onBindViewHolder(HotelsTypeViewHolder holder, int position)
  {
    holder.bind(mItems.get(position));
  }

  @Override
  public int getItemCount()
  {
    return mItems.size();
  }

  void updateItems(@NonNull Set<HotelsFilter.HotelType> selectedTypes)
  {
    for (Item item : mItems)
      item.mSelected = selectedTypes.contains(item.mType);

    notifyDataSetChanged();
  }

  static class HotelsTypeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
  {
    @NonNull
    private final View mFrame;
    @NonNull
    private final TextView mTitle;
    @NonNull
    private final List<Item> mItems;
    @Nullable
    private OnTypeSelectedListener mListener;

    HotelsTypeViewHolder(@NonNull View itemView, @NonNull List<Item> items,
                         @Nullable OnTypeSelectedListener listener)
    {
      super(itemView);

      mFrame = itemView;
      mItems = items;
      mListener = listener;
      mTitle = (TextView) itemView.findViewById(R.id.tv__tag);
      mFrame.setOnClickListener(this);
    }

    void bind(@NonNull Item item)
    {
      mTitle.setText(getStringResourceByTag(item.mType.getTag()));
      mFrame.setSelected(item.mSelected);
      updateTitleColor();
    }

    @Override
    public void onClick(View v)
    {
      int position = getAdapterPosition();
      if(position == RecyclerView.NO_POSITION)
        return;

      mFrame.setSelected(!mFrame.isSelected());
      updateTitleColor();
      Item item = mItems.get(position);
      item.mSelected = mFrame.isSelected();
      if (mListener != null)
        mListener.onTypeSelected(item.mSelected, item.mType);
    }

    @NonNull
    private String getStringResourceByTag(@NonNull String tag)
    {
      try
      {
        Context context = mFrame.getContext();
        Resources resources = context.getResources();
        return resources.getString(resources.getIdentifier(String.format(SEARCH_HOTEL_FILTER, tag),
                                                    "string",
                                                    context.getPackageName()));
      }
      catch (Resources.NotFoundException e)
      {
        LOGGER.e(TAG, "Not found resource for hotel tag " + tag, e);
      }

      return tag;
    }

    private void updateTitleColor()
    {
      boolean select = mFrame.isSelected();
      @ColorRes
      int titleColor =
          select ? UiUtils.getStyledResourceId(mFrame.getContext(), R.attr.accentButtonTextColor)
                 : UiUtils.getStyledResourceId(mFrame.getContext(), android.R.attr.textColorPrimary);
      mTitle.setTextColor(ContextCompat.getColor(mFrame.getContext(), titleColor));
    }
  }

  interface OnTypeSelectedListener
  {
    void onTypeSelected(boolean selected, @NonNull HotelsFilter.HotelType type);
  }

  static class Item
  {
    @NonNull
    private final HotelsFilter.HotelType mType;
    private boolean mSelected;

    Item(@NonNull HotelsFilter.HotelType type)
    {
      mType = type;
    }
  }
}