This commit is contained in:
Aran Zaiger 2016-07-10 17:56:55 +03:00
commit 871b8dc75c
4 changed files with 716 additions and 750 deletions

View file

@ -1,22 +1,11 @@
package com.android_app.matan.ara.sagi.thesocialnotework;
import android.*;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
@ -29,54 +18,42 @@ import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static final String LOCAL_DATA_TSN = "TSN_DATA_STORE";
protected final String TAG = "[TSN / MainActivity]";
protected User user;
private GPSUtils gpsUtils;
private boolean locationPermission;
private GmapFragment gmapFragment;
private PersonalFragment personalFragment;
private SettingsFragment settingsFragment;
private ExploreFragment exploreFragment;
private Toolbar toolbar;
public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
private ImageView menu_avatar;
private MainActivity self;
private NavigationView nav_view;
private final int FINE_PERM = 0, CAMERA_PERM = 1;
private final int FINE_PERM = 0;
/**
* On Activity Start
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.self = this;
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Personal Notes");
setSupportActionBar(toolbar);
gmapFragment = new GmapFragment();
personalFragment = new PersonalFragment();
settingsFragment = new SettingsFragment();
exploreFragment = new ExploreFragment();
this.toolbar = (Toolbar) findViewById(R.id.toolbar);
this.toolbar.setTitle("Personal Notes");
this.setSupportActionBar(toolbar);
this.gmapFragment = new GmapFragment();
this.personalFragment = new PersonalFragment();
this.settingsFragment = new SettingsFragment();
this.exploreFragment = new ExploreFragment();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
@ -99,7 +76,6 @@ public class MainActivity extends AppCompatActivity
//Change Layout
Log.d(TAG, "Changing Fragment to Personal Activity");
// PersonalFragment personalFragment = new PersonalFragment();
personalFragment.setArguments(b);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, personalFragment);
@ -129,15 +105,18 @@ public class MainActivity extends AppCompatActivity
return true;
}
/**
* Handle action bar item clicks here. The action bar will
* automatically handle clicks on the Home/Up button, so long
* as you specify a parent activity in AndroidManifest.xml.
* @param item - Action bar item (Menu)
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.d(TAG, "onOptionsItemSelected: id -> "+id);
//noinspection SimplifiableIfStatement
// Settings
if (id == R.id.action_settings) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
toolbar.setTitle("Settings");
@ -146,17 +125,19 @@ public class MainActivity extends AppCompatActivity
ft.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* This function will be called when an item is selected from the nav bar.
* @param item
* @return
*/
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_explore) {
toolbar.setTitle("Explore");
setSupportActionBar(toolbar);
@ -193,16 +174,17 @@ public class MainActivity extends AppCompatActivity
finish();
}
// Close the drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public GPSUtils getGPSUtils() {
return this.gpsUtils;
}
public User getUser() {
return user;
}
@ -211,6 +193,12 @@ public class MainActivity extends AppCompatActivity
return user.getId();
}
/**
* Permittion handlers
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
@ -248,6 +236,9 @@ public class MainActivity extends AppCompatActivity
}
/**
* Update the Navbar avatar
*/
public void updateNavAvatar() {
//Change The Avatar
Utils.URLtoImageView(menu_avatar, user.getAvatar());

View file

@ -1,10 +1,10 @@
package com.android_app.matan.ara.sagi.thesocialnotework;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* Created by JERLocal on 7/8/2016.
* This class will hold information about a Mark on the Map.
*/
public class MarkerNoteStruct {
private Note note;

View file

@ -10,6 +10,7 @@ import java.util.Date;
/**
* Created by JERLocal on 7/1/2016.
* A Note Class - Represents a note Object
*/
public class Note implements Parcelable {
@ -25,7 +26,6 @@ public class Note implements Parcelable {
this.lat = lat;
this.lon = lon;
this.address = address;
this.title = title;
this.body = body;
this.timestamp = timestamp;
@ -42,7 +42,6 @@ public class Note implements Parcelable {
tags = in.createStringArrayList();
lat = in.readFloat();
lon = in.readFloat();
id = in.readString();
address = in.readString();
title = in.readString();
@ -205,12 +204,22 @@ public class Note implements Parcelable {
dest.writeByte((byte) (isPublic ? 1 : 0));
}
/**
* Get a Simplified Readable Time (Creation of the note)
*
* @return String
*/
public String getTime() {
Date d = new Date(timestamp);
DateFormat format = new SimpleDateFormat("HH:mm");
return format.format(d);
}
/**
* Get a Simplified Readable Date (Creation of the note)
*
* @return String
*/
public String getDate() {
Date d = new Date(timestamp);
DateFormat format = new SimpleDateFormat("dd MMM yyy");

View file

@ -5,18 +5,10 @@ import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.location.Location;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -36,8 +28,6 @@ import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.android.gms.maps.model.LatLng;
import org.json.JSONArray;
import org.json.JSONException;
@ -54,7 +44,6 @@ import java.util.List;
* to handle interaction events.
*/
public class PersonalFragment extends Fragment {
protected ListView noteList;
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
private GPSUtils gpsUtils;
@ -63,11 +52,9 @@ public class PersonalFragment extends Fragment {
private String userId;
private final String TAG = "[TSN/PersonalFragment]";
private MainActivity activity;
private final int FINE_PERM = 0, CAMERA_PERM = 1;
private final int FINE_PERM = 0;
private int userFilterSelection;
private Long dateFilterSelection;
private ImageButton dateFilter;
private ImageButton userFilter;
private Button map_small_filter;
@ -86,7 +73,7 @@ public class PersonalFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_personal, container, false);
// Inflate the layout for this fragment
activity = (MainActivity) getActivity();
this.activity = (MainActivity) getActivity();
Bundle bundle = getArguments();
this.userId = activity.getUserId();
Log.d(TAG, "onCreateView: userID: " + userId);
@ -130,6 +117,7 @@ public class PersonalFragment extends Fragment {
personalSpaceFilters = (LinearLayout) view.findViewById(R.id.personalSpace_filter_options);
// Date Filter Listener
dateFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
@ -151,7 +139,7 @@ public class PersonalFragment extends Fragment {
}
});
// User Filter Listener
userFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -175,21 +163,13 @@ public class PersonalFragment extends Fragment {
}
});
// get all notes according to some default filter ? // TODO: Aran?
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>
// The New "Add Button"
// The New "Add Button" - Floating Fab Button
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(addNewNoteDialog);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
@ -203,14 +183,14 @@ public class PersonalFragment extends Fragment {
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);
}
// 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);
// }
// New Note Dialog (View)
private View.OnClickListener addNewNoteDialog = new View.OnClickListener() {
public void onClick(View v) {
//create and configure dialog
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
@ -223,20 +203,17 @@ public class PersonalFragment extends Fragment {
dialog.show();
dialog.getWindow().setAttributes(lp);
//get note_view_full layout elements
final Switch permissionSwitch = (Switch) dialog.findViewById(R.id.nvf_note_permission);
final EditText newTitle = (EditText) dialog.findViewById(R.id.nvf_note_title);
final EditText newBody = (EditText) dialog.findViewById(R.id.nvf_note_content);
Button saveBtn = (Button) dialog.findViewById(R.id.nvf_note_submit_btn);
Button cancelBtn = (Button) dialog.findViewById(R.id.nvf_note_cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
@ -253,6 +230,10 @@ public class PersonalFragment extends Fragment {
toast.show();
return;
}
if (newBody.getText().length() < 1) {
Toast.makeText(getActivity(), "Cant Submit an empty body", Toast.LENGTH_LONG).show();
return;
}
//volley post
final JSONObject noteJson = new JSONObject();
try {
@ -265,20 +246,15 @@ public class PersonalFragment extends Fragment {
noteJson.put("address", gpsUtils.getAddress());
noteJson.put("body", newBody.getText());
noteJson.put("is_public", permissionSwitch.isChecked());
// noteJson.put("tags",);
Log.d(TAG, "Json: " + noteJson.toString());
} catch (Exception e) {
Log.d(TAG, "saveBtn: " + e.toString());
}
//send request and close dialog
VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccessListener, Utils.genericErrorListener);
dialog.dismiss();
}
});
//change text of switch according to state.
permissionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -288,8 +264,6 @@ public class PersonalFragment extends Fragment {
permissionSwitch.setText(R.string.nvf_private_label);
}
});
}
};
@ -302,11 +276,8 @@ public class PersonalFragment extends Fragment {
Date time = new Date();
JSONObject noteObject = response.getJSONObject("note");
time.setTime(noteObject.getLong("created_at"));
addNoteFromJsonObj(noteObject, time,0);
addNoteFromJsonObj(noteObject, time, 0);
updateShowedNotes();
// presentedNotes = listOfNotes;
// noteList.setAdapter(noteListAdapter);
} catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage());
}
@ -315,15 +286,6 @@ public class PersonalFragment extends Fragment {
};
// //response Error listener for adding new note
// Response.ErrorListener newNoteErrorListener = new Response.ErrorListener() {
// @Override
// public void onErrorResponse(VolleyError error) {
// Log.d(TAG, "newNoteError: msg: " + error.getMessage());
// }
// };
//response listener for getting all user notes
Response.Listener<JSONObject> getNotesSuccessListener = new Response.Listener<JSONObject>() {
@Override
@ -339,11 +301,9 @@ public class PersonalFragment extends Fragment {
JSONObject noteObject = noteObjectsArray.getJSONObject(i);
time.setTime(noteObject.getLong("created_at"));
addNoteFromJsonObj(noteObject, time,-1);
addNoteFromJsonObj(noteObject, time, -1);
}
updateShowedNotes();
// presentedNotes = listOfNotes;
// noteList.setAdapter(noteListAdapter);
} catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage());
}
@ -351,6 +311,11 @@ public class PersonalFragment extends Fragment {
}
};
/**
* Converts JSON Array to A Java String ArrayList
* @param jArray JSONArray Object
* @return ArrayList<String>
*/
private ArrayList<String> jsonArrayToStringArray(JSONArray jArray) {
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0, count = jArray.length(); i < count; i++) {
@ -365,6 +330,13 @@ public class PersonalFragment extends Fragment {
}
/**
* Add note from JSON Object to the list of notes
* @param noteObject
* @param time
* @param position
* @throws JSONException
*/
private void addNoteFromJsonObj(JSONObject noteObject, Date time, int position) throws JSONException {
Note addNote = new Note(
noteObject.getString("id"),
@ -380,16 +352,18 @@ public class PersonalFragment extends Fragment {
noteObject.getString("owner_id"),
jsonArrayToStringArray(noteObject.getJSONArray("tags"))
);
Log.d(TAG, "addNoteFromJsonObj: "+ addNote.getDate() + " " + addNote.getTime());
Log.d(TAG, "addNoteFromJsonObj: " + addNote.getDate() + " " + addNote.getTime());
if (position != 0)
listOfNotes.add(addNote);
else
listOfNotes.add(position,addNote);
listOfNotes.add(position, addNote);
}
// click on listView item
/**
* The ListView Item Listener
*/
class ItemClickedListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
@ -398,14 +372,12 @@ public class PersonalFragment extends Fragment {
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
@ -415,7 +387,6 @@ public class PersonalFragment extends Fragment {
final TextView date = (TextView) noteViewDialog.findViewById(R.id.ndf_date_textview);
final TextView location = (TextView) noteViewDialog.findViewById(R.id.ndf_address_textview);
final TextView likes = (TextView) noteViewDialog.findViewById(R.id.ndf_likes_textview);
// final TextView tags = (TextView) noteViewDialog.findViewById(R.id.ndf_tags_textview);
final TextView permission = (TextView) noteViewDialog.findViewById(R.id.ndf_permission_textview);
final ImageButton deleteBtn = (ImageButton) noteViewDialog.findViewById(R.id.ndf_delete_imagebutton);
final ImageView avatar = (RoundAvatarImageView) noteViewDialog.findViewById(R.id.note_user_avatar);
@ -427,10 +398,9 @@ public class PersonalFragment extends Fragment {
time.setText(note.getTime());
location.setText(note.getAddress());
if (likes != null) likes.setText("" + note.getLikes());
// tags.setText("Tags: "+ note.getTags().toString());
permission.setText("" + (note.isPublic() ? "Public" : "Private"));
Utils.URLtoImageView(avatar, note.getAvatar());
// DElete Button Listener
deleteBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Put up the Yes/No message box
@ -444,7 +414,6 @@ public class PersonalFragment extends Fragment {
//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);
@ -452,13 +421,11 @@ public class PersonalFragment extends Fragment {
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();
}
})
@ -477,7 +444,6 @@ public class PersonalFragment extends Fragment {
}
}
//all buttons listener
public View.OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
@ -534,21 +500,19 @@ public class PersonalFragment extends Fragment {
};
/**
* Update The Shown Notes (FIlters)
*/
public void updateShowedNotes() {
presentedNotes = new ArrayList<>();
long timeDifference;
// Location currLocation = new Location(gpsUtils.getLocation());
Date now = new Date();
// Location targetLocation = new Location("");//provider name is unecessary
Date targetDate;
for (Note note : listOfNotes) {
//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);
//add to currently presented list according to filters.
if (timeDifference <= dateFilterSelection
&& ((!note.isPublic && userFilterSelection == 1) || (note.isPublic && userFilterSelection == 2) || (userFilterSelection == 3))) {
@ -560,13 +524,15 @@ public class PersonalFragment extends Fragment {
noteList.setAdapter(noteListAdapter);
}
//set secondery filter colors filter colors
/**
* Set The Filter Button Colors
*/
private void setButtonsColor() {
Log.d(TAG, "setButtonsColor: start");
//set date filter colors
if (dateFilterIsVisible) {
Log.d(TAG, "setButtonsColor: dateselection :"+ dateFilterSelection);
Log.d(TAG, "setButtonsColor: dateselection :" + dateFilterSelection);
dateFilter.setBackgroundColor(Utils.filterColor);
if (dateFilterSelection == Utils.DAY_MILI) {
@ -588,7 +554,7 @@ public class PersonalFragment extends Fragment {
//set date filter colors
if (userFilterIsVisible) {
Log.d(TAG, "setButtonsColor: userFilter: "+ userFilterSelection);
Log.d(TAG, "setButtonsColor: userFilter: " + userFilterSelection);
userFilter.setBackgroundColor(Utils.filterColor);
if (userFilterSelection == 1) {
map_small_filter.setBackgroundColor(Utils.filterColor);