1 package com.moiseum.wolnelektury.view.library;
3 import android.os.Bundle;
4 import android.support.v7.widget.LinearLayoutManager;
5 import android.view.Menu;
6 import android.view.MenuInflater;
7 import android.view.MenuItem;
8 import android.view.View;
9 import android.widget.ImageButton;
10 import android.widget.ImageView;
11 import android.widget.ProgressBar;
12 import android.widget.RelativeLayout;
13 import android.widget.TextView;
14 import android.widget.Toast;
16 import com.bumptech.glide.Glide;
17 import com.bumptech.glide.load.engine.DiskCacheStrategy;
18 import com.moiseum.wolnelektury.R;
19 import com.moiseum.wolnelektury.base.mvp.PresenterFragment;
20 import com.moiseum.wolnelektury.components.ProgressRecyclerView;
21 import com.moiseum.wolnelektury.connection.RestClient;
22 import com.moiseum.wolnelektury.connection.models.BookDetailsModel;
23 import com.moiseum.wolnelektury.connection.models.BookModel;
24 import com.moiseum.wolnelektury.utils.StringUtils;
25 import com.moiseum.wolnelektury.view.book.BookActivity;
26 import com.moiseum.wolnelektury.view.book.BookType;
27 import com.moiseum.wolnelektury.view.book.list.BookListActivity;
28 import com.moiseum.wolnelektury.view.book.list.BookListType;
29 import com.moiseum.wolnelektury.view.main.MainActivity;
30 import com.moiseum.wolnelektury.view.main.NavigationElement;
32 import org.greenrobot.eventbus.EventBus;
34 import java.util.List;
36 import butterknife.BindView;
37 import butterknife.OnClick;
43 public class LibraryFragment extends PresenterFragment<LibraryPresenter> implements LibraryView {
45 @BindView(R.id.rlReadingNowContainer)
46 View rlReadingNowContainer;
47 @BindView(R.id.rvNowReading)
48 ProgressRecyclerView<BookModel> rvNowReading;
49 @BindView(R.id.rvNewest)
50 ProgressRecyclerView<BookModel> rvNewest;
51 @BindView(R.id.rvRecommended)
52 ProgressRecyclerView<BookModel> rvRecommended;
53 @BindView(R.id.ivBookCover)
54 ImageView ivBookCover;
55 @BindView(R.id.tvBookAuthor)
56 TextView tvBookAuthor;
57 @BindView(R.id.tvBookTitle)
59 @BindView(R.id.tvBookEpoch)
61 @BindView(R.id.tvBookKind)
63 @BindView(R.id.tvBookGenre)
65 @BindView(R.id.pbHeaderLoading)
66 ProgressBar pbHeaderLoading;
67 @BindView(R.id.tvEmpty)
69 @BindView(R.id.ibRetry)
71 @BindView(R.id.rlHeaderLoadingContainer)
72 RelativeLayout rlHeaderLoadingContainer;
73 @BindView(R.id.rlBecomeAFriend)
75 @BindView(R.id.vBecomeAFriendSeparator)
76 View vBecomeAFriendSeparator;
77 @BindView(R.id.ivAudiobook)
78 ImageView ivHeaderAudiobook;
80 public static LibraryFragment newInstance() {
81 return new LibraryFragment();
85 protected LibraryPresenter createPresenter() {
86 return new LibraryPresenter(this);
90 public int getLayoutResourceId() {
91 return R.layout.fragment_library;
95 public void prepareView(View view, Bundle savedInstanceState) {
96 setHasOptionsMenu(true);
97 initList(rvNowReading);
99 initList(rvRecommended);
103 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
104 super.onCreateOptionsMenu(menu, inflater);
105 inflater.inflate(R.menu.menu_searchable, menu);
109 public boolean onOptionsItemSelected(MenuItem item) {
110 if (item.getItemId() == R.id.action_search) {
111 EventBus.getDefault().post(new MainActivity.ChangeNavigationEvent(NavigationElement.SEARCH));
113 return super.onOptionsItemSelected(item);
117 public void showBecomeAFriendHeader(boolean premium) {
118 rlBecomeAFriend.setVisibility(premium ? View.GONE : View.VISIBLE);
119 vBecomeAFriendSeparator.setVisibility(premium ? View.VISIBLE : View.GONE);
123 public void initHeader(BookDetailsModel item) {
124 if (item.getCoverThumb() != null) {
125 String coverUrl = item.getCoverThumb();
126 if (!coverUrl.contains(RestClient.MEDIA_URL) && !coverUrl.contains(RestClient.MEDIA_URL_HTTPS)) {
127 coverUrl = RestClient.MEDIA_URL_HTTPS + coverUrl;
129 Glide.with(getContext()).load(coverUrl).placeholder(R.drawable.list_nocover).diskCacheStrategy(DiskCacheStrategy.ALL).dontTransform().into(ivBookCover);
131 ivBookCover.setImageResource(R.drawable.list_nocover);
133 tvBookAuthor.setText(StringUtils.joinCategory(item.getAuthors(), ", "));
134 tvBookTitle.setText(item.getTitle());
135 tvBookEpoch.setText(StringUtils.joinCategory(item.getEpochs(), ", "));
136 tvBookKind.setText(StringUtils.joinCategory(item.getKinds(), ", "));
137 tvBookGenre.setText(StringUtils.joinCategory(item.getGenres(), ", "));
138 ivHeaderAudiobook.setVisibility(item.hasAudio() ? View.VISIBLE : View.GONE);
142 public void setProgressContainerVisible(boolean visible) {
144 rlHeaderLoadingContainer.setVisibility(View.VISIBLE);
146 rlHeaderLoadingContainer.setVisibility(View.GONE);
151 public void showHeaderError() {
152 ibRetry.setVisibility(View.VISIBLE);
156 public void setHeaderProgressVisible(boolean visible) {
157 pbHeaderLoading.setVisibility(visible ? View.VISIBLE : View.GONE);
159 tvEmpty.setVisibility(View.GONE);
164 public void showHeaderEmpty(boolean userLoggedIn) {
165 tvEmpty.setVisibility(View.VISIBLE);
166 tvEmpty.setText(userLoggedIn ? R.string.no_prapremiere_message_logged : R.string.no_prapremiere_message);
169 private void initList(ProgressRecyclerView<BookModel> rvList) {
170 rvList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
171 rvList.setHasFixedSize(true);
172 rvList.setEmptyText(R.string.read_now_library_empty);
173 LibraryAdapter adapter = new LibraryAdapter(getContext());
174 adapter.setOnItemClickListener((item, view, position) -> getPresenter().onBookClicked(item));
175 rvList.setAdapter(adapter);
179 public void setNowReadingVisibility(boolean visible) {
180 rlReadingNowContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
181 rvNowReading.setVisibility(visible ? View.VISIBLE : View.GONE);
185 public void setNewest(List<BookModel> books) {
186 rvNewest.setItems(books);
190 public void setNewestProgressVisible(boolean visible) {
191 rvNewest.setProgressVisible(visible);
195 public void showNewestError(Exception e) {
196 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
197 rvNewest.showRetryButton(() -> getPresenter().reloadNewest());
201 public void setRecommended(List<BookModel> books) {
202 rvRecommended.setItems(books);
206 public void setRecommendedProgressVisible(boolean visible) {
207 rvRecommended.setProgressVisible(visible);
211 public void showRecommendedError(Exception e) {
212 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
213 rvRecommended.showRetryButton(() -> getPresenter().reloadRecommended());
217 public void setNowReading(List<BookModel> books) {
218 rvNowReading.setItems(books);
222 public void setNowReadingProgressVisible(boolean visible) {
223 rvNowReading.setProgressVisible(visible);
227 public void showNowReadingError(Exception e) {
228 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
229 rvNowReading.showRetryButton(() -> getPresenter().reloadNowReading());
233 public void openBookDetailsView(String slug, BookType bookType) {
234 startActivity(new BookActivity.BookIntent(slug, bookType, getContext()));
237 @OnClick(R.id.btnBecomeAFriend)
238 public void onBecomeAFriendClick() {
242 @OnClick(R.id.btnNewestSeeAll)
243 public void onNewestSeeAllClicked() {
244 startActivity(new BookListActivity.BookListIntent(BookListType.NEWEST, getActivity()));
247 @OnClick(R.id.btnRecommendedSeeAll)
248 public void onRecommendedSeeAllClicked() {
249 startActivity(new BookListActivity.BookListIntent(BookListType.RECOMMENDED, getActivity()));
252 @OnClick(R.id.btnNowReadingSeeAll)
253 public void onNowReadingSeeAllClicked() {
254 getPresenter().onNowReadingSeeAllClicked();
257 @OnClick(R.id.ibRetry)
258 public void onHeaderRetryClicked() {
259 getPresenter().fetchHeader();
260 ibRetry.setVisibility(View.GONE);
263 @OnClick(R.id.libraryHeader)
264 public void onLibraryHeaderClicked() {
265 getPresenter().onPremiereHeaderClicked();