Live updates state saved.

This commit is contained in:
GaidamakUA 2015-12-10 16:48:35 +02:00
parent ef3b7d0727
commit 2f97919233
3 changed files with 60 additions and 19 deletions

View file

@ -42,7 +42,7 @@
android:layout_row="1"
android:text="@string/live_update"/>
<Switch
<android.support.v7.widget.SwitchCompat
android:id="@+id/liveUpdatesSwitch"
android:layout_gravity="right"/>
@ -52,7 +52,7 @@
android:layout_row="2"
android:text="@string/only_download_over_wifi"/>
<Switch
<android.support.v7.widget.SwitchCompat
android:id="@+id/downloadOnlyOverWiFi"
android:layout_gravity="right"/>

View file

@ -40,6 +40,7 @@ import java.util.List;
public class LiveUpdatesFragment extends Fragment {
public static final String TITILE = "Live Updates";
public static final String LIVE_UPDATES_ON_POSTFIX = "_live_updates_on";
public static final Comparator<LocalIndexInfo> LOCAL_INDEX_INFO_COMPARATOR = new Comparator<LocalIndexInfo>() {
@Override
public int compare(LocalIndexInfo lhs, LocalIndexInfo rhs) {
@ -92,6 +93,10 @@ public class LiveUpdatesFragment extends Fragment {
return (AbstractDownloadActivity) getActivity();
}
public void notifyLiveUpdatesChanged() {
adapter.notifyLiveUpdatesChanged();
}
protected class LocalIndexesAdapter extends OsmandBaseExpandableListAdapter {
final ArrayList<LocalIndexInfo> dataShouldUpdate = new ArrayList<>();
final ArrayList<LocalIndexInfo> dataShouldNotUpdate = new ArrayList<>();
@ -104,8 +109,34 @@ public class LiveUpdatesFragment extends Fragment {
}
public void add(LocalIndexInfo info) {
OsmandSettings.CommonPreference<Boolean> preference =
preferenceForLocalIndex(LIVE_UPDATES_ON_POSTFIX, info);
if (preference.get()) {
dataShouldUpdate.add(info);
} else {
dataShouldNotUpdate.add(info);
}
}
public void notifyLiveUpdatesChanged() {
for (LocalIndexInfo localIndexInfo : dataShouldUpdate) {
OsmandSettings.CommonPreference<Boolean> preference =
preferenceForLocalIndex(LIVE_UPDATES_ON_POSTFIX, localIndexInfo);
if (!preference.get()) {
dataShouldUpdate.remove(localIndexInfo);
dataShouldNotUpdate.add(localIndexInfo);
}
}
for (LocalIndexInfo localIndexInfo : dataShouldNotUpdate) {
OsmandSettings.CommonPreference<Boolean> preference =
preferenceForLocalIndex(LIVE_UPDATES_ON_POSTFIX, localIndexInfo);
if (preference.get()) {
dataShouldUpdate.add(localIndexInfo);
dataShouldNotUpdate.remove(localIndexInfo);
}
}
notifyDataSetChanged();
}
public void sort() {
Collections.sort(dataShouldUpdate, LOCAL_INDEX_INFO_COMPARATOR);
@ -215,6 +246,13 @@ public class LiveUpdatesFragment extends Fragment {
return true;
}
private OsmandSettings.CommonPreference<Boolean> preferenceForLocalIndex(String idPostfix,
LocalIndexInfo item) {
final OsmandApplication myApplication = fragment.getMyActivity().getMyApplication();
final OsmandSettings settings = myApplication.getSettings();
final String settingId = item.getFileName() + idPostfix;
return settings.registerBooleanPreference(settingId, false);
}
}
private void expandAllGroups() {
@ -340,5 +378,4 @@ public class LiveUpdatesFragment extends Fragment {
adapter.sort();
}
}
}

View file

@ -6,10 +6,9 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
@ -24,11 +23,24 @@ public class SettingsDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LocalIndexInfo localIndexInfo = getArguments().getParcelable(LOCAL_INDEX);
View view = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_live_updates_item_settings, null);
final LocalIndexInfo localIndexInfo = getArguments().getParcelable(LOCAL_INDEX);
final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.liveUpdatesSwitch);
final OsmandSettings.CommonPreference<Boolean> liveUpdatePreference =
preferenceForLocalIndex(LiveUpdatesFragment.LIVE_UPDATES_ON_POSTFIX, localIndexInfo);
liveUpdatesSwitch.setChecked(liveUpdatePreference.get());
builder.setView(view)
.setPositiveButton(R.string.shared_string_save, null)
.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
liveUpdatePreference.set(liveUpdatesSwitch.isChecked());
getLiveUpdatesFragment().notifyLiveUpdatesChanged();
}
})
.setNegativeButton(R.string.shared_string_cancel, null)
.setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() {
@Override
@ -43,20 +55,12 @@ public class SettingsDialogFragment extends DialogFragment {
return (LiveUpdatesFragment) getParentFragment();
}
private void initSwitch(ToggleButton toggleButton, String idPostfix, LocalIndexInfo item) {
private OsmandSettings.CommonPreference<Boolean> preferenceForLocalIndex(String idPostfix,
LocalIndexInfo item) {
final OsmandApplication myApplication = ((OsmandActionBarActivity) this.getActivity()).getMyApplication();
final OsmandSettings settings = myApplication.getSettings();
final String settingId = item.getFileName() + idPostfix;
final OsmandSettings.CommonPreference<Boolean> preference =
settings.registerBooleanPreference(settingId, false);
boolean initialValue = preference.get();
toggleButton.setChecked(initialValue);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
preference.set(isChecked);
}
});
return settings.registerBooleanPreference(settingId, false);
}
public static SettingsDialogFragment createInstance(LocalIndexInfo localIndexInfo) {