# Conflicts:
#	app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalSpaceActivity.java
This commit is contained in:
Matan Bar Yosef 2016-07-02 19:36:31 +03:00
commit 8f5b43854f
6 changed files with 245 additions and 111 deletions

View file

@ -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 ( ) ;
}
}
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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=<userID>
@ -43,6 +47,7 @@ public class PersonalSpaceActivity extends AppCompatActivity {
private boolean locationPermission;
private GPSUtils gpsUtils;
private List<Note> listOfNotes;
private ListAdapter noteListAdapter;
@Override
@ -64,30 +69,40 @@ 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);
addBtn.setOnClickListener(new View.OnClickListener() {
noteList.setAdapter(noteListAdapter);
new HeavyWorker(this).execute();
//https://thesocialnotework-api.appspot.com/api/note/all?uid=<USER_ID>
addBtn.setOnClickListener(addNewNoteDialog);
}
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) {
final Dialog dialog = new Dialog(PersonalSpaceActivity.this);
//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;
// 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);
//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);
@ -100,14 +115,14 @@ public class PersonalSpaceActivity extends AppCompatActivity {
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//volley post
final JSONObject noteJson = new JSONObject();
try {
noteJson.put("owner_id", "5634472569470976");
//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());
@ -122,86 +137,79 @@ public class PersonalSpaceActivity extends AppCompatActivity {
Log.d(TAG, e.toString());
}
VolleyUtilSingleton.getInstance(PersonalSpaceActivity.this).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccess, newNoteError);
//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);
// do something, the isChecked will be
// true if the switch is in the On position
}
});
}
}
);
};
}
//TODO remove
public void addDemoNotes(List<Note> 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);
listOfNotes.add(n4);
}
public void setLocationPermission(boolean locationPermission) {
this.locationPermission = locationPermission;
}
Response.Listener<JSONObject> newNoteSuccess = new Response.Listener<JSONObject>() {
//response listener for adding new note
Response.Listener<JSONObject> newNoteSuccessListener = new Response.Listener<JSONObject>() {
@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);
// addNoteToArray(addNote);
} catch (JSONException e) {
noteList.setAdapter(noteListAdapter);
} catch (Exception e) {
Log.e(TAG, "newNoteSuccess:" + e.getMessage());
}
}
};
// 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());
@ -209,4 +217,58 @@ public class PersonalSpaceActivity extends AppCompatActivity {
};
//response listener for getting all user notes
Response.Listener<JSONObject> getNotesSuccessListener = new Response.Listener<JSONObject>() {
@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());
}
};
}

View file

@ -98,4 +98,16 @@ public class VolleyUtilSingleton {
addToRequestQueue(request);
}
public void get(String url, Response.Listener<JSONObject> successFunction, Response.ErrorListener errorFunction) {
JsonObjectRequest request =
new JsonObjectRequest(
Request.Method.GET,
url,
null,
successFunction,
errorFunction
);
addToRequestQueue(request);
}
}

View file

@ -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" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="location"
android:id="@+id/nvm_location_textview" />
</LinearLayout>