Fix blacklists

This commit is contained in:
Mieszko Wrzeszczyński 2019-01-06 23:45:47 +01:00
commit 4bb9452cbc
4 changed files with 78 additions and 75 deletions

View File

@ -29,7 +29,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8 (1)" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -162,10 +162,10 @@ public class BlackList extends AppCompatActivity {
private void fetchBlackListedUsers() { private void fetchBlackListedUsers() {
prevSize = blacklistedUsers.size(); prevSize = blacklistedUsers.size();
blacklistedUsers.clear(); blacklistedUsers.clear();
disposable.add(getListOfBlacklistedUsers(tutorId) disposable.add(getListOfBlacklistedUsers(tutorId)
.doOnSubscribe(t -> didFetched = false)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable) .flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable)
@ -179,11 +179,13 @@ public class BlackList extends AppCompatActivity {
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
showError(e); showError(e);
didFetched = false;
} }
@Override @Override
public void onComplete() { public void onComplete() {
Collections.sort(blacklistedUsers, (a, b) -> sortByUserName(a,b)); Collections.sort(blacklistedUsers, (a, b) -> sortByUserName(a,b));
didFetched = true;
refreshUI(); refreshUI();
} }
})); }));

View File

@ -123,7 +123,26 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
List<String> predefinedLocationsNames = Stream.of(coords).map(PredefinedCoordViewModel::getName).toList(); List<String> predefinedLocationsNames = Stream.of(coords).map(PredefinedCoordViewModel::getName).toList();
List<String> predefinedLocationsUUIDs = Stream.of(coords).map(PredefinedCoordViewModel::getPredefinedCoordinateId).toList(); List<String> predefinedLocationsUUIDs = Stream.of(coords).map(PredefinedCoordViewModel::getPredefinedCoordinateId).toList();
predefinedCoordsList.addAll(coords); predefinedCoordsList.addAll(coords);
if (!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) {
preferenceCategory.removePreference(manualLocationList);
preferenceCategory.removePreference(removeManualLocation);
preferenceCategory.removePreference(manualLocationButton);
}else{
if(predefinedCoordsList.isEmpty()){
locationSharing.setEnabled(false);
locationSharing.setChecked(false);
PrefUtils.disableSharing(getApplicationContext());
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
removeManualLocation.setEnabled(false);
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
}else{
manualLocationList.setEnabled(true);
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext()));
removeManualLocation.setEnabled(true);
locationSharing.setEnabled(true);
}
}
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));
List<Integer> activesId = Stream.of(coords).indexed() List<Integer> activesId = Stream.of(coords).indexed()
@ -180,26 +199,7 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
statusMapping.put(0, "available"); statusMapping.put(0, "available");
statusMapping.put(1, "consultation"); statusMapping.put(1, "consultation");
statusMapping.put(2, "busy"); statusMapping.put(2, "busy");
if (!PrefUtils.getLocationLevel(getApplicationContext()).equals("manual")) {
preferenceCategory.removePreference(manualLocationList);
preferenceCategory.removePreference(removeManualLocation);
preferenceCategory.removePreference(manualLocationButton);
}else{
if(predefinedCoordsList.isEmpty()){
locationSharing.setEnabled(false);
locationSharing.setChecked(false);
PrefUtils.disableSharing(getApplicationContext());
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
removeManualLocation.setEnabled(false);
manualLocationList.setEnabled(false);
manualLocationList.setSummary("");
}else{
manualLocationList.setEnabled(true);
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext()));
removeManualLocation.setEnabled(true);
locationSharing.setEnabled(true);
}
}
statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext())); statusList.setSummary(PrefUtils.getUserStatus(getApplicationContext()));
manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext())); manualLocationList.setSummary(PrefUtils.getCurrentManualLocationName(getApplicationContext()));
@ -214,9 +214,9 @@ public class SharingFragment extends PreferenceFragment implements SharedPrefere
/** Sharing level list **/ /** Sharing level list **/
locationMode.setOnPreferenceChangeListener((preference, newValue) -> { locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
((MapActivity) getActivity()).handleBackgroundTaskLifeCycle(); ((MapActivity) getActivity()).handleBackgroundTaskLifeCycle();
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); preferenceCategory.addPreference(manualLocationList);

View File

@ -44,7 +44,6 @@ import com.uam.wmi.findmytutor.utils.WrapContentLinearLayoutManager;
import java.text.Collator; import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -61,11 +60,6 @@ import okhttp3.ResponseBody;
public class WhiteList extends AppCompatActivity { public class WhiteList extends AppCompatActivity {
private CompositeDisposable disposable = new CompositeDisposable();
private UserService userService;
private boolean didFetched = false;
private String tutorId;
@BindView(R.id.recycler_view_whitelist) @BindView(R.id.recycler_view_whitelist)
RecyclerView recyclerView; RecyclerView recyclerView;
@BindView(R.id.white_list_empty_text_view) @BindView(R.id.white_list_empty_text_view)
@ -74,7 +68,10 @@ public class WhiteList extends AppCompatActivity {
Switch aSwitch; Switch aSwitch;
@BindView(R.id.add_to_white_list_fab) @BindView(R.id.add_to_white_list_fab)
FloatingActionButton addToWhiteListFab; FloatingActionButton addToWhiteListFab;
private CompositeDisposable disposable = new CompositeDisposable();
private UserService userService;
private boolean didFetched = false;
private String tutorId;
private WhiteListAdapter mAdapter; private WhiteListAdapter mAdapter;
private Integer prevSize; private Integer prevSize;
private List<User> whitelistedUsers = new ArrayList<>(); private List<User> whitelistedUsers = new ArrayList<>();
@ -90,12 +87,12 @@ public class WhiteList extends AppCompatActivity {
userService = ApiClient.getClient(getApplicationContext()) userService = ApiClient.getClient(getApplicationContext())
.create(UserService.class); .create(UserService.class);
if (PrefUtils.isWhiteListing(this)){ if (PrefUtils.isWhiteListing(this)) {
aSwitch.setText(getString(R.string.action_white_list) +" ON"); aSwitch.setText(getString(R.string.action_white_list) + " ON");
aSwitch.setChecked(true); aSwitch.setChecked(true);
handleChangeRequest(true); handleChangeRequest(true);
}else{ } else {
aSwitch.setText(getString(R.string.action_white_list) +" OFF"); aSwitch.setText(getString(R.string.action_white_list) + " OFF");
aSwitch.setChecked(false); aSwitch.setChecked(false);
handleChangeRequest(false); handleChangeRequest(false);
} }
@ -132,7 +129,7 @@ public class WhiteList extends AppCompatActivity {
} }
private Observable<List<String>> getListOfWhitelistedUsers(String userId) { private Observable<List<String>> getListOfWhitelistedUsers(String userId) {
return userService.getTutorWhitelistedByID(userId) return userService.getTutorWhitelistedByID(userId)
.toObservable() .toObservable()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
@ -151,30 +148,33 @@ public class WhiteList extends AppCompatActivity {
whitelistedUsers.clear(); whitelistedUsers.clear();
disposable.add(getListOfWhitelistedUsers(tutorId) disposable.add(getListOfWhitelistedUsers(tutorId)
.subscribeOn(Schedulers.io()) .doOnSubscribe(t -> didFetched = false)
.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io())
.flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable) .observeOn(AndroidSchedulers.mainThread())
.flatMap((Function<String, ObservableSource<User>>) this::getUserObservable) .flatMap((Function<List<String>, Observable<String>>) Observable::fromIterable)
.subscribeWith(new DisposableObserver<User>() { .flatMap((Function<String, ObservableSource<User>>) this::getUserObservable)
@Override .subscribeWith(new DisposableObserver<User>() {
public void onNext(User user) { @Override
whitelistedUsers.add(user); public void onNext(User user) {
} whitelistedUsers.add(user);
}
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
showError(e); showError(e);
} didFetched = false;
}
@Override @Override
public void onComplete() { public void onComplete() {
Collections.sort(whitelistedUsers, (a, b) -> sortByUserName(a,b)); Collections.sort(whitelistedUsers, (a, b) -> sortByUserName(a, b));
refreshUI(); didFetched = true;
} refreshUI();
})); }
}));
} }
private void refreshUI(){ private void refreshUI() {
toggleEmptyNotes(); toggleEmptyNotes();
mAdapter.notifyItemRangeInserted(prevSize, whitelistedUsers.size() - prevSize); mAdapter.notifyItemRangeInserted(prevSize, whitelistedUsers.size() - prevSize);
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
@ -184,7 +184,7 @@ public class WhiteList extends AppCompatActivity {
return plCollator.compare(t1.getLastName(), t2.getLastName()); return plCollator.compare(t1.getLastName(), t2.getLastName());
} }
private void showFabDialog(View v){ private void showFabDialog(View v) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext()); LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
@SuppressLint("InflateParams") View view = layoutInflaterAndroid.inflate(R.layout.white_list_fab_modal, null); @SuppressLint("InflateParams") View view = layoutInflaterAndroid.inflate(R.layout.white_list_fab_modal, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(this); AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(this);
@ -230,7 +230,7 @@ public class WhiteList extends AppCompatActivity {
userService.addStudentToWhitelist(tutorId, studentIdModel) userService.addStudentToWhitelist(tutorId, studentIdModel)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleAddUser,this::showError) .subscribe(this::handleAddUser, this::showError)
); );
} }
@ -251,15 +251,16 @@ public class WhiteList extends AppCompatActivity {
} else { } else {
message = "Network Error !"; message = "Network Error !";
} }
Log.e("ERR",message); Log.e("ERR", message);
Toast.makeText(this, message, Snackbar.LENGTH_LONG).show(); Toast.makeText(this, message, Snackbar.LENGTH_LONG).show();
} }
private void toggleEmptyNotes() { private void toggleEmptyNotes() {
if (didFetched && whitelistedUsers.size() == 0) { if (didFetched && whitelistedUsers.size() == 0) {
noNotesView.setText(R.string.list_is_empty); noNotesView.setText(R.string.list_is_empty);
noNotesView.setVisibility(View.VISIBLE); noNotesView.setVisibility(View.VISIBLE);
}else if (whitelistedUsers.size() > 0) { } else if (whitelistedUsers.size() > 0) {
noNotesView.setVisibility(View.GONE); noNotesView.setVisibility(View.GONE);
} else { } else {
noNotesView.setText(getString(R.string.loading)); noNotesView.setText(getString(R.string.loading));
@ -271,10 +272,10 @@ public class WhiteList extends AppCompatActivity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.action_whitelist_info_popup){ if (item.getItemId() == R.id.action_whitelist_info_popup) {
int layoutID = R.layout.info_popup_whitelist; int layoutID = R.layout.info_popup_whitelist;
View popupView = getLayoutInflater().inflate(layoutID,null); View popupView = getLayoutInflater().inflate(layoutID, null);
PopupWindow popupWindow = new PopupWindow(popupView, PopupWindow popupWindow = new PopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
@ -283,36 +284,36 @@ public class WhiteList extends AppCompatActivity {
// If you need the PopupWindow to dismiss when when touched outside // If you need the PopupWindow to dismiss when when touched outside
popupWindow.setBackgroundDrawable(new ColorDrawable()); popupWindow.setBackgroundDrawable(new ColorDrawable());
// Get the View's(the one that was clicked in the Fragment) location // Get the View's(the one that was clicked in the Fragment) location
View anchorView= getWindow().getDecorView().findViewById(android.R.id.content); View anchorView = getWindow().getDecorView().findViewById(android.R.id.content);
popupWindow.showAtLocation(anchorView,Gravity.TOP|Gravity.END, 0, 0); popupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.END, 0, 0);
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void handleSwitch(){ private void handleSwitch() {
aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked){ if (isChecked) {
aSwitch.setText(getString(R.string.action_white_list) + " "+ getString(R.string.on)); aSwitch.setText(getString(R.string.action_white_list) + " " + getString(R.string.on));
handleChangeRequest(true); handleChangeRequest(true);
PrefUtils.useWhitelist(this, true); PrefUtils.useWhitelist(this, true);
}else { } else {
aSwitch.setText(getString(R.string.action_white_list) + " "+getString(R.string.off)); aSwitch.setText(getString(R.string.action_white_list) + " " + getString(R.string.off));
handleChangeRequest(false); handleChangeRequest(false);
PrefUtils.useWhitelist(this, false); PrefUtils.useWhitelist(this, false);
} }
}); });
} }
private void handleChangeRequest(boolean value){ private void handleChangeRequest(boolean value) {
IsUsingListBool isUsingListBool = new IsUsingListBool(); IsUsingListBool isUsingListBool = new IsUsingListBool();
isUsingListBool.setIsUsing(value); isUsingListBool.setIsUsing(value);
disposable.add( disposable.add(
userService.setTutorWhitelist(tutorId, isUsingListBool) userService.setTutorWhitelist(tutorId, isUsingListBool)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(()->{ .subscribe(() -> {
},this::showError) }, this::showError)
); );
} }