Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / library / LibraryFragment.java
1 package com.moiseum.wolnelektury.view.library;
2
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;
15
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;
31
32 import org.greenrobot.eventbus.EventBus;
33
34 import java.util.List;
35
36 import butterknife.BindView;
37 import butterknife.OnClick;
38
39 /**
40  * @author golonkos
41  */
42
43 public class LibraryFragment extends PresenterFragment<LibraryPresenter> implements LibraryView {
44
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)
58         TextView tvBookTitle;
59         @BindView(R.id.tvBookEpoch)
60         TextView tvBookEpoch;
61         @BindView(R.id.tvBookKind)
62         TextView tvBookKind;
63         @BindView(R.id.tvBookGenre)
64         TextView tvBookGenre;
65         @BindView(R.id.pbHeaderLoading)
66         ProgressBar pbHeaderLoading;
67         @BindView(R.id.tvEmpty)
68         TextView tvEmpty;
69         @BindView(R.id.ibRetry)
70         ImageButton ibRetry;
71         @BindView(R.id.rlHeaderLoadingContainer)
72         RelativeLayout rlHeaderLoadingContainer;
73         @BindView(R.id.rlBecomeAFriend)
74         View rlBecomeAFriend;
75         @BindView(R.id.vBecomeAFriendSeparator)
76         View vBecomeAFriendSeparator;
77         @BindView(R.id.ivAudiobook)
78         ImageView ivHeaderAudiobook;
79
80         public static LibraryFragment newInstance() {
81                 return new LibraryFragment();
82         }
83
84         @Override
85         protected LibraryPresenter createPresenter() {
86                 return new LibraryPresenter(this);
87         }
88
89         @Override
90         public int getLayoutResourceId() {
91                 return R.layout.fragment_library;
92         }
93
94         @Override
95         public void prepareView(View view, Bundle savedInstanceState) {
96                 setHasOptionsMenu(true);
97                 initList(rvNowReading);
98                 initList(rvNewest);
99                 initList(rvRecommended);
100         }
101
102         @Override
103         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
104                 super.onCreateOptionsMenu(menu, inflater);
105                 inflater.inflate(R.menu.menu_searchable, menu);
106         }
107
108         @Override
109         public boolean onOptionsItemSelected(MenuItem item) {
110                 if (item.getItemId() == R.id.action_search) {
111                         EventBus.getDefault().post(new MainActivity.ChangeNavigationEvent(NavigationElement.SEARCH));
112                 }
113                 return super.onOptionsItemSelected(item);
114         }
115
116         @Override
117         public void showBecomeAFriendHeader(boolean premium) {
118                 rlBecomeAFriend.setVisibility(premium ? View.GONE : View.VISIBLE);
119                 vBecomeAFriendSeparator.setVisibility(premium ? View.VISIBLE : View.GONE);
120         }
121
122         @Override
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;
128                         }
129                         Glide.with(getContext()).load(coverUrl).placeholder(R.drawable.list_nocover).diskCacheStrategy(DiskCacheStrategy.ALL).dontTransform().into(ivBookCover);
130                 } else {
131                         ivBookCover.setImageResource(R.drawable.list_nocover);
132                 }
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);
139         }
140
141         @Override
142         public void setProgressContainerVisible(boolean visible) {
143                 if (visible) {
144                         rlHeaderLoadingContainer.setVisibility(View.VISIBLE);
145                 } else {
146                         rlHeaderLoadingContainer.setVisibility(View.GONE);
147                 }
148         }
149
150         @Override
151         public void showHeaderError() {
152                 ibRetry.setVisibility(View.VISIBLE);
153         }
154
155         @Override
156         public void setHeaderProgressVisible(boolean visible) {
157                 pbHeaderLoading.setVisibility(visible ? View.VISIBLE : View.GONE);
158                 if (visible) {
159                         tvEmpty.setVisibility(View.GONE);
160                 }
161         }
162
163         @Override
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);
167         }
168
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);
176         }
177
178         @Override
179         public void setNowReadingVisibility(boolean visible) {
180                 rlReadingNowContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
181                 rvNowReading.setVisibility(visible ? View.VISIBLE : View.GONE);
182         }
183
184         @Override
185         public void setNewest(List<BookModel> books) {
186                 rvNewest.setItems(books);
187         }
188
189         @Override
190         public void setNewestProgressVisible(boolean visible) {
191                 rvNewest.setProgressVisible(visible);
192         }
193
194         @Override
195         public void showNewestError(Exception e) {
196                 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
197                 rvNewest.showRetryButton(() -> getPresenter().reloadNewest());
198         }
199
200         @Override
201         public void setRecommended(List<BookModel> books) {
202                 rvRecommended.setItems(books);
203         }
204
205         @Override
206         public void setRecommendedProgressVisible(boolean visible) {
207                 rvRecommended.setProgressVisible(visible);
208         }
209
210         @Override
211         public void showRecommendedError(Exception e) {
212                 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
213                 rvRecommended.showRetryButton(() -> getPresenter().reloadRecommended());
214         }
215
216         @Override
217         public void setNowReading(List<BookModel> books) {
218                 rvNowReading.setItems(books);
219         }
220
221         @Override
222         public void setNowReadingProgressVisible(boolean visible) {
223                 rvNowReading.setProgressVisible(visible);
224         }
225
226         @Override
227         public void showNowReadingError(Exception e) {
228                 Toast.makeText(getContext(), R.string.loading_results_failed, Toast.LENGTH_SHORT).show();
229                 rvNowReading.showRetryButton(() -> getPresenter().reloadNowReading());
230         }
231
232         @Override
233         public void openBookDetailsView(String slug, BookType bookType) {
234                 startActivity(new BookActivity.BookIntent(slug, bookType, getContext()));
235         }
236
237         @OnClick(R.id.btnBecomeAFriend)
238         public void onBecomeAFriendClick() {
239                 showPayPalForm();
240         }
241
242         @OnClick(R.id.btnNewestSeeAll)
243         public void onNewestSeeAllClicked() {
244                 startActivity(new BookListActivity.BookListIntent(BookListType.NEWEST, getActivity()));
245         }
246
247         @OnClick(R.id.btnRecommendedSeeAll)
248         public void onRecommendedSeeAllClicked() {
249                 startActivity(new BookListActivity.BookListIntent(BookListType.RECOMMENDED, getActivity()));
250         }
251
252         @OnClick(R.id.btnNowReadingSeeAll)
253         public void onNowReadingSeeAllClicked() {
254                 getPresenter().onNowReadingSeeAllClicked();
255         }
256
257         @OnClick(R.id.ibRetry)
258         public void onHeaderRetryClicked() {
259                 getPresenter().fetchHeader();
260                 ibRetry.setVisibility(View.GONE);
261         }
262
263         @OnClick(R.id.libraryHeader)
264         public void onLibraryHeaderClicked() {
265                 getPresenter().onPremiereHeaderClicked();
266         }
267 }