Skip to content

Commit

Permalink
Extracted hobby pairing logic into a class, and prepared Icebreak fra…
Browse files Browse the repository at this point in the history
…gment for user search
  • Loading branch information
jorge-cab committed Aug 2, 2021
1 parent f3b474f commit 585e955
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.fbu.icebreaker.R;
import com.fbu.icebreaker.adapters.HobbyPairingAdapter;
import com.fbu.icebreaker.hobbyPairingLogic.HobbyMethods;
import com.fbu.icebreaker.subclasses.Hobby;
import com.fbu.icebreaker.subclasses.PairingsByTag;

Expand All @@ -32,6 +33,8 @@ public class HobbyPairingsByTagFragment extends Fragment {

private HobbyPairingAdapter adapter;

private HobbyMethods hobbyMethods;

private List<Hobby> qrHobbies;
private List<Hobby> userHobbies;

Expand Down Expand Up @@ -60,6 +63,7 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
rvHobbyPairings.setHasFixedSize(true);
final GridLayoutManager layout = new GridLayoutManager(getContext(), 2);

hobbyMethods = new HobbyMethods();
userHobbies = new ArrayList<>();
qrHobbies = new ArrayList<>();
pairingsByTag = new ArrayList<>();
Expand All @@ -75,72 +79,8 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
userHobbies = getArguments().getParcelableArrayList("userHobbies");
qrHobbies = getArguments().getParcelableArrayList("qrHobbies");

getEqualHobbies();

Hashtable<String, List<Hobby>> userHobbiesPerTag = new Hashtable<>();

for (Hobby hobby : userHobbies) {
if (!setAlreadyPairedHobbies.contains(hobby)) {
for(String tag : hobby.getTags()) {
List<Hobby> hobbyListTag;

if (userHobbiesPerTag.get(tag) == null) {
hobbyListTag = new ArrayList<>();
} else {
hobbyListTag = userHobbiesPerTag.get(tag);
}
hobbyListTag.add(hobby);
userHobbiesPerTag.put(tag, hobbyListTag);
}
}
}

Hashtable<String, List<Hobby>> qrHobbiesPerTag = new Hashtable<>();

for (Hobby hobby : qrHobbies) {
if (!setAlreadyPairedHobbies.contains(hobby)) {
for (String tag : hobby.getTags()) {
List<Hobby> qrHobbyListTag;

if (userHobbiesPerTag.get(tag) != null) {
if (qrHobbiesPerTag.get(tag) == null) {
qrHobbyListTag = new ArrayList<>();
} else {
qrHobbyListTag = qrHobbiesPerTag.get(tag);
}
qrHobbyListTag.add(hobby);
qrHobbiesPerTag.put(tag, qrHobbyListTag);
}
}
}
}

for (String tag : qrHobbiesPerTag.keySet()) {

PairingsByTag currPairing = new PairingsByTag();

qrHobbiesPerTag.get(tag).addAll(userHobbiesPerTag.get(tag));

currPairing.setPairedTag(tag);
currPairing.setPairedHobbies(qrHobbiesPerTag.get(tag));
pairingsByTag.add(currPairing);
}
pairingsByTag.addAll(hobbyMethods.getSimilarHobbiesByTag(userHobbies, qrHobbies));

adapter.notifyDataSetChanged();
}

private void getEqualHobbies() {
// Use set so that .contains is an O(1) operation and keep a time complexity of O(n)
// Considering the creation of the set O(2n)
Set<String> setQr = new HashSet<>();

for (Hobby hobby : qrHobbies) {
setQr.add(hobby.getObjectId());
}

for (Hobby hobby : userHobbies) {
if (setQr.contains(hobby.getObjectId()))
setAlreadyPairedHobbies.add(hobby);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.bumptech.glide.Glide;
import com.fbu.icebreaker.adapters.HobbiesAdapter;
import com.fbu.icebreaker.R;
import com.fbu.icebreaker.hobbyPairingLogic.HobbyMethods;
import com.fbu.icebreaker.subclasses.Hobby;
import com.parse.ParseUser;

Expand All @@ -35,11 +36,16 @@ public class PairedProfileFragment extends Fragment {

private final static String TAG = "PairedProfileFragment";

private HobbyMethods hobbyMethods;

private ImageView ivProfilePicture;
private TextView tvUsername;

private TextView tvHobbiesNumber;
private TextView tvBio;

private ParseUser user;

private List<Hobby> userHobbies;
private List<Hobby> qrHobbies;
private List<Hobby> allHobbies;
Expand All @@ -63,6 +69,7 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
super.onViewCreated(view, savedInstanceState);

ivProfilePicture = view.findViewById(R.id.ivProfilePicture);
rvPairedHobbies = view.findViewById(R.id.rvPairedHobbies);
tvUsername = view.findViewById(R.id.tvUsername);
tvBio = view.findViewById(R.id.tvBio);
tvHobbiesNumber = view.findViewById(R.id.tvHobbiesNumber);
Expand All @@ -71,17 +78,18 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
userHobbies = new ArrayList<>();
qrHobbies = new ArrayList<>();

hobbyMethods = new HobbyMethods();

assert getArguments() != null;
userHobbies = getArguments().getParcelableArrayList("userHobbies");
qrHobbies = getArguments().getParcelableArrayList("qrHobbies");

rvPairedHobbies = view.findViewById(R.id.rvPairedHobbies);
adapter = new HobbiesAdapter(getContext(), allHobbies, null);
rvPairedHobbies.setAdapter(adapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rvPairedHobbies.setLayoutManager(linearLayoutManager);

tvHobbiesNumber.setText(qrHobbies.size());
tvHobbiesNumber.setText(String.valueOf(qrHobbies.size()));

assert getArguments() != null;
user = getArguments().getParcelable("user");
Expand All @@ -93,22 +101,8 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
.load(Objects.requireNonNull(user.getParseFile("profilePicture")).getUrl())
.into(ivProfilePicture);

getEqualHobbies();
}

private void getEqualHobbies() {
// Use set so that .contains is an O(1) operation and keep a time complexity of O(n)
// Considering the creation of the set O(2n)
Set<String> setQr = new HashSet<>();

for (Hobby hobby : qrHobbies) {
setQr.add(hobby.getObjectId());
}
allHobbies.addAll(hobbyMethods.getEqualHobbies(qrHobbies, userHobbies));

for (Hobby hobby : userHobbies) {
if (setQr.contains(hobby.getObjectId()))
allHobbies.add(hobby);
}
adapter.notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.fbu.icebreaker.hobbyPairingLogic;

import com.fbu.icebreaker.subclasses.Hobby;
import com.fbu.icebreaker.subclasses.PairingsByTag;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

public class HobbyMethods {

public List<Hobby> getEqualHobbies(List<Hobby> hobbies1, List<Hobby> hobbies2) {
Set<String> setQr = new HashSet<>();
List<Hobby> equalHobbies = new ArrayList<>();

for (Hobby hobby : hobbies1) {
setQr.add(hobby.getObjectId());
}

for (Hobby hobby : hobbies2) {
if (setQr.contains(hobby.getObjectId())) {
equalHobbies.add(hobby);
}
}
return equalHobbies;
}

public List<PairingsByTag> getSimilarHobbiesByTag(List<Hobby> hobbies1, List<Hobby> hobbies2) {
List<PairingsByTag> pairingsByTags = new ArrayList<>();
List<Hobby> equalHobbies = getEqualHobbies(hobbies1, hobbies2);
Hashtable<String, List<Hobby>> userHobbiesPerTag = new Hashtable<>();
Hashtable<String, List<Hobby>> qrHobbiesPerTag = new Hashtable<>();
Set<Hobby> setAlreadyPairedHobbies = new HashSet<>();

for (Hobby hobby : equalHobbies) {
setAlreadyPairedHobbies.add(hobby);
}

for (Hobby hobby : hobbies1) {
if (!setAlreadyPairedHobbies.contains(hobby)) {
for(String tag : hobby.getTags()) {
List<Hobby> hobbyListTag;

if (userHobbiesPerTag.get(tag) == null) {
hobbyListTag = new ArrayList<>();
} else {
hobbyListTag = userHobbiesPerTag.get(tag);
}
hobbyListTag.add(hobby);
userHobbiesPerTag.put(tag, hobbyListTag);
}
}
}

for (Hobby hobby : hobbies2) {
if (!setAlreadyPairedHobbies.contains(hobby)) {
for (String tag : hobby.getTags()) {
List<Hobby> qrHobbyListTag;

if (userHobbiesPerTag.get(tag) != null) {
if (qrHobbiesPerTag.get(tag) == null) {
qrHobbyListTag = new ArrayList<>();
} else {
qrHobbyListTag = qrHobbiesPerTag.get(tag);
}
qrHobbyListTag.add(hobby);
qrHobbiesPerTag.put(tag, qrHobbyListTag);
}
}
}
}

for (String tag : qrHobbiesPerTag.keySet()) {

PairingsByTag currPairing = new PairingsByTag();

qrHobbiesPerTag.get(tag).addAll(userHobbiesPerTag.get(tag));

currPairing.setPairedTag(tag);
currPairing.setPairedHobbies(qrHobbiesPerTag.get(tag));
pairingsByTags.add(currPairing);
}

return pairingsByTags;
}
}
20 changes: 16 additions & 4 deletions app/src/main/res/layout/fragment_icebreak.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@
tools:context=".fragments.IcebreakFragment"
android:background="@color/off_white">

<EditText
android:id="@+id/etSearchUser"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="40dp"
android:background="@drawable/custom_input"
android:hint="@string/search_user"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="14sp" />

<com.ortiz.touchview.TouchImageView
android:id="@+id/ivQRCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_width="420dp"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
tools:src="@tools:sample/avatars" />


<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnCamera"
android:layout_width="wrap_content"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<string name="edit_profile">Edit Profile</string>
<string name="logout">Logout</string>
<string name="hobbies_in_common">Hobbies in common!</string>
<string name="hobbies">Hobbies</string>
<string name="similar_hobbies_per_tag">Similar hobbies per tag.</string>
<string name="change_profile_photo">Change Profile Photo</string>
<string name="submit_changes">Submit Changes</string>
Expand All @@ -63,9 +62,9 @@
<string name="welcome_to_icebreaker">Welcome to Icebreaker!</string>
<string name="log_in_to_start_breaking_the_ice">Log in to start breaking the ice!</string>
<string name="password">Password</string>
<string name="username">Username</string>
<string name="continue_with_facebook">Continue with Facebook</string>
<string name="log_in">Log In</string>
<string name="or">Or</string>
<string name="register">Register</string>
<string name="search_user">Search User</string>
</resources>

0 comments on commit 585e955

Please sign in to comment.