X-Git-Url: https://git.mdrn.pl/wl-app.git/blobdiff_plain/48b2fe9f7c2dc3d9aeaaa6dbfb27c7da4f3235ff..269195b3729c1bdc22e9053ee4ebca667ea8549d:/Android/folioreader/src/main/java/com/folioreader/ui/folio/adapter/DictionaryAdapter.java diff --git a/Android/folioreader/src/main/java/com/folioreader/ui/folio/adapter/DictionaryAdapter.java b/Android/folioreader/src/main/java/com/folioreader/ui/folio/adapter/DictionaryAdapter.java new file mode 100755 index 0000000..1ad8e11 --- /dev/null +++ b/Android/folioreader/src/main/java/com/folioreader/ui/folio/adapter/DictionaryAdapter.java @@ -0,0 +1,152 @@ +package com.folioreader.ui.folio.adapter; + +import android.content.Context; +import android.graphics.Typeface; +import android.support.v7.widget.RecyclerView; +import android.text.SpannableString; +import android.text.style.StyleSpan; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.folioreader.R; +import com.folioreader.model.dictionary.Audio; +import com.folioreader.model.dictionary.DictionaryResults; +import com.folioreader.model.dictionary.Example; +import com.folioreader.model.dictionary.Pronunciations; +import com.folioreader.model.dictionary.Senses; +import com.folioreader.ui.base.DictionaryCallBack; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author gautam chibde on 4/7/17. + */ + +public class DictionaryAdapter extends RecyclerView.Adapter { + + private List results; + private Context context; + private DictionaryCallBack callBack; + + public DictionaryAdapter(Context context, DictionaryCallBack callBack) { + this.results = new ArrayList<>(); + this.context = context; + this.callBack = callBack; + } + + @Override + public DictionaryHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new DictionaryHolder(LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_dictionary, parent, false)); + } + + @Override + public void onBindViewHolder(DictionaryHolder holder, int position) { + final DictionaryResults res = results.get(position); + if (res.getPartOfSpeech() != null) { + int wordLength = res.getHeadword().length(); + SpannableString spannableString = new SpannableString(res.getHeadword() + " - " + res.getPartOfSpeech()); + spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, wordLength, 0); + spannableString.setSpan(new StyleSpan(Typeface.ITALIC), wordLength + 2, spannableString.length(), 0); + holder.name.setText(spannableString); + } else { + holder.name.setTypeface(Typeface.DEFAULT_BOLD); + holder.name.setText(res.getHeadword()); + } + StringBuilder def = new StringBuilder(); + StringBuilder exp = new StringBuilder(); + + if (res.getSenses() != null) { + for (Senses senses : res.getSenses()) { + if (senses.getDefinition() != null) { + for (String s : senses.getDefinition()) { + def.append("\u2022 ").append(s).append('\n'); + } + } + } + + for (Senses senses : res.getSenses()) { + if (senses.getExamples() != null) { + for (Example s : senses.getExamples()) { + exp.append("\u2022 ").append(s.getText()).append('\n'); + } + } + } + } + if (!def.toString().trim().isEmpty()) { + def.insert(0, "Definition\n"); + holder.definition.setText(def.toString()); + } else { + holder.definition.setVisibility(View.GONE); + } + + if (!exp.toString().trim().isEmpty()) { + exp.insert(0, "Example\n"); + holder.example.setText(exp.toString()); + } else { + holder.example.setVisibility(View.GONE); + } +// if (res.getPronunciations() != null) { +// final String url = getAudioUrl(res.getPronunciations()); +// if (url == null) { +// holder.sound.setVisibility(View.GONE); +// } +// } + +// holder.sound.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View v) { +// Log.i("DictionaryAdapter", "clicked"); +// if (res.getPronunciations() != null) { +// final String url = getAudioUrl(res.getPronunciations()); +// callBack.playMedia(url); +// } +// } +// }); + } + + private String getAudioUrl(List pronunciations) { + if (!pronunciations.isEmpty() + && pronunciations.get(0).getAudio() != null + && !pronunciations.get(0).getAudio().isEmpty()) { + Audio audio = pronunciations.get(0).getAudio().get(0); + if (audio.getUrl() != null) { + return audio.getUrl(); + } + } + return null; + } + + public void setResults(List resultsList) { + if(resultsList != null && !resultsList.isEmpty()) { + results.addAll(resultsList); + notifyDataSetChanged(); + } + } + + public void clear() { + results.clear(); + notifyItemRangeRemoved(0, results.size()); + } + + @Override + public int getItemCount() { + return results.size(); + } + + public static class DictionaryHolder extends RecyclerView.ViewHolder { + private TextView name, definition, example; + //TODO private ImageButton sound; + + public DictionaryHolder(View itemView) { + super(itemView); + name = (TextView) itemView.findViewById(R.id.tv_word); + //sound = (ImageButton) itemView.findViewById(R.id.ib_speak); + definition = (TextView) itemView.findViewById(R.id.tv_definition); + example = (TextView) itemView.findViewById(R.id.tv_examples); + } + } +}