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

View File

@ -60,6 +60,16 @@ public class PrefUtils {
return getSharedPreferences(context).getBoolean("key_status_enabled", false); 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) { public static String getUserStatus(Context context) {
return getSharedPreferences(context).getString("status_entry", "Dostępny"); return getSharedPreferences(context).getString("status_entry", "Dostępny");
@ -94,6 +104,11 @@ public class PrefUtils {
public static boolean isEnableSharingLocalization(Context context) { public static boolean isEnableSharingLocalization(Context context) {
return getSharedPreferences(context).getBoolean("key_sharing_enabled", false); 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) { public static void storeEnableSharingLocalization(Context context, Boolean isChecked) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); SharedPreferences.Editor editor = getSharedPreferences(context).edit();