From 854ddadeea04302730118328046072e2e8e80c1a Mon Sep 17 00:00:00 2001 From: "marcin.jedynski" Date: Mon, 26 Nov 2018 16:21:29 +0100 Subject: [PATCH] fixed switching location mode, adding custom status, and switching on location service when choosing manual location from map tab --- .../wmi/findmytutor/activity/MapActivity.java | 3 +- .../findmytutor/activity/SharingFragment.java | 93 +++++++++++-------- .../uam/wmi/findmytutor/utils/PrefUtils.java | 2 +- 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java index 84c640f..b4c0b9c 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java @@ -95,7 +95,6 @@ public class MapActivity extends BaseActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - myID = PrefUtils.getUserId(getApplicationContext()); // fetching coords service coordinateService = ApiClient.getClient(getApplicationContext()) @@ -269,6 +268,8 @@ public class MapActivity extends BaseActivity PrefUtils.getLocationLevel(getApplicationContext()) ); PrefUtils.putManualLocation(this, latLng); + handleBackgroundTaskLifeCycle(); + // // TODO remove after BG sending // manualLocStatusChecker = () -> { 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 7358b59..d5beb1f 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 @@ -63,14 +63,17 @@ public class SharingFragment extends PreferenceFragment { protected Preference manualStatus; protected ListPreference statusList; - void getStatuses(CompositeDisposable disposable){ + void getStatuses(CompositeDisposable disposable) { disposable.add(statusesService.getUserPredefinedStatuses(PrefUtils.getUserId(getApplicationContext())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSingleObserver>() { @Override public void onSuccess(List strings) { - setListPreferenceData(statusList.getKey(),strings.toArray(new String[strings.size()])); + String[] statusesArray = strings.toArray(new String[strings.size()]); + setListPreferenceData(statusList.getKey(), statusesArray); + Log.d("GETSTATUSES", Arrays.toString(statusesArray)); + } @Override @@ -78,6 +81,7 @@ public class SharingFragment extends PreferenceFragment { Toast.makeText(getApplicationContext(), "Error handling status fetch", Toast.LENGTH_SHORT).show(); } + })); } @@ -93,7 +97,7 @@ public class SharingFragment extends PreferenceFragment { manualLocationList = findPreference("key_manual_location_value"); manualLocationButton = (RightButtonPreference) findPreference("manual_location_button"); manualStatus = findPreference("key_manual_status"); - statusList =(ListPreference) findPreference("key_status_value"); + statusList = (ListPreference) findPreference("key_status_value"); statusesService = ApiClient.getClient(getApplicationContext()).create(PredefinedStatusesService.class); disposable = new CompositeDisposable(); @@ -106,26 +110,26 @@ public class SharingFragment extends PreferenceFragment { locationLevelMapping.put(3, SharingLevel.MANUAL.toString()); statusMapping = new HashMap(); - statusMapping.put(0,"available"); - statusMapping.put(1,"consultation"); - statusMapping.put(2,"busy"); + statusMapping.put(0, "available"); + statusMapping.put(1, "consultation"); + statusMapping.put(2, "busy"); /** Main sharing switch**/ locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> { PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) newValue); - ((MapActivity)getActivity()).handleBackgroundTaskLifeCycle(); + ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle(); return true; }); /** Sharing level list **/ - locationMode.setSummary(PrefUtils.getLocationLevel(getApplicationContext())); +// locationMode.setSummary(PrefUtils.getLocationLevel(getApplicationContext())); locationMode.setOnPreferenceChangeListener((preference, newValue) -> { - PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue))); - if(PrefUtils.getLocationLevel(getApplicationContext()) == "manual"){ + PrefUtils.storeLocationMode(getApplicationContext(), locationLevelMapping.get(Integer.parseInt((String) newValue))); + if (PrefUtils.getLocationLevel(getApplicationContext()).equals("manual") ) { preferenceCategory.addPreference(manualLocationList); preferenceCategory.addPreference(manualLocationButton); - }else{ + } else { preferenceCategory.removePreference(manualLocationList); preferenceCategory.removePreference(manualLocationButton); } @@ -133,7 +137,7 @@ public class SharingFragment extends PreferenceFragment { }); /** Manual location category hiding when location level is != manual **/ - if(!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")){ + if (!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) { preferenceCategory.removePreference(manualLocationList); preferenceCategory.removePreference(manualLocationButton); } @@ -160,7 +164,7 @@ public class SharingFragment extends PreferenceFragment { statusList.setOnPreferenceChangeListener((preference, newValue) -> { ListPreference lp = (ListPreference) preference; CharSequence [] entries = lp.getEntries(); - PrefUtils.storeStatus(getApplicationContext(),(String) entries[Integer.parseInt((String) newValue)]); + PrefUtils.storeStatus(getApplicationContext(), (String) entries[Integer.parseInt((String) newValue)]); // PrefUtils.storeStatus(getApplicationContext(),statusMapping.get(Integer.parseInt((String) newValue))); return true; @@ -171,10 +175,16 @@ public class SharingFragment extends PreferenceFragment { // updateListPreference(lp, newValue, "manual_statuses"); // PrefUtils.storeStatus(getApplicationContext(),(String) newValue); // statusList.setValue((String) newValue); - disposable.add(statusesService.postUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()),(String) newValue) + disposable.add(statusesService.postUserPredefinedStatus(PrefUtils.getUserId(getApplicationContext()), (String) newValue) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::handleResponse, this::handleError)); +// Log.d("GETSTATUSES", "statusy po dodaniu nowego"); +// getStatuses(disposable); +// CharSequence[] entriesIndexes = statusList.getEntries(); +//// Log.d("GETENTRIES", Arrays.toString(entriesIndexes)); +// Log.d("GETENTRIES ostatni w handle response", (String) entriesIndexes[entriesIndexes.length - 1]); + return true; }); @@ -194,25 +204,26 @@ public class SharingFragment extends PreferenceFragment { return view; } - public String getListPreferenceValue(String key){ + public String getListPreferenceValue(String key) { ListPreference lp = (ListPreference) findPreference(key); - return (String)lp.getEntry(); + return (String) lp.getEntry(); } - protected void updateListPreference(ListPreference lp,Object newValue,String storageKey){ - CharSequence [] entries = lp.getEntries(); + + protected void updateListPreference(ListPreference lp, Object newValue, String storageKey) { + CharSequence[] entries = lp.getEntries(); Set defaultEntries = new HashSet(Arrays.asList(entries)); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); - Set manualStatusSet = sharedPref.getStringSet(storageKey,defaultEntries); + Set manualStatusSet = sharedPref.getStringSet(storageKey, defaultEntries); manualStatusSet.add((String) newValue); - String [] manualStatusArr = manualStatusSet.toArray(new String[0]); + String[] manualStatusArr = manualStatusSet.toArray(new String[0]); //Arrays.sort(manualStatusArr); - setListPreferenceData(lp.getKey(),manualStatusArr); + setListPreferenceData(lp.getKey(), manualStatusArr); // lp.setValue((String) newValue); SharedPreferences.Editor editor = sharedPref.edit(); - editor.putStringSet(storageKey,manualStatusSet); + editor.putStringSet(storageKey, manualStatusSet); editor.apply(); } @@ -220,36 +231,38 @@ public class SharingFragment extends PreferenceFragment { //todo bug z pustym statusem ListPreference lp = (ListPreference) findPreference(lp_name); lp.setEntries(entries); - CharSequence[] entryValues = new CharSequence [entries.length]; + CharSequence[] entryValues = new CharSequence[entries.length]; - for (int i = 0; i < entries.length; i++){ + for (int i = 0; i < entries.length; i++) { entryValues[i] = Integer.toString(i); } lp.setDefaultValue("1"); lp.setEntryValues(entryValues); } + private void handleResponse(List resp) { - getStatuses(disposable); - String newStatus = resp.toArray(new String[resp.size()])[resp.size()-1]; -// Toast.makeText(getApplicationContext(), newStatus, Toast.LENGTH_SHORT).show(); - - statusList.setValue(Integer.toString(resp.size()-1)); - statusList.setSummary(newStatus); + String newStatus = resp.toArray(new String[resp.size()])[resp.size() - 1]; + Log.d("GETSTATUSES new status", newStatus); + String[] statusesArray = resp.toArray(new String[resp.size()]); + setListPreferenceData(statusList.getKey(), statusesArray); +// Log.d("GETSTATUSES nowe lokalne", Arrays.toString(statusList.getEntries())); +// statusList.setValue(newStatus); + statusList.setValueIndex(resp.size() - 1); } - private void handleError(Throwable error) { - if (error instanceof HttpException) { + private void handleError (Throwable error){ + if (error instanceof HttpException) { - ResponseBody responseBody = ((HttpException) error).response().errorBody(); - Toast.makeText(getApplicationContext(), - RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); + ResponseBody responseBody = ((HttpException) error).response().errorBody(); + Toast.makeText(getApplicationContext(), + RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(getApplicationContext(), - "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); - Log.d("FEEDBACK",error.getMessage()); + } else { + Toast.makeText(getApplicationContext(), + "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); + Log.d("FEEDBACK", error.getMessage()); + } } - } } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/PrefUtils.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/PrefUtils.java index b70f36f..5fb12a2 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/PrefUtils.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/PrefUtils.java @@ -159,7 +159,7 @@ public class PrefUtils { public static void putManualLocation(Context context, LatLng latLng) { SharedPreferences.Editor editor = getSharedPreferences(context).edit(); - + editor.putString("key_location_level","3"); editor.putString("location_mode", "manual"); editor.putBoolean("key_sharing_enabled", true); editor.putLong("longitude_manual_location", Double.doubleToRawLongBits(latLng.getLongitude()));