Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / book / BookFragment.java
1 package com.moiseum.wolnelektury.view.book;
2
3 import android.content.Intent;
4 import android.graphics.Color;
5 import android.graphics.PorterDuff;
6 import android.os.Bundle;
7 import android.support.constraint.ConstraintLayout;
8 import android.support.design.widget.AppBarLayout;
9 import android.support.design.widget.CollapsingToolbarLayout;
10 import android.support.design.widget.FloatingActionButton;
11 import android.support.v7.widget.Toolbar;
12 import android.view.View;
13 import android.view.ViewTreeObserver;
14 import android.widget.ImageButton;
15 import android.widget.ImageView;
16 import android.widget.ProgressBar;
17 import android.widget.RelativeLayout;
18 import android.widget.TextView;
19 import android.widget.Toast;
20
21 import com.bumptech.glide.Glide;
22 import com.bumptech.glide.load.engine.DiskCacheStrategy;
23 import com.facebook.shimmer.ShimmerFrameLayout;
24 import com.folioreader.Config;
25 import com.folioreader.util.FolioReader;
26 import com.moiseum.wolnelektury.R;
27 import com.moiseum.wolnelektury.base.mvp.PresenterFragment;
28 import com.moiseum.wolnelektury.connection.models.BookDetailsModel;
29 import com.moiseum.wolnelektury.utils.StringUtils;
30 import com.moiseum.wolnelektury.view.book.components.ProgressDownloadButton;
31 import com.moiseum.wolnelektury.view.player.PlayerActivity;
32
33 import org.sufficientlysecure.htmltextview.HtmlTextView;
34
35 import butterknife.BindView;
36 import butterknife.OnClick;
37
38 import static android.app.Activity.RESULT_OK;
39 import static com.folioreader.ui.folio.activity.FolioActivity.PARAM_CHAPTERS_COUNT;
40 import static com.folioreader.ui.folio.activity.FolioActivity.PARAM_CURRENT_CHAPTER;
41 import static com.folioreader.ui.folio.activity.FolioActivity.PARAM_FILE_NAME;
42 import static com.moiseum.wolnelektury.view.book.BookActivity.BOOK_SLUG_KEY;
43 import static com.moiseum.wolnelektury.view.book.BookActivity.BOOK_TYPE_KEY;
44
45 /**
46  * Created by Piotr Ostrowski on 17.11.2017.
47  */
48
49 public class BookFragment extends PresenterFragment<BookPresenter> implements BookView {
50
51         private static final int BOOK_READER_CODE = 32434;
52         private static final String DEFAULT_OVERLAY_COLOR = "#80db4b16";
53
54         public static BookFragment newInstance(String slug, BookType type) {
55                 BookFragment bookFragment = new BookFragment();
56                 Bundle args = new Bundle();
57                 args.putString(BOOK_SLUG_KEY, slug);
58                 args.putString(BOOK_TYPE_KEY, type.name());
59                 bookFragment.setArguments(args);
60                 return bookFragment;
61         }
62
63         @BindView(R.id.clMainView)
64         View clMainView;
65         @BindView(R.id.ctlCollapse)
66         CollapsingToolbarLayout ctlCollapse;
67         @BindView(R.id.ivCoverBackground)
68         ImageView ivCoverBackground;
69         @BindView(R.id.ivCover)
70         ImageView ivCover;
71         @BindView(R.id.vCoverOverlay)
72         View vCoverOverlay;
73         @BindView(R.id.tvBookAuthor)
74         TextView tvBookAuthor;
75         @BindView(R.id.tvBookTitle)
76         TextView tvBookTitle;
77         @BindView(R.id.btnEbook)
78         ProgressDownloadButton btnEbook;
79         @BindView(R.id.btnAudiobook)
80         ProgressDownloadButton btnAudiobook;
81         @BindView(R.id.tvBookKind)
82         TextView tvBookKind;
83         @BindView(R.id.tvBookGenre)
84         TextView tvBookGenre;
85         @BindView(R.id.tvBookEpoch)
86         TextView tvBookEpoch;
87         @BindView(R.id.tvQuotationText)
88         HtmlTextView tvQuotationText;
89         @BindView(R.id.tvQuotationAuthor)
90         TextView tvQuotationAuthor;
91         @BindView(R.id.ibDeleteEbook)
92         ImageButton ibDeleteEbook;
93         @BindView(R.id.ibDeleteAudiobook)
94         ImageButton ibDeleteAudiobook;
95         @BindView(R.id.pbHeaderLoading)
96         ProgressBar pbHeaderLoading;
97         @BindView(R.id.rlHeaderLoadingContainer)
98         RelativeLayout rlHeaderLoadingContainer;
99         @BindView(R.id.fabShare)
100         FloatingActionButton fabShare;
101         @BindView(R.id.fabFavourite)
102         FloatingActionButton fabFavourite;
103         @BindView(R.id.shimmerContentContainer)
104         ShimmerFrameLayout shimmerContentContainer;
105         @BindView(R.id.rlEbookButtonsContainer)
106         RelativeLayout rlEbookButtonsContainer;
107         @BindView(R.id.rlAudioButtonsContainer)
108         RelativeLayout rlAudioButtonsContainer;
109         @BindView(R.id.vSecondDivider)
110         View vSecondDivider;
111         @BindView(R.id.ibRetry)
112         ImageButton ibRetry;
113         @BindView(R.id.clPremium)
114         View clPremium;
115
116         @Override
117         protected BookPresenter createPresenter() {
118                 if (getArguments() == null || getArguments().getString(BOOK_SLUG_KEY) == null || getArguments().getString(BOOK_TYPE_KEY) == null) {
119                         throw new IllegalStateException("Missing BookDetails data!");
120                 }
121                 BookType type = BookType.valueOf(getArguments().getString(BOOK_TYPE_KEY));
122                 return new BookPresenter(getArguments().getString(BOOK_SLUG_KEY), type, this);
123         }
124
125         @Override
126         public int getLayoutResourceId() {
127                 return R.layout.fragment_book;
128         }
129
130         @SuppressWarnings("ConstantConditions")
131         @Override
132         public void prepareView(View view, Bundle savedInstanceState) {
133                 shimmerContentContainer.startShimmerAnimation();
134                 Toolbar toolbar = view.findViewById(R.id.bookToolbar);
135                 setupToolbar(toolbar);
136         }
137
138         @Override
139         public void onActivityResult(int requestCode, int resultCode, Intent data) {
140                 super.onActivityResult(requestCode, resultCode, data);
141                 if (requestCode == BOOK_READER_CODE && resultCode == RESULT_OK) {
142                         String bookName = data.getStringExtra(PARAM_FILE_NAME);
143                         int currentChapter = data.getIntExtra(PARAM_CURRENT_CHAPTER, 0);
144                         int count = data.getIntExtra(PARAM_CHAPTERS_COUNT, 0);
145                         getPresenter().onBackFromReader(bookName, currentChapter, count);
146                 }
147         }
148
149         @OnClick(R.id.btnEbook)
150         public void onEbookClick() {
151                 getPresenter().launchEbookForState(btnEbook.getState());
152         }
153
154         @OnClick(R.id.btnAudiobook)
155         public void onAudiobookClick() {
156                 getPresenter().launchAudiobookForState(btnAudiobook.getState());
157         }
158
159         @OnClick(R.id.ibDeleteEbook)
160         public void onDeleteEbookClick() {
161                 getPresenter().deleteEbook();
162                 Toast.makeText(getContext() ,getString(R.string.book_deleted_message), Toast.LENGTH_SHORT).show();
163         }
164
165         @OnClick(R.id.ibDeleteAudiobook)
166         public void onDeleteAudiobookClick() {
167                 getPresenter().deleteAudiobook();
168                 Toast.makeText(getContext() ,getString(R.string.book_deleted_message), Toast.LENGTH_SHORT).show();
169         }
170
171         @OnClick(R.id.ibRetry)
172         public void onRetryClick() {
173                 ibRetry.setVisibility(View.GONE);
174                 pbHeaderLoading.setVisibility(View.VISIBLE);
175                 getPresenter().reloadBookDetails();
176         }
177
178         @OnClick(R.id.fabShare)
179         public void onShareClick() {
180                 getPresenter().onShareEbookClicked();
181         }
182
183         @OnClick(R.id.fabFavourite)
184         public void onFavouriteClick() {
185                 getPresenter().onFavouriteEbookClicked();
186         }
187
188         @OnClick(R.id.bSupportUs)
189         public void onSupportUsClick() {
190                 showPayPalForm();
191         }
192
193         // ------------------------------------------------------------------------------------------------------------------------------------------
194         // BookView
195         // ------------------------------------------------------------------------------------------------------------------------------------------
196
197         @Override
198         public void initializeBookView(BookDetailsModel book) {
199                 shimmerContentContainer.stopShimmerAnimation();
200                 fabShare.setVisibility(View.VISIBLE);
201                 getPresenter().showFavouriteButton(book);
202                 rlHeaderLoadingContainer.setVisibility(View.GONE);
203                 rlEbookButtonsContainer.setVisibility(View.VISIBLE);
204
205                 if (book.hasAudio()) {
206                         rlAudioButtonsContainer.setVisibility(View.VISIBLE);
207                 }
208                 ctlCollapse.setTitle(book.getTitle());
209                 ctlCollapse.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
210
211                 Glide.with(getContext()).load(book.getCover()).diskCacheStrategy(DiskCacheStrategy.ALL).dontTransform().into(ivCoverBackground);
212                 Glide.with(getContext()).load(book.getCover()).placeholder(R.drawable.list_nocover).diskCacheStrategy(DiskCacheStrategy.ALL).dontTransform().into(ivCover);
213
214                 vCoverOverlay.setAlpha(0.7f);
215                 String colorHash = book.getCoverColor() != null ? book.getCoverColor() : DEFAULT_OVERLAY_COLOR;
216                 vCoverOverlay.setBackgroundColor(Color.parseColor(colorHash));
217
218                 if (book.getAuthors() != null && book.getAuthors().size() > 0) {
219                         tvBookAuthor.setText(StringUtils.joinCategory(book.getAuthors(), ", "));
220                 }
221                 tvBookTitle.setText(book.getTitle());
222
223                 if (book.getKinds() != null && book.getKinds().size() > 0) {
224                         tvBookKind.setText(StringUtils.joinCategory(book.getKinds(), ", "));
225                 }
226                 tvBookKind.setBackgroundColor(Color.TRANSPARENT);
227                 if (book.getGenres() != null && book.getGenres().size() > 0) {
228                         tvBookGenre.setText(StringUtils.joinCategory(book.getGenres(), ", "));
229                 }
230                 tvBookGenre.setBackgroundColor(Color.TRANSPARENT);
231                 if (book.getEpochs() != null && book.getEpochs().size() > 0) {
232                         tvBookEpoch.setText(StringUtils.joinCategory(book.getEpochs(), ", "));
233                 }
234                 tvBookEpoch.setBackgroundColor(Color.TRANSPARENT);
235
236                 if (book.getFragment() != null) {
237                         tvQuotationText.setMinLines(0);
238                         tvQuotationText.setHtml(book.getFragment().getHtml());
239                         tvQuotationText.setBackgroundColor(Color.TRANSPARENT);
240                         tvQuotationAuthor.setText(book.getFragment().getTitle());
241                         tvQuotationAuthor.setBackgroundColor(Color.TRANSPARENT);
242                 } else {
243                         tvQuotationText.setVisibility(View.GONE);
244                         tvQuotationAuthor.setVisibility(View.GONE);
245                         vSecondDivider.setVisibility(View.GONE);
246                 }
247                 enableToolbarCollapse();
248         }
249
250         private void enableToolbarCollapse() {
251                 ViewTreeObserver viewTreeObserver = shimmerContentContainer.getViewTreeObserver();
252                 if (viewTreeObserver.isAlive()) {
253                         viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
254                                 @Override
255                                 public void onGlobalLayout() {
256                                         shimmerContentContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
257                                         if (ctlCollapse.getHeight() + shimmerContentContainer.getHeight() > clMainView.getHeight()) {
258                                                 AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) ctlCollapse.getLayoutParams();
259                                                 params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); // list other flags here by |
260                                                 ctlCollapse.setLayoutParams(params);
261                                         }
262                                 }
263                         });
264                 }
265         }
266
267         @Override
268         public void changeDownloadButtonState(ProgressDownloadButton.ProgressDownloadButtonState state, boolean forAudiobook) {
269                 if (forAudiobook) {
270                         ibDeleteAudiobook.setVisibility(state.isDeletable() ? View.VISIBLE : View.INVISIBLE);
271                         btnAudiobook.setState(state);
272                 } else {
273                         ibDeleteEbook.setVisibility(state.isDeletable() ? View.VISIBLE : View.INVISIBLE);
274                         btnEbook.setState(state);
275                 }
276         }
277
278         @Override
279         public void showCurrentStateProgress(int percentage, boolean forAudiobook) {
280                 if (forAudiobook) {
281                         btnAudiobook.setProgress(percentage);
282                 } else {
283                         btnEbook.setProgress(percentage);
284                 }
285         }
286
287         @Override
288         public void showInitializationError() {
289                 Toast.makeText(getContext(), R.string.book_loading_error, Toast.LENGTH_LONG).show();
290                 pbHeaderLoading.setVisibility(View.GONE);
291                 ibRetry.setVisibility(View.VISIBLE);
292         }
293
294         @Override
295         public void showDownloadFileError() {
296                 Toast.makeText(getContext(), R.string.book_download_error, Toast.LENGTH_LONG).show();
297         }
298
299         @Override
300         public void startShareActivity(String shareUrl) {
301                 showShareActivity(shareUrl);
302         }
303
304         @Override
305         public void openBook(String downloadedBookUrl) {
306                 FolioReader reader = new FolioReader(getContext());
307                 Config config = new Config.ConfigBuilder().build();
308                 Intent bookIntent = reader.createBookIntent(downloadedBookUrl, config);
309                 startActivityForResult(bookIntent, BOOK_READER_CODE);
310         }
311
312         @Override
313         public void launchPlayer(BookDetailsModel book) {
314                 if (getArguments() != null) {
315                         PlayerActivity.PlayerIntent intent = new PlayerActivity.PlayerIntent(book, getArguments().getString(BOOK_SLUG_KEY), getContext());
316                         startActivity(intent);
317                 }
318         }
319
320         @Override
321         public void updateReadingProgress(int currentChapter, int count, boolean forAudiobook) {
322                 if (forAudiobook) {
323                         btnAudiobook.setCurrentReadCount(currentChapter + 1, count);
324                 } else {
325                         btnEbook.setCurrentReadCount(currentChapter, count);
326                 }
327         }
328
329         @Override
330         public void startLikeClicked() {
331                 fabFavourite.setImageResource(R.drawable.ic_fav_active);
332         }
333
334         @Override
335         public void stopLikeClicked() {
336                 fabFavourite.setImageResource(R.drawable.ic_fav);
337         }
338
339         public void showFavouriteButton(BookDetailsModel book) {
340                 fabFavourite.setVisibility(View.VISIBLE);
341                 if(book.getFavouriteState()) {
342                         fabFavourite.setImageResource(R.drawable.ic_fav_active);
343                 }
344         }
345
346         @Override
347         public void showPremiumLock(boolean lock) {
348                 if (!lock) {
349                         clPremium.setVisibility(View.GONE);
350                 } else {
351                         clPremium.setVisibility(View.VISIBLE);
352                         btnEbook.setClickable(false);
353                         btnAudiobook.setClickable(false);
354                         fabFavourite.setClickable(false);
355                         fabShare.setClickable(false);
356                 }
357         }
358 }