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 { public class SharingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
protected SwitchPreference locationSharing; protected SwitchPreference locationSharing;
protected SwitchPreference statusSwitch;
protected Preference locationMode; protected Preference locationMode;
protected ListPreference manualLocationList; protected ListPreference manualLocationList;
protected PreferenceCategory preferenceCategory; protected PreferenceCategory preferenceCategory;
@ -70,6 +71,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
private PredefinedCoordinatesService locationService; private PredefinedCoordinatesService locationService;
private CompositeDisposable disposable; private CompositeDisposable disposable;
private AlertDialog.Builder builder; private AlertDialog.Builder builder;
private String[] statusesArray;
private ArrayList<String> predefinedLocationsList;
public static SharingFragment newInstance() { public static SharingFragment newInstance() {
return new SharingFragment(); return new SharingFragment();
@ -83,7 +86,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
.subscribeWith(new DisposableSingleObserver<List<String>>() { .subscribeWith(new DisposableSingleObserver<List<String>>() {
@Override @Override
public void onSuccess(List<String> strings) { 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); setListPreferenceData(statusList, statusesArray, null);
} }
@ -114,6 +117,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
predefinedCoordsList.addAll(coords); predefinedCoordsList.addAll(coords);
String[] stringnames = predefinedLocationsNames.toArray(new String[0]); String[] stringnames = predefinedLocationsNames.toArray(new String[0]);
predefinedLocationsList = new ArrayList<>(Arrays.asList(stringnames));
List<Integer> activesId = Stream.of(coords).indexed() List<Integer> activesId = Stream.of(coords).indexed()
.filter(v -> v.getSecond().getPredefinedCoordinateId().equals(currentCoordId)).map(IntPair::getFirst).toList(); .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) { public void onError(Throwable e) {
Toast.makeText(getApplicationContext(), R.string.error_location_fetch, Toast.LENGTH_SHORT).show(); 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); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.pref_sharing); addPreferencesFromResource(R.layout.pref_sharing);
locationSharing = (SwitchPreference) findPreference("key_sharing_enabled"); locationSharing = (SwitchPreference) findPreference("key_sharing_enabled");
statusSwitch = (SwitchPreference) findPreference("key_status_enabled");
locationMode = findPreference("key_location_level"); locationMode = findPreference("key_location_level");
preferenceCategory = (PreferenceCategory) findPreference("category_sharing"); preferenceCategory = (PreferenceCategory) findPreference("category_sharing");
manualLocationList = (ListPreference) findPreference("key_manual_location_value"); 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); statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class);
locationService = ApiClient.getClient(getApplicationContext()).create(PredefinedCoordinatesService.class); locationService = ApiClient.getClient(getApplicationContext()).create(PredefinedCoordinatesService.class);
disposable = new CompositeDisposable(); disposable = new CompositeDisposable();
statusesArray = new String[0];
predefinedLocationsList = new ArrayList<String>();
getStatuses(disposable); getStatuses(disposable);
getLocations(disposable); getLocations(disposable);
locationLevelMapping = new HashMap<Integer, String>(); locationLevelMapping = new HashMap<Integer, String>();
@ -166,6 +174,26 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext())); statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(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**/ /** Main sharing switch**/
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> { locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
@ -366,11 +394,37 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
} }
} }
private void handleDeleteStatuses(List<String> resp){ private void handleDeleteStatuses(List<String> resp){
if(resp.isEmpty()){
statusSwitch.setChecked(false);
statusSwitch.setEnabled(false);
statusList.setSummary("");
}else{
String[] statusesArray = resp.toArray(new String[resp.size()]); String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null); 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){ private void handleDeleteLocations(List<PredefinedCoordViewModel> resp){
getLocations(disposable); 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 @Override