| 1 | package be.nikiroo.fanfix.reader.android; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.app.Fragment; |
| 5 | import android.graphics.Bitmap; |
| 6 | import android.os.AsyncTask; |
| 7 | import android.os.Bundle; |
| 8 | import android.view.LayoutInflater; |
| 9 | import android.view.View; |
| 10 | import android.view.ViewGroup; |
| 11 | import android.widget.FrameLayout; |
| 12 | import android.widget.ImageView; |
| 13 | import android.widget.TextView; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | |
| 17 | import be.nikiroo.fanfix.data.MetaData; |
| 18 | import be.nikiroo.fanfix.reader.Reader; |
| 19 | import be.nikiroo.utils.Image; |
| 20 | import be.nikiroo.utils.android.ImageUtilsAndroid; |
| 21 | |
| 22 | public class AndroidReaderBook extends Fragment { |
| 23 | private OnFragmentInteractionListener listener; |
| 24 | |
| 25 | /** |
| 26 | * This interface must be implemented by activities that contain this |
| 27 | * fragment to allow an interaction in this fragment to be communicated to |
| 28 | * the activity and potentially other fragments contained in that activity. |
| 29 | * <p> |
| 30 | * See the Android Training lesson <a href= |
| 31 | * "http://developer.android.com/training/basics/fragments/communicating.html" |
| 32 | * >Communicating with Other Fragments</a> for more information. |
| 33 | */ |
| 34 | public interface OnFragmentInteractionListener { |
| 35 | void onFragmentInteraction(MetaData meta); |
| 36 | } |
| 37 | |
| 38 | public AndroidReaderBook() { |
| 39 | // Required empty public constructor |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 44 | Bundle savedInstanceState) { |
| 45 | return inflater.inflate(R.layout.fragment_android_reader_book, |
| 46 | container, false); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onAttach(Activity context) { |
| 51 | super.onAttach(context); |
| 52 | if (context instanceof OnFragmentInteractionListener) { |
| 53 | listener = (OnFragmentInteractionListener) context; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void onDetach() { |
| 59 | super.onDetach(); |
| 60 | listener = null; |
| 61 | } |
| 62 | |
| 63 | public void fill(final MetaData meta, final Reader reader) { |
| 64 | ViewHolder viewHolder = new ViewHolder(getView()); |
| 65 | |
| 66 | viewHolder.title.setText(meta.getTitle()); |
| 67 | viewHolder.author.setText(meta.getAuthor()); |
| 68 | viewHolder.frame.setClickable(true); |
| 69 | viewHolder.frame.setFocusable(true); |
| 70 | viewHolder.frame.setOnClickListener(new View.OnClickListener() { |
| 71 | @Override |
| 72 | public void onClick(View v) { |
| 73 | OnFragmentInteractionListener llistener = listener; |
| 74 | if (llistener != null) { |
| 75 | llistener.onFragmentInteraction(meta); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | new AsyncTask<MetaData, Void, Image>() { |
| 81 | @Override |
| 82 | protected Image doInBackground(MetaData[] metas) { |
| 83 | if (metas[0].getCover() != null) { |
| 84 | return metas[0].getCover(); |
| 85 | } |
| 86 | |
| 87 | return reader.getLibrary().getCover(metas[0].getLuid()); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void onPostExecute(Image coverImage) { |
| 92 | ViewHolder viewHolder = new ViewHolder(getView()); |
| 93 | |
| 94 | try { |
| 95 | if (coverImage != null) { |
| 96 | Bitmap coverBitmap = ImageUtilsAndroid |
| 97 | .fromImage(coverImage); |
| 98 | coverBitmap = Bitmap.createScaledBitmap(coverBitmap, |
| 99 | 128, 128, true); |
| 100 | viewHolder.cover.setImageBitmap(coverBitmap); |
| 101 | } |
| 102 | } catch (IOException e) { |
| 103 | e.printStackTrace(); |
| 104 | } |
| 105 | } |
| 106 | }.execute(meta); |
| 107 | } |
| 108 | |
| 109 | private class ViewHolder { |
| 110 | public FrameLayout frame; |
| 111 | public TextView title; |
| 112 | public TextView author; |
| 113 | public ImageView cover; |
| 114 | |
| 115 | public ViewHolder(View book) { |
| 116 | frame = book.findViewById(R.id.Book); |
| 117 | title = book.findViewById(R.id.Book_lblTitle); |
| 118 | author = book.findViewById(R.id.Book_lblAuthor); |
| 119 | cover = book.findViewById(R.id.Book_imgCover); |
| 120 | } |
| 121 | } |
| 122 | } |