resolved case when selected entries are deleted, working towards enabling/disabling views when there are no items

This commit is contained in:
marcin.jedynski 2019-01-03 10:38:58 +01:00
parent 94368b05d1
commit a4fd031a36

View File

@ -53,6 +53,7 @@ import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class SharingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
protected SwitchPreference locationSharing;
protected SwitchPreference statusSwitch;
protected Preference locationMode;
protected ListPreference manualLocationList;
protected PreferenceCategory preferenceCategory;
@ -70,8 +71,10 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
private PredefinedCoordinatesService locationService;
private CompositeDisposable disposable;
private AlertDialog.Builder builder;
private String[] statusesArray;
private ArrayList<String> predefinedLocationsList;
public static SharingFragment newInstance() {
public static SharingFragment newInstance() {
return new SharingFragment();
}
@ -83,7 +86,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
.subscribeWith(new DisposableSingleObserver<List<String>>() {
@Override
public void onSuccess(List<String> strings) {
String[] statusesArray = strings.toArray(new String[strings.size()]);
statusesArray = strings.toArray(new String[strings.size()]);
setListPreferenceData(statusList, statusesArray, null);
}
@ -114,6 +117,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
predefinedCoordsList.addAll(coords);
String[] stringnames = predefinedLocationsNames.toArray(new String[0]);
predefinedLocationsList = new ArrayList<>(Arrays.asList(stringnames));
List<Integer> activesId = Stream.of(coords).indexed()
.filter(v -> v.getSecond().getPredefinedCoordinateId().equals(currentCoordId)).map(IntPair::getFirst).toList();
@ -130,6 +134,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
public void onError(Throwable e) {
Toast.makeText(getApplicationContext(), R.string.error_location_fetch, Toast.LENGTH_SHORT).show();
}
}));
}
@ -139,6 +144,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.pref_sharing);
locationSharing = (SwitchPreference) findPreference("key_sharing_enabled");
statusSwitch = (SwitchPreference) findPreference("key_status_enabled");
locationMode = findPreference("key_location_level");
preferenceCategory = (PreferenceCategory) findPreference("category_sharing");
manualLocationList = (ListPreference) findPreference("key_manual_location_value");
@ -151,6 +157,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
locationService = ApiClient.getClient(getApplicationContext()).create(PredefinedCoordinatesService.class);
disposable = new CompositeDisposable();
statusesArray = new String[0];
predefinedLocationsList = new ArrayList<String>();
getStatuses(disposable);
getLocations(disposable);
locationLevelMapping = new HashMap<Integer, String>();
@ -166,6 +174,26 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext()));
Log.d("STATUS",statusesArray[0]);
if(statusesArray.length == 0){
removeManualStatus.setEnabled(false);
statusList.setEnabled(false);
statusSwitch.setChecked(false);
statusSwitch.setEnabled(false);
statusList.setSummary("");
}else{
removeManualStatus.setEnabled(true);
statusList.setEnabled(true);
statusSwitch.setChecked(true);
statusSwitch.setEnabled(true);
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
}
// && PrefUtils.getLocationLevel(getApplicationContext()).equals("manual"
if(predefinedLocationsList.isEmpty()){
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
removeManualLocation.setEnabled(false);
}
/** Main sharing switch**/
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
@ -366,12 +394,38 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
}
}
private void handleDeleteStatuses(List<String> resp){
String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null);
if(resp.isEmpty()){
statusSwitch.setChecked(false);
statusSwitch.setEnabled(false);
statusList.setSummary("");
}else{
String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null);
String currentEntry = PrefUtils.getUserStatus(getApplicationContext());
if(resp.contains(currentEntry)){
statusList.setValueIndex(resp.indexOf(currentEntry));
}else{
statusList.setValueIndex(0);
statusList.setSummary(resp.get(0));
}
}
}
private void handleDeleteLocations(List<PredefinedCoordViewModel> resp){
getLocations(disposable);
}
String currentEntry = PrefUtils.getCurrentManualLocation(getApplicationContext());
for (PredefinedCoordViewModel location: resp) {
if(location.getName().equals(currentEntry)){
statusList.setValueIndex(resp.indexOf(location));
statusList.setSummary(location.getName());
return;
}
}
statusList.setValueIndex(0);
statusList.setSummary(resp.get(0).getName());
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {