Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / ui / folio / adapter / DictionaryAdapter.java
1 package com.folioreader.ui.folio.adapter;
2
3 import android.content.Context;
4 import android.graphics.Typeface;
5 import android.support.v7.widget.RecyclerView;
6 import android.text.SpannableString;
7 import android.text.style.StyleSpan;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.TextView;
12
13 import com.folioreader.R;
14 import com.folioreader.model.dictionary.Audio;
15 import com.folioreader.model.dictionary.DictionaryResults;
16 import com.folioreader.model.dictionary.Example;
17 import com.folioreader.model.dictionary.Pronunciations;
18 import com.folioreader.model.dictionary.Senses;
19 import com.folioreader.ui.base.DictionaryCallBack;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25  * @author gautam chibde on 4/7/17.
26  */
27
28 public class DictionaryAdapter extends RecyclerView.Adapter<DictionaryAdapter.DictionaryHolder> {
29
30     private List<DictionaryResults> results;
31     private Context context;
32     private DictionaryCallBack callBack;
33
34     public DictionaryAdapter(Context context, DictionaryCallBack callBack) {
35         this.results = new ArrayList<>();
36         this.context = context;
37         this.callBack = callBack;
38     }
39
40     @Override
41     public DictionaryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
42         return new DictionaryHolder(LayoutInflater.from(parent.getContext())
43                 .inflate(R.layout.item_dictionary, parent, false));
44     }
45
46     @Override
47     public void onBindViewHolder(DictionaryHolder holder, int position) {
48         final DictionaryResults res = results.get(position);
49         if (res.getPartOfSpeech() != null) {
50             int wordLength = res.getHeadword().length();
51             SpannableString spannableString = new SpannableString(res.getHeadword() + " - " + res.getPartOfSpeech());
52             spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, wordLength, 0);
53             spannableString.setSpan(new StyleSpan(Typeface.ITALIC), wordLength + 2, spannableString.length(), 0);
54             holder.name.setText(spannableString);
55         } else {
56             holder.name.setTypeface(Typeface.DEFAULT_BOLD);
57             holder.name.setText(res.getHeadword());
58         }
59         StringBuilder def = new StringBuilder();
60         StringBuilder exp = new StringBuilder();
61
62         if (res.getSenses() != null) {
63             for (Senses senses : res.getSenses()) {
64                 if (senses.getDefinition() != null) {
65                     for (String s : senses.getDefinition()) {
66                         def.append("\u2022 ").append(s).append('\n');
67                     }
68                 }
69             }
70
71             for (Senses senses : res.getSenses()) {
72                 if (senses.getExamples() != null) {
73                     for (Example s : senses.getExamples()) {
74                         exp.append("\u2022 ").append(s.getText()).append('\n');
75                     }
76                 }
77             }
78         }
79         if (!def.toString().trim().isEmpty()) {
80             def.insert(0, "Definition\n");
81             holder.definition.setText(def.toString());
82         } else {
83             holder.definition.setVisibility(View.GONE);
84         }
85
86         if (!exp.toString().trim().isEmpty()) {
87             exp.insert(0, "Example\n");
88             holder.example.setText(exp.toString());
89         } else {
90             holder.example.setVisibility(View.GONE);
91         }
92 //        if (res.getPronunciations() != null) {
93 //            final String url = getAudioUrl(res.getPronunciations());
94 //            if (url == null) {
95 //                holder.sound.setVisibility(View.GONE);
96 //            }
97 //        }
98
99 //        holder.sound.setOnClickListener(new View.OnClickListener() {
100 //            @Override
101 //            public void onClick(View v) {
102 //                Log.i("DictionaryAdapter", "clicked");
103 //                if (res.getPronunciations() != null) {
104 //                    final String url = getAudioUrl(res.getPronunciations());
105 //                    callBack.playMedia(url);
106 //                }
107 //            }
108 //        });
109     }
110
111     private String getAudioUrl(List<Pronunciations> pronunciations) {
112         if (!pronunciations.isEmpty()
113                 && pronunciations.get(0).getAudio() != null
114                 && !pronunciations.get(0).getAudio().isEmpty()) {
115             Audio audio = pronunciations.get(0).getAudio().get(0);
116             if (audio.getUrl() != null) {
117                 return audio.getUrl();
118             }
119         }
120         return null;
121     }
122
123     public void setResults(List<DictionaryResults> resultsList) {
124         if(resultsList != null && !resultsList.isEmpty()) {
125             results.addAll(resultsList);
126             notifyDataSetChanged();
127         }
128     }
129
130     public void clear() {
131         results.clear();
132         notifyItemRangeRemoved(0, results.size());
133     }
134
135     @Override
136     public int getItemCount() {
137         return results.size();
138     }
139
140     public static class DictionaryHolder extends RecyclerView.ViewHolder {
141         private TextView name, definition, example;
142         //TODO private ImageButton sound;
143
144         public DictionaryHolder(View itemView) {
145             super(itemView);
146             name = (TextView) itemView.findViewById(R.id.tv_word);
147             //sound = (ImageButton) itemView.findViewById(R.id.ib_speak);
148             definition = (TextView) itemView.findViewById(R.id.tv_definition);
149             example = (TextView) itemView.findViewById(R.id.tv_examples);
150         }
151     }
152 }