managed hiding/disabling preferences depending on contents of response from api

This commit is contained in:
marcin.jedynski 2019-01-04 03:04:26 +01:00
parent a4fd031a36
commit 8a88629b9a
2 changed files with 127 additions and 40 deletions

View File

@ -72,6 +72,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
private CompositeDisposable disposable;
private AlertDialog.Builder builder;
private String[] statusesArray;
private boolean statusSwitchFlag;
private ArrayList<String> predefinedLocationsList;
public static SharingFragment newInstance() {
@ -87,6 +88,12 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
@Override
public void onSuccess(List<String> strings) {
statusesArray = strings.toArray(new String[strings.size()]);
if(strings.isEmpty()){
disableStatusPreferences();
}else{
enableStatusPreferences();
}
Log.d("STATUSES",Integer.toString(statusesArray.length ));
setListPreferenceData(statusList, statusesArray, null);
}
@ -106,8 +113,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
@Override
public void onSuccess(List<PredefinedCoordViewModel> coords) {
String currentCoordId = PrefUtils.getCurrentManualLocation(getApplicationContext());
String currentCoordId = PrefUtils.getCurrentManualLocation(getApplicationContext());
locationMap = new HashMap<String,String>();
locationUUIDs = new ArrayList<String>();
for (PredefinedCoordViewModel i : coords) locationMap.put(i.getPredefinedCoordinateId(),i.getName());
@ -118,7 +125,32 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
String[] stringnames = predefinedLocationsNames.toArray(new String[0]);
predefinedLocationsList = new ArrayList<>(Arrays.asList(stringnames));
if (!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) {
preferenceCategory.removePreference(manualLocationList);
preferenceCategory.removePreference(removeManualLocation);
preferenceCategory.removePreference(manualLocationButton);
}else{
if(predefinedCoordsList.isEmpty()){
Log.d("SANITY","yeblo");
Log.d("SANITY",predefinedCoordsList.toString());
locationSharing.setEnabled(false);
locationSharing.setChecked(false);
PrefUtils.disableSharing(getApplicationContext());
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
removeManualLocation.setEnabled(false);
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
}else{
Log.d("SANITY","niepojszo");
Log.d("SANITY",predefinedCoordsList.toString());
manualLocationList.setEnabled(true);
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext()));
removeManualLocation.setEnabled(true);
locationSharing.setEnabled(true);
}
}
List<Integer> activesId = Stream.of(coords).indexed()
.filter(v -> v.getSecond().getPredefinedCoordinateId().equals(currentCoordId)).map(IntPair::getFirst).toList();
@ -128,6 +160,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
else {
setListPreferenceData(manualLocationList, stringnames, activesId.get(0));
}
}
@Override
@ -174,26 +207,14 @@ 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()));
}
// Log.d("STATUS",statusesArray[0]);
// && PrefUtils.getLocationLevel(getApplicationContext()).equals("manual"
if(predefinedLocationsList.isEmpty()){
/* if(predefinedLocationsList.isEmpty()){
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
removeManualLocation.setEnabled(false);
}
}*/
/** Main sharing switch**/
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
@ -208,23 +229,30 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue)));
if (PrefUtils.getLocationLevel(getApplicationContext()).equals(SharingLevel.MANUAL.toString())) {
if (!predefinedCoordsList.isEmpty()) {
// Log.d("SANITY","niepojszo");
// Log.d("SANITY",predefinedCoordsList.toString());
preferenceCategory.addPreference(manualLocationList);
preferenceCategory.addPreference(removeManualLocation);
}else{
// Log.d("SANITY","pojszło");
locationSharing.setEnabled(false);
}
preferenceCategory.addPreference(manualLocationButton);
} else {
locationSharing.setEnabled(true);
preferenceCategory.removePreference(manualLocationList);
preferenceCategory.removePreference(manualLocationButton);
preferenceCategory.removePreference(removeManualLocation);
}
return true;
});
/** Manual location category hiding when location level is != manual **/
if (!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) {
preferenceCategory.removePreference(manualLocationList);
preferenceCategory.removePreference(manualLocationButton);
}
/** Custom manual location list change listener **/
manualLocationList.setOnPreferenceChangeListener((preference, newValue) -> {
@ -256,7 +284,9 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
fragmentTransaction.commit();
return true;
});
statusSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
return true;
});
/** Status list change listener **/
statusList.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) preference;
@ -266,7 +296,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
return true;
});
/** Custom status list change listener **/
/** Custom status edittext change listener **/
manualStatus.setOnPreferenceChangeListener((preference, newValue) -> {
disposable.add(statusesService.postUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), (String) newValue)
@ -339,7 +369,8 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
}
}
for (String uuid : uuidsToBeDeleted) {
// predefinedCoordsList.removeIf(x -> x.getPredefinedCoordinateId().equals(uuid));
predefinedCoordsList.removeAll(Stream.of(predefinedCoordsList).filter(x -> x.getPredefinedCoordinateId().equals(uuid)).toList());
disposable.add(locationService.deleteUserPredefinedCoord(PrefUtils.getUserId(getApplicationContext()), uuid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
@ -373,10 +404,15 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
;
private void handleResponse(List<String> resp) {
if(resp.size() == 1){
enableStatusPreferences();
if(PrefUtils.isStatusEnabled(getApplicationContext()) == false){
PrefUtils.enableStatus(getApplicationContext());
statusSwitch.setChecked(true);
}
}
String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, resp.size() - 1);
statusList.setValueIndex(resp.size() - 1);
PrefUtils.storeStatus(getApplicationContext(), resp.get(resp.size() - 1));
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
@ -395,10 +431,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
}
private void handleDeleteStatuses(List<String> resp){
if(resp.isEmpty()){
statusSwitch.setChecked(false);
statusSwitch.setEnabled(false);
statusList.setSummary("");
disableStatusPreferences();
}else{
String[] statusesArray = resp.toArray(new String[resp.size()]);
setListPreferenceData(statusList, statusesArray, null);
@ -413,18 +446,57 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
}
private void handleDeleteLocations(List<PredefinedCoordViewModel> resp){
getLocations(disposable);
String currentEntry = PrefUtils.getCurrentManualLocation(getApplicationContext());
if(resp.isEmpty()){
Log.d("LOKACJE puste",resp.toString());
disableManualLocationPreferences();
}else{
if(!Stream.of(resp).filter(x -> x.getName().equals(currentEntry)).toList().isEmpty())
{
Log.d("LOKACJE pełne",resp.toString());
for (PredefinedCoordViewModel location: resp) {
if(location.getName().equals(currentEntry)){
statusList.setValueIndex(resp.indexOf(location));
statusList.setSummary(location.getName());
return;
manualLocationList.setValueIndex(resp.indexOf(location));
break;
}
}
statusList.setValueIndex(0);
statusList.setSummary(resp.get(0).getName());
}else{
manualLocationList.setValueIndex(0);
manualLocationList.setSummary(resp.get(0).getName());
//todo czy na pewno w shared pref sa dobre wartosci
}
}
}
private void disableStatusPreferences(){
removeManualStatus.setEnabled(false);
statusList.setEnabled(false);
statusSwitch.setEnabled(false);
statusList.setSummary("");
PrefUtils.disableStatus(getApplicationContext());
}
private void enableStatusPreferences(){
removeManualStatus.setEnabled(true);
statusList.setEnabled(true);
statusSwitch.setEnabled(true);
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
}
private void disableManualLocationPreferences(){
removeManualLocation.setEnabled(false);
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
locationSharing.setChecked(false);
locationSharing.setEnabled(false);
PrefUtils.disableSharing(getApplicationContext());
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
}
private void enableManualLocationPreferences(){
removeManualLocation.setEnabled(true);
manualLocationList.setEnabled(true);
}
@Override

View File

@ -60,6 +60,16 @@ public class PrefUtils {
return getSharedPreferences(context).getBoolean("key_status_enabled", false);
}
public static void enableStatus(Context context){
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("key_status_enabled", true);
editor.apply();
}
public static void disableStatus(Context context) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("key_status_enabled", false);
editor.apply();
}
public static String getUserStatus(Context context) {
return getSharedPreferences(context).getString("status_entry", "Dostępny");
@ -94,6 +104,11 @@ public class PrefUtils {
public static boolean isEnableSharingLocalization(Context context) {
return getSharedPreferences(context).getBoolean("key_sharing_enabled", false);
}
public static void disableSharing(Context context){
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("key_sharing_enabled", false);
editor.apply();
}
public static void storeEnableSharingLocalization(Context context, Boolean isChecked) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();