Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / library / LibraryPresenter.java
1 package com.moiseum.wolnelektury.view.library;
2
3 import android.os.Bundle;
4
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;
24
25 import org.greenrobot.eventbus.EventBus;
26 import org.greenrobot.eventbus.Subscribe;
27 import org.greenrobot.eventbus.ThreadMode;
28
29 import java.util.List;
30
31 import retrofit2.Call;
32
33 /**
34  * @author golonkos
35  */
36 class LibraryPresenter extends FragmentPresenter<LibraryView> {
37
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();
45
46         private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
47
48         LibraryPresenter(LibraryView view) {
49                 super(view);
50                 newestBooksDataProvider = new NewestBooksDataProvider();
51                 newestBooksDataProvider.setDataObserver(new NewestDataObserver());
52
53                 recommendedBooksDataProvider = new RecommendedBooksDataProvider();
54                 recommendedBooksDataProvider.setDataObserver(new RecommendedDataObserver());
55
56                 nowReadingBooksDataProvider = new ReadingStateDataProvider(ReadingStateModel.ReadingState.STATE_READING);
57                 nowReadingBooksDataProvider.setDataObserver(new NowReadingDataObserver());
58         }
59
60         @Override
61         public void onCreate(Bundle savedInstanceState) {
62                 super.onCreate(savedInstanceState);
63                 EventBus.getDefault().register(this);
64         }
65
66         @Override
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);
74                 }
75                 fetchHeader();
76         }
77
78         @Override
79         public void onResume() {
80                 super.onResume();
81                 getView().showBecomeAFriendHeader(preferences.isUserPremium());
82         }
83
84         @Override
85         public void onDestroy() {
86                 super.onDestroy();
87                 newestBooksDataProvider.cancel();
88                 recommendedBooksDataProvider.cancel();
89                 nowReadingBooksDataProvider.cancel();
90                 if (currentCall != null) {
91                         currentCall.cancel();
92                 }
93                 EventBus.getDefault().unregister(this);
94         }
95
96         void onNowReadingSeeAllClicked() {
97                 EventBus.getDefault().post(new MainActivity.ChangeNavigationEvent(NavigationElement.NOW_READING));
98         }
99
100         void onBookClicked(BookModel book) {
101                 getView().openBookDetailsView(book.getSlug(), BookType.TYPE_DEFAULT);
102         }
103
104         void reloadNewest() {
105                 newestBooksDataProvider.load(null);
106         }
107
108         void reloadRecommended() {
109                 recommendedBooksDataProvider.load(null);
110         }
111
112         void reloadNowReading() {
113                 nowReadingBooksDataProvider.load(null);
114         }
115
116         @SuppressWarnings("unused")
117         @Subscribe(threadMode = ThreadMode.MAIN)
118         public void onBookAdded(BookStorage.BookAddedEvent event) {
119                 if (preferences.isUserLoggedIn()) {
120                         nowReadingBooksDataProvider.load(null);
121                 }
122         }
123
124         @SuppressWarnings("unused")
125         @Subscribe(threadMode = ThreadMode.MAIN)
126         public void onBookDeleted(BookStorage.BookDeletedEvent event) {
127                 if (preferences.isUserLoggedIn()) {
128                         nowReadingBooksDataProvider.load(null);
129                 }
130         }
131
132         @SuppressWarnings("unused")
133         @Subscribe(threadMode = ThreadMode.MAIN)
134         public void onPremiumChanged(PremiumStatusEvent event) {
135                 getView().showBecomeAFriendHeader(event.isPremium());
136         }
137
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);
144         }
145
146         void onPremiereHeaderClicked() {
147                 if (premiereBook != null) {
148                         getView().openBookDetailsView(premiereBook.getSlug(), BookType.TYPE_PREMIUM);
149                 }
150         }
151
152         void fetchHeader() {
153                 getView().setProgressContainerVisible(true);
154                 getView().setHeaderProgressVisible(true);
155                 currentCall = client.call(new RestClientCallback<List<BookDetailsModel>, BooksService>() {
156                         @Override
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);
163                                 } else {
164                                         getView().showHeaderEmpty(preferences.isUserLoggedIn());
165                                 }
166                         }
167
168                         @Override
169                         public void onFailure(Exception e) {
170                                 getView().showHeaderError();
171                         }
172
173                         @Override
174                         public void onCancel() {
175                                 // nop.
176                         }
177
178                         @Override
179                         public Call<List<BookDetailsModel>> execute(BooksService service) {
180                                 return service.getPreview();
181                         }
182                 }, BooksService.class);
183         }
184
185
186         private class NewestDataObserver implements DataObserver<List<BookModel>> {
187
188                 @Override
189                 public void onLoadStarted() {
190                         getView().setNewestProgressVisible(true);
191                 }
192
193                 @Override
194                 public void onLoadSuccess(List<BookModel> data) {
195                         getView().setNewestProgressVisible(false);
196                         getView().setNewest(data);
197                 }
198
199                 @Override
200                 public void onLoadFailed(Exception e) {
201                         getView().setNewestProgressVisible(false);
202                         getView().showNewestError(e);
203                 }
204         }
205
206         private class RecommendedDataObserver implements DataObserver<List<BookModel>> {
207
208                 @Override
209                 public void onLoadStarted() {
210                         getView().setRecommendedProgressVisible(true);
211                 }
212
213                 @Override
214                 public void onLoadSuccess(List<BookModel> data) {
215                         getView().setRecommendedProgressVisible(false);
216                         getView().setRecommended(data);
217                 }
218
219                 @Override
220                 public void onLoadFailed(Exception e) {
221                         getView().setRecommendedProgressVisible(false);
222                         getView().showRecommendedError(e);
223                 }
224         }
225
226         private class NowReadingDataObserver implements DataObserver<List<BookModel>> {
227
228                 @Override
229                 public void onLoadStarted() {
230                         getView().setNowReadingProgressVisible(true);
231                 }
232
233                 @Override
234                 public void onLoadSuccess(List<BookModel> data) {
235                         getView().setNowReadingProgressVisible(false);
236                         getView().setNowReading(data);
237                 }
238
239                 @Override
240                 public void onLoadFailed(Exception e) {
241                         getView().setNowReadingProgressVisible(false);
242                         getView().showNowReadingError(e);
243                 }
244         }
245 }