fixed crashing when navigating from a notification to the Settings screen; eliminated unnecessary passing of SettingsStore around the screens
This commit is contained in:
parent
c63804c5e2
commit
962792728f
12 changed files with 56 additions and 70 deletions
|
|
@ -32,7 +32,7 @@ import io.github.sspanak.tt9.preferences.screens.languages.LanguagesScreen;
|
|||
import io.github.sspanak.tt9.preferences.screens.setup.SetupScreen;
|
||||
|
||||
public class PreferencesActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||
public SettingsStore settings;
|
||||
private SettingsStore settings;
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -85,6 +85,7 @@ public class PreferencesActivity extends AppCompatActivity implements Preference
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getScreenName
|
||||
* Determines the name of the screen for the given preference, as defined in the preference's "fragment" attribute.
|
||||
|
|
@ -166,6 +167,15 @@ public class PreferencesActivity extends AppCompatActivity implements Preference
|
|||
}
|
||||
|
||||
|
||||
public SettingsStore getSettings() {
|
||||
if (settings == null) {
|
||||
settings = new SettingsStore(this);
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
private void applyTheme() {
|
||||
AppCompatDelegate.setDefaultNightMode(settings.getTheme());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class DebugScreen extends BaseScreenFragment {
|
|||
@Override
|
||||
protected void onCreate() {
|
||||
(new ItemLogLevel(findPreference(ItemLogLevel.NAME))).populate().preview().enableClickHandler();
|
||||
(new ItemInputHandlingMode(findPreference(ItemInputHandlingMode.NAME), activity.settings)).populate().preview().enableClickHandler();
|
||||
(new ItemInputHandlingMode(findPreference(ItemInputHandlingMode.NAME), activity.getSettings())).populate().preview().enableClickHandler();
|
||||
initSystemLogsSwitch();
|
||||
enableLogsCopy();
|
||||
|
||||
|
|
|
|||
|
|
@ -31,10 +31,10 @@ public class HotkeysScreen extends BaseScreenFragment {
|
|||
findPreference(SectionKeymap.ITEM_NEXT_LANGUAGE),
|
||||
findPreference(SectionKeymap.ITEM_SHOW_SETTINGS),
|
||||
};
|
||||
SectionKeymap section = new SectionKeymap(Arrays.asList(dropDowns), activity, activity.settings);
|
||||
SectionKeymap section = new SectionKeymap(Arrays.asList(dropDowns), activity);
|
||||
section.populate().activate();
|
||||
|
||||
(new ItemResetKeys(findPreference(ItemResetKeys.NAME), activity, activity.settings, section))
|
||||
(new ItemResetKeys(findPreference(ItemResetKeys.NAME), activity, section))
|
||||
.enableClickHandler();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import android.content.Context;
|
|||
import androidx.preference.Preference;
|
||||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
|
||||
import io.github.sspanak.tt9.preferences.items.ItemClickable;
|
||||
import io.github.sspanak.tt9.ui.UI;
|
||||
|
|
@ -14,23 +14,21 @@ import io.github.sspanak.tt9.ui.UI;
|
|||
class ItemResetKeys extends ItemClickable {
|
||||
public static final String NAME = "reset_keys";
|
||||
|
||||
private final Context context;
|
||||
private final PreferencesActivity activity;
|
||||
private final SectionKeymap dropdowns;
|
||||
private final SettingsStore settings;
|
||||
|
||||
|
||||
ItemResetKeys(Preference item, Context context, SettingsStore settings, SectionKeymap dropdowns) {
|
||||
ItemResetKeys(Preference item, PreferencesActivity activity, SectionKeymap dropdowns) {
|
||||
super(item);
|
||||
this.context = context;
|
||||
this.activity = activity;
|
||||
this.dropdowns = dropdowns;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onClick(Preference p) {
|
||||
Hotkeys.setDefault(settings);
|
||||
Hotkeys.setDefault(activity.getSettings());
|
||||
dropdowns.reloadSettings();
|
||||
UI.toast(context, R.string.function_reset_keys_done);
|
||||
UI.toast(activity, R.string.function_reset_keys_done);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import java.util.Collection;
|
|||
import java.util.Objects;
|
||||
|
||||
import io.github.sspanak.tt9.Logger;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
|
||||
|
||||
|
|
@ -29,10 +30,10 @@ public class SectionKeymap {
|
|||
private final SettingsStore settings;
|
||||
|
||||
|
||||
public SectionKeymap(Collection<DropDownPreference> dropDowns, Context context, SettingsStore settings) {
|
||||
public SectionKeymap(Collection<DropDownPreference> dropDowns, PreferencesActivity activity) {
|
||||
items = dropDowns;
|
||||
hotkeys = new Hotkeys(context);
|
||||
this.settings = settings;
|
||||
hotkeys = new Hotkeys(activity);
|
||||
this.settings = activity.getSettings();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
package io.github.sspanak.tt9.preferences.screens.languages;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.db.exporter.AbstractExporter;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.items.ItemClickable;
|
||||
import io.github.sspanak.tt9.ui.DictionaryNotification;
|
||||
|
||||
abstract class ItemExportAbstract extends ItemClickable {
|
||||
final protected Activity activity;
|
||||
final protected PreferencesActivity activity;
|
||||
final private Runnable onStart;
|
||||
final private Runnable onFinish;
|
||||
|
||||
ItemExportAbstract(Preference item, Activity activity, Runnable onStart, Runnable onFinish) {
|
||||
ItemExportAbstract(Preference item, PreferencesActivity activity, Runnable onStart, Runnable onFinish) {
|
||||
super(item);
|
||||
this.activity = activity;
|
||||
this.onStart = onStart;
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
package io.github.sspanak.tt9.preferences.screens.languages;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.db.exporter.CustomWordsExporter;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
|
||||
class ItemExportCustomWords extends ItemExportAbstract {
|
||||
final public static String NAME = "dictionary_export_custom";
|
||||
|
||||
|
||||
ItemExportCustomWords(Preference item, Activity activity, Runnable onStart, Runnable onFinish) {
|
||||
ItemExportCustomWords(Preference item, PreferencesActivity activity, Runnable onStart, Runnable onFinish) {
|
||||
super(item, activity, onStart, onFinish);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
package io.github.sspanak.tt9.preferences.screens.languages;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import io.github.sspanak.tt9.Logger;
|
||||
|
|
@ -9,17 +7,13 @@ import io.github.sspanak.tt9.R;
|
|||
import io.github.sspanak.tt9.db.exporter.DictionaryExporter;
|
||||
import io.github.sspanak.tt9.languages.Language;
|
||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
|
||||
class ItemExportDictionary extends ItemExportAbstract {
|
||||
final public static String NAME = "dictionary_export";
|
||||
|
||||
protected final SettingsStore settings;
|
||||
|
||||
|
||||
ItemExportDictionary(Preference item, Activity activity, SettingsStore settings, Runnable onStart, Runnable onFinish) {
|
||||
ItemExportDictionary(Preference item, PreferencesActivity activity, Runnable onStart, Runnable onFinish) {
|
||||
super(item, activity, onStart, onFinish);
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -40,7 +34,7 @@ class ItemExportDictionary extends ItemExportAbstract {
|
|||
|
||||
protected boolean onStartExporting() {
|
||||
return DictionaryExporter.getInstance()
|
||||
.setLanguages(LanguageCollection.getAll(activity, settings.getEnabledLanguageIds()))
|
||||
.setLanguages(LanguageCollection.getAll(activity, activity.getSettings().getEnabledLanguageIds()))
|
||||
.export(activity);
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +42,7 @@ class ItemExportDictionary extends ItemExportAbstract {
|
|||
protected String getLoadingMessage() {
|
||||
String message = activity.getString(R.string.dictionary_export_generating_csv);
|
||||
|
||||
Language language = LanguageCollection.getLanguage(activity, settings.getInputLanguage());
|
||||
Language language = LanguageCollection.getLanguage(activity, activity.getSettings().getInputLanguage());
|
||||
if (language != null) {
|
||||
message = activity.getString(R.string.dictionary_export_generating_csv_for_language, language.getName());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package io.github.sspanak.tt9.preferences.screens.languages;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
|
@ -11,7 +10,7 @@ import io.github.sspanak.tt9.R;
|
|||
import io.github.sspanak.tt9.db.DictionaryLoader;
|
||||
import io.github.sspanak.tt9.languages.Language;
|
||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.items.ItemClickable;
|
||||
import io.github.sspanak.tt9.ui.DictionaryLoadingBar;
|
||||
import io.github.sspanak.tt9.ui.UI;
|
||||
|
|
@ -20,8 +19,7 @@ import io.github.sspanak.tt9.ui.UI;
|
|||
class ItemLoadDictionary extends ItemClickable {
|
||||
public final static String NAME = "dictionary_load";
|
||||
|
||||
private final Context context;
|
||||
private final SettingsStore settings;
|
||||
private final PreferencesActivity activity;
|
||||
private final Runnable onStart;
|
||||
private final Runnable onFinish;
|
||||
|
||||
|
|
@ -29,13 +27,12 @@ class ItemLoadDictionary extends ItemClickable {
|
|||
private final DictionaryLoadingBar progressBar;
|
||||
|
||||
|
||||
ItemLoadDictionary(Preference item, Context context, SettingsStore settings, Runnable onStart, Runnable onFinish) {
|
||||
ItemLoadDictionary(Preference item, PreferencesActivity context, Runnable onStart, Runnable onFinish) {
|
||||
super(item);
|
||||
|
||||
this.context = context;
|
||||
this.activity = context;
|
||||
this.loader = DictionaryLoader.getInstance(context);
|
||||
this.progressBar = DictionaryLoadingBar.getInstance(context);
|
||||
this.settings = settings;
|
||||
this.onStart = onStart;
|
||||
this.onFinish = onFinish;
|
||||
|
||||
|
|
@ -53,24 +50,24 @@ class ItemLoadDictionary extends ItemClickable {
|
|||
|
||||
|
||||
private void onLoadingStatusChange(Bundle status) {
|
||||
progressBar.show(context, status);
|
||||
progressBar.show(activity, status);
|
||||
item.setSummary(progressBar.getTitle() + " " + progressBar.getMessage());
|
||||
|
||||
if (progressBar.isCancelled()) {
|
||||
setReadyStatus();
|
||||
} else if (progressBar.isFailed()) {
|
||||
setReadyStatus();
|
||||
UI.toastFromAsync(context, progressBar.getMessage());
|
||||
UI.toastFromAsync(activity, progressBar.getMessage());
|
||||
} else if (!progressBar.inProgress()) {
|
||||
setReadyStatus();
|
||||
UI.toastFromAsync(context, R.string.dictionary_loaded);
|
||||
UI.toastFromAsync(activity, R.string.dictionary_loaded);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean onClick(Preference p) {
|
||||
ArrayList<Language> languages = LanguageCollection.getAll(context, settings.getEnabledLanguageIds());
|
||||
ArrayList<Language> languages = LanguageCollection.getAll(activity, activity.getSettings().getEnabledLanguageIds());
|
||||
|
||||
setLoadingStatus();
|
||||
if (!loader.load(languages)) {
|
||||
|
|
@ -85,13 +82,13 @@ class ItemLoadDictionary extends ItemClickable {
|
|||
private void setLoadingStatus() {
|
||||
loader.setOnStatusChange(this::onLoadingStatusChange);
|
||||
onStart.run();
|
||||
item.setTitle(context.getString(R.string.dictionary_cancel_load));
|
||||
item.setTitle(activity.getString(R.string.dictionary_cancel_load));
|
||||
}
|
||||
|
||||
|
||||
private void setReadyStatus() {
|
||||
onFinish.run();
|
||||
item.setTitle(context.getString(R.string.dictionary_load_title));
|
||||
item.setTitle(activity.getString(R.string.dictionary_load_title));
|
||||
item.setSummary(progressBar.isFailed() || progressBar.isCancelled() ? progressBar.getMessage() : "");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
package io.github.sspanak.tt9.preferences.screens.languages;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.preference.MultiSelectListPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -10,20 +8,18 @@ import java.util.HashSet;
|
|||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.languages.Language;
|
||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.ui.UI;
|
||||
|
||||
class ItemSelectLanguage {
|
||||
public static final String NAME = "pref_languages";
|
||||
|
||||
private final Context context;
|
||||
private final SettingsStore settings;
|
||||
private final PreferencesActivity activity;
|
||||
private final MultiSelectListPreference item;
|
||||
|
||||
ItemSelectLanguage(Context context, MultiSelectListPreference multiSelect, SettingsStore settings) {
|
||||
this.context = context;
|
||||
ItemSelectLanguage(PreferencesActivity activity, MultiSelectListPreference multiSelect) {
|
||||
this.activity = activity;
|
||||
this.item = multiSelect;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public ItemSelectLanguage populate() {
|
||||
|
|
@ -31,9 +27,9 @@ class ItemSelectLanguage {
|
|||
return this;
|
||||
}
|
||||
|
||||
ArrayList<Language> languages = LanguageCollection.getAll(context, true);
|
||||
ArrayList<Language> languages = LanguageCollection.getAll(activity, true);
|
||||
if (languages.isEmpty()) {
|
||||
UI.alert(context, R.string.error, R.string.failed_loading_language_definitions);
|
||||
UI.alert(activity, R.string.error, R.string.failed_loading_language_definitions);
|
||||
// do not return, the MultiSelect component requires arrays, even if empty, otherwise it crashes
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +45,7 @@ class ItemSelectLanguage {
|
|||
|
||||
item.setEntries(keys.toArray(new CharSequence[0]));
|
||||
item.setEntryValues(values.toArray(new CharSequence[0]));
|
||||
item.setValues(settings.getEnabledLanguagesIdsAsStrings());
|
||||
item.setValues(activity.getSettings().getEnabledLanguagesIdsAsStrings());
|
||||
previewSelection();
|
||||
|
||||
return this;
|
||||
|
|
@ -67,8 +63,8 @@ class ItemSelectLanguage {
|
|||
newLanguages.add("1");
|
||||
}
|
||||
|
||||
settings.saveEnabledLanguageIds(newLanguages);
|
||||
item.setValues(settings.getEnabledLanguagesIdsAsStrings());
|
||||
activity.getSettings().saveEnabledLanguageIds(newLanguages);
|
||||
item.setValues(activity.getSettings().getEnabledLanguagesIdsAsStrings());
|
||||
previewSelection();
|
||||
|
||||
// we validate and save manually above, so "false" disables automatic save
|
||||
|
|
@ -80,7 +76,7 @@ class ItemSelectLanguage {
|
|||
|
||||
private void previewSelection() {
|
||||
item.setSummary(
|
||||
LanguageCollection.toString(LanguageCollection.getAll(context, settings.getEnabledLanguageIds(), true))
|
||||
LanguageCollection.toString(LanguageCollection.getAll(activity, activity.getSettings().getEnabledLanguageIds(), true))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,25 +8,21 @@ import io.github.sspanak.tt9.db.WordStoreAsync;
|
|||
import io.github.sspanak.tt9.languages.Language;
|
||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.SettingsStore;
|
||||
|
||||
|
||||
class ItemTruncateUnselected extends ItemTruncateAll {
|
||||
public static final String NAME = "dictionary_truncate_unselected";
|
||||
|
||||
private final SettingsStore settings;
|
||||
|
||||
|
||||
ItemTruncateUnselected(Preference item, PreferencesActivity context, SettingsStore settings, Runnable onStart, Runnable onFinish) {
|
||||
ItemTruncateUnselected(Preference item, PreferencesActivity context, Runnable onStart, Runnable onFinish) {
|
||||
super(item, context, onStart, onFinish);
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean onClick(Preference p) {
|
||||
ArrayList<Integer> unselectedLanguageIds = new ArrayList<>();
|
||||
ArrayList<Integer> selectedLanguageIds = settings.getEnabledLanguageIds();
|
||||
ArrayList<Integer> selectedLanguageIds = activity.getSettings().getEnabledLanguageIds();
|
||||
for (Language lang : LanguageCollection.getAll(activity, false)) {
|
||||
if (!selectedLanguageIds.contains(lang.getId())) {
|
||||
unselectedLanguageIds.add(lang.getId());
|
||||
|
|
|
|||
|
|
@ -30,21 +30,18 @@ public class LanguagesScreen extends BaseScreenFragment {
|
|||
protected void onCreate() {
|
||||
ItemSelectLanguage multiSelect = new ItemSelectLanguage(
|
||||
activity,
|
||||
findPreference(ItemSelectLanguage.NAME),
|
||||
activity.settings
|
||||
findPreference(ItemSelectLanguage.NAME)
|
||||
);
|
||||
multiSelect.populate().enableValidation();
|
||||
|
||||
loadItem = new ItemLoadDictionary(findPreference(ItemLoadDictionary.NAME),
|
||||
activity,
|
||||
activity.settings,
|
||||
() -> ItemClickable.disableOthers(clickables, loadItem),
|
||||
this::onActionFinish
|
||||
);
|
||||
|
||||
exportDictionaryItem = new ItemExportDictionary(findPreference(ItemExportDictionary.NAME),
|
||||
activity,
|
||||
activity.settings,
|
||||
this::onActionStart,
|
||||
this::onActionFinish
|
||||
);
|
||||
|
|
@ -55,7 +52,6 @@ public class LanguagesScreen extends BaseScreenFragment {
|
|||
clickables.add(new ItemTruncateUnselected(
|
||||
findPreference(ItemTruncateUnselected.NAME),
|
||||
activity,
|
||||
activity.settings,
|
||||
this::onActionStart,
|
||||
this::onActionFinish
|
||||
));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue