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

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

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

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

import com.mapswithme.maps.R;
import com.mapswithme.util.StringUtils;

public class CuisineAdapter extends RecyclerView.Adapter<CuisineAdapter.ViewHolder>
{
  private static class Item implements Comparable<Item>
  {
    String cuisineTranslated;
    String cuisineKey;

    public Item(String key, String translation)
    {
      this.cuisineKey = key;
      this.cuisineTranslated = translation;
    }

    @Override
    public int compareTo(@NonNull Item another)
    {
      return cuisineTranslated.compareTo(another.cuisineTranslated);
    }
  }

  private List<Item> mItems = new ArrayList<>();
  private Set<String> mSelectedKeys = new HashSet<>();
  private String mFilter;

  public CuisineAdapter()
  {
    final String[] keys = Editor.nativeGetCuisines();
    final String[] selectedKeys = Editor.nativeGetSelectedCuisines();
    final String[] translations = Editor.nativeTranslateCuisines(keys);

    Arrays.sort(selectedKeys);
    for (int i = 0; i < keys.length; i++)
    {
      final String key = keys[i];
      mItems.add(new Item(key, translations[i]));

      if (Arrays.binarySearch(selectedKeys, key) >= 0)
        mSelectedKeys.add(key);
    }
  }

  public void setFilter(@NonNull String filter)
  {
    if (filter.equals(mFilter))
      return;
    mFilter = filter;

    final String[] filteredKeys = StringUtils.nativeFilterContainsNormalized(Editor.nativeGetCuisines(), filter);
    final String[] filteredValues = Editor.nativeTranslateCuisines(filteredKeys);

    mItems.clear();
    for (int i = 0; i < filteredKeys.length; i++)
      mItems.add(new Item(filteredKeys[i], filteredValues[i]));

    notifyDataSetChanged();
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cuisine, parent, false));
  }

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

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

  public String[] getCuisines()
  {
    return mSelectedKeys.toArray(new String[mSelectedKeys.size()]);
  }

  protected class ViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener
  {
    final TextView cuisine;
    final CheckBox selected;

    public ViewHolder(View itemView)
    {
      super(itemView);
      cuisine = (TextView) itemView.findViewById(R.id.cuisine);
      selected = (CheckBox) itemView.findViewById(R.id.selected);
      selected.setOnCheckedChangeListener(this);
      itemView.setOnClickListener(new View.OnClickListener()
      {
        @Override
        public void onClick(View v)
        {
          selected.toggle();
        }
      });
    }

    public void bind(int position)
    {
      final String text = mItems.get(position).cuisineTranslated;
      cuisine.setText(text);
      selected.setOnCheckedChangeListener(null);
      selected.setChecked(mSelectedKeys.contains(mItems.get(position).cuisineKey));
      selected.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
      final String key = mItems.get(getAdapterPosition()).cuisineKey;
      if (isChecked)
        mSelectedKeys.add(key);
      else
        mSelectedKeys.remove(key);
    }
  }
}