88import android .support .design .widget .FloatingActionButton ;
99import android .support .v7 .app .AppCompatActivity ;
1010import android .text .InputType ;
11+ import android .view .ContextMenu ;
12+ import android .view .MenuInflater ;
13+ import android .view .MenuItem ;
1114import android .view .View ;
1215import android .widget .AdapterView ;
1316import android .widget .ImageButton ;
2831import java .util .ArrayList ;
2932import java .util .Date ;
3033import java .util .List ;
34+ import java .util .Locale ;
3135
3236import map .net .apscanner .R ;
3337import map .net .apscanner .classes .facility .Facility ;
3438import map .net .apscanner .classes .facility .FacilityAdapter ;
3539import map .net .apscanner .utils .GsonUtil ;
3640import map .net .apscanner .utils .UserInfo ;
41+ import okhttp3 .Call ;
42+ import okhttp3 .Callback ;
43+ import okhttp3 .HttpUrl ;
3744import okhttp3 .MediaType ;
3845import okhttp3 .OkHttpClient ;
3946import okhttp3 .Request ;
@@ -66,7 +73,6 @@ public void onClick(View v) {
6673 /* On button's click, calls AsyncTask to send new Facility to server */
6774 newFacilityFAB .setOnClickListener (new View .OnClickListener () {
6875 public void onClick (View v ) {
69-
7076 MaterialDialog .Builder newFacilityDialog =
7177 new MaterialDialog .Builder (FacilitiesActivity .this )
7278 .title ("Create a new facility" )
@@ -92,23 +98,43 @@ public void onClick(@NonNull MaterialDialog dialog,
9298 }
9399 });
94100
95-
96101 newFacilityDialog .input ("Enter your facility name" , null ,
97102 new MaterialDialog .InputCallback () {
98103 @ Override
99104 public void onInput (@ NonNull MaterialDialog dialog , CharSequence input ) {
100105
101106 }
102107 });
103-
104108 newFacilityDialog .show ();
105109 }
106110 });
107111
112+ registerForContextMenu (facilitiesListView );
108113
109114 new getFacilitiesFromServer ().execute ();
110115 }
111116
117+ @ Override
118+ public void onCreateContextMenu (ContextMenu menu , View v , ContextMenu .ContextMenuInfo menuInfo ) {
119+ super .onCreateContextMenu (menu , v , menuInfo );
120+ if (v .getId () == R .id .facilitiesListView ) {
121+ MenuInflater inflater = getMenuInflater ();
122+ inflater .inflate (R .menu .facility_menu_list , menu );
123+ }
124+ }
125+
126+ @ Override
127+ public boolean onContextItemSelected (MenuItem item ) {
128+ AdapterView .AdapterContextMenuInfo info = (AdapterView .AdapterContextMenuInfo ) item .getMenuInfo ();
129+ switch (item .getItemId ()) {
130+ case R .id .deleteFacility :
131+ new DeleteFacilityFromServer ().run ((Facility ) facilitiesListView .getItemAtPosition (info .position ));
132+ return true ;
133+ default :
134+ return super .onContextItemSelected (item );
135+ }
136+ }
137+
112138 /**
113139 * This async task gets a list of User's facilities data from server and put them into a
114140 * ListView. The user can touch on the facility to access its zones.
@@ -117,9 +143,15 @@ private class getFacilitiesFromServer extends AsyncTask<Object, Object, Void> {
117143
118144 @ Override
119145 protected void onPreExecute () {
120- loadingDialog = ProgressDialog .show (FacilitiesActivity .this ,
121- "Please wait..." , "Getting data from server" );
122- loadingDialog .setCancelable (false );
146+
147+ runOnUiThread (new Runnable () {
148+ @ Override
149+ public void run () {
150+ loadingDialog = ProgressDialog .show (FacilitiesActivity .this ,
151+ "Please wait..." , "Getting data from server" );
152+ loadingDialog .setCancelable (false );
153+ }
154+ });
123155 }
124156
125157 @ Override
@@ -162,27 +194,29 @@ public void run() {
162194
163195 List <Facility > facilitiesList = new ArrayList <>();
164196
165- for (int i = 0 ; i < facilitiesJSON .length (); i ++) {
166- try {
167-
168- /* Creates a new Facility object from JSON */
169- JSONObject facilityJSON = facilitiesJSON .getJSONObject (i );
170- Facility facility = new Facility (facilityJSON .get ("name" ).toString ());
171- facility .setId (facilityJSON .getJSONObject ("_id" ).get ("$oid" ).toString ());
172-
173- /* Sets up a ISO format and convert servers format to it */
174- DateFormat dateFormatISO = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
175- String facilityCreatedAtDate = facilityJSON .get ("created_at" ).toString ();
176- Date completeDate = dateFormatISO .parse (facilityCreatedAtDate );
177-
178- /* Setting up days only date*/
179- DateFormat daysOnlyDataFormat = new SimpleDateFormat ("dd/MMM/yy" );
180- String daysOnlyDate = daysOnlyDataFormat .format (completeDate );
181- facility .setDate (daysOnlyDate );
182-
183- facilitiesList .add (facility );
184- } catch (JSONException | ParseException e ) {
185- e .printStackTrace ();
197+ if (facilitiesJSON != null ) {
198+ for (int i = 0 ; i < facilitiesJSON .length (); i ++) {
199+ try {
200+
201+ /* Creates a new Facility object from JSON */
202+ JSONObject facilityJSON = facilitiesJSON .getJSONObject (i );
203+ Facility facility = new Facility (facilityJSON .get ("name" ).toString ());
204+ facility .setId (facilityJSON .getJSONObject ("_id" ).get ("$oid" ).toString ());
205+
206+ /* Sets up a ISO format and convert servers format to it */
207+ DateFormat dateFormatISO = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" , Locale .US );
208+ String facilityCreatedAtDate = facilityJSON .get ("created_at" ).toString ();
209+ Date completeDate = dateFormatISO .parse (facilityCreatedAtDate );
210+
211+ /* Setting up days only date*/
212+ DateFormat daysOnlyDataFormat = new SimpleDateFormat ("dd/MMM/yy" , Locale .US );
213+ String daysOnlyDate = daysOnlyDataFormat .format (completeDate );
214+ facility .setDate (daysOnlyDate );
215+
216+ facilitiesList .add (facility );
217+ } catch (JSONException | ParseException e ) {
218+ e .printStackTrace ();
219+ }
186220 }
187221 }
188222
@@ -308,5 +342,56 @@ else if (response.isSuccessful()) {
308342 }
309343
310344 }
345+
346+ private class DeleteFacilityFromServer {
347+
348+ void run (Facility facility ) {
349+
350+ OkHttpClient client = new OkHttpClient ();
351+
352+ HttpUrl deleteFacility_URL = new HttpUrl .Builder ()
353+ .scheme ("http" )
354+ .host ("52.67.171.39" )
355+ .port (3000 )
356+ .addPathSegment ("delete_facility" )
357+ .addQueryParameter ("id" , facility .getId ())
358+ .build ();
359+
360+ Request request = new Request .Builder ()
361+ .url (deleteFacility_URL )
362+ .delete ()
363+ .header ("X-User-Email" , UserInfo .getUserEmail ())
364+ .header ("X-User-Token" , UserInfo .getUserToken ())
365+ .build ();
366+
367+ client .newCall (request ).enqueue (new Callback () {
368+ @ Override
369+ public void onFailure (Call call , IOException e ) {
370+ e .printStackTrace ();
371+ }
372+
373+ @ Override
374+ public void onResponse (Call call , Response response ) throws IOException {
375+ if (!response .isSuccessful ())
376+ throw new IOException ("Unexpected code " + response );
377+ final String body = response .body ().string ();
378+ runOnUiThread (new Runnable () {
379+ @ Override
380+ public void run () {
381+ Toast toast = null ;
382+ toast = Toast .makeText (FacilitiesActivity .this ,
383+ body , Toast .LENGTH_SHORT );
384+ if (toast != null ) {
385+ toast .show ();
386+ }
387+ }
388+ });
389+
390+ new getFacilitiesFromServer ().execute ();
391+ response .close ();
392+ }
393+ });
394+ }
395+ }
311396}
312397
0 commit comments