1 package com.moiseum.wolnelektury.view.book;
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;
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;
33 import org.sufficientlysecure.htmltextview.HtmlTextView;
35 import butterknife.BindView;
36 import butterknife.OnClick;
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;
46 * Created by Piotr Ostrowski on 17.11.2017.
49 public class BookFragment extends PresenterFragment<BookPresenter> implements BookView {
51 private static final int BOOK_READER_CODE = 32434;
52 private static final String DEFAULT_OVERLAY_COLOR = "#80db4b16";
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);
63 @BindView(R.id.clMainView)
65 @BindView(R.id.ctlCollapse)
66 CollapsingToolbarLayout ctlCollapse;
67 @BindView(R.id.ivCoverBackground)
68 ImageView ivCoverBackground;
69 @BindView(R.id.ivCover)
71 @BindView(R.id.vCoverOverlay)
73 @BindView(R.id.tvBookAuthor)
74 TextView tvBookAuthor;
75 @BindView(R.id.tvBookTitle)
77 @BindView(R.id.btnEbook)
78 ProgressDownloadButton btnEbook;
79 @BindView(R.id.btnAudiobook)
80 ProgressDownloadButton btnAudiobook;
81 @BindView(R.id.tvBookKind)
83 @BindView(R.id.tvBookGenre)
85 @BindView(R.id.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)
111 @BindView(R.id.ibRetry)
113 @BindView(R.id.clPremium)
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!");
121 BookType type = BookType.valueOf(getArguments().getString(BOOK_TYPE_KEY));
122 return new BookPresenter(getArguments().getString(BOOK_SLUG_KEY), type, this);
126 public int getLayoutResourceId() {
127 return R.layout.fragment_book;
130 @SuppressWarnings("ConstantConditions")
132 public void prepareView(View view, Bundle savedInstanceState) {
133 shimmerContentContainer.startShimmerAnimation();
134 Toolbar toolbar = view.findViewById(R.id.bookToolbar);
135 setupToolbar(toolbar);
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);
149 @OnClick(R.id.btnEbook)
150 public void onEbookClick() {
151 getPresenter().launchEbookForState(btnEbook.getState());
154 @OnClick(R.id.btnAudiobook)
155 public void onAudiobookClick() {
156 getPresenter().launchAudiobookForState(btnAudiobook.getState());
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();
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();
171 @OnClick(R.id.ibRetry)
172 public void onRetryClick() {
173 ibRetry.setVisibility(View.GONE);
174 pbHeaderLoading.setVisibility(View.VISIBLE);
175 getPresenter().reloadBookDetails();
178 @OnClick(R.id.fabShare)
179 public void onShareClick() {
180 getPresenter().onShareEbookClicked();
183 @OnClick(R.id.fabFavourite)
184 public void onFavouriteClick() {
185 getPresenter().onFavouriteEbookClicked();
188 @OnClick(R.id.bSupportUs)
189 public void onSupportUsClick() {
193 // ------------------------------------------------------------------------------------------------------------------------------------------
195 // ------------------------------------------------------------------------------------------------------------------------------------------
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);
205 if (book.hasAudio()) {
206 rlAudioButtonsContainer.setVisibility(View.VISIBLE);
208 ctlCollapse.setTitle(book.getTitle());
209 ctlCollapse.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
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);
214 vCoverOverlay.setAlpha(0.7f);
215 String colorHash = book.getCoverColor() != null ? book.getCoverColor() : DEFAULT_OVERLAY_COLOR;
216 vCoverOverlay.setBackgroundColor(Color.parseColor(colorHash));
218 if (book.getAuthors() != null && book.getAuthors().size() > 0) {
219 tvBookAuthor.setText(StringUtils.joinCategory(book.getAuthors(), ", "));
221 tvBookTitle.setText(book.getTitle());
223 if (book.getKinds() != null && book.getKinds().size() > 0) {
224 tvBookKind.setText(StringUtils.joinCategory(book.getKinds(), ", "));
226 tvBookKind.setBackgroundColor(Color.TRANSPARENT);
227 if (book.getGenres() != null && book.getGenres().size() > 0) {
228 tvBookGenre.setText(StringUtils.joinCategory(book.getGenres(), ", "));
230 tvBookGenre.setBackgroundColor(Color.TRANSPARENT);
231 if (book.getEpochs() != null && book.getEpochs().size() > 0) {
232 tvBookEpoch.setText(StringUtils.joinCategory(book.getEpochs(), ", "));
234 tvBookEpoch.setBackgroundColor(Color.TRANSPARENT);
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);
243 tvQuotationText.setVisibility(View.GONE);
244 tvQuotationAuthor.setVisibility(View.GONE);
245 vSecondDivider.setVisibility(View.GONE);
247 enableToolbarCollapse();
250 private void enableToolbarCollapse() {
251 ViewTreeObserver viewTreeObserver = shimmerContentContainer.getViewTreeObserver();
252 if (viewTreeObserver.isAlive()) {
253 viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
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);
268 public void changeDownloadButtonState(ProgressDownloadButton.ProgressDownloadButtonState state, boolean forAudiobook) {
270 ibDeleteAudiobook.setVisibility(state.isDeletable() ? View.VISIBLE : View.INVISIBLE);
271 btnAudiobook.setState(state);
273 ibDeleteEbook.setVisibility(state.isDeletable() ? View.VISIBLE : View.INVISIBLE);
274 btnEbook.setState(state);
279 public void showCurrentStateProgress(int percentage, boolean forAudiobook) {
281 btnAudiobook.setProgress(percentage);
283 btnEbook.setProgress(percentage);
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);
295 public void showDownloadFileError() {
296 Toast.makeText(getContext(), R.string.book_download_error, Toast.LENGTH_LONG).show();
300 public void startShareActivity(String shareUrl) {
301 showShareActivity(shareUrl);
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);
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);
321 public void updateReadingProgress(int currentChapter, int count, boolean forAudiobook) {
323 btnAudiobook.setCurrentReadCount(currentChapter + 1, count);
325 btnEbook.setCurrentReadCount(currentChapter, count);
330 public void startLikeClicked() {
331 fabFavourite.setImageResource(R.drawable.ic_fav_active);
335 public void stopLikeClicked() {
336 fabFavourite.setImageResource(R.drawable.ic_fav);
339 public void showFavouriteButton(BookDetailsModel book) {
340 fabFavourite.setVisibility(View.VISIBLE);
341 if(book.getFavouriteState()) {
342 fabFavourite.setImageResource(R.drawable.ic_fav_active);
347 public void showPremiumLock(boolean lock) {
349 clPremium.setVisibility(View.GONE);
351 clPremium.setVisibility(View.VISIBLE);
352 btnEbook.setClickable(false);
353 btnAudiobook.setClickable(false);
354 fabFavourite.setClickable(false);
355 fabShare.setClickable(false);