1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

update library on changing download directory

This commit is contained in:
Nikolay Pultsin 2014-04-19 04:11:42 +01:00
parent affff7e149
commit 7d8c3de94d
5 changed files with 43 additions and 45 deletions

View file

@ -91,30 +91,31 @@ public class PreferenceActivity extends ZLPreferenceActivity {
String.valueOf(new DecimalFormatSymbols(Locale.getDefault()).getDecimalSeparator()); String.valueOf(new DecimalFormatSymbols(Locale.getDefault()).getDecimalSeparator());
final Screen directoriesScreen = createPreferenceScreen("directories"); final Screen directoriesScreen = createPreferenceScreen("directories");
directoriesScreen.addPreference(myChooserCollection.createPreference( final Runnable libraryUpdater = new Runnable() {
directoriesScreen.Resource, "bookPath", Paths.BookPathOption, new Runnable() { public void run() {
public void run() { final BookCollectionShadow collection = new BookCollectionShadow();
final BookCollectionShadow collection = new BookCollectionShadow(); collection.bindToService(PreferenceActivity.this, new Runnable() {
collection.bindToService(PreferenceActivity.this, new Runnable() { public void run() {
public void run() { collection.reset(false);
collection.reset(false); collection.unbind();
collection.unbind(); }
} });
});
}
} }
};
directoriesScreen.addPreference(myChooserCollection.createPreference(
directoriesScreen.Resource, "bookPath", Paths.BookPathOption, libraryUpdater
)); ));
directoriesScreen.addPreference(myChooserCollection.createPreference( directoriesScreen.addPreference(myChooserCollection.createPreference(
directoriesScreen.Resource, "downloadDir", Paths.DownloadsDirectoryOption() directoriesScreen.Resource, "downloadDir", Paths.DownloadsDirectoryOption(), libraryUpdater
)); ));
directoriesScreen.addPreference(myChooserCollection.createPreference( directoriesScreen.addPreference(myChooserCollection.createPreference(
directoriesScreen.Resource, "fontPath", Paths.FontPathOption directoriesScreen.Resource, "fontPath", Paths.FontPathOption, null
)); ));
directoriesScreen.addPreference(myChooserCollection.createPreference( directoriesScreen.addPreference(myChooserCollection.createPreference(
directoriesScreen.Resource, "wallpaperPath", Paths.WallpaperPathOption directoriesScreen.Resource, "wallpaperPath", Paths.WallpaperPathOption, null
)); ));
directoriesScreen.addPreference(myChooserCollection.createPreference( directoriesScreen.addPreference(myChooserCollection.createPreference(
directoriesScreen.Resource, "tempDir", Paths.TempDirectoryOption() directoriesScreen.Resource, "tempDir", Paths.TempDirectoryOption(), null
)); ));
final Screen appearanceScreen = createPreferenceScreen("appearance"); final Screen appearanceScreen = createPreferenceScreen("appearance");

View file

@ -39,10 +39,6 @@ public class FileChooserCollection {
myContext = context; myContext = context;
} }
public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringListOption option) {
return createPreference(rootResource, resourceKey, option, null);
}
public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringListOption option, Runnable onValueSetAction) { public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringListOption option, Runnable onValueSetAction) {
final FileChooserPreference preference = new FileChooserStringListPreference( final FileChooserPreference preference = new FileChooserStringListPreference(
myContext, rootResource, resourceKey, option, myPreferences.size(), onValueSetAction myContext, rootResource, resourceKey, option, myPreferences.size(), onValueSetAction
@ -51,9 +47,9 @@ public class FileChooserCollection {
return preference; return preference;
} }
public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringOption option) { public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringOption option, Runnable onValueSetAction) {
final FileChooserPreference preference = new FileChooserStringPreference( final FileChooserPreference preference = new FileChooserStringPreference(
myContext, rootResource, resourceKey, option, myPreferences.size() myContext, rootResource, resourceKey, option, myPreferences.size(), onValueSetAction
); );
myPreferences.add(preference); myPreferences.add(preference);
return preference; return preference;

View file

@ -24,6 +24,7 @@ import android.content.Context;
import android.preference.Preference; import android.preference.Preference;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.util.MiscUtil;
import org.geometerplus.android.util.FileChooserUtil; import org.geometerplus.android.util.FileChooserUtil;
@ -31,14 +32,17 @@ abstract class FileChooserPreference extends Preference {
private final int myRegCode; private final int myRegCode;
private final ZLResource myResource; private final ZLResource myResource;
private final boolean myChooseWritableDirectoriesOnly; private final boolean myChooseWritableDirectoriesOnly;
private final Runnable myOnValueSetAction;
FileChooserPreference(Context context, ZLResource rootResource, String resourceKey, boolean chooseWritableDirectoriesOnly, int regCode) { FileChooserPreference(Context context, ZLResource rootResource, String resourceKey, boolean chooseWritableDirectoriesOnly, int regCode, Runnable onValueSetAction) {
super(context); super(context);
myRegCode = regCode; myRegCode = regCode;
myChooseWritableDirectoriesOnly = chooseWritableDirectoriesOnly;
myResource = rootResource.getResource(resourceKey); myResource = rootResource.getResource(resourceKey);
setTitle(myResource.getValue()); setTitle(myResource.getValue());
myChooseWritableDirectoriesOnly = chooseWritableDirectoriesOnly;
myOnValueSetAction = onValueSetAction;
} }
@Override @Override
@ -53,5 +57,18 @@ abstract class FileChooserPreference extends Preference {
} }
protected abstract String getStringValue(); protected abstract String getStringValue();
protected abstract void setValue(String value);
protected final void setValue(String value) {
if (MiscUtil.isEmptyString(value)) {
return;
}
setValueInternal(value);
if (myOnValueSetAction != null) {
myOnValueSetAction.run();
}
}
protected abstract void setValueInternal(String value);
} }

View file

@ -26,17 +26,14 @@ import android.content.Context;
import org.geometerplus.zlibrary.core.options.ZLStringListOption; import org.geometerplus.zlibrary.core.options.ZLStringListOption;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.util.MiscUtil;
class FileChooserStringListPreference extends FileChooserPreference { class FileChooserStringListPreference extends FileChooserPreference {
private final ZLStringListOption myOption; private final ZLStringListOption myOption;
private final Runnable myOnValueSetAction;
FileChooserStringListPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringListOption option, int regCode, Runnable onValueSetAction) { FileChooserStringListPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringListOption option, int regCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, false, regCode); super(context, rootResource, resourceKey, false, regCode, onValueSetAction);
myOption = option; myOption = option;
myOnValueSetAction = onValueSetAction;
setSummary(getStringValue()); setSummary(getStringValue());
} }
@ -48,19 +45,11 @@ class FileChooserStringListPreference extends FileChooserPreference {
} }
@Override @Override
protected void setValue(String value) { protected void setValueInternal(String value) {
if (MiscUtil.isEmptyString(value)) {
return;
}
final List<String> currentValues = myOption.getValue(); final List<String> currentValues = myOption.getValue();
if (currentValues.size() != 1 || !currentValues.get(0).equals(value)) { if (currentValues.size() != 1 || !currentValues.get(0).equals(value)) {
myOption.setValue(Collections.singletonList(value)); myOption.setValue(Collections.singletonList(value));
setSummary(value); setSummary(value);
} }
if (myOnValueSetAction != null) {
myOnValueSetAction.run();
}
} }
} }

View file

@ -23,13 +23,12 @@ import android.content.Context;
import org.geometerplus.zlibrary.core.options.ZLStringOption; import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.util.MiscUtil;
class FileChooserStringPreference extends FileChooserPreference { class FileChooserStringPreference extends FileChooserPreference {
private final ZLStringOption myOption; private final ZLStringOption myOption;
FileChooserStringPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringOption option, int regCode) { FileChooserStringPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringOption option, int regCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, true, regCode); super(context, rootResource, resourceKey, true, regCode, onValueSetAction);
myOption = option; myOption = option;
setSummary(getStringValue()); setSummary(getStringValue());
@ -41,11 +40,7 @@ class FileChooserStringPreference extends FileChooserPreference {
} }
@Override @Override
protected void setValue(String value) { protected void setValueInternal(String value) {
if (MiscUtil.isEmptyString(value)) {
return;
}
final String currentValue = myOption.getValue(); final String currentValue = myOption.getValue();
if (!currentValue.equals(value)) { if (!currentValue.equals(value)) {
myOption.setValue(value); myOption.setValue(value);