Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / settings / SettingsPresenter.java
1 package com.moiseum.wolnelektury.view.settings;
2
3 import android.os.Bundle;
4
5 import com.moiseum.wolnelektury.base.WLApplication;
6 import com.moiseum.wolnelektury.base.mvp.FragmentPresenter;
7 import com.moiseum.wolnelektury.connection.downloads.FileCacheUtils;
8 import com.moiseum.wolnelektury.connection.models.BookModel;
9 import com.moiseum.wolnelektury.events.LoggedInEvent;
10 import com.moiseum.wolnelektury.storage.BookStorage;
11 import com.moiseum.wolnelektury.utils.SharedPreferencesUtils;
12
13 import org.greenrobot.eventbus.EventBus;
14 import org.greenrobot.eventbus.Subscribe;
15 import org.greenrobot.eventbus.ThreadMode;
16
17 import java.util.List;
18
19 import io.reactivex.Completable;
20 import io.reactivex.android.schedulers.AndroidSchedulers;
21 import io.reactivex.schedulers.Schedulers;
22
23 class SettingsPresenter extends FragmentPresenter<SettingsView> {
24
25         private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
26         private BookStorage storage = WLApplication.getInstance().getBookStorage();
27
28         SettingsPresenter(SettingsView view) {
29                 super(view);
30         }
31
32         @Override
33         public void onCreate(Bundle savedInstanceState) {
34                 super.onCreate(savedInstanceState);
35                 EventBus.getDefault().register(this);
36         }
37
38         @Override
39         public void onViewCreated(Bundle savedInstanceState) {
40                 super.onViewCreated(savedInstanceState);
41                 getView().initializeSettings(preferences.getNotifications(), preferences.isUserPremium());
42         }
43
44         @Override
45         public void onDestroy() {
46                 super.onDestroy();
47                 EventBus.getDefault().unregister(this);
48         }
49
50         @SuppressWarnings("unused")
51         @Subscribe(threadMode = ThreadMode.MAIN)
52         public void onLoggedIn(LoggedInEvent event) {
53                 getView().initializeSettings(preferences.getNotifications(), preferences.isUserPremium());
54         }
55
56         public void onNotificationsChanged(boolean checked) {
57                 preferences.setNotifications(checked);
58         }
59
60         public void onDeleteAllClicked() {
61                 getView().showProgressDialog(true);
62                 addDisposable(deleteAllFiles()
63                                 .subscribeOn(Schedulers.io())
64                                 .observeOn(AndroidSchedulers.mainThread())
65                                 .subscribe(() -> {
66                                         getView().showProgressDialog(false);
67                                         getView().showDeletionCompleted();
68                                 }, error -> {
69                                         getView().showProgressDialog(false);
70                                         getView().showDeletionFailed(error);
71                                 })
72                 );
73         }
74
75         private Completable deleteAllFiles() {
76                 return Completable.fromAction(() -> {
77                         List<BookModel> storedBooks = storage.all();
78                         for (BookModel book : storedBooks) {
79                                 if (book.getEbookFileUrl() != null) {
80                                         FileCacheUtils.deleteEbookFile(book.getEbookFileUrl());
81                                 }
82                                 if (book.getAudioFileUrls() != null) {
83                                         FileCacheUtils.deleteAudiobookFiles(book.getAudioFileUrls());
84                                 }
85                         }
86                         storage.removeAll();
87                 });
88         }
89 }