1 package com.moiseum.wolnelektury.view.settings;
3 import android.os.Bundle;
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;
13 import org.greenrobot.eventbus.EventBus;
14 import org.greenrobot.eventbus.Subscribe;
15 import org.greenrobot.eventbus.ThreadMode;
17 import java.util.List;
19 import io.reactivex.Completable;
20 import io.reactivex.android.schedulers.AndroidSchedulers;
21 import io.reactivex.schedulers.Schedulers;
23 class SettingsPresenter extends FragmentPresenter<SettingsView> {
25 private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
26 private BookStorage storage = WLApplication.getInstance().getBookStorage();
28 SettingsPresenter(SettingsView view) {
33 public void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35 EventBus.getDefault().register(this);
39 public void onViewCreated(Bundle savedInstanceState) {
40 super.onViewCreated(savedInstanceState);
41 getView().initializeSettings(preferences.getNotifications(), preferences.isUserPremium());
45 public void onDestroy() {
47 EventBus.getDefault().unregister(this);
50 @SuppressWarnings("unused")
51 @Subscribe(threadMode = ThreadMode.MAIN)
52 public void onLoggedIn(LoggedInEvent event) {
53 getView().initializeSettings(preferences.getNotifications(), preferences.isUserPremium());
56 public void onNotificationsChanged(boolean checked) {
57 preferences.setNotifications(checked);
60 public void onDeleteAllClicked() {
61 getView().showProgressDialog(true);
62 addDisposable(deleteAllFiles()
63 .subscribeOn(Schedulers.io())
64 .observeOn(AndroidSchedulers.mainThread())
66 getView().showProgressDialog(false);
67 getView().showDeletionCompleted();
69 getView().showProgressDialog(false);
70 getView().showDeletionFailed(error);
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());
82 if (book.getAudioFileUrls() != null) {
83 FileCacheUtils.deleteAudiobookFiles(book.getAudioFileUrls());