Add a temporary way to select download directory

This commit is contained in:
inorichi
2015-11-03 20:04:07 +01:00
parent 13ff612ce0
commit 62ae572c72
9 changed files with 92 additions and 13 deletions

View File

@ -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<Page> 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) {

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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());
}