From 58519d243104bf4e9fd93982883222694e59500d Mon Sep 17 00:00:00 2001 From: Matan Bar Yosef Date: Sun, 10 Jul 2016 16:14:35 +0300 Subject: [PATCH] - likes on explore implemented - likes on explore update the list as well --- .../thesocialnotework/ExploreFragment.java | 86 +++++++++---------- .../sagi/thesocialnotework/GmapFragment.java | 13 +-- .../ara/sagi/thesocialnotework/Utils.java | 12 ++- 3 files changed, 48 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ExploreFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ExploreFragment.java index 5a7bd99..ce8f689 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ExploreFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ExploreFragment.java @@ -43,6 +43,7 @@ import java.util.ListIterator; public class ExploreFragment extends Fragment { private static final String TAG = "[TSN/Explore]"; +// protected final User user = ((MainActivity) getActivity()).getUser(); protected User user; protected MainActivity parent; private ListAdapter noteListAdapter; @@ -76,7 +77,14 @@ public class ExploreFragment extends Fragment { public ExploreFragment() { // Required empty public constructor } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + parent = (MainActivity) getActivity(); + gpsUtils = parent.getGPSUtils(); + user = parent.getUser(); + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -84,9 +92,7 @@ public class ExploreFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_explore, container, false); - parent = (MainActivity) getActivity(); - gpsUtils = parent.getGPSUtils(); - user = parent.getUser(); + notes = new ArrayList<>(); //Get Views list_notes = (ListView) view.findViewById(R.id.list_notes); @@ -205,6 +211,7 @@ public class ExploreFragment extends Fragment { public void onItemClick(AdapterView parent, View view, final int position, long id) { //create and configure dialog final Note note = notes.get(position); + final MainActivity localParent = (MainActivity)getActivity(); final Dialog noteViewDialog = new Dialog(getActivity()); noteViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); noteViewDialog.setContentView(R.layout.note_display_full); @@ -235,57 +242,42 @@ public class ExploreFragment extends Fragment { date.setText(note.getDate()); time.setText(note.getTime()); location.setText(note.getAddress()); - if (likes != null) likes.setText("" + note.getLikes()); - likeBtn.setBackgroundResource(R.drawable.like_icon); -// tags.setText("Tags: "+ note.getTags().toString()); -// permission.setText("" + (note.isPublic() ? "Public" : "Private")); + + if (likes != null) { + likeBtn.setBackgroundResource(R.drawable.like_icon); + if (localParent.getUser().getLiked_notes().contains(note.getId())) { + likeBtn.setBackgroundResource(R.drawable.like_icon); + } else { + likeBtn.setBackgroundResource(R.drawable.unlike_icon); + } + } + likes.setText("" + note.getLikes()); + permission.setVisibility(View.GONE); Utils.URLtoImageView(avatar, note.getAvatar()); permission_image.setVisibility(View.GONE); likeBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { -// //Put up the Yes/No message box -// AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); -// builder -// .setTitle("Delete Note") -// .setMessage("Are you sure you want to delete the note?") -// .setIcon(android.R.drawable.ic_dialog_alert) -// .setPositiveButton("Yes", new DialogInterface.OnClickListener() { -// public void onClick(DialogInterface dialog, int which) { -// //Yes button clicked, do something -// Toast.makeText(getActivity(), "Item Deleted!", -// Toast.LENGTH_SHORT).show(); -// //TODO send delete -// JSONObject delNote = new JSONObject(); -// try { -// delNote.put("uid", userId); -// delNote.put("nid", note.getId()); -// VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, Utils.deleteNoteSuccessListener, Utils.genericErrorListener); -// listOfNotes.remove(presentedNotes.get(position)); -// presentedNotes.remove(position); -// -// } catch (JSONException e) { -// Toast.makeText(getActivity(), "Something went wrong.\n Failed to delete note...", Toast.LENGTH_LONG).show(); -// e.printStackTrace(); -// } -// updateShowedNotes(); -//// noteList.setAdapter(noteListAdapter); -// noteViewDialog.dismiss(); -// } -// }) -// .setNegativeButton("No", new DialogInterface.OnClickListener() { -// public void onClick(DialogInterface dialog, int which) { -// //Yes button clicked, do something -// Toast.makeText(getActivity(), "Canceled", -// Toast.LENGTH_SHORT).show(); -// noteViewDialog.dismiss(); -// } -// }) //Do nothing on no -// .show(); -// } -// }); + //add like only if user didnt like already + if (!user.getLiked_notes().contains(note.getId())) { + JSONObject jsonObj = new JSONObject(); + try { + jsonObj.put("uid", localParent.getUserId()); + jsonObj.put("nid", note.getId()); + } catch (JSONException e) { + e.printStackTrace(); + } + VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, Utils.genericSuccessListener, Utils.genericErrorListener); + user.getLiked_notes().add(note.getId()); + user.updateUser(localParent); + note.setLikes(note.getLikes()+1); + likes.setText("" + note.getLikes()); + noteListAdapter.updateList(notes); + list_notes.setAdapter(noteListAdapter); + likeBtn.setBackgroundResource(R.drawable.like_icon); + } } }); } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java index b216eb2..ee45c08 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java @@ -338,7 +338,6 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { permissionImg.setVisibility(View.INVISIBLE); permission.setText(""); deleteBtn.setBackgroundResource(R.drawable.unlike_icon); - int filterColor; if (mainActivity.getUser().getLiked_notes().contains(note.getId())) { deleteBtn.setBackgroundResource(R.drawable.like_icon); } else { @@ -404,7 +403,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { } catch (JSONException e) { e.printStackTrace(); } - VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, likeNotesSuccessListener, Utils.genericErrorListener); + VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, Utils.genericSuccessListener, Utils.genericErrorListener); mainActivity.getUser().getLiked_notes().add(note.getId()); mainActivity.getUser().updateUser(mainActivity); note.setLikes(note.getLikes()+1); @@ -450,16 +449,6 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { } }; - //response listener for getting all user notes - Response.Listener likeNotesSuccessListener = new Response.Listener() { - @Override - public void onResponse(JSONObject response) { - Log.d(TAG, "likeNotesSuccessListener: " + response.toString()); - } - }; - - - private class getMarkersFromNotes extends AsyncTask, MarkerNoteStruct, Void> { GoogleMap mMap; HashMap eventMarkerMap; diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java index 8b2de97..5b6f234 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java @@ -96,6 +96,14 @@ public class Utils { } }; + //response listener for getting all user notes + public static Response.Listener genericSuccessListener = new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + Log.d(TAG, "likeNotesSuccessListener: " + response.toString()); + } + }; + public static void showLoadingDialog(Context context, String title, String msg) { progress = new ProgressDialog(context); progress.setTitle(title); @@ -240,8 +248,4 @@ public class Utils { editor.remove("UserData"); editor.commit(); } - - - - }