added filters to personal space

This commit is contained in:
Aran Zaiger 2016-07-09 21:23:55 +03:00
parent 86352c8669
commit 5b33ee7bf5
2 changed files with 440 additions and 231 deletions

View file

@ -7,6 +7,8 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color;
import android.location.Location;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -35,6 +37,7 @@ import android.widget.Toast;
import com.android.volley.Response; import com.android.volley.Response;
import com.android.volley.VolleyError; import com.android.volley.VolleyError;
import com.google.android.gms.maps.model.LatLng;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -55,12 +58,14 @@ public class PersonalFragment extends Fragment {
protected ListView noteList; protected ListView noteList;
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
private GPSUtils gpsUtils; private GPSUtils gpsUtils;
private List<Note> listOfNotes; private List<Note> listOfNotes, presentedNotes;
private ListAdapter noteListAdapter; private ListAdapter noteListAdapter;
private String userId; private String userId;
private final String TAG = "[TSN/PersonalFragment]"; private final String TAG = "[TSN/PersonalFragment]";
private MainActivity activity; private MainActivity activity;
private final int FINE_PERM = 0, CAMERA_PERM = 1; private final int FINE_PERM = 0, CAMERA_PERM = 1;
private int userFilterSelection;
private Long dateFilterSelection;
private ImageButton dateFilter; private ImageButton dateFilter;
@ -92,6 +97,9 @@ public class PersonalFragment extends Fragment {
Bundle bundle = getArguments(); Bundle bundle = getArguments();
this.userId = activity.getUserId(); this.userId = activity.getUserId();
Log.d(TAG, "onCreateView: userID: " + userId); Log.d(TAG, "onCreateView: userID: " + userId);
dateFilterSelection = 2592000000L;
userFilterSelection = 3;
ActivityCompat.requestPermissions(activity, new String[]{ ActivityCompat.requestPermissions(activity, new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION,
@ -102,18 +110,13 @@ public class PersonalFragment extends Fragment {
); );
// ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM);
// if (!Utils.arePermissionsGranted()) {
// Toast.makeText(getContext(), "Missing some Permissions...\nPlease go to app info and enable them", Toast.LENGTH_LONG).show();
// }
this.noteList = (ListView) view.findViewById(R.id.ps_list_listview); this.noteList = (ListView) view.findViewById(R.id.ps_list_listview);
gpsUtils = activity.getGPSUtils(); gpsUtils = activity.getGPSUtils();
gpsUtils.getLocation(); gpsUtils.getLocation();
listOfNotes = new ArrayList<>(); listOfNotes = new ArrayList<>();
noteListAdapter = new ListAdapter(getContext(), listOfNotes); presentedNotes = new ArrayList<>();
noteListAdapter = new ListAdapter(getContext(), presentedNotes);
noteList.setAdapter(noteListAdapter); noteList.setAdapter(noteListAdapter);
noteList.setOnItemClickListener(new ItemClickedListener()); noteList.setOnItemClickListener(new ItemClickedListener());
Utils.showLoadingDialog(getActivity(), "Fetching..", "getting your notes"); Utils.showLoadingDialog(getActivity(), "Fetching..", "getting your notes");
@ -123,13 +126,20 @@ public class PersonalFragment extends Fragment {
map_small_filter = (Button) view.findViewById(R.id.personalSpace_small_filter); map_small_filter = (Button) view.findViewById(R.id.personalSpace_small_filter);
map_medium_filter = (Button) view.findViewById(R.id.personalSpace_medium_filter); map_medium_filter = (Button) view.findViewById(R.id.personalSpace_medium_filter);
Log.d(TAG, "onCreateView: personalSpace_filter_options = "+R.id.personalSpace_filter_options);
map_large_filter = (Button) view.findViewById(R.id.personalSpace_large_filter); map_large_filter = (Button) view.findViewById(R.id.personalSpace_large_filter);
map_small_filter.setOnClickListener(button1ClickListener);
map_medium_filter.setOnClickListener(button2ClickListener);
map_large_filter.setOnClickListener(button3ClickListener);
personalSpaceFilters = (LinearLayout) view.findViewById(R.id.personalSpace_filter_options); personalSpaceFilters = (LinearLayout) view.findViewById(R.id.personalSpace_filter_options);
dateFilter.setOnClickListener(new View.OnClickListener() { dateFilter.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(final View v) {
Log.d(TAG, "onClick: dateFilter pressed");
if (dateFilterIsVisible) { if (dateFilterIsVisible) {
dateFilterIsVisible = false; dateFilterIsVisible = false;
personalSpaceFilters.setVisibility(View.INVISIBLE); personalSpaceFilters.setVisibility(View.INVISIBLE);
@ -149,6 +159,8 @@ public class PersonalFragment extends Fragment {
userFilter.setOnClickListener(new View.OnClickListener() { userFilter.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Log.d(TAG, "onClick: userFilter pressed");
//if pressed same filter twice - close filters.
if (userFilterIsVisible) { if (userFilterIsVisible) {
userFilterIsVisible = false; userFilterIsVisible = false;
personalSpaceFilters.setVisibility(View.INVISIBLE); personalSpaceFilters.setVisibility(View.INVISIBLE);
@ -166,7 +178,9 @@ public class PersonalFragment extends Fragment {
}); });
// get all notes according to some default filter ? // TODO: Aran? // get all notes according to some default filter ? // TODO: Aran?
getAllNotes(); VolleyUtilSingleton.getInstance(getActivity()).get(BASE_URL + "/note/all?uid=" + userId, getNotesSuccessListener, Utils.genericErrorListener);
// getAllNotes();
//https://thesocialnotework-api.appspot.com/api/note/all?uid=<USER_ID> //https://thesocialnotework-api.appspot.com/api/note/all?uid=<USER_ID>
@ -191,10 +205,7 @@ public class PersonalFragment extends Fragment {
Utils.dismissLoadingDialog(); Utils.dismissLoadingDialog();
} }
public void getAllNotes() {
Log.d(TAG, "url: " + BASE_URL + "/note/all?uid=" + userId);
VolleyUtilSingleton.getInstance(getActivity()).get(BASE_URL + "/note/all?uid=" + userId, getNotesSuccessListener, Utils.genericErrorListener);
}
private View.OnClickListener addNewNoteDialog = new View.OnClickListener() { private View.OnClickListener addNewNoteDialog = new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
@ -291,7 +302,10 @@ public class PersonalFragment extends Fragment {
JSONObject noteObject = response.getJSONObject("note"); JSONObject noteObject = response.getJSONObject("note");
time.setTime(noteObject.getLong("created_at")); time.setTime(noteObject.getLong("created_at"));
addNoteFromJsonObj(noteObject, time); addNoteFromJsonObj(noteObject, time);
noteList.setAdapter(noteListAdapter); updateShowedNotes();
// presentedNotes = listOfNotes;
// noteList.setAdapter(noteListAdapter);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage()); Log.e(TAG, "newNoteSuccess:" + e.getMessage());
} }
@ -326,7 +340,9 @@ public class PersonalFragment extends Fragment {
addNoteFromJsonObj(noteObject, time); addNoteFromJsonObj(noteObject, time);
} }
noteList.setAdapter(noteListAdapter); updateShowedNotes();
// presentedNotes = listOfNotes;
// noteList.setAdapter(noteListAdapter);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage()); Log.e(TAG, "newNoteSuccess:" + e.getMessage());
} }
@ -393,7 +409,7 @@ public class PersonalFragment extends Fragment {
@Override @Override
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 = listOfNotes.get(position); final Note note = presentedNotes.get(position);
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);
@ -449,13 +465,15 @@ public class PersonalFragment extends Fragment {
delNote.put("uid", userId); delNote.put("uid", userId);
delNote.put("nid", note.getId()); delNote.put("nid", note.getId());
VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, Utils.deleteNoteSuccessListener, Utils.genericErrorListener); VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, Utils.deleteNoteSuccessListener, Utils.genericErrorListener);
listOfNotes.remove(position); listOfNotes.remove(presentedNotes.get(position));
presentedNotes.remove(position);
} catch (JSONException e) { } catch (JSONException e) {
Toast.makeText(getActivity(), "Something went wrong.\n Failed to delete note...", Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), "Something went wrong.\n Failed to delete note...", Toast.LENGTH_LONG).show();
e.printStackTrace(); e.printStackTrace();
} }
noteList.setAdapter(noteListAdapter); updateShowedNotes();
// noteList.setAdapter(noteListAdapter);
noteViewDialog.dismiss(); noteViewDialog.dismiss();
} }
}) })
@ -476,4 +494,194 @@ public class PersonalFragment extends Fragment {
} }
//all buttons listener
public View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
Log.d(TAG, "onClick: start");
Log.d(TAG, "onClick: v id: "+ v.getId());
Log.d(TAG, "onClick: map_small_filter id: "+ R.id.map_small_filter);
Log.d(TAG, "onCreateView: personalSpace_filter_options = "+R.id.personalSpace_filter_options);
Log.d(TAG, "onClick: are equal? "+ (v.getId() ==R.id.map_small_filter));
Log.d(TAG, "onClick: case map_small_filter");
//user filters
if (userFilterIsVisible) {
userFilterSelection = 1;
}
//date filters
else {
dateFilterSelection = 86400000L;
}
updateShowedNotes();
}
};
//all buttons listener
public View.OnClickListener button2ClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
Log.d(TAG, "onClick: start");
Log.d(TAG, "onClick: v id: " + v.getId());
Log.d(TAG, "onClick: map_small_filter id: " + R.id.map_small_filter);
Log.d(TAG, "onCreateView: personalSpace_filter_options = " + R.id.personalSpace_filter_options);
Log.d(TAG, "onClick: are equal? " + (v.getId() == R.id.map_small_filter));
Log.d(TAG, "onClick: case map_medium_filter");
v.setBackgroundColor(Color.BLUE);
//user filters
if (userFilterIsVisible) {
userFilterSelection = 2;
}
//date filters
else {
dateFilterSelection = 604800000L;
}
updateShowedNotes();
}
};
//all buttons listener
public View.OnClickListener button3ClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
Log.d(TAG, "onClick: start");
Log.d(TAG, "onClick: v id: "+ v.getId());
Log.d(TAG, "onClick: map_small_filter id: "+ R.id.map_small_filter);
Log.d(TAG, "onCreateView: personalSpace_filter_options = "+R.id.personalSpace_filter_options);
Log.d(TAG, "onClick: are equal? "+ (v.getId() ==R.id.map_small_filter));
Log.d(TAG, "onClick: case map_large_filter");
//user filters
if (userFilterIsVisible) {
userFilterSelection = 3;
}
//date filters
else {
dateFilterSelection = 2592000000L;
}
updateShowedNotes();
}
};
// //all buttons listener
// public View.OnClickListener buttonClickListener = new View.OnClickListener() {
// @Override
// public void onClick(final View v) {
//
// Log.d(TAG, "onClick: start");
// Log.d(TAG, "onClick: v id: "+ v.getId());
// Log.d(TAG, "onClick: map_small_filter id: "+ R.id.map_small_filter);
// Log.d(TAG, "onCreateView: personalSpace_filter_options = "+R.id.personalSpace_filter_options);
//
// Log.d(TAG, "onClick: are equal? "+ (v.getId() ==R.id.map_small_filter));
//
// switch (v.getId()) {
// case R.id.map_small_filter:
// Log.d(TAG, "onClick: case map_small_filter");
// //user filters
// if (userFilterIsVisible) {
// userFilterSelection = 1;
// }
// //date filters
// else {
// dateFilterSelection = 86400000L;
//
// }
// updateShowedNotes();
// break;
// case R.id.map_medium_filter:
// Log.d(TAG, "onClick: case map_medium_filter");
//
// //user filters
// if (userFilterIsVisible) {
// userFilterSelection = 2;
//
// }
// //date filters
// else {
// dateFilterSelection = 604800000L;
//
// }
// updateShowedNotes();
// break;
// case R.id.map_large_filter:
// Log.d(TAG, "onClick: case map_large_filter");
//
// //user filters
// if (userFilterIsVisible) {
// userFilterSelection = 3;
// }
// //date filters
// else {
// dateFilterSelection = 2592000000L;
//
// }
// updateShowedNotes();
// break;
// default:
// Log.d(TAG, "onClick: in default");
// break;
// }
// }
// };
public void updateShowedNotes() {
presentedNotes = new ArrayList<>();
long timeDifference;
// float distance;
Log.d(TAG, "updateShowedNotes: start");
Log.d(TAG, "updateShowedNotes: userPrefFilter = "+userFilterSelection);
Log.d(TAG, "updateShowedNotes: timeDifferencePerf = "+dateFilterSelection);
Log.d(TAG, "updateShowedNotes: +++++++++++++++++++++++++++++++++++++++++++++++++");
// Location currLocation = new Location(gpsUtils.getLocation());
Date now = new Date();
// Location targetLocation = new Location("");//provider name is unecessary
Date targetDate;
for (Note note : listOfNotes){
Log.d(TAG, "updateShowedNotes: chekcing note with title: "+ note.title);
//get note location and date
// targetLocation.setLatitude(note.getLat());//your coords of course
// targetLocation.setLongitude(note.getLon());
targetDate = new Date(note.getTimestamp());
//get time and date differences
timeDifference = now.getTime() - targetDate.getTime();
// distance = currLocation.distanceTo(targetLocation);
Log.d(TAG, "updateShowedNotes: time difference = "+ timeDifference);
//add to currently presented list according to filters.
if(timeDifference <= dateFilterSelection
&& ((!note.isPublic && userFilterSelection==1) || (note.isPublic && userFilterSelection == 2) || (userFilterSelection == 3))){
presentedNotes.add(note);
}
Log.d(TAG, "updateShowedNotes: ======================================");
}
Log.d(TAG, "updateShowedNotes: presentedNotes size = "+ presentedNotes.size());
noteListAdapter.updateList(presentedNotes);
noteList.setAdapter(noteListAdapter);
}
} }

View file

@ -9,71 +9,71 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:orientation="vertical" android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android_app.matan.ara.sagi.thesocialnotework.PersonalSpaceActivity" tools:context="com.android_app.matan.ara.sagi.thesocialnotework.PersonalSpaceActivity">
android:layout_marginBottom="5dp">
<LinearLayout <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:contextClickable="false" > android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contextClickable="false"
android:orientation="horizontal">
<ImageButton <ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/personalSpace_date_filter" android:id="@+id/personalSpace_date_filter"
android:layout_weight="1"
android:src="@drawable/date_icon"
android:scaleType="fitCenter" />
<ImageButton
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="50dp" android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/date_icon" />
<ImageButton
android:id="@+id/personalSpace_premission_filter" android:id="@+id/personalSpace_premission_filter"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1" android:layout_weight="1"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/permission_icon" /> android:src="@drawable/permission_icon" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:orientation="horizontal" android:id="@+id/personalSpace_filter_options"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:contextClickable="false"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:visibility="invisible" android:contextClickable="false"
android:id="@+id/personalSpace_filter_options" > android:orientation="horizontal"
android:visibility="invisible">
<Button <Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="filter 1"
android:id="@+id/personalSpace_small_filter" android:id="@+id/personalSpace_small_filter"
android:layout_weight="1" />
<Button
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="50dp" android:layout_height="50dp"
android:text="filter 2" android:layout_weight="1"
android:text="filter 1" />
<Button
android:id="@+id/personalSpace_medium_filter" android:id="@+id/personalSpace_medium_filter"
android:layout_weight="1" />
<Button
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="50dp" android:layout_height="50dp"
android:text="filter 3" android:layout_weight="1"
android:text="filter 2" />
<Button
android:id="@+id/personalSpace_large_filter" android:id="@+id/personalSpace_large_filter"
android:layout_weight="1" /> android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="filter 3" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
@ -81,9 +81,10 @@
android:id="@+id/ps_list_listview" android:id="@+id/ps_list_listview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="0.8" android:layout_marginTop="10dp"
android:layout_marginTop="10dp" /> android:layout_weight="0.8" />
</LinearLayout> </LinearLayout>
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"