Skip to content

Commit a170cae

Browse files
Prepare for version 2.0.0
1 parent a992699 commit a170cae

32 files changed

Lines changed: 294 additions & 38 deletions

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 16
1010
targetSdkVersion 24
1111
versionCode 1
12-
versionName "1.1.0"
12+
versionName "2.0.0"
1313
}
1414
buildTypes {
1515
release {

app/src/main/ic_launcher-web.png

23 KB
Loading

app/src/main/java/map/net/apscanner/activities/FacilitiesActivity.java

Lines changed: 112 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import android.support.design.widget.FloatingActionButton;
99
import android.support.v7.app.AppCompatActivity;
1010
import android.text.InputType;
11+
import android.view.ContextMenu;
12+
import android.view.MenuInflater;
13+
import android.view.MenuItem;
1114
import android.view.View;
1215
import android.widget.AdapterView;
1316
import android.widget.ImageButton;
@@ -28,12 +31,16 @@
2831
import java.util.ArrayList;
2932
import java.util.Date;
3033
import java.util.List;
34+
import java.util.Locale;
3135

3236
import map.net.apscanner.R;
3337
import map.net.apscanner.classes.facility.Facility;
3438
import map.net.apscanner.classes.facility.FacilityAdapter;
3539
import map.net.apscanner.utils.GsonUtil;
3640
import map.net.apscanner.utils.UserInfo;
41+
import okhttp3.Call;
42+
import okhttp3.Callback;
43+
import okhttp3.HttpUrl;
3744
import okhttp3.MediaType;
3845
import okhttp3.OkHttpClient;
3946
import 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

app/src/main/java/map/net/apscanner/activities/PredictZoneActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ public void run() {
290290
throw new IOException("Unexpected code " + response);
291291
}
292292

293-
294293
currentZone = response.body().string();
295294
currentZone = currentZone.substring(2, currentZone.length() - 2);
296295

@@ -300,7 +299,7 @@ public void run() {
300299
zoneName.setText(currentZone);
301300
}
302301
});
303-
302+
response.close();
304303
}
305304
});
306305
}

0 commit comments

Comments
 (0)