1package eu.siacs.conversations.ui.adapter;
2
3import androidx.databinding.DataBindingUtil;
4import androidx.annotation.NonNull;
5import androidx.recyclerview.widget.RecyclerView;
6import android.view.LayoutInflater;
7import android.view.ViewGroup;
8
9import java.util.List;
10
11import eu.siacs.conversations.R;
12import eu.siacs.conversations.databinding.CountryItemBinding;
13import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
14
15public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountryViewHolder> {
16
17 private final List<PhoneNumberUtilWrapper.Country> countries;
18
19 private OnCountryClicked onCountryClicked;
20
21 public CountryAdapter(List<PhoneNumberUtilWrapper.Country> countries) {
22 this.countries = countries;
23 }
24
25 @NonNull
26 @Override
27 public CountryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
28 LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
29 CountryItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.country_item, parent, false);
30 return new CountryViewHolder(binding);
31 }
32
33 @Override
34 public void onBindViewHolder(@NonNull CountryViewHolder holder, int position) {
35 final PhoneNumberUtilWrapper.Country county = countries.get(position);
36 holder.binding.country.setText(county.getName());
37 holder.binding.countryCode.setText(county.getCode());
38 holder.itemView.setOnClickListener(v -> {
39 if (onCountryClicked != null) {
40 onCountryClicked.onCountryClicked(county);
41 }
42 });
43 }
44
45 public void setOnCountryClicked(OnCountryClicked listener) {
46 this.onCountryClicked = listener;
47 }
48
49
50 @Override
51 public int getItemCount() {
52 return countries.size();
53 }
54
55
56 class CountryViewHolder extends RecyclerView.ViewHolder {
57
58 private final CountryItemBinding binding;
59
60 CountryViewHolder(CountryItemBinding binding) {
61 super(binding.getRoot());
62 this.binding = binding;
63 }
64 }
65
66 public interface OnCountryClicked {
67 void onCountryClicked(PhoneNumberUtilWrapper.Country country);
68 }
69
70}