浏览代码

Remove cache size setting. Add advanced settings section. Other minor changes.

inorichi 9 年之前
父节点
当前提交
453a187938

+ 4 - 4
app/src/main/java/eu/kanade/mangafeed/data/cache/CacheManager.java → app/src/main/java/eu/kanade/mangafeed/data/cache/ChapterCache.java

@@ -15,25 +15,25 @@ import java.io.OutputStream;
 import java.lang.reflect.Type;
 import java.util.List;
 
-import eu.kanade.mangafeed.data.preference.PreferencesHelper;
 import eu.kanade.mangafeed.data.source.model.Page;
 import eu.kanade.mangafeed.util.DiskUtils;
 import okio.BufferedSink;
 import okio.Okio;
 import rx.Observable;
 
-public class CacheManager {
+public class ChapterCache {
 
     private static final String PARAMETER_CACHE_DIRECTORY = "chapter_disk_cache";
     private static final int PARAMETER_APP_VERSION = 1;
     private static final int PARAMETER_VALUE_COUNT = 1;
+    private static final int PARAMETER_CACHE_SIZE = 75 * 1024 * 1024;
 
     private Context context;
     private Gson gson;
 
     private DiskLruCache diskCache;
 
-    public CacheManager(Context context, PreferencesHelper preferences) {
+    public ChapterCache(Context context) {
         this.context = context;
         gson = new Gson();
 
@@ -42,7 +42,7 @@ public class CacheManager {
                     new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY),
                     PARAMETER_APP_VERSION,
                     PARAMETER_VALUE_COUNT,
-                    preferences.cacheSize() * 1024 * 1024
+                    PARAMETER_CACHE_SIZE
             );
         } catch (IOException e) {
             // Do Nothing.

+ 10 - 0
app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java

@@ -136,6 +136,16 @@ public class DatabaseHelper {
                 .prepare();
     }
 
+    public PreparedDeleteByQuery deleteMangasNotInLibrary() {
+        return db.delete()
+                .byQuery(DeleteQuery.builder()
+                        .table(MangaTable.TABLE)
+                        .where(MangaTable.COLUMN_FAVORITE + "=?")
+                        .whereArgs(0)
+                        .build())
+                .prepare();
+    }
+
 
     // Chapters related queries
 

+ 0 - 4
app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java

@@ -56,10 +56,6 @@ public class PreferencesHelper {
         prefs.edit().clear().apply();
     }
 
-    public int cacheSize() {
-        return prefs.getInt(getKey(R.string.pref_chapter_cache_size_key), 75);
-    }
-
     public Preference<Boolean> lockOrientation() {
         return rxPrefs.getBoolean(getKey(R.string.pref_lock_orientation_key), true);
     }

+ 7 - 7
app/src/main/java/eu/kanade/mangafeed/data/source/base/Source.java

@@ -16,7 +16,7 @@ import java.util.Map;
 import javax.inject.Inject;
 
 import eu.kanade.mangafeed.App;
-import eu.kanade.mangafeed.data.cache.CacheManager;
+import eu.kanade.mangafeed.data.cache.ChapterCache;
 import eu.kanade.mangafeed.data.database.models.Chapter;
 import eu.kanade.mangafeed.data.database.models.Manga;
 import eu.kanade.mangafeed.data.network.NetworkHelper;
@@ -29,7 +29,7 @@ import rx.schedulers.Schedulers;
 public abstract class Source extends BaseSource {
 
     @Inject protected NetworkHelper networkService;
-    @Inject protected CacheManager cacheManager;
+    @Inject protected ChapterCache chapterCache;
     @Inject protected PreferencesHelper prefs;
     protected Headers requestHeaders;
     protected LazyHeaders glideHeaders;
@@ -89,7 +89,7 @@ public abstract class Source extends BaseSource {
     }
 
     public Observable<List<Page>> getCachedPageListOrPullFromNetwork(final String chapterUrl) {
-        return cacheManager.getPageUrlsFromDiskCache(getChapterCacheKey(chapterUrl))
+        return chapterCache.getPageUrlsFromDiskCache(getChapterCacheKey(chapterUrl))
                 .onErrorResumeNext(throwable -> {
                     return pullPageListFromNetwork(chapterUrl);
                 })
@@ -141,13 +141,13 @@ public abstract class Source extends BaseSource {
 
         return pageObservable
                 .flatMap(p -> {
-                    if (!cacheManager.isImageInCache(page.getImageUrl())) {
+                    if (!chapterCache.isImageInCache(page.getImageUrl())) {
                         return cacheImage(page);
                     }
                     return Observable.just(page);
                 })
                 .flatMap(p -> {
-                    page.setImagePath(cacheManager.getImagePath(page.getImageUrl()));
+                    page.setImagePath(chapterCache.getImagePath(page.getImageUrl()));
                     page.setStatus(Page.READY);
                     return Observable.just(page);
                 })
@@ -162,7 +162,7 @@ public abstract class Source extends BaseSource {
         return getImageProgressResponse(page)
                 .flatMap(resp -> {
                     try {
-                        cacheManager.putImageToDiskCache(page.getImageUrl(), resp);
+                        chapterCache.putImageToDiskCache(page.getImageUrl(), resp);
                     } catch (IOException e) {
                         return Observable.error(e);
                     }
@@ -176,7 +176,7 @@ public abstract class Source extends BaseSource {
 
     public void savePageList(String chapterUrl, List<Page> pages) {
         if (pages != null)
-            cacheManager.putPageUrlsToDiskCache(getChapterCacheKey(chapterUrl), pages);
+            chapterCache.putPageUrlsToDiskCache(getChapterCacheKey(chapterUrl), pages);
     }
 
     protected List<Page> convertToPages(List<String> pageUrls) {

+ 3 - 3
app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java

@@ -6,7 +6,7 @@ import javax.inject.Singleton;
 
 import dagger.Module;
 import dagger.Provides;
-import eu.kanade.mangafeed.data.cache.CacheManager;
+import eu.kanade.mangafeed.data.cache.ChapterCache;
 import eu.kanade.mangafeed.data.cache.CoverCache;
 import eu.kanade.mangafeed.data.mangasync.MangaSyncManager;
 import eu.kanade.mangafeed.data.database.DatabaseHelper;
@@ -35,8 +35,8 @@ public class DataModule {
 
     @Provides
     @Singleton
-    CacheManager provideCacheManager(Application app, PreferencesHelper preferences) {
-        return new CacheManager(app, preferences);
+    ChapterCache provideChapterCache(Application app) {
+        return new ChapterCache(app);
     }
 
     @Provides

+ 10 - 6
app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderActivity.java

@@ -87,8 +87,7 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
             readerMenu.show(false);
 
         readerTheme = preferences.getReaderTheme();
-        if (readerTheme == BLACK_THEME)
-            setBlackTheme();
+        applyTheme();
 
         initializeSettings();
 
@@ -265,10 +264,15 @@ public class ReaderActivity extends BaseRxActivity<ReaderPresenter> {
         recreate();
     }
 
-    private void setBlackTheme() {
-        getWindow().getDecorView().getRootView().setBackgroundColor(Color.BLACK);
-        pageNumber.setTextColor(ContextCompat.getColor(this, R.color.light_grey));
-        pageNumber.setBackgroundColor(ContextCompat.getColor(this, R.color.page_number_background_black));
+    private void applyTheme() {
+        View rootView = getWindow().getDecorView().getRootView();
+        if (readerTheme == BLACK_THEME) {
+            rootView.setBackgroundColor(Color.BLACK);
+            pageNumber.setTextColor(ContextCompat.getColor(this, R.color.light_grey));
+            pageNumber.setBackgroundColor(ContextCompat.getColor(this, R.color.page_number_background_black));
+        } else {
+            rootView.setBackgroundColor(Color.WHITE);
+        }
     }
 
     public int getReaderTheme() {

+ 7 - 5
app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsActivity.java

@@ -10,14 +10,16 @@ import butterknife.Bind;
 import butterknife.ButterKnife;
 import eu.kanade.mangafeed.App;
 import eu.kanade.mangafeed.R;
-import eu.kanade.mangafeed.data.cache.CacheManager;
+import eu.kanade.mangafeed.data.cache.ChapterCache;
+import eu.kanade.mangafeed.data.database.DatabaseHelper;
 import eu.kanade.mangafeed.data.preference.PreferencesHelper;
 import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
 
 public class SettingsActivity extends BaseActivity {
 
     @Inject PreferencesHelper preferences;
-    @Inject CacheManager cacheManager;
+    @Inject ChapterCache chapterCache;
+    @Inject DatabaseHelper db;
 
     @Bind(R.id.toolbar) Toolbar toolbar;
 
@@ -64,9 +66,9 @@ public class SettingsActivity extends BaseActivity {
                     SettingsAccountsFragment.newInstance(
                             R.xml.pref_accounts, R.string.pref_category_accounts));
 
-            registerSubpreference(R.string.pref_category_cache_key,
-                    SettingsCacheFragment.newInstance(
-                            R.xml.pref_cache, R.string.pref_category_cache));
+            registerSubpreference(R.string.pref_category_advanced_key,
+                    SettingsAdvancedFragment.newInstance(
+                            R.xml.pref_advanced, R.string.pref_category_advanced));
 
             registerSubpreference(R.string.pref_category_about_key,
                     SettingsAboutFragment.newInstance(

+ 109 - 0
app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsAdvancedFragment.java

@@ -0,0 +1,109 @@
+package eu.kanade.mangafeed.ui.setting;
+
+import android.os.Bundle;
+import android.preference.Preference;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import com.afollestad.materialdialogs.MaterialDialog;
+
+import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import eu.kanade.mangafeed.R;
+import eu.kanade.mangafeed.data.cache.ChapterCache;
+import eu.kanade.mangafeed.data.database.DatabaseHelper;
+import eu.kanade.mangafeed.util.ToastUtil;
+import rx.Observable;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.schedulers.Schedulers;
+import rx.subscriptions.CompositeSubscription;
+
+public class SettingsAdvancedFragment extends SettingsNestedFragment {
+
+    private CompositeSubscription subscriptions;
+
+    public static SettingsNestedFragment newInstance(int resourcePreference, int resourceTitle) {
+        SettingsNestedFragment fragment = new SettingsAdvancedFragment();
+        fragment.setArgs(resourcePreference, resourceTitle);
+        return fragment;
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
+        View view = super.onCreateView(inflater, container, savedState);
+        subscriptions = new CompositeSubscription();
+
+        Preference clearCache = findPreference(getString(R.string.pref_clear_chapter_cache_key));
+        clearCache.setOnPreferenceClickListener(preference -> {
+            clearChapterCache(preference);
+            return true;
+        });
+        clearCache.setSummary(getString(R.string.used_cache, getChapterCache().getReadableSize()));
+
+        Preference clearDatabase = findPreference(getString(R.string.pref_clear_database_key));
+        clearDatabase.setOnPreferenceClickListener(preference -> {
+            clearDatabase();
+            return true;
+        });
+
+        return view;
+    }
+
+    @Override
+    public void onDestroyView() {
+        subscriptions.unsubscribe();
+        super.onDestroyView();
+    }
+
+    private void clearChapterCache(Preference preference) {
+        final ChapterCache chapterCache = getChapterCache();
+        final AtomicInteger deletedFiles = new AtomicInteger();
+
+        File[] files = chapterCache.getCacheDir().listFiles();
+
+        MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
+                .title(R.string.deleting)
+                .progress(false, files.length, true)
+                .cancelable(false)
+                .show();
+
+        subscriptions.add(Observable.defer(() -> Observable.from(files))
+                .concatMap(file -> {
+                    if (chapterCache.remove(file.getName())) {
+                        deletedFiles.incrementAndGet();
+                    }
+                    return Observable.just(file);
+                })
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(file -> dialog.incrementProgress(1),
+                        error -> {
+                            dialog.dismiss();
+                            ToastUtil.showShort(getActivity(), getString(R.string.cache_delete_error));
+                        }, () -> {
+                            dialog.dismiss();
+                            ToastUtil.showShort(getActivity(), getString(R.string.cache_deleted, deletedFiles.get()));
+                            preference.setSummary(getString(R.string.used_cache, chapterCache.getReadableSize()));
+                        }));
+    }
+
+    private void clearDatabase() {
+        final DatabaseHelper db = getSettingsActivity().db;
+
+        new MaterialDialog.Builder(getActivity())
+                .content(R.string.clear_database_confirmation)
+                .positiveText(R.string.button_yes)
+                .negativeText(R.string.button_no)
+                .onPositive((dialog1, which) -> {
+                    db.deleteMangasNotInLibrary().executeAsBlocking();
+                })
+                .show();
+    }
+
+    private ChapterCache getChapterCache() {
+        return getSettingsActivity().chapterCache;
+    }
+
+}

+ 0 - 98
app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsCacheFragment.java

@@ -1,98 +0,0 @@
-package eu.kanade.mangafeed.ui.setting;
-
-import android.os.Bundle;
-import android.preference.Preference;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-
-import com.afollestad.materialdialogs.MaterialDialog;
-
-import java.io.File;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import eu.kanade.mangafeed.R;
-import eu.kanade.mangafeed.data.cache.CacheManager;
-import eu.kanade.mangafeed.ui.setting.preference.IntListPreference;
-import eu.kanade.mangafeed.util.ToastUtil;
-import rx.Observable;
-import rx.Subscription;
-import rx.android.schedulers.AndroidSchedulers;
-import rx.schedulers.Schedulers;
-
-public class SettingsCacheFragment extends SettingsNestedFragment implements Preference.OnPreferenceClickListener {
-
-    private CacheManager cacheManager;
-    private Preference clearCache;
-    private Subscription clearChapterCacheSubscription;
-
-    public static SettingsNestedFragment newInstance(int resourcePreference, int resourceTitle) {
-        SettingsNestedFragment fragment = new SettingsCacheFragment();
-        fragment.setArgs(resourcePreference, resourceTitle);
-        return fragment;
-    }
-
-    @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
-        View view = super.onCreateView(inflater, container, savedState);
-
-        cacheManager = getSettingsActivity().cacheManager;
-
-        IntListPreference cacheSize = (IntListPreference)findPreference(getString(R.string.pref_chapter_cache_size_key));
-        cacheSize.setOnPreferenceChangeListener(
-                (preference, newValue) -> {
-                    cacheManager.setSize(Integer.parseInt(newValue.toString()));
-                    return true;
-                });
-
-        clearCache = findPreference(getString(R.string.pref_clear_chapter_cache_key));
-        clearCache.setOnPreferenceClickListener(this);
-        clearCache.setSummary(getString(R.string.used_cache, cacheManager.getReadableSize()));
-
-        return view;
-    }
-
-    @Override
-    public boolean onPreferenceClick(Preference preference) {
-        if (preference.equals(clearCache)) {
-            clearChapterCache();
-        }
-        return true;
-    }
-
-    private void clearChapterCache() {
-        final AtomicInteger deletedFiles = new AtomicInteger();
-
-        File[] files = cacheManager.getCacheDir().listFiles();
-
-        MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
-                .title(R.string.deleting)
-                .progress(false, files.length, true)
-                .cancelable(false)
-                .dismissListener(d -> {
-                    if (clearChapterCacheSubscription != null && !clearChapterCacheSubscription.isUnsubscribed())
-                        clearChapterCacheSubscription.unsubscribe();
-                })
-                .show();
-
-        clearChapterCacheSubscription = Observable.defer(() -> Observable.from(files))
-                .concatMap(file -> {
-                    if (cacheManager.remove(file.getName())) {
-                        deletedFiles.incrementAndGet();
-                    }
-                    return Observable.just(file);
-                })
-                .subscribeOn(Schedulers.io())
-                .observeOn(AndroidSchedulers.mainThread())
-                .subscribe(file -> dialog.incrementProgress(1),
-                        error -> {
-                            dialog.dismiss();
-                            ToastUtil.showShort(getActivity(), getString(R.string.cache_delete_error));
-                        }, () -> {
-                            dialog.dismiss();
-                            ToastUtil.showShort(getActivity(), getString(R.string.cache_deleted, deletedFiles.get()));
-                            clearCache.setSummary(getString(R.string.used_cache, cacheManager.getReadableSize()));
-                        });
-    }
-
-}

+ 1 - 1
app/src/main/res/layout/reader_brightness.xml

@@ -3,7 +3,7 @@
     android:orientation="vertical"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
-    android:background="#333333"
+    android:background="@color/reader_menu_background"
     android:paddingRight="10dp"
     android:paddingLeft="5dp"
     android:paddingTop="5dp"

+ 1 - 1
app/src/main/res/layout/reader_popup.xml

@@ -34,6 +34,6 @@
         android:layout_height="wrap_content"
         android:id="@+id/keep_screen_on"
         style="@style/grey_text"
-        android:text="Keep screen on"/>
+        android:text="@string/pref_keep_screen_on"/>
 
 </LinearLayout>

+ 0 - 12
app/src/main/res/values/arrays.xml

@@ -28,18 +28,6 @@
         <item>3</item>
     </string-array>
 
-    <string-array name="cache_size">
-        <item>50 MB</item>
-        <item>75 MB</item>
-        <item>100 MB</item>
-    </string-array>
-
-    <string-array name="cache_values">
-        <item>50</item>
-        <item>75</item>
-        <item>100</item>
-    </string-array>
-
     <string-array name="reader_themes">
         <item>@string/white_theme</item>
         <item>@string/black_theme</item>

+ 2 - 2
app/src/main/res/values/keys.xml

@@ -4,7 +4,7 @@
     <string name="pref_category_reader_key">pref_category_reader_key</string>
     <string name="pref_category_accounts_key">pref_category_accounts_key</string>
     <string name="pref_category_downloads_key">pref_category_downloads_key</string>
-    <string name="pref_category_cache_key">pref_category_cache_key</string>
+    <string name="pref_category_advanced_key">pref_category_advanced_key</string>
     <string name="pref_category_about_key">pref_category_about_key</string>
 
     <string name="pref_library_columns_dialog_key">pref_library_columns_dialog_key</string>
@@ -24,8 +24,8 @@
     <string name="pref_download_directory_key">pref_download_directory_key</string>
     <string name="pref_download_slots_key">pref_download_slots_key</string>
 
-    <string name="pref_chapter_cache_size_key">pref_chapter_cache_size_key</string>
     <string name="pref_clear_chapter_cache_key">pref_clear_chapter_cache_key</string>
+    <string name="pref_clear_database_key">pref_clear_database_key</string>
 
     <string name="pref_version">pref_version</string>
     <string name="pref_build_time">pref_build_time</string>

+ 9 - 5
app/src/main/res/values/strings.xml

@@ -38,6 +38,8 @@
     <!-- Buttons -->
     <string name="button_ok">OK</string>
     <string name="button_cancel">Cancel</string>
+    <string name="button_yes">Yes</string>
+    <string name="button_no">No</string>
 
     <!-- Operations -->
     <string name="deleting">Deleting…</string>
@@ -49,7 +51,7 @@
     <string name="pref_category_reader">Reader</string>
     <string name="pref_category_accounts">Accounts</string>
     <string name="pref_category_downloads">Downloads</string>
-    <string name="pref_category_cache">Cache</string>
+    <string name="pref_category_advanced">Advanced</string>
     <string name="pref_category_about">About</string>
 
       <!-- General section -->
@@ -58,14 +60,14 @@
     <string name="landscape">Landscape</string>
     <string name="default_columns">Default</string>
 
-
       <!-- Reader section -->
     <string name="pref_hide_status_bar">Hide status bar</string>
     <string name="pref_lock_orientation">Lock orientation</string>
     <string name="pref_enable_transitions">Enable transitions</string>
     <string name="pref_show_page_number">Show page number</string>
     <string name="pref_custom_brightness">Use custom brightness</string>
-    <string name="pref_reader_theme">Reader theme</string>
+    <string name="pref_keep_screen_on">Keep screen on</string>
+    <string name="pref_reader_theme">Background color</string>
     <string name="white_theme">White</string>
     <string name="black_theme">Black</string>
     <string name="pref_viewer_type">Default viewer</string>
@@ -79,12 +81,14 @@
     <string name="pref_download_directory">Downloads directory</string>
     <string name="pref_download_slots">Simultaneous downloads</string>
 
-      <!-- Cache section -->
-    <string name="pref_chapter_cache_size">Chapters cache size</string>
+      <!-- Advanced section -->
     <string name="pref_clear_chapter_cache">Clear chapter cache</string>
     <string name="used_cache">Used: %1$s</string>
     <string name="cache_deleted">Cache cleared. %1$d files have been deleted</string>
     <string name="cache_delete_error">An error occurred clearing cache</string>
+    <string name="pref_clear_database">Clear database</string>
+    <string name="pref_clear_database_summary">Delete mangas and chapters that are not in your library</string>
+    <string name="clear_database_confirmation">Are you sure? Read chapters and progress of non library mangas will be lost</string>
 
       <!-- About section -->
     <string name="version">Version</string>

+ 13 - 0
app/src/main/res/xml/pref_advanced.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <Preference
+        android:title="@string/pref_clear_chapter_cache"
+        android:key="@string/pref_clear_chapter_cache_key" />
+
+    <Preference
+        android:title="@string/pref_clear_database"
+        android:key="@string/pref_clear_database_key"
+        android:summary="@string/pref_clear_database_summary"/>
+
+</PreferenceScreen>

+ 0 - 16
app/src/main/res/xml/pref_cache.xml

@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <eu.kanade.mangafeed.ui.setting.preference.IntListPreference
-        android:title="@string/pref_chapter_cache_size"
-        android:key="@string/pref_chapter_cache_size_key"
-        android:entries="@array/cache_size"
-        android:entryValues="@array/cache_values"
-        android:summary="%s"
-        android:defaultValue="75"/>
-
-    <Preference
-        android:title="@string/pref_clear_chapter_cache"
-        android:key="@string/pref_clear_chapter_cache_key" />
-
-</PreferenceScreen>

+ 2 - 2
app/src/main/res/xml/pref_main.xml

@@ -22,9 +22,9 @@
         android:title="@string/pref_category_accounts" />
 
     <Preference
-        android:key="@string/pref_category_cache_key"
+        android:key="@string/pref_category_advanced_key"
         android:persistent="false"
-        android:title="@string/pref_category_cache" />
+        android:title="@string/pref_category_advanced" />
 
     <Preference
         android:key="@string/pref_category_about_key"