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

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

import android.content.res.Resources;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
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.util.ThemeUtils;
import com.mapswithme.util.statistics.Statistics;

class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolder>
{
  @StringRes
  private final int mCategoryResIds[];
  @DrawableRes
  private final int mIconResIds[];

  private final LayoutInflater mInflater;
  private final Resources mResources;

  interface OnCategorySelectedListener
  {
    void onCategorySelected(@Nullable String category);
  }

  private OnCategorySelectedListener mListener;

  CategoriesAdapter(Fragment fragment)
  {
    final String packageName = fragment.getActivity().getPackageName();
    final boolean isNightTheme = ThemeUtils.isNightTheme();
    final Resources resources = fragment.getActivity().getResources();

    final String[] keys = DisplayedCategories.getKeys();
    final int numKeys = keys.length;

    mCategoryResIds = new int[numKeys];
    mIconResIds = new int[numKeys];
    for (int i = 0; i < numKeys; i++)
    {
      String key = keys[i];

      mCategoryResIds[i] = resources.getIdentifier(key, "string", packageName);
      PromoCategory promo = PromoCategory.findByKey(key);
      if (promo != null)
      {
        Statistics.INSTANCE.trackSponsoredEventForCustomProvider(
            Statistics.EventName.SEARCH_SPONSOR_CATEGORY_SHOWN,
            promo.getStatisticValue());
        mCategoryResIds[i] = promo.getStringId();
      }

      if (mCategoryResIds[i] == 0)
        throw new IllegalStateException("Can't get string resource id for category:" + key);

      String iconId = "ic_category_" + key;
      if (isNightTheme)
        iconId = iconId + "_night";
      mIconResIds[i] = resources.getIdentifier(iconId, "drawable", packageName);
      if (mIconResIds[i] == 0)
        throw new IllegalStateException("Can't get icon resource id:" + iconId);
    }

    if (fragment instanceof OnCategorySelectedListener)
      mListener = (OnCategorySelectedListener) fragment;
    mResources = fragment.getResources();
    mInflater = LayoutInflater.from(fragment.getActivity());
  }

  @Override
  public int getItemViewType(int position)
  {
    if (mCategoryResIds[position] == R.string.luggage_storage)
      return R.layout.item_search_category_luggage;
    return R.layout.item_search_category;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    final View view;
    if (viewType == R.layout.item_search_category_luggage)
    {
      view = mInflater.inflate(R.layout.item_search_category_luggage, parent, false);
      return new ViewHolder(view, (TextView) view.findViewById(R.id.tv__category));
    }

    view = mInflater.inflate(R.layout.item_search_category, parent, false);
    return new ViewHolder(view, (TextView)view);
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, int position)
  {
    holder.setTextAndIcon(mCategoryResIds[position], mIconResIds[position]);
  }

  @Override
  public int getItemCount()
  {
    return mCategoryResIds.length;
  }

  @NonNull
  private String getSuggestionFromCategory(@StringRes int resId)
  {
    PromoCategory promoCategory = PromoCategory.findByStringId(resId);
    if (promoCategory != null)
      return promoCategory.getKey();
    return mResources.getString(resId) + ' ';
  }

  public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
  {
    @NonNull
    private final TextView mTitle;

    ViewHolder(@NonNull View v, @NonNull TextView tv)
    {
      super(v);
      v.setOnClickListener(this);
      mTitle = tv;
    }

    @Override
    public void onClick(View v)
    {
      final int position = getAdapterPosition();
      Statistics.INSTANCE.trackSearchCategoryClicked(mResources.getResourceEntryName(mCategoryResIds[position]));
      if (mListener != null)
        mListener.onCategorySelected(getSuggestionFromCategory(mCategoryResIds[position]));
    }

    void setTextAndIcon(@StringRes int textResId, @DrawableRes int iconResId)
    {
      mTitle.setText(textResId);
      mTitle.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
    }
  }
}