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

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

import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import java.util.List;

import com.mapswithme.maps.R;
import com.mapswithme.maps.editor.data.LocalizedName;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;

public class MultilanguageAdapter extends RecyclerView.Adapter<MultilanguageAdapter.Holder>
{
  private final List<LocalizedName> mNames;
  private int mMandatoryNamesCount;
  private boolean mAdditionalLanguagesShown;

  MultilanguageAdapter(EditorHostFragment hostFragment)
  {
    mNames = hostFragment.getNames();
    mMandatoryNamesCount = hostFragment.getMandatoryNamesCount();
  }

  @Override
  public Holder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_localized_name, parent, false);
    // TODO(mgsergio): Deletion is not implemented.
    UiUtils.hide(view.findViewById(R.id.delete));
    return new Holder(view);
  }

  @Override
  public void onBindViewHolder(Holder holder, int position)
  {
    LocalizedName name = mNames.get(position);
    holder.input.setText(name.name);
    holder.inputLayout.setHint(name.langName);
  }

  @Override
  public int getItemCount()
  {
    return mAdditionalLanguagesShown ? mNames.size() : mMandatoryNamesCount;
  }

  public int getNamesCount()
  {
    return mNames.size();
  }

  @NonNull
  LocalizedName getNameAtPos(int pos) { return mNames.get(pos); }

  public int getMandatoryNamesCount()
  {
    return mMandatoryNamesCount;
  }

  public boolean areAdditionalLanguagesShown()
  {
    return mAdditionalLanguagesShown;
  }

  public void showAdditionalLanguages(boolean show)
  {
    if (mAdditionalLanguagesShown == show)
      return;

    mAdditionalLanguagesShown = show;

    if (mNames.size() != mMandatoryNamesCount)
    {
      if (show)
      {
        notifyItemRangeInserted(mMandatoryNamesCount, mNames.size() - mMandatoryNamesCount);
      }
      else
      {
        notifyItemRangeRemoved(mMandatoryNamesCount, mNames.size() - mMandatoryNamesCount);
      }
    }
  }

  public class Holder extends RecyclerView.ViewHolder
  {
    EditText input;
    TextInputLayout inputLayout;

    public Holder(View itemView)
    {
      super(itemView);
      input = (EditText) itemView.findViewById(R.id.input);
      inputLayout = (TextInputLayout) itemView.findViewById(R.id.input_layout);
      input.addTextChangedListener(new StringUtils.SimpleTextWatcher()
      {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
          UiUtils.setInputError(inputLayout, Editor.nativeIsNameValid(s.toString()) ?
                                                                      Utils.INVALID_ID :
                                                                      R.string.error_enter_correct_name);
          mNames.get(getAdapterPosition()).name = s.toString();
        }
      });

      itemView.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener()
                                                            {
                                                              @Override
                                                              public void onClick(View v)
                                                              {
                                                                // TODO(mgsergio): Implement item deletion.
                                                                // int position = getAdapterPosition();
                                                                // mHostFragment.removeLocalizedName(position + 1);
                                                                // mNames.remove(position);
                                                                // notifyItemRemoved(position);
                                                              }
                                                            });
    }
  }
}