Skip to content

Commit

Permalink
User can now click on hobbies to enter a details screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-cab committed Aug 6, 2021
1 parent 61d1a0d commit f4374bc
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 74 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IceBreaker">
<activity android:name=".HobbyDetails"></activity>
<activity android:name=".HobbyDetailsActivity"></activity>
<activity
android:name=".UserPairingActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/com/fbu/icebreaker/HobbyDetails.java

This file was deleted.

67 changes: 67 additions & 0 deletions app/src/main/java/com/fbu/icebreaker/HobbyDetailsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fbu.icebreaker;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fbu.icebreaker.subclasses.Hobby;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import java.util.List;

public class HobbyDetailsActivity extends AppCompatActivity {

private static final String TAG = "HobbyDetailsActivity";

private Hobby hobby;

private ImageView ivHobbyImage;

private TextView tvHobbyName;
private TextView tvHobbyDescription;

private Button btnAddHobby;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hobby_details);

ivHobbyImage = findViewById(R.id.ivHobbyImage);
tvHobbyName = findViewById(R.id.tvHobbyName);
tvHobbyDescription = findViewById(R.id.tvHobbyDescription);
btnAddHobby = findViewById(R.id.btnAddHobby);

hobby = getIntent().getParcelableExtra("hobby");

Glide.with(this)
.load(hobby.getImage())
.into(ivHobbyImage);

tvHobbyName.setText(hobby.getName());

tvHobbyDescription.setText(hobby.getDescription());

btnAddHobby.setOnClickListener(v -> {
hobby.getRelation("usersWithHobby").add(ParseUser.getCurrentUser());
hobby.saveInBackground(e -> {
if (e != null) {
Log.e(TAG, "Error while saving hobby.", e);
Toast.makeText(HobbyDetailsActivity.this, R.string.saving_error, Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Hobby save successful");
});
});
}
}
23 changes: 18 additions & 5 deletions app/src/main/java/com/fbu/icebreaker/adapters/HobbiesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -35,7 +37,7 @@ public class HobbiesAdapter extends RecyclerView.Adapter<HobbiesAdapter.ViewHold
private final List<Hobby> hobbies;

public interface OnClickListener {
void onRemoveClicked(int position);
void onHobbyClicked(int position);
}

public HobbiesAdapter(Context context, List<Hobby> hobbies, OnClickListener clickListener) {
Expand All @@ -62,26 +64,30 @@ public int getItemCount() {
return hobbies.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {

private final TextView tvHobbyName;
private final TextView tvDescription;

private final ImageView ivRemove;
private final ImageView ivHobbyImage;

private final TagView tvTags;

private final RelativeLayout rlHobby;

public ViewHolder(@NonNull View itemView) {
super(itemView);

tvHobbyName = itemView.findViewById(R.id.tvHobbyName);
tvDescription = itemView.findViewById(R.id.tvDescription);

ivRemove = itemView.findViewById(R.id.ivRemove);
ivHobbyImage = itemView.findViewById(R.id.ivHobbyImage);

tvTags = itemView.findViewById(R.id.tvTag);

rlHobby = itemView.findViewById(R.id.rlHobby);

rlHobby.setOnCreateContextMenuListener(this);
}

public void bind(Hobby hobby) {
Expand All @@ -107,7 +113,14 @@ public void bind(Hobby hobby) {
.transform(new RoundedCornersTransformation(radius, margin))
.into(ivHobbyImage);

ivRemove.setOnClickListener(v -> clickListener.onRemoveClicked(getAdapterPosition()));
rlHobby.setOnClickListener(v -> clickListener.onHobbyClicked(getAdapterPosition()));
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(this.getAdapterPosition(), 121, 0, "Delete this item");
}
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fbu.icebreaker.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -11,6 +12,7 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fbu.icebreaker.HobbyDetailsActivity;
import com.fbu.icebreaker.R;
import com.fbu.icebreaker.subclasses.PairingsByTag;

Expand Down Expand Up @@ -64,7 +66,17 @@ public ViewHolder(@NonNull View itemView) {
public void bind(PairingsByTag pairingsByTag) {

tvPairTag.setText(pairingsByTag.getPairedTag());
adapter = new PairedIndividualHobbiesByTagAdapter(context, pairingsByTag.getPairedHobbies());

PairedIndividualHobbiesByTagAdapter.OnClickListener clickListener = new PairedIndividualHobbiesByTagAdapter.OnClickListener() {
@Override
public void onHobbyClicked(int position) {
Intent i = new Intent(context, HobbyDetailsActivity.class);
i.putExtra("hobby", pairingsByTag.getPairedHobbies().get(position));
context.startActivity(i);
}
};

adapter = new PairedIndividualHobbiesByTagAdapter(context, pairingsByTag.getPairedHobbies(), clickListener);

RecyclerView.OnItemTouchListener mScrollTouchListener = new RecyclerView.OnItemTouchListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@

public class PairedIndividualHobbiesByTagAdapter extends RecyclerView.Adapter<PairedIndividualHobbiesByTagAdapter.ViewHolder> {

List<Hobby> pairedHobbies;
private final List<Hobby> pairedHobbies;

Context context;
private final Context context;

public PairedIndividualHobbiesByTagAdapter(Context context, List<Hobby> pairedHobbies) {
private final OnClickListener clickListener;

public interface OnClickListener {
void onHobbyClicked(int position);
}

public PairedIndividualHobbiesByTagAdapter(Context context, List<Hobby> pairedHobbies, OnClickListener clickListener) {
this.context = context;
this.pairedHobbies = pairedHobbies;
this.clickListener = clickListener;
}

@NonNull
Expand Down Expand Up @@ -61,6 +68,11 @@ public void bind(Hobby hobby) {
Random rnd = new Random();
tvHobbyName.setText(hobby.getName());
tvHobbyName.getBackground().setTint(Color.argb(255, rnd.nextInt(250), rnd.nextInt(250), rnd.nextInt(250)));

tvHobbyName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { clickListener.onHobbyClicked(getAdapterPosition()); }
});
}
}
}
61 changes: 31 additions & 30 deletions app/src/main/java/com/fbu/icebreaker/fragments/HobbiesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
Expand All @@ -18,13 +20,16 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.fbu.icebreaker.HobbyDetailsActivity;
import com.fbu.icebreaker.R;
import com.fbu.icebreaker.adapters.HobbiesAdapter;
import com.fbu.icebreaker.subclasses.Hobby;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -60,40 +65,18 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
final RecyclerView rvHobbies = view.findViewById(R.id.rvHobbies);
final FloatingActionButton btnAddHobby = view.findViewById(R.id.btnAddHobby);

HobbiesAdapter.OnClickListener onClickListener = position -> {
Log.i(TAG, "Click at post position: " + position);

DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:

allHobbies.get(position).getRelation("usersWithHobby").remove(ParseUser.getCurrentUser());
allHobbies.get(position).saveInBackground(e -> {
if (e != null) {
Log.e(TAG, "Error while removing hobby", e);
Toast.makeText(getContext(), getString(R.string.removing_hobby_error), Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Hobby removal successful");
});

queryHobbiesUpdate();
break;
case DialogInterface.BUTTON_NEGATIVE:
// do nothing
break;
}
};

AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
ab.setMessage(getString(R.string.remove_hobby_question))
.setPositiveButton(R.string.remove, dialogClickListener)
.setNegativeButton(R.string.Cancel, dialogClickListener)
.show();
HobbiesAdapter.OnClickListener clickListener = new HobbiesAdapter.OnClickListener() {
@Override
public void onHobbyClicked(int position) {
Intent i = new Intent(getContext(), HobbyDetailsActivity.class);
i.putExtra("hobby", allHobbies.get(position));
startActivity(i);
}
};

// Initialize the array
allHobbies = new ArrayList<>();
adapter = new HobbiesAdapter(getContext(), allHobbies, onClickListener);
adapter = new HobbiesAdapter(getContext(), allHobbies, clickListener);

// Lookup the swipe container view
swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
Expand Down Expand Up @@ -172,4 +155,22 @@ private void queryHobbiesUpdate() {
adapter.notifyDataSetChanged();
});
}

@Override
public boolean onContextItemSelected(@NonNull @NotNull MenuItem item) {

if (item.getItemId() == 121) {
allHobbies.get(item.getGroupId()).getRelation("usersWithHobby").remove(ParseUser.getCurrentUser());
allHobbies.get(item.getGroupId()).saveInBackground(e -> {
if (e != null) {
Log.e(TAG, "Error while removing hobby", e);
Toast.makeText(getContext(), getString(R.string.removing_hobby_error), Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "Hobby removal successful");
queryHobbiesUpdate();
});

}
return super.onContextItemSelected(item);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.fbu.icebreaker.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
Expand All @@ -15,6 +18,7 @@
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fbu.icebreaker.HobbyDetailsActivity;
import com.fbu.icebreaker.R;
import com.fbu.icebreaker.adapters.HobbiesAdapter;
import com.fbu.icebreaker.subclasses.Hobby;
Expand Down Expand Up @@ -82,9 +86,18 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
userHobbies = getArguments().getParcelableArrayList("userHobbies");
qrHobbies = getArguments().getParcelableArrayList("qrHobbies");

HobbiesAdapter.OnClickListener onClickListener = position -> Toast.makeText(getContext(), R.string.sorry_delete_hobby_screen, Toast.LENGTH_SHORT).show();
allHobbies.addAll(hobbyMethods.getEqualHobbies(qrHobbies, userHobbies));

HobbiesAdapter.OnClickListener clickListener = new HobbiesAdapter.OnClickListener() {
@Override
public void onHobbyClicked(int position) {
Intent i = new Intent(getContext(), HobbyDetailsActivity.class);
i.putExtra("hobby", allHobbies.get(position));
startActivity(i);
}
};

adapter = new HobbiesAdapter(getContext(), allHobbies, onClickListener);
adapter = new HobbiesAdapter(getContext(), allHobbies, clickListener);
rvPairedHobbies.setAdapter(adapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rvPairedHobbies.setLayoutManager(linearLayoutManager);
Expand All @@ -101,8 +114,16 @@ public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.
.load(Objects.requireNonNull(user.getParseFile("profilePicture")).getUrl())
.into(ivProfilePicture);

allHobbies.addAll(hobbyMethods.getEqualHobbies(qrHobbies, userHobbies));

adapter.notifyDataSetChanged();
}

@Override
public boolean onContextItemSelected(@NonNull @NotNull MenuItem item) {

if (item.getItemId() == 121) {
Toast.makeText(getContext(), R.string.removing_hobby_error, Toast.LENGTH_SHORT).show();
}
return super.onContextItemSelected(item);
}
}
Loading

0 comments on commit f4374bc

Please sign in to comment.