1 package com.moiseum.wolnelektury.view.library;
3 import android.os.Bundle;
5 import com.moiseum.wolnelektury.base.DataObserver;
6 import com.moiseum.wolnelektury.base.WLApplication;
7 import com.moiseum.wolnelektury.base.mvp.FragmentPresenter;
8 import com.moiseum.wolnelektury.connection.RestClient;
9 import com.moiseum.wolnelektury.connection.RestClientCallback;
10 import com.moiseum.wolnelektury.connection.models.BookDetailsModel;
11 import com.moiseum.wolnelektury.connection.models.BookModel;
12 import com.moiseum.wolnelektury.connection.models.ReadingStateModel;
13 import com.moiseum.wolnelektury.connection.services.BooksService;
14 import com.moiseum.wolnelektury.events.LoggedInEvent;
15 import com.moiseum.wolnelektury.storage.BookStorage;
16 import com.moiseum.wolnelektury.utils.SharedPreferencesUtils;
17 import com.moiseum.wolnelektury.view.book.BookType;
18 import com.moiseum.wolnelektury.view.book.list.NewestBooksDataProvider;
19 import com.moiseum.wolnelektury.view.book.list.ReadingStateDataProvider;
20 import com.moiseum.wolnelektury.view.book.list.RecommendedBooksDataProvider;
21 import com.moiseum.wolnelektury.view.main.MainActivity;
22 import com.moiseum.wolnelektury.view.main.NavigationElement;
23 import com.moiseum.wolnelektury.view.main.events.PremiumStatusEvent;
25 import org.greenrobot.eventbus.EventBus;
26 import org.greenrobot.eventbus.Subscribe;
27 import org.greenrobot.eventbus.ThreadMode;
29 import java.util.List;
31 import retrofit2.Call;
36 class LibraryPresenter extends FragmentPresenter<LibraryView> {
38 private static final String TAG = LibraryPresenter.class.getSimpleName();
39 private final NewestBooksDataProvider newestBooksDataProvider;
40 private final RecommendedBooksDataProvider recommendedBooksDataProvider;
41 private final ReadingStateDataProvider nowReadingBooksDataProvider;
42 private BookDetailsModel premiereBook;
43 private Call currentCall;
44 private RestClient client = WLApplication.getInstance().getRestClient();
46 private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
48 LibraryPresenter(LibraryView view) {
50 newestBooksDataProvider = new NewestBooksDataProvider();
51 newestBooksDataProvider.setDataObserver(new NewestDataObserver());
53 recommendedBooksDataProvider = new RecommendedBooksDataProvider();
54 recommendedBooksDataProvider.setDataObserver(new RecommendedDataObserver());
56 nowReadingBooksDataProvider = new ReadingStateDataProvider(ReadingStateModel.ReadingState.STATE_READING);
57 nowReadingBooksDataProvider.setDataObserver(new NowReadingDataObserver());
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 EventBus.getDefault().register(this);
67 public void onViewCreated(Bundle savedInstanceState) {
68 super.onViewCreated(savedInstanceState);
69 newestBooksDataProvider.load(null);
70 recommendedBooksDataProvider.load(null);
71 if (preferences.isUserLoggedIn()) {
72 getView().setNowReadingVisibility(true);
73 nowReadingBooksDataProvider.load(null);
79 public void onResume() {
81 getView().showBecomeAFriendHeader(preferences.isUserPremium());
85 public void onDestroy() {
87 newestBooksDataProvider.cancel();
88 recommendedBooksDataProvider.cancel();
89 nowReadingBooksDataProvider.cancel();
90 if (currentCall != null) {
93 EventBus.getDefault().unregister(this);
96 void onNowReadingSeeAllClicked() {
97 EventBus.getDefault().post(new MainActivity.ChangeNavigationEvent(NavigationElement.NOW_READING));
100 void onBookClicked(BookModel book) {
101 getView().openBookDetailsView(book.getSlug(), BookType.TYPE_DEFAULT);
104 void reloadNewest() {
105 newestBooksDataProvider.load(null);
108 void reloadRecommended() {
109 recommendedBooksDataProvider.load(null);
112 void reloadNowReading() {
113 nowReadingBooksDataProvider.load(null);
116 @SuppressWarnings("unused")
117 @Subscribe(threadMode = ThreadMode.MAIN)
118 public void onBookAdded(BookStorage.BookAddedEvent event) {
119 if (preferences.isUserLoggedIn()) {
120 nowReadingBooksDataProvider.load(null);
124 @SuppressWarnings("unused")
125 @Subscribe(threadMode = ThreadMode.MAIN)
126 public void onBookDeleted(BookStorage.BookDeletedEvent event) {
127 if (preferences.isUserLoggedIn()) {
128 nowReadingBooksDataProvider.load(null);
132 @SuppressWarnings("unused")
133 @Subscribe(threadMode = ThreadMode.MAIN)
134 public void onPremiumChanged(PremiumStatusEvent event) {
135 getView().showBecomeAFriendHeader(event.isPremium());
138 @SuppressWarnings("unused")
139 @Subscribe(threadMode = ThreadMode.MAIN)
140 public void onLoggedIn(LoggedInEvent event) {
141 getView().showBecomeAFriendHeader(preferences.isUserPremium());
142 getView().setNowReadingVisibility(true);
143 nowReadingBooksDataProvider.load(null);
146 void onPremiereHeaderClicked() {
147 if (premiereBook != null) {
148 getView().openBookDetailsView(premiereBook.getSlug(), BookType.TYPE_PREMIUM);
153 getView().setProgressContainerVisible(true);
154 getView().setHeaderProgressVisible(true);
155 currentCall = client.call(new RestClientCallback<List<BookDetailsModel>, BooksService>() {
157 public void onSuccess(List<BookDetailsModel> data) {
158 getView().setHeaderProgressVisible(false);
159 if (data.size() > 0) {
160 premiereBook = data.get(0);
161 getView().initHeader(premiereBook);
162 getView().setProgressContainerVisible(false);
164 getView().showHeaderEmpty(preferences.isUserLoggedIn());
169 public void onFailure(Exception e) {
170 getView().showHeaderError();
174 public void onCancel() {
179 public Call<List<BookDetailsModel>> execute(BooksService service) {
180 return service.getPreview();
182 }, BooksService.class);
186 private class NewestDataObserver implements DataObserver<List<BookModel>> {
189 public void onLoadStarted() {
190 getView().setNewestProgressVisible(true);
194 public void onLoadSuccess(List<BookModel> data) {
195 getView().setNewestProgressVisible(false);
196 getView().setNewest(data);
200 public void onLoadFailed(Exception e) {
201 getView().setNewestProgressVisible(false);
202 getView().showNewestError(e);
206 private class RecommendedDataObserver implements DataObserver<List<BookModel>> {
209 public void onLoadStarted() {
210 getView().setRecommendedProgressVisible(true);
214 public void onLoadSuccess(List<BookModel> data) {
215 getView().setRecommendedProgressVisible(false);
216 getView().setRecommended(data);
220 public void onLoadFailed(Exception e) {
221 getView().setRecommendedProgressVisible(false);
222 getView().showRecommendedError(e);
226 private class NowReadingDataObserver implements DataObserver<List<BookModel>> {
229 public void onLoadStarted() {
230 getView().setNowReadingProgressVisible(true);
234 public void onLoadSuccess(List<BookModel> data) {
235 getView().setNowReadingProgressVisible(false);
236 getView().setNowReading(data);
240 public void onLoadFailed(Exception e) {
241 getView().setNowReadingProgressVisible(false);
242 getView().showNowReadingError(e);