From 87e09af1c44577dcc72884352bb9f12e74e1b4b7 Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Sat, 2 Jul 2016 17:49:45 +0300 Subject: [PATCH 1/2] new notes added to list --- .../ara/sagi/thesocialnotework/Note.java | 8 +-- .../PersonalSpaceActivity.java | 51 ++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Note.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Note.java index 147a70e..4311fb2 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Note.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Note.java @@ -8,10 +8,10 @@ public class Note { // protected int id; protected float lat, lon; protected String id, address, title, body; - protected long timestamp; + protected String timestamp; protected boolean isPublic; - public Note(String id, float lat, float lon, String address, String title, String body, long timestamp, boolean isPublic) { + public Note(String id, float lat, float lon, String address, String title, String body, String timestamp, boolean isPublic) { this.id = id; this.lat = lat; this.lon = lon; @@ -84,11 +84,11 @@ public class Note { this.body = body; } - public long getTimestamp() { + public String getTimestamp() { return timestamp; } - public void setTimestamp(long timestamp) { + public void setTimestamp(String timestamp) { this.timestamp = timestamp; } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java index 5e99073..f59a846 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java @@ -26,10 +26,14 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TimeZone; //http://thesocialnotework.appspot.com/api/status | http://localhost:8080/api/note/all?uid= @@ -43,6 +47,7 @@ public class PersonalSpaceActivity extends AppCompatActivity { private boolean locationPermission; private GPSUtils gpsUtils; private List listOfNotes; + private ListAdapter noteListAdapter; @Override @@ -64,8 +69,8 @@ public class PersonalSpaceActivity extends AppCompatActivity { listOfNotes = new ArrayList<>(); //add demo notes to view addDemoNotes(listOfNotes); - ListAdapter la = new ListAdapter(this, listOfNotes); - noteList.setAdapter(la); + noteListAdapter = new ListAdapter(this, listOfNotes); + noteList.setAdapter(noteListAdapter); addBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { @@ -122,7 +127,7 @@ public class PersonalSpaceActivity extends AppCompatActivity { Log.d(TAG, e.toString()); } - VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).newUser(BASE_URL + "/note/upsert", noteJson); + VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccess, newNoteError); dialog.dismiss(); } }); @@ -150,10 +155,10 @@ public class PersonalSpaceActivity extends AppCompatActivity { public void addDemoNotes(List listOfNotes) { - Note n1 = new Note("1", 100, 100, "location1", "My 1st Title", "ohh i'm so sexy1", System.currentTimeMillis() / 1000, true); - Note n2 = new Note("2", 200, 200, "location2", "My 2st Title", "ohh i'm so sexy2", System.currentTimeMillis() / 1000, true); - Note n3 = new Note("3", 300, 300, "hell", "My 3st Title", "ohh i'm so sexy3", System.currentTimeMillis() / 1000, true); - Note n4 = new Note("4", 400, 400, "hell2", "My 4st Title", "ohh i'm so sexy4", System.currentTimeMillis() / 1000, true); + Note n1 = new Note("1", 100, 100, "location1", "My 1st Title", "ohh i'm so sexy1", ""+System.currentTimeMillis() / 1000, true); + Note n2 = new Note("2", 200, 200, "location2", "My 2st Title", "ohh i'm so sexy2", ""+System.currentTimeMillis() / 1000, true); + Note n3 = new Note("3", 300, 300, "hell", "My 3st Title", "ohh i'm so sexy3", ""+System.currentTimeMillis() / 1000, true); + Note n4 = new Note("4", 400, 400, "hell2", "My 4st Title", "ohh i'm so sexy4", ""+System.currentTimeMillis() / 1000, true); listOfNotes.add(n1); listOfNotes.add(n2); listOfNotes.add(n3); @@ -168,29 +173,27 @@ public class PersonalSpaceActivity extends AppCompatActivity { Response.Listener newNoteSuccess = new Response.Listener() { @Override public void onResponse(JSONObject response) { - String s = ""; - - try { - s= response.getString("id"); - } catch (JSONException e) { - e.printStackTrace(); - } Log.d(TAG, "newNoteSuccess: response - " + response.toString()); - Log.d(TAG, "newNoteSuccess: id response - " + s); try { + Date time = new Date(); + JSONObject noteObject = response.getJSONObject("note"); + time.setTime(noteObject.getLong("created_at")); + Note addNote = new Note( - "12345", - Float.parseFloat(response.getJSONObject("location").getString("lat")), - Float.parseFloat(response.getJSONObject("location").getString("lng")), - response.getJSONObject("location").getString("address"), - response.getString("title"), - response.getString("body"), - response.getLong("created_at"), - response.getBoolean("is_public") + noteObject.getString("id"), + Float.parseFloat(noteObject.getJSONObject("location").getString("lat")), + Float.parseFloat(noteObject.getJSONObject("location").getString("lng")), + noteObject.getJSONObject("location").getString("address"), + noteObject.getString("title"), + noteObject.getString("body"), + time.toString(), + noteObject.getBoolean("is_public") ); listOfNotes.add(addNote); +// noteListAdapter = new ListAdapter(PersonalSpaceActivity.this, listOfNotes); + noteList.setAdapter(noteListAdapter); // addNoteToArray(addNote); - } catch (JSONException e) { + } catch (Exception e) { Log.e(TAG, "newNoteSuccess:" + e.getMessage()); } From 14ff165c2023a6e7024ebf02dbe646e2d7e55636 Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Sat, 2 Jul 2016 19:03:31 +0300 Subject: [PATCH 2/2] get all notes now showing all existing notes for user when entering --- .../sagi/thesocialnotework/HeavyWorker.java | 51 ++++ .../sagi/thesocialnotework/ListAdapter.java | 6 +- .../PersonalSpaceActivity.java | 223 +++++++++++------- .../VolleyUtilSingleton.java | 12 + app/src/main/res/layout/note_view_mini.xml | 11 +- 5 files changed, 217 insertions(+), 86 deletions(-) create mode 100644 app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/HeavyWorker.java diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/HeavyWorker.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/HeavyWorker.java new file mode 100644 index 0000000..2ed5f77 --- /dev/null +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/HeavyWorker.java @@ -0,0 +1,51 @@ +package com.android_app.matan.ara.sagi.thesocialnotework; + +import android.app.ProgressDialog; +import android.content.Context; +import android.os.AsyncTask; + +/** + * Created by JERLocal on 7/2/2016. + */ +public class HeavyWorker extends AsyncTask< String , Context , Void > { + + private ProgressDialog progressDialog ; + private Context targetCtx ; + + public HeavyWorker ( Context context ) { + this.targetCtx = context ; + progressDialog = new ProgressDialog ( targetCtx ) ; + progressDialog.setCancelable ( false ) ; + progressDialog.setMessage ( "Retrieving data..." ) ; + progressDialog.setTitle ( "Please wait" ) ; + progressDialog.setIndeterminate ( true ) ; + } + + @ Override + protected void onPreExecute ( ) { + progressDialog.show ( ) ; + } + + @ Override + protected Void doInBackground ( String ... params ) { + // Do Your WORK here + + PersonalSpaceActivity ps = (PersonalSpaceActivity)targetCtx; + ps.getAllNotes(); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + + return null ; + } + + @ Override + protected void onPostExecute ( Void result ) { + if(progressDialog != null && progressDialog.isShowing()){ + progressDialog.dismiss ( ) ; + } + } +} diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ListAdapter.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ListAdapter.java index b44e79c..9070d6a 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ListAdapter.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/ListAdapter.java @@ -57,12 +57,14 @@ public class ListAdapter extends BaseAdapter { v = inflater.inflate(R.layout.note_view_mini , parent , false); } TextView title = (TextView) v.findViewById(R.id.nvm_title_textview); - TextView datetime = (TextView) v.findViewById(R.id.nvm_time_location_textview); + TextView datetime = (TextView) v.findViewById(R.id.nvm_time_textview); + TextView location = (TextView) v.findViewById(R.id.nvm_location_textview); // NetworkImageView thumbNail = (NetworkImageView) v.findViewById(R.id.infoImageImageView); // String url = mVideos.get(position).getImgURL(); // thumbNail.setImageUrl(url, VolleyUtilSingleTone.getInstance(mContext).getImageLoader()); title.setText(mNotes.get(position).getTitle()); - datetime.setText(Html.fromHtml(mNotes.get(position).getLocationAndTime())); + datetime.setText(Html.fromHtml(mNotes.get(position).getTimestamp())); + location.setText(mNotes.get(position).getAddress()); // Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top); // v.startAnimation(animation); lastPosition = position; diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java index f59a846..bc9739a 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java @@ -70,90 +70,96 @@ public class PersonalSpaceActivity extends AppCompatActivity { //add demo notes to view addDemoNotes(listOfNotes); noteListAdapter = new ListAdapter(this, listOfNotes); + noteList.setAdapter(noteListAdapter); - - addBtn.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - final Dialog dialog = new Dialog(PersonalSpaceActivity.this); - - dialog.setContentView(R.layout.note_view_full); - dialog.setTitle("New Note"); - WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); - - lp.copyFrom(dialog.getWindow().getAttributes()); - lp.width = WindowManager.LayoutParams.MATCH_PARENT; - lp.height = WindowManager.LayoutParams.MATCH_PARENT; + new HeavyWorker(this).execute(); - // final EditText editText = (EditText) dialog.findViewById(R.id.editText); - // Button btnSave = (Button) dialog.findViewById(R.id.save); - // Button btnCancel = (Button) dialog.findViewById(R.id.cancel); - dialog.setCancelable(false); - dialog.show(); - dialog.getWindow().setAttributes(lp); +//https://thesocialnotework-api.appspot.com/api/note/all?uid= + addBtn.setOnClickListener(addNewNoteDialog); - 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) { - //volley post - final JSONObject noteJson = new JSONObject(); - try { - - noteJson.put("owner_id", "5634472569470976"); - noteJson.put("title", newTitle.getText()); - noteJson.put("lat", gpsUtils.getLatitude()); - noteJson.put("lng", gpsUtils.getLongitude()); - 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, e.toString()); - } - - VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccess, newNoteError); - dialog.dismiss(); - } - }); - - permissionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (isChecked) - permissionSwitch.setText(R.string.nvf_public_label); - else - permissionSwitch.setText(R.string.nvf_private_label); - - // do something, the isChecked will be - // true if the switch is in the On position - } - }); - - - } - } - - ); - } + public void getAllNotes(){ + VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).get(BASE_URL + "/note/all?uid=5719238044024832", getNotesSuccessListener, genericErrorListener); + } + private View.OnClickListener addNewNoteDialog = new View.OnClickListener() { + public void onClick(View v) { + + //create and configure dialog + final Dialog dialog = new Dialog(PersonalSpaceActivity.this); + dialog.setContentView(R.layout.note_view_full); + dialog.setTitle("New Note"); + WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); + lp.copyFrom(dialog.getWindow().getAttributes()); + lp.width = WindowManager.LayoutParams.MATCH_PARENT; + lp.height = WindowManager.LayoutParams.MATCH_PARENT; + dialog.setCancelable(false); + 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) { + //volley post + final JSONObject noteJson = new JSONObject(); + try { + + //TODO need to get owner id from login screen + noteJson.put("owner_id", "5719238044024832"); + noteJson.put("title", newTitle.getText()); + noteJson.put("lat", gpsUtils.getLatitude()); + noteJson.put("lng", gpsUtils.getLongitude()); + 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, e.toString()); + } + + //send request and close dialog + VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccessListener, genericErrorListener); + dialog.dismiss(); + } + }); + + //change text of switch according to state. + permissionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) + permissionSwitch.setText(R.string.nvf_public_label); + else + permissionSwitch.setText(R.string.nvf_private_label); + } + }); + + + } + }; + + + + //TODO remove public void addDemoNotes(List listOfNotes) { Note n1 = new Note("1", 100, 100, "location1", "My 1st Title", "ohh i'm so sexy1", ""+System.currentTimeMillis() / 1000, true); Note n2 = new Note("2", 200, 200, "location2", "My 2st Title", "ohh i'm so sexy2", ""+System.currentTimeMillis() / 1000, true); @@ -165,12 +171,14 @@ public class PersonalSpaceActivity extends AppCompatActivity { listOfNotes.add(n4); } + public void setLocationPermission(boolean locationPermission) { this.locationPermission = locationPermission; } - Response.Listener newNoteSuccess = new Response.Listener() { + //response listener for adding new note + Response.Listener newNoteSuccessListener = new Response.Listener() { @Override public void onResponse(JSONObject response) { Log.d(TAG, "newNoteSuccess: response - " + response.toString()); @@ -189,10 +197,9 @@ public class PersonalSpaceActivity extends AppCompatActivity { time.toString(), noteObject.getBoolean("is_public") ); + listOfNotes.add(addNote); -// noteListAdapter = new ListAdapter(PersonalSpaceActivity.this, listOfNotes); noteList.setAdapter(noteListAdapter); -// addNoteToArray(addNote); } catch (Exception e) { Log.e(TAG, "newNoteSuccess:" + e.getMessage()); } @@ -200,11 +207,9 @@ public class PersonalSpaceActivity extends AppCompatActivity { } }; -// private void addNoteToArray(Note addNote) { -// listOfNotes.addNote -// } - Response.ErrorListener newNoteError = new Response.ErrorListener() { + //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()); @@ -212,4 +217,58 @@ public class PersonalSpaceActivity extends AppCompatActivity { }; + //response listener for getting all user notes + Response.Listener getNotesSuccessListener = new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + Log.d(TAG,response.toString()); + try { + //need to get all notes and add to listOfNotes + JSONArray noteObjectsArray = response.getJSONArray("notes"); + Date time = new Date(); + for (int i = 0; i < noteObjectsArray.length(); i++) { + JSONObject noteObject = noteObjectsArray.getJSONObject(i); + time.setTime(noteObject.getLong("created_at")); + + Note addNote = new Note( + noteObject.getString("id"), + Float.parseFloat(noteObject.getJSONObject("location").getString("lat")), + Float.parseFloat(noteObject.getJSONObject("location").getString("lng")), + noteObject.getJSONObject("location").getString("address"), + noteObject.getString("title"), + noteObject.getString("body"), + time.toString(), + noteObject.getBoolean("is_public") + ); + listOfNotes.add(addNote); + } + noteList.setAdapter(noteListAdapter); + } catch (Exception e) { + Log.e(TAG, "newNoteSuccess:" + e.getMessage()); + } + + } + }; + + + //response ErrorListener for getting all user notes + Response.ErrorListener getNotesErrorListener = new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Log.d(TAG,error.getMessage()); + } + }; + + //Generic response ErrorListener + Response.ErrorListener genericErrorListener = new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Log.d(TAG,error.getMessage()); + } + }; + + + + + } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/VolleyUtilSingleton.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/VolleyUtilSingleton.java index e038572..96eee6a 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/VolleyUtilSingleton.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/VolleyUtilSingleton.java @@ -98,4 +98,16 @@ public class VolleyUtilSingleton { addToRequestQueue(request); } + public void get(String url, Response.Listener successFunction, Response.ErrorListener errorFunction) { + JsonObjectRequest request = + new JsonObjectRequest( + Request.Method.GET, + url, + null, + successFunction, + errorFunction + ); + addToRequestQueue(request); + } + } diff --git a/app/src/main/res/layout/note_view_mini.xml b/app/src/main/res/layout/note_view_mini.xml index d421d83..9259bb3 100644 --- a/app/src/main/res/layout/note_view_mini.xml +++ b/app/src/main/res/layout/note_view_mini.xml @@ -14,7 +14,14 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" - android:text="Date and Location" - android:id="@+id/nvm_time_location_textview" /> + android:text="Date and time" + android:id="@+id/nvm_time_textview" /> + + \ No newline at end of file