From 62ae572c72a291fcce7158163216e4ef9ec5d8d7 Mon Sep 17 00:00:00 2001 From: inorichi Date: Tue, 3 Nov 2015 20:04:07 +0100 Subject: [PATCH] Add a temporary way to select download directory --- .../data/helpers/DownloadManager.java | 20 ++++--- .../injection/component/AppComponent.java | 2 + .../injection/module/DataModule.java | 5 +- .../fragment/SettingsDownloadsFragment.java | 56 +++++++++++++++++++ .../ui/fragment/SettingsMainFragment.java | 3 + app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 8 ++- app/src/main/res/xml/pref_downloads.xml | 5 ++ app/src/main/res/xml/pref_main.xml | 5 ++ 9 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsDownloadsFragment.java create mode 100644 app/src/main/res/xml/pref_downloads.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java index fe3badc8d..f7a38b786 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DownloadManager.java @@ -24,10 +24,12 @@ public class DownloadManager { private Context context; private SourceManager sourceManager; + private PreferencesHelper preferences; - public DownloadManager(Context context, SourceManager sourceManager) { + public DownloadManager(Context context, SourceManager sourceManager, PreferencesHelper preferences) { this.context = context; this.sourceManager = sourceManager; + this.preferences = preferences; initializeDownloadSubscription(); } @@ -48,7 +50,8 @@ public class DownloadManager { private Observable downloadChapter(Manga manga, Chapter chapter) { final Source source = sourceManager.get(manga.source); - final File chapterDirectory = new File(getDownloadsDirectory(), getChapterDirectory(chapter)); + final File chapterDirectory = new File( + preferences.getDownloadsDirectory(), getChapterDirectory(source, manga, chapter)); return source .pullPageListFromNetwork(chapter.url) @@ -62,13 +65,12 @@ public class DownloadManager { .flatMap(page -> getDownloadedImage(page, source, chapterDirectory)); } - private File getDownloadsDirectory() { - // TODO - return new File(DiskUtils.getStorageDirectories(context)[0]); - } - - private String getChapterDirectory(Chapter chapter) { - return chapter.name.replaceAll("[^a-zA-Z0-9.-]", "_"); + private String getChapterDirectory(Source source, Manga manga, Chapter chapter) { + return source.getName() + + File.separator + + manga.title.replaceAll("[^a-zA-Z0-9.-]", "_") + + File.separator + + chapter.name.replaceAll("[^a-zA-Z0-9.-]", "_"); } private String getImageFilename(Page page) { diff --git a/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java index 4cc52fc5d..c24ba8848 100644 --- a/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java @@ -19,6 +19,7 @@ import eu.kanade.mangafeed.presenter.SourcePresenter; import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.fragment.SettingsAccountsFragment; +import eu.kanade.mangafeed.ui.fragment.SettingsDownloadsFragment; @Singleton @Component( @@ -39,6 +40,7 @@ public interface AppComponent { void inject(ReaderActivity readerActivity); void inject(SettingsAccountsFragment settingsAccountsFragment); + void inject(SettingsDownloadsFragment settingsDownloadsFragment); void inject(Source source); diff --git a/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java b/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java index c98dfcbce..c81f8cbb8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java +++ b/app/src/main/java/eu/kanade/mangafeed/injection/module/DataModule.java @@ -51,8 +51,9 @@ public class DataModule { @Provides @Singleton - DownloadManager provideDownloadManager(Application app, SourceManager sourceManager) { - return new DownloadManager(app, sourceManager); + DownloadManager provideDownloadManager( + Application app, SourceManager sourceManager, PreferencesHelper preferences) { + return new DownloadManager(app, sourceManager, preferences); } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsDownloadsFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsDownloadsFragment.java new file mode 100644 index 000000000..3699c2cda --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsDownloadsFragment.java @@ -0,0 +1,56 @@ +package eu.kanade.mangafeed.ui.fragment; + +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; + +import javax.inject.Inject; + +import eu.kanade.mangafeed.App; +import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.helpers.PreferencesHelper; +import eu.kanade.mangafeed.ui.activity.base.BaseActivity; +import eu.kanade.mangafeed.util.DiskUtils; + +public class SettingsDownloadsFragment extends PreferenceFragment { + + @Inject PreferencesHelper preferences; + + public static SettingsDownloadsFragment newInstance() { + return new SettingsDownloadsFragment(); + } + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + App.get(getActivity()).getComponent().inject(this); + + addPreferencesFromResource(R.xml.pref_downloads); + + PreferenceScreen screen = getPreferenceScreen(); + + ListPreference directoriesPref = new ListPreference(getActivity(), null); + + String[] externalDirs = DiskUtils.getStorageDirectories(getActivity()); + directoriesPref.setKey(getString(R.string.pref_download_directory_key)); + directoriesPref.setTitle(R.string.pref_download_directory); + directoriesPref.setEntryValues(externalDirs); + directoriesPref.setEntries(externalDirs); + directoriesPref.setSummary(preferences.getDownloadsDirectory()); + + directoriesPref.setOnPreferenceChangeListener((preference, newValue) -> { + preference.setSummary(newValue.toString()); + return true; + }); + + screen.addPreference(directoriesPref); + } + + @Override + public void onResume() { + super.onResume(); + ((BaseActivity)getActivity()) + .setToolbarTitle(getString(R.string.pref_category_downloads)); + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsMainFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsMainFragment.java index 381d33687..115f37bf9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsMainFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SettingsMainFragment.java @@ -17,6 +17,9 @@ public class SettingsMainFragment extends PreferenceFragment { SettingsNestedFragment.newInstance( R.xml.pref_reader, R.string.pref_category_reader)); + registerSubpreference(R.string.pref_category_downloads_key, + SettingsDownloadsFragment.newInstance()); + registerSubpreference(R.string.pref_category_accounts_key, SettingsAccountsFragment.newInstance()); } diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 2e7703459..3e7b28ee4 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -2,6 +2,7 @@ pref_category_reader_key pref_category_accounts_key + pref_category_downloads_key pref_fullscreen_key pref_default_viewer_key pref_download_directory_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index af17e2016..265678495 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,6 +52,7 @@ Settings Reader Accounts + Downloads Read in fullscreen @@ -61,6 +62,8 @@ Vertical Webtoon (experimental) + Downloads directory + Login for %1$s Username Password @@ -70,6 +73,8 @@ Success Login error Loading… + + Add to favorites Remove from favorites Downloading… @@ -80,13 +85,12 @@ Select all Mark as read Mark as unread + Download Selected chapters: %1$d Update progress: %1$d/%2$d Update completed No new chapters found Found new chapters for: - Download - diff --git a/app/src/main/res/xml/pref_downloads.xml b/app/src/main/res/xml/pref_downloads.xml new file mode 100644 index 000000000..70056be23 --- /dev/null +++ b/app/src/main/res/xml/pref_downloads.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index ef8d2e490..4b1f273fc 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -6,6 +6,11 @@ android:persistent="false" android:title="@string/pref_category_reader" /> + +