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