From a4fd031a3623bd2f79da73dba2e85fedeb8cee93 Mon Sep 17 00:00:00 2001 From: "marcin.jedynski" Date: Thu, 3 Jan 2019 10:38:58 +0100 Subject: [PATCH] resolved case when selected entries are deleted, working towards enabling/disabling views when there are no items --- .../findmytutor/activity/SharingFragment.java | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java index f955f6b..082cc07 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/SharingFragment.java @@ -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 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>() { @Override public void onSuccess(List 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 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(); getStatuses(disposable); getLocations(disposable); locationLevelMapping = new HashMap(); @@ -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 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 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) {