Merge pull request #10703 from osmandapp/wikipedia_languages_global

Save the global settings Wikipedia languages
This commit is contained in:
Vitaliy 2021-01-31 15:34:59 +02:00 committed by GitHub
commit 956951d042
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 7 deletions

View file

@ -1,8 +1,12 @@
package net.osmand.plus.wikipedia; package net.osmand.plus.wikipedia;
import android.app.Activity;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View; import android.view.View;
import android.widget.CompoundButton; import android.widget.CompoundButton;
@ -12,6 +16,8 @@ import androidx.core.content.ContextCompat;
import androidx.core.os.ConfigurationCompat; import androidx.core.os.ConfigurationCompat;
import androidx.core.os.LocaleListCompat; import androidx.core.os.LocaleListCompat;
import com.google.android.material.snackbar.Snackbar;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
@ -124,6 +130,12 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen
} }
} }
@Nullable
public MapActivity getMapActivity() {
Activity activity = getActivity();
return (MapActivity) activity;
}
private void initLanguagesData() { private void initLanguagesData() {
languages = new ArrayList<>(); languages = new ArrayList<>();
@ -188,12 +200,44 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen
localesForSaving.add(language.getLocale()); localesForSaving.add(language.getLocale());
} }
} }
wikiPlugin.setLanguagesToShow(localesForSaving); applyPreferenceWithSnackBar(localesForSaving, isGlobalWikiPoiEnabled);
wikiPlugin.setShowAllLanguages(isGlobalWikiPoiEnabled);
wikiPlugin.updateWikipediaState();
dismiss(); dismiss();
} }
protected final void applyPreference(boolean applyToAllProfiles, List<String> localesForSaving, boolean global) {
if (applyToAllProfiles) {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
wikiPlugin.setLanguagesToShow(mode, localesForSaving);
wikiPlugin.setShowAllLanguages(mode, global);
}
} else {
wikiPlugin.setLanguagesToShow(localesForSaving);
wikiPlugin.setShowAllLanguages(global);
}
wikiPlugin.updateWikipediaState();
}
protected void applyPreferenceWithSnackBar(final List<String> localesForSaving, final boolean global) {
applyPreference(false, localesForSaving, global);
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
String modeName = appMode.toHumanString();
String text = app.getString(R.string.changes_applied_to_profile, modeName);
SpannableString message = UiUtilities.createSpannableString(text, new StyleSpan(Typeface.BOLD), modeName);
Snackbar snackbar = Snackbar.make(mapActivity.getLayout(), message, Snackbar.LENGTH_LONG)
.setAction(R.string.apply_to_all_profiles, new View.OnClickListener() {
@Override
public void onClick(View view) {
applyPreference(true, localesForSaving, global);
}
});
UiUtilities.setupSnackbarVerticalLayout(snackbar);
UiUtilities.setupSnackbar(snackbar, nightMode);
snackbar.show();
}
}
private View getCustomButtonView() { private View getCustomButtonView() {
OsmandApplication app = getMyApplication(); OsmandApplication app = getMyApplication();
if (app == null) { if (app == null) {
@ -265,7 +309,7 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen
} }
public static void showInstance(@NonNull MapActivity mapActivity, public static void showInstance(@NonNull MapActivity mapActivity,
boolean usedOnMap) { boolean usedOnMap) {
SelectWikiLanguagesBottomSheet fragment = new SelectWikiLanguagesBottomSheet(); SelectWikiLanguagesBottomSheet fragment = new SelectWikiLanguagesBottomSheet();
fragment.setUsedOnMap(usedOnMap); fragment.setUsedOnMap(usedOnMap);
fragment.show(mapActivity.getSupportFragmentManager(), SelectWikiLanguagesBottomSheet.TAG); fragment.show(mapActivity.getSupportFragmentManager(), SelectWikiLanguagesBottomSheet.TAG);

View file

@ -30,6 +30,7 @@ import net.osmand.plus.search.QuickSearchDialogFragment;
import net.osmand.plus.search.QuickSearchListAdapter; import net.osmand.plus.search.QuickSearchListAdapter;
import net.osmand.plus.search.listitems.QuickSearchBannerListItem; import net.osmand.plus.search.listitems.QuickSearchBannerListItem;
import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem; import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.layers.DownloadedRegionsLayer; import net.osmand.plus.views.layers.DownloadedRegionsLayer;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
@ -97,8 +98,8 @@ public class WikipediaPlugin extends OsmandPlugin {
@Override @Override
protected void registerLayerContextMenuActions(OsmandMapTileView mapView, protected void registerLayerContextMenuActions(OsmandMapTileView mapView,
ContextMenuAdapter adapter, ContextMenuAdapter adapter,
final MapActivity mapActivity) { final MapActivity mapActivity) {
ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() {
@Override @Override
@ -113,7 +114,7 @@ public class WikipediaPlugin extends OsmandPlugin {
@Override @Override
public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId, public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId,
final int pos, boolean isChecked, int[] viewCoordinates) { final int pos, boolean isChecked, int[] viewCoordinates) {
if (itemId == R.string.shared_string_wikipedia) { if (itemId == R.string.shared_string_wikipedia) {
toggleWikipediaPoi(isChecked, new CallbackWithObject<Boolean>() { toggleWikipediaPoi(isChecked, new CallbackWithObject<Boolean>() {
@Override @Override
@ -189,26 +190,50 @@ public class WikipediaPlugin extends OsmandPlugin {
return !isShowAllLanguages() && getLanguagesToShow() != null; return !isShowAllLanguages() && getLanguagesToShow() != null;
} }
public boolean hasCustomSettings(ApplicationMode profile) {
return !isShowAllLanguages(profile) && getLanguagesToShow(profile) != null;
}
public boolean hasLanguagesFilter() { public boolean hasLanguagesFilter() {
return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.get() != null; return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.get() != null;
} }
public boolean hasLanguagesFilter(ApplicationMode profile) {
return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getModeValue(profile) != null;
}
public boolean isShowAllLanguages() { public boolean isShowAllLanguages() {
return settings.GLOBAL_WIKIPEDIA_POI_ENABLED.get(); return settings.GLOBAL_WIKIPEDIA_POI_ENABLED.get();
} }
public boolean isShowAllLanguages(ApplicationMode mode) {
return settings.GLOBAL_WIKIPEDIA_POI_ENABLED.getModeValue(mode);
}
public void setShowAllLanguages(boolean showAllLanguages) { public void setShowAllLanguages(boolean showAllLanguages) {
settings.GLOBAL_WIKIPEDIA_POI_ENABLED.set(showAllLanguages); settings.GLOBAL_WIKIPEDIA_POI_ENABLED.set(showAllLanguages);
} }
public void setShowAllLanguages(ApplicationMode mode, boolean showAllLanguages) {
settings.GLOBAL_WIKIPEDIA_POI_ENABLED.setModeValue(mode, showAllLanguages);
}
public List<String> getLanguagesToShow() { public List<String> getLanguagesToShow() {
return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getStringsList(); return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getStringsList();
} }
public List<String> getLanguagesToShow(ApplicationMode mode) {
return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getStringsListForProfile(mode);
}
public void setLanguagesToShow(List<String> languagesToShow) { public void setLanguagesToShow(List<String> languagesToShow) {
settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.setStringsList(languagesToShow); settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.setStringsList(languagesToShow);
} }
public void setLanguagesToShow(ApplicationMode mode, List<String> languagesToShow) {
settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.setStringsListForProfile(mode, languagesToShow);
}
public void toggleWikipediaPoi(boolean enable, CallbackWithObject<Boolean> callback) { public void toggleWikipediaPoi(boolean enable, CallbackWithObject<Boolean> callback) {
if (enable) { if (enable) {
showWikiOnMap(); showWikiOnMap();