Skip to content

Commit 5ee8648

Browse files
committed
requests and parsing
1 parent b18fb46 commit 5ee8648

File tree

12 files changed

+325
-6
lines changed

12 files changed

+325
-6
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ dependencies {
3636
testImplementation 'junit:junit:4.13.2'
3737
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
3838
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
39+
implementation 'com.android.volley:volley:1.2.1'
3940
}

app/src/main/java/dev/palmes/farapp/AboutActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
2222
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2323

2424
WebView myWebView = findViewById(R.id.webview);
25-
myWebView.loadUrl("https://find-a-room.palmes.dev/");
25+
myWebView.loadUrl("https://find-a-room.palmes.dev/docs");
2626
}
2727

2828
@Override

app/src/main/java/dev/palmes/farapp/FindActivity.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,40 @@
33
import androidx.appcompat.app.AppCompatActivity;
44

55
import android.os.Bundle;
6+
import android.widget.TextView;
7+
8+
import org.json.JSONException;
9+
10+
import java.io.IOException;
11+
import java.text.ParseException;
12+
13+
import dev.palmes.farapp.models.Room;
14+
615

716
public class FindActivity extends AppCompatActivity {
817

918
@Override
1019
protected void onCreate(Bundle savedInstanceState) {
1120
super.onCreate(savedInstanceState);
1221
setContentView(R.layout.activity_find);
22+
23+
TextView debug = findViewById(R.id.debug);
24+
25+
RequestsManager requestsManager = new RequestsManager(this);
26+
try {
27+
requestsManager.getOne("find", response -> {
28+
debug.setText(response.toString());
29+
try {
30+
System.out.println(new Room(response));
31+
} catch (JSONException | ParseException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}, error -> {
35+
debug.setText(error.toString());
36+
});
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
}
40+
1341
}
1442
}

app/src/main/java/dev/palmes/farapp/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ protected void onCreate(Bundle savedInstanceState) {
1717
aboutButton.setOnClickListener(v -> {
1818
startActivity(new Intent(MainActivity.this, AboutActivity.class));
1919
});
20+
21+
Button findButton = findViewById(R.id.button_find);
22+
findButton.setOnClickListener(v -> {
23+
startActivity(new Intent(MainActivity.this, FindActivity.class));
24+
});
2025
}
2126
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dev.palmes.farapp;
2+
3+
import android.content.Context;
4+
5+
import com.android.volley.Request;
6+
import com.android.volley.RequestQueue;
7+
import com.android.volley.Response;
8+
import com.android.volley.toolbox.JsonArrayRequest;
9+
import com.android.volley.toolbox.JsonObjectRequest;
10+
import com.android.volley.toolbox.Volley;
11+
12+
import org.json.JSONArray;
13+
import org.json.JSONObject;
14+
15+
import java.io.IOException;
16+
import java.net.URL;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
public class RequestsManager {
21+
private final RequestQueue queue;
22+
private static final String url = "https://find-a-room.palmes.dev/api/v1/";
23+
24+
private static String buildQuery(Map<String, String> query) {
25+
String queryString = "";
26+
if (!query.isEmpty()) {
27+
queryString += "?";
28+
for (String key : query.keySet()) {
29+
queryString += key + "=" + query.get(key) + "&";
30+
}
31+
queryString = queryString.substring(0, queryString.length() - 1);
32+
}
33+
return queryString;
34+
}
35+
36+
public RequestsManager(Context context) {
37+
this.queue = Volley.newRequestQueue(context);
38+
}
39+
40+
public void getOne(String endpoint, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, Map<String, String> query) throws IOException {
41+
URL url = new URL(RequestsManager.url + endpoint + buildQuery(query));
42+
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url.toString(), null, listener, errorListener);
43+
queue.add(request);
44+
}
45+
46+
public void getMany(String endpoint, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener, Map<String, String> query) throws IOException {
47+
URL url = new URL(RequestsManager.url + endpoint + buildQuery(query));
48+
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url.toString(), null, listener, errorListener);
49+
queue.add(request);
50+
}
51+
52+
public void getOne(String endpoint, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) throws IOException {
53+
getOne(endpoint, listener, errorListener, new HashMap<>());
54+
}
55+
56+
public void getMany(String endpoint, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) throws IOException {
57+
getMany(endpoint, listener, errorListener, new HashMap<>());
58+
}
59+
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.palmes.farapp.models;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
import java.util.Locale;
10+
11+
public class Event {
12+
private Date start;
13+
private Date end;
14+
15+
public Event(Date start, Date end) {
16+
this.start = start;
17+
this.end = end;
18+
}
19+
20+
public Event(JSONObject event) throws JSONException, ParseException {
21+
SimpleDateFormat parser = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
22+
23+
this.start = parser.parse(event.getJSONObject("event").getString("start"));
24+
this.end = parser.parse(event.getJSONObject("event").getString("end"));
25+
}
26+
27+
public Date getStart() {
28+
return start;
29+
}
30+
31+
public void setStart(Date start) {
32+
this.start = start;
33+
}
34+
35+
public Date getEnd() {
36+
return end;
37+
}
38+
39+
public void setEnd(Date end) {
40+
this.end = end;
41+
}
42+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package dev.palmes.farapp.models;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import java.text.ParseException;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
public class Room {
12+
private String name;
13+
private String code;
14+
private Optional<Integer> capacity;
15+
private boolean available;
16+
List<Event> currentEvents;
17+
Optional<Event> nextEvent;
18+
19+
public Room(String name, String code, Optional<Integer> capacity, boolean available, List<Event> currentEvents, Optional<Event> nextEvent) {
20+
this.name = name;
21+
this.code = code;
22+
this.capacity = capacity;
23+
this.available = available;
24+
this.currentEvents = currentEvents;
25+
this.nextEvent = nextEvent;
26+
}
27+
28+
public Room(JSONObject room) throws JSONException, ParseException {
29+
this.name = room.getString("name");
30+
this.code = room.getString("code");
31+
this.capacity = Optional.of(room.getInt("capacity"));
32+
this.available = room.getBoolean("available");
33+
this.currentEvents = new ArrayList<>();
34+
this.nextEvent = !room.getJSONObject("nextEvent").isNull("event") ? Optional.of(new Event(room.getJSONObject("nextEvent"))) : null;
35+
36+
if (room.has("currentEvents")) {
37+
for (int i = 0; i < room.getJSONArray("currentEvents").length(); i++) {
38+
this.currentEvents.add(new Event(room.getJSONArray("currentEvents").getJSONObject(i)));
39+
}
40+
}
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public String getCode() {
52+
return code;
53+
}
54+
55+
public void setCode(String code) {
56+
this.code = code;
57+
}
58+
59+
public Optional<Integer> getCapacity() {
60+
return capacity;
61+
}
62+
63+
public void setCapacity(Optional<Integer> capacity) {
64+
this.capacity = capacity;
65+
}
66+
67+
public boolean isAvailable() {
68+
return available;
69+
}
70+
71+
public void setAvailable(boolean available) {
72+
this.available = available;
73+
}
74+
75+
public List<Event> getCurrentEvents() {
76+
return currentEvents;
77+
}
78+
79+
public void setCurrentEvents(List<Event> currentEvents) {
80+
this.currentEvents = currentEvents;
81+
}
82+
83+
public Optional<Event> getNextEvent() {
84+
return nextEvent;
85+
}
86+
87+
public void setNextEvent(Optional<Event> nextEvent) {
88+
this.nextEvent = nextEvent;
89+
}
90+
}
91+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package dev.palmes.farapp.models;
2+
3+
import java.util.Date;
4+
import java.util.Optional;
5+
6+
public class RoomFilter {
7+
private Optional<Integer> date;
8+
private Optional<Integer> floor;
9+
private int capacity = 0;
10+
private int delta = 0;
11+
private Optional<Boolean> available;
12+
13+
public RoomFilter(Optional<Integer> date, Optional<Integer> floor, int capacity, int delta, Optional<Boolean> available) {
14+
this.date = date;
15+
this.floor = floor;
16+
this.capacity = capacity;
17+
this.delta = delta;
18+
this.available = available;
19+
}
20+
21+
public Optional<Integer> getDate() {
22+
return date;
23+
}
24+
25+
public void setDate(Optional<Integer> date) {
26+
this.date = date;
27+
}
28+
29+
public void setDate(int date) {
30+
this.date = Optional.of(date);
31+
}
32+
33+
public Optional<Integer> getFloor() {
34+
return floor;
35+
}
36+
37+
public void setFloor(Optional<Integer> floor) {
38+
this.floor = floor;
39+
}
40+
41+
public void setFloor(int floor) {
42+
this.floor = Optional.of(floor);
43+
}
44+
45+
public int getCapacity() {
46+
return capacity;
47+
}
48+
49+
public void setCapacity(int capacity) {
50+
this.capacity = capacity;
51+
}
52+
53+
public int getDelta() {
54+
return delta;
55+
}
56+
57+
public void setDelta(int delta) {
58+
this.delta = delta;
59+
}
60+
61+
public Optional<Boolean> getAvailable() {
62+
return available;
63+
}
64+
65+
public void setAvailable(Optional<Boolean> available) {
66+
this.available = available;
67+
}
68+
69+
public void setAvailable(boolean available) {
70+
this.available = Optional.of(available);
71+
}
72+
}

0 commit comments

Comments
 (0)