ExplorFragment Doc

This commit is contained in:
Sagi Dayan 2016-07-10 17:24:15 +03:00
parent 22bb04b0d2
commit 1e092db5f7

View file

@ -31,21 +31,18 @@ import java.util.List;
/** /**
* A simple {@link Fragment} subclass. * The Explore Fragment - See all public notes
*/ */
public class ExploreFragment extends Fragment { public class ExploreFragment extends Fragment {
private static final String TAG = "[TSN/Explore]"; private static final String TAG = "[TSN/Explore]";
// protected final User user = ((MainActivity) getActivity()).getUser();
protected User user; protected User user;
protected MainActivity parent; protected MainActivity parent;
private ListAdapter noteListAdapter; private ListAdapter noteListAdapter;
private List<Note> notes; private List<Note> notes;
protected ListView list_notes; protected ListView list_notes;
private ImageButton dateFilter; private ImageButton dateFilter;
private ImageButton locationFilter; private ImageButton locationFilter;
// private ImageButton userFilter;
private Button map_small_filter; private Button map_small_filter;
private Button map_medium_filter; private Button map_medium_filter;
private Button map_large_filter; private Button map_large_filter;
@ -59,6 +56,7 @@ public class ExploreFragment extends Fragment {
public ExploreFragment() { public ExploreFragment() {
// Required empty public constructor // Required empty public constructor
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -82,16 +80,11 @@ public class ExploreFragment extends Fragment {
list_notes.setOnItemClickListener(new ItemClickedListener()); list_notes.setOnItemClickListener(new ItemClickedListener());
dateFilterSelection = Utils.MONTH_MILI; dateFilterSelection = Utils.MONTH_MILI;
locationFilterSelection = Utils.DISTANCE_LONG; locationFilterSelection = Utils.DISTANCE_LONG;
dateFilter = (ImageButton) view.findViewById(R.id.explore_date_filter); dateFilter = (ImageButton) view.findViewById(R.id.explore_date_filter);
locationFilter = (ImageButton) view.findViewById(R.id.explore_location_filter); locationFilter = (ImageButton) view.findViewById(R.id.explore_location_filter);
map_small_filter = (Button) view.findViewById(R.id.explore_small_filter); map_small_filter = (Button) view.findViewById(R.id.explore_small_filter);
map_medium_filter = (Button) view.findViewById(R.id.explore_medium_filter); map_medium_filter = (Button) view.findViewById(R.id.explore_medium_filter);
map_large_filter = (Button) view.findViewById(R.id.explore_large_filter); map_large_filter = (Button) view.findViewById(R.id.explore_large_filter);
map_small_filter.setOnClickListener(button1ClickListener); map_small_filter.setOnClickListener(button1ClickListener);
map_medium_filter.setOnClickListener(button2ClickListener); map_medium_filter.setOnClickListener(button2ClickListener);
map_large_filter.setOnClickListener(button3ClickListener); map_large_filter.setOnClickListener(button3ClickListener);
@ -108,8 +101,6 @@ public class ExploreFragment extends Fragment {
exploreFilters.setVisibility(View.VISIBLE); exploreFilters.setVisibility(View.VISIBLE);
dateFilterIsVisible = true; dateFilterIsVisible = true;
locationFilterIsVisible = false; locationFilterIsVisible = false;
// userFilterIsVisible = false;
// set text button in the right filter string // set text button in the right filter string
map_small_filter.setText(R.string.day); map_small_filter.setText(R.string.day);
map_medium_filter.setText(R.string.week); map_medium_filter.setText(R.string.week);
@ -129,8 +120,6 @@ public class ExploreFragment extends Fragment {
exploreFilters.setVisibility(View.VISIBLE); exploreFilters.setVisibility(View.VISIBLE);
locationFilterIsVisible = true; locationFilterIsVisible = true;
dateFilterIsVisible = false; dateFilterIsVisible = false;
// userFilterIsVisible = false;
// set text button in the right filter string // set text button in the right filter string
map_small_filter.setText(R.string.shortDistance); map_small_filter.setText(R.string.shortDistance);
map_medium_filter.setText(R.string.mediumDistance); map_medium_filter.setText(R.string.mediumDistance);
@ -139,10 +128,6 @@ public class ExploreFragment extends Fragment {
setButtonsColor(); setButtonsColor();
} }
}); });
// TODO: choose a default filter for openning explore mode
try { try {
getAllNotes(); getAllNotes();
} catch (JSONException e) { } catch (JSONException e) {
@ -151,7 +136,11 @@ public class ExploreFragment extends Fragment {
return view; return view;
} }
/**
* This function will retrieve all the Public Notes from our Server.
* And will throw JSON exception on error
* @throws JSONException
*/
public void getAllNotes() throws JSONException { public void getAllNotes() throws JSONException {
Utils.showLoadingDialog(parent, "Exploring...", "Finding some new interesting notes just for you"); Utils.showLoadingDialog(parent, "Exploring...", "Finding some new interesting notes just for you");
Log.d(TAG, "url: " + Utils.BASE_URL + "/note/getPublic"); Log.d(TAG, "url: " + Utils.BASE_URL + "/note/getPublic");
@ -161,6 +150,9 @@ public class ExploreFragment extends Fragment {
VolleyUtilSingleton.getInstance(getActivity()).post(url, payload, getNotesSuccessListener, Utils.genericErrorListener); VolleyUtilSingleton.getInstance(getActivity()).post(url, payload, getNotesSuccessListener, Utils.genericErrorListener);
} }
/**
* The Success callback for our HTTP API call
*/
Response.Listener<JSONObject> getNotesSuccessListener = new Response.Listener<JSONObject>() { Response.Listener<JSONObject> getNotesSuccessListener = new Response.Listener<JSONObject>() {
@Override @Override
public void onResponse(JSONObject response) { public void onResponse(JSONObject response) {
@ -176,7 +168,6 @@ public class ExploreFragment extends Fragment {
time.setTime(noteObject.getLong("created_at")); time.setTime(noteObject.getLong("created_at"));
notes.add(Utils.getNoteFromJsonObj(noteObject, time)); notes.add(Utils.getNoteFromJsonObj(noteObject, time));
} }
// list_notes.setAdapter(noteListAdapter);
updateShowedNotes(); updateShowedNotes();
Utils.dismissLoadingDialog(); Utils.dismissLoadingDialog();
} catch (Exception e) { } catch (Exception e) {
@ -193,19 +184,16 @@ public class ExploreFragment extends Fragment {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//create and configure dialog //create and configure dialog
final Note note = notes.get(position); final Note note = notes.get(position);
final MainActivity localParent = (MainActivity)getActivity(); final MainActivity localParent = (MainActivity) getActivity();
final Dialog noteViewDialog = new Dialog(getActivity()); final Dialog noteViewDialog = new Dialog(getActivity());
noteViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); noteViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
noteViewDialog.setContentView(R.layout.note_display_full); noteViewDialog.setContentView(R.layout.note_display_full);
// noteViewDialog.setTitle("You wrote...");
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(noteViewDialog.getWindow().getAttributes()); lp.copyFrom(noteViewDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.MATCH_PARENT;
noteViewDialog.show(); noteViewDialog.show();
// dialog.getWindow().setAttributes(lp);
//get note_view_full layout elements //get note_view_full layout elements
final TextView title = (TextView) noteViewDialog.findViewById(R.id.ndf_title_textview); final TextView title = (TextView) noteViewDialog.findViewById(R.id.ndf_title_textview);
@ -254,7 +242,7 @@ public class ExploreFragment extends Fragment {
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, Utils.genericSuccessListener, Utils.genericErrorListener); VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, Utils.genericSuccessListener, Utils.genericErrorListener);
user.getLiked_notes().add(note.getId()); user.getLiked_notes().add(note.getId());
user.updateUser(localParent); user.updateUser(localParent);
note.setLikes(note.getLikes()+1); note.setLikes(note.getLikes() + 1);
likes.setText("" + note.getLikes()); likes.setText("" + note.getLikes());
noteListAdapter.updateList(notes); noteListAdapter.updateList(notes);
list_notes.setAdapter(noteListAdapter); list_notes.setAdapter(noteListAdapter);
@ -372,6 +360,9 @@ public class ExploreFragment extends Fragment {
} }
} }
/**
* Will update the notes within the list view
*/
public void updateShowedNotes() { public void updateShowedNotes() {
List<Note> presentedNotes = new ArrayList<>(); List<Note> presentedNotes = new ArrayList<>();
long timeDifference; long timeDifference;
@ -383,7 +374,7 @@ public class ExploreFragment extends Fragment {
Date targetDate; Date targetDate;
for (Note note : notes) { for (Note note : notes) {
// get note location and date // get note location and date
targetLocation.setLatitude(note.getLat());//your coords of course targetLocation.setLatitude(note.getLat());//your coords of course
targetLocation.setLongitude(note.getLon()); targetLocation.setLongitude(note.getLon());
targetDate = new Date(note.getTimestamp()); targetDate = new Date(note.getTimestamp());
@ -392,13 +383,11 @@ public class ExploreFragment extends Fragment {
distance = currLocation.distanceTo(targetLocation); distance = currLocation.distanceTo(targetLocation);
//add to currently presented list according to filters. //add to currently presented list according to filters.
if (timeDifference <= dateFilterSelection if (timeDifference <= dateFilterSelection
&& distance <= locationFilterSelection){ && distance <= locationFilterSelection) {
presentedNotes.add(note); presentedNotes.add(note);
} }
} }
noteListAdapter.updateList(presentedNotes); noteListAdapter.updateList(presentedNotes);
list_notes.setAdapter(noteListAdapter); list_notes.setAdapter(noteListAdapter);
} }