New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fb search by keywords #160
Changes from all commits
f60d57c
68e04c9
490eca6
63b7af3
e0bed1c
e2d2e1c
ea3e8cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,24 +52,31 @@ public CategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int | |
public void onBindViewHolder(@NonNull CategoriesViewHolder categoriesViewHolder, int i) { | ||
categoriesViewHolder.nameView_.setText(capabilities_[i].toString()); | ||
categoriesViewHolder.checkBox_.setChecked(userCapabilities_.contains(capabilities_[i])); | ||
categoriesViewHolder.checkBox_.setOnClickListener(v -> { | ||
categoriesViewHolder.checkBox_.setOnCheckedChangeListener((v, isChecked) -> { | ||
((ProfileSettings) v.getContext()).updateUserCapabilities(capabilities_[i], ((CheckBox) v).isChecked()); | ||
if(!isChecked){ | ||
categoriesViewHolder.keyWords_.setText(""); | ||
v.setChecked(false); | ||
} | ||
}); | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
if(keyWords_.get(capabilities_[i].toString()) != null) { | ||
for (String kw : keyWords_.get(capabilities_[i].toString())) { | ||
builder.append(kw).append(";"); | ||
if(!kw.equals("")) { | ||
builder.append(kw).append(";"); | ||
} | ||
} | ||
if (builder.length() > 2) { | ||
builder.delete(builder.length() - 1, builder.length()); | ||
} | ||
categoriesViewHolder.keyWords_.setText(builder.toString()); | ||
} | ||
addKeyWordsListener(categoriesViewHolder.keyWords_, capabilities_[i]); | ||
addKeyWordsListener(categoriesViewHolder.keyWords_, capabilities_[i], categoriesViewHolder); | ||
|
||
} | ||
|
||
private void addKeyWordsListener(EditText edittext, Categories capability){ | ||
private void addKeyWordsListener(EditText edittext, Categories capability, CategoriesViewHolder holder){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Identical blocks of code found in 2 locations. Consider refactoring. |
||
edittext.addTextChangedListener(new TextWatcher() { | ||
|
||
@Override | ||
|
@@ -87,6 +94,9 @@ public void beforeTextChanged(CharSequence s, int start, int count, | |
public void afterTextChanged(Editable s) { | ||
|
||
((ProfileSettings) edittext.getContext()).addKeyWords(capability, s.toString()); | ||
if (!holder.checkBox_.isChecked()){ | ||
holder.checkBox_.setChecked(true); | ||
} | ||
} | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,20 @@ | |
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.support.v7.widget.Toolbar; | ||
import android.text.Editable; | ||
import android.text.TextWatcher; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Spinner; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
/** | ||
|
@@ -28,6 +33,8 @@ public class ServicesFragment extends Fragment { | |
private RecyclerView.Adapter mAdapter_; | ||
private ArrayList<User> users = new ArrayList<>(); | ||
private Location currentUserLocation_; | ||
private ArrayList<String> keywords_; | ||
private Categories currentCategory_; | ||
|
||
|
||
public ServicesFragment() { | ||
|
@@ -57,6 +64,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, | |
Toolbar toolbar = getActivity().findViewById(R.id.toolbar); | ||
toolbar.setTitle(R.string.toolbar_services); | ||
|
||
keywords_ = new ArrayList<>(); | ||
|
||
RecyclerView mRecyclerView = view.findViewById(R.id.services_recycler); | ||
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); | ||
mAdapter_ = new UserAdapter(users, getContext()); | ||
|
@@ -71,7 +80,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, | |
filterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
@Override | ||
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | ||
initDataSet((Categories) adapterView.getItemAtPosition(i)); | ||
currentCategory_ = (Categories) adapterView.getItemAtPosition(i); | ||
} | ||
|
||
@Override | ||
|
@@ -80,19 +89,53 @@ public void onNothingSelected(AdapterView<?> adapterView) { | |
} | ||
}); | ||
|
||
EditText keywordsInput = (EditText) view.findViewById(R.id.edittext_services_keywordsinput); | ||
keywordsInput.addTextChangedListener(new TextWatcher() { | ||
|
||
@Override | ||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
|
||
} | ||
|
||
@Override | ||
public void beforeTextChanged(CharSequence s, int start, int count, | ||
int after) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterTextChanged(Editable s) { | ||
|
||
String inKeywords = s.toString(); | ||
Log.i("KEYWORDS_LENGTH", "length = " + s.length()); | ||
keywords_ = new ArrayList<>(Arrays.asList(inKeywords.split(" "))); | ||
Log.i("LIST_LENGTH", "list length = " + keywords_.size()); | ||
keywords_.remove(""); | ||
Log.i("LIST_LENGTH", "list length = " + keywords_.size()); | ||
} | ||
}); | ||
|
||
Button searchButton = (Button) view.findViewById(R.id.button_services_search); | ||
searchButton.setOnClickListener(v ->{ | ||
Log.i("KEYWORDS", "list of keywords is empty ? " + keywords_.isEmpty()); | ||
initDataSet(currentCategory_, keywords_); | ||
}); | ||
|
||
|
||
return view; | ||
} | ||
|
||
private void initDataSet(Categories category) { | ||
private void initDataSet(Categories category, ArrayList<String> keywords) { | ||
View view = getView(); | ||
if (category == Categories.ALL) { | ||
DBUtility.get().getAllUsers((usersdb -> { | ||
users.clear(); | ||
for (User u : usersdb) { | ||
if(! u.getGoogleId_().equals(GoogleSignInSingleton.get().getClientUniqueID())){ | ||
//don't add current user to the list | ||
users.add(u); | ||
if (userContainsKeywords(u, keywords, category)) { | ||
if (!u.getGoogleId_().equals(GoogleSignInSingleton.get().getClientUniqueID())) { | ||
//don't add current user to the list | ||
users.add(u); | ||
} | ||
} | ||
} | ||
Collections.sort(users, this::compareUsersUsingDistanceWithRef); | ||
|
@@ -104,10 +147,10 @@ private void initDataSet(Categories category) { | |
users.clear(); | ||
services_problem_text_udpate(view, googleIds.isEmpty()); | ||
mAdapter_.notifyDataSetChanged(); | ||
|
||
for (String googleId : googleIds) { | ||
DBUtility.get().getUser(googleId, user -> { | ||
if (user != null && !users.contains(user) && !user.getGoogleId_().equals(GoogleSignInSingleton.get().getClientUniqueID())) { | ||
if (user != null && !users.contains(user) && !user.getGoogleId_().equals(GoogleSignInSingleton.get().getClientUniqueID()) && | ||
userContainsKeywords(user, keywords, category)) { | ||
users.add(user); | ||
Collections.sort(users, this::compareUsersUsingDistanceWithRef); | ||
mAdapter_.notifyDataSetChanged(); | ||
|
@@ -129,6 +172,37 @@ private void services_problem_text_udpate(View view, boolean empty) { | |
} | ||
} | ||
|
||
/** | ||
* return true if keywords of the user contains at least one of the list kw | ||
* for the given category | ||
* the list of keywords kw must contains ONLY LOWERCASE words | ||
* @param u the user | ||
* @param kw list of keywords to search for | ||
* @param cat the category for which want to search the keywords | ||
* @return boolean user has or not | ||
*/ | ||
private boolean userContainsKeywords(User u, ArrayList<String> kw, Categories cat){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
ArrayList<String> listForCat; | ||
if(cat.compareTo(Categories.ALL) == 0){ | ||
listForCat = new ArrayList<>(); | ||
for(Categories c : Categories.values()){ | ||
listForCat.addAll(u.getKeyWords(c)); | ||
} | ||
|
||
}else{ | ||
listForCat = u.getKeyWords(cat); | ||
} | ||
if(kw.isEmpty()){ | ||
return true; | ||
} | ||
for (String k : kw){ | ||
if(listForCat.contains(k)){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private int compareUsersUsingDistanceWithRef(User u1, User u2){ | ||
Location ref = LocationManager.get().getCurrentLocation_(); | ||
int result = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identical blocks of code found in 2 locations. Consider refactoring.