Commit b55c0aaed09641df31972c5ed33155560fd9cb1e

Authored by kristinlaahneolsen
1 parent 8157129c

fikset litt på brukergrensesnitt for å få mer sammenheng i layoutstil, samt fort…

…satte å implementere endpoints
Risiko/app/build.gradle
... ... @@ -23,6 +23,8 @@ android {
23 23 }
24 24  
25 25 dependencies {
  26 + implementation 'com.squareup.retrofit2:retrofit:2.4.0'
  27 + implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
26 28 implementation fileTree(dir: 'libs', include: ['*.jar'])
27 29 implementation 'com.android.support:appcompat-v7:26.1.0'
28 30 implementation 'com.android.support.constraint:constraint-layout:1.1.3'
... ...
Risiko/app/src/main/AndroidManifest.xml
... ... @@ -2,6 +2,8 @@
2 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 3 package="com.risiko.risiko">
4 4  
  5 + <uses-permission android:name="android.permission.INTERNET" />
  6 +
5 7 <application
6 8 android:allowBackup="true"
7 9 android:icon="@mipmap/ic_launcher"
... ...
Risiko/app/src/main/java/activity/Activities.java
... ... @@ -3,15 +3,18 @@ package activity;
3 3 import android.content.Intent;
4 4 import android.support.v7.app.AppCompatActivity;
5 5 import android.os.Bundle;
6   -import android.view.View;
7   -import android.widget.AdapterView;
  6 +import android.util.Log;
8 7 import android.widget.ListView;
9 8 import com.risiko.risiko.R;
10 9  
11 10 import java.util.ArrayList;
12 11 import java.util.List;
13 12 import adapters.ListAdapterActivities;
  13 +import api.Api;
14 14 import dataclasses.Activity;
  15 +import retrofit2.Call;
  16 +import retrofit2.Callback;
  17 +import retrofit2.Response;
15 18  
16 19 public class Activities extends AppCompatActivity {
17 20 ListView lv;
... ... @@ -22,20 +25,42 @@ public class Activities extends AppCompatActivity {
22 25 setContentView(R.layout.activity_activities);
23 26  
24 27  
25   - //TODO Bytte ut Mockdata på sikt
  28 + /* //TODO Bytte ut Mockdata på sikt
26 29 ArrayList<Activity> activityList = new ArrayList<>();
27 30 activityList.add(new Activity(1, "Gi blodtrykksmedisin"));
28 31 activityList.add(new Activity(2, "Kjøre bil"));
29 32 activityList.add(new Activity(3, "Dosering av smertestillende"));
30 33 activityList.add(new Activity(4, "Gå ned trappen"));
31   - insertActivities(activityList);
  34 + insertActivities(activityList);*/
  35 +
  36 +
  37 + //TESTING AV RETROFIT: FUNKER KUN VED Å BRUKE DELING AV MOBILNETT
  38 +
  39 + Api.get().getAllActivities()
  40 + .enqueue(new Callback<List<Activity>>() {
  41 + @Override
  42 + public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
  43 + if (response.isSuccessful()) {
  44 + List<Activity> activities = response.body();
  45 + insertActivities(activities);
  46 + }
  47 + }
  48 +
  49 + @Override
  50 + public void onFailure(Call<List<Activity>> call, Throwable t) {
  51 + //Something went completely south (maybe you lost internet connection)
  52 + Log.d("Error", t.getMessage());
  53 + }
  54 + });
32 55 }
33 56  
  57 +
  58 +
34 59 private void insertActivities(List<Activity> activities) {
35 60 lv = findViewById(R.id.lv_activities);
36 61 lv.setAdapter(new ListAdapterActivities(this, activities));
37 62 lv.setOnItemClickListener((parent, view, position, id) -> {
38   - String nameOfChosenActivity = ((Activity) lv.getItemAtPosition(position)).getNameOfActivity();
  63 + String nameOfChosenActivity = ((Activity) lv.getItemAtPosition(position)).getTitle();
39 64 int activityId = ((Activity) lv.getItemAtPosition(position)).getId();
40 65 goToChosenActivity(nameOfChosenActivity, activityId);
41 66 });
... ...
Risiko/app/src/main/java/activity/Evaluations.java
... ... @@ -5,6 +5,7 @@ import android.graphics.Typeface;
5 5 import android.support.v7.app.AlertDialog;
6 6 import android.support.v7.app.AppCompatActivity;
7 7 import android.os.Bundle;
  8 +import android.util.Log;
8 9 import android.view.LayoutInflater;
9 10 import android.view.View;
10 11 import android.widget.Button;
... ... @@ -18,8 +19,12 @@ import java.util.List;
18 19  
19 20 import adapters.ListAdapterEvaluations;
20 21  
  22 +import api.Api;
21 23 import database.DatabaseSource;
22 24 import dataclasses.Evaluation;
  25 +import retrofit2.Call;
  26 +import retrofit2.Callback;
  27 +import retrofit2.Response;
23 28  
24 29 public class Evaluations extends AppCompatActivity {
25 30 ListView lv;
... ... @@ -34,6 +39,27 @@ public class Evaluations extends AppCompatActivity {
34 39  
35 40 dbs = new DatabaseSource(this);
36 41  
  42 +
  43 + //TESTING AV RETROFIT: FUNKER KUN VED Å BRUKE DELING AV MOBILNETT
  44 +
  45 + Api.get().getAllEvaluations()
  46 + .enqueue(new Callback<List<Evaluation>>() {
  47 + @Override
  48 + public void onResponse(Call<List<Evaluation>> call, Response<List<Evaluation>> response) {
  49 + if (response.isSuccessful()) {
  50 + List<Evaluation> evaluations = response.body();
  51 + insertEvaluations(evaluations);
  52 + }
  53 + }
  54 +
  55 + @Override
  56 + public void onFailure(Call<List<Evaluation>> call, Throwable t) {
  57 + //Something went completely south (maybe you lost internet connection)
  58 + Log.d("Error", t.getMessage());
  59 + }
  60 + });
  61 +
  62 +
37 63 Intent intent = getIntent();
38 64 String activityName = intent.getStringExtra("NAME_OF_CHOSEN_ACTIVITY");
39 65 int activityId = intent.getIntExtra("ID_OF_CHOSEN_ACTIVITY", -1);
... ...
Risiko/app/src/main/java/activity/Incident.java
... ... @@ -3,6 +3,7 @@ package activity;
3 3 import android.support.v7.app.AlertDialog;
4 4 import android.support.v7.app.AppCompatActivity;
5 5 import android.os.Bundle;
  6 +import android.util.Log;
6 7 import android.view.LayoutInflater;
7 8 import android.view.View;
8 9 import android.widget.AdapterView;
... ... @@ -15,8 +16,12 @@ import java.util.ArrayList;
15 16 import java.util.List;
16 17  
17 18 import adapters.ListAdapterIncidents;
  19 +import api.Api;
18 20 import database.DatabaseSource;
19 21 import dataclasses.Evaluation;
  22 +import retrofit2.Call;
  23 +import retrofit2.Callback;
  24 +import retrofit2.Response;
20 25  
21 26 public class Incident extends AppCompatActivity {
22 27 ListView lv;
... ... @@ -33,6 +38,25 @@ public class Incident extends AppCompatActivity {
33 38 ArrayList<dataclasses.Incident> incidentList = new ArrayList<>();
34 39 dbs.getAllIncidents(incidentList);
35 40 insertEvents(incidentList);
  41 +
  42 +
  43 + Api.get().getAllIncidents()
  44 + .enqueue(new Callback<List<dataclasses.Incident>>() {
  45 +
  46 + @Override
  47 + public void onResponse(Call<List<dataclasses.Incident>> call, Response<List<dataclasses.Incident>> response) {
  48 + if (response.isSuccessful()) {
  49 + List<dataclasses.Incident> incidents = response.body();
  50 + insertEvents(incidents);
  51 + }
  52 + }
  53 +
  54 + @Override
  55 + public void onFailure(Call<List<dataclasses.Incident>> call, Throwable t) {
  56 + //Something went completely south (maybe you lost internet connection)
  57 + Log.d("Error", t.getMessage());
  58 + }
  59 + });
36 60 }
37 61  
38 62  
... ...
Risiko/app/src/main/java/adapters/ListAdapterActivities.java
... ... @@ -30,7 +30,7 @@ public class ListAdapterActivities extends ArrayAdapter&lt;Activity&gt; {
30 30 }
31 31  
32 32 TextView activityName = convertView.findViewById(R.id.tv_activityName);
33   - activityName.setText(activity.getNameOfActivity());
  33 + activityName.setText(activity.getTitle());
34 34  
35 35  
36 36 return convertView;
... ...
Risiko/app/src/main/java/api/Api.java
1 1 package api;
2 2  
  3 +import retrofit2.Retrofit;
  4 +import retrofit2.converter.gson.GsonConverterFactory;
  5 +
3 6 /**
4   - * Created by kristinlaahneolsen on 01.11.2018.
  7 + * Retrofit singleton for API Communication
5 8 */
6 9  
7 10 public class Api {
  11 +
  12 + private static Endpoints instance;
  13 +
  14 + public static Endpoints get(){
  15 + if (instance == null){
  16 + Retrofit retrofit = new Retrofit.Builder()
  17 + .baseUrl(Endpoints.BASEURL)
  18 + .addConverterFactory(GsonConverterFactory.create())
  19 + .build();
  20 + instance = retrofit.create(Endpoints.class);
  21 + }
  22 + return instance;
  23 + }
8 24 }
... ...
Risiko/app/src/main/java/api/Endpoints.java
  1 +package api;
  2 +
1 3 import java.util.List;
2   -import java.util.Observable;
3 4  
  5 +import dataclasses.Activity;
  6 +import dataclasses.Evaluation;
4 7 import dataclasses.Incident;
  8 +import retrofit2.Call;
  9 +import retrofit2.http.Field;
5 10 import retrofit2.http.GET;
  11 +import retrofit2.http.POST;
  12 +import retrofit2.http.Path;
6 13  
7 14 /**
8   - * Interface for API Endpoints using Retrofit
  15 + * Interface for API api.Endpoints using Retrofit
9 16 */
10 17 public interface Endpoints {
11 18  
12 19  
  20 + // THE BASEURL HAS TO BE CHANGE TO THE IP-ADDRESS YOU ARE CONNECTED TO AT LOCALHOST
  21 + String BASEURL = "http://172.20.10.2:3000/";
  22 +
  23 + @GET("getAllEvaluations")
  24 + Call<List<Evaluation>> getAllEvaluations();
  25 +
  26 + @GET("getEvaluation/{id}")
  27 + Call<List<Evaluation>> getEvaluation(@Path("id") int id);
  28 +
  29 + @GET("getAllActivities")
  30 + Call<List<Activity>> getAllActivities();
  31 +
  32 + @GET("getAllIncidents")
  33 + Call<List<Incident>> getAllIncidents();
  34 +
  35 + @GET("getIncident/{id}")
  36 + Call<List<Incident>> getIncident(@Path("id") int id);
  37 +
  38 + @POST("newIncident")
  39 + Call<List<Incident>> newIncident(@Field("title") String title, @Field("id") int id, @Field("associatedeval") int associatedeval,
  40 + @Field("description") String description, @Field("location") String location);
  41 +
  42 + @POST("newEvaluation")
  43 + Call<List<Evaluation>> newEvaluation(@Field("title") String title, @Field("id") int id, @Field("associatedactivity") int associatedactivity,
  44 + @Field("consequence") int consequence, @Field("measures") String measures, @Field("outcome") String outcome, @Field("probability") int probability);
13 45  
14   - String BASEURL = "http://localhost:3000/";
15   - String API_BASEURL = BASEURL + "api/";
16 46  
17 47  
18   - @GET("incidents")
19   - Observable<List<Incident>> /
20 48 }
... ...
Risiko/app/src/main/java/dataclasses/Activity.java
... ... @@ -11,19 +11,19 @@ public class Activity {
11 11  
12 12 private UUID uuid;
13 13 private int id;
14   - private String nameOfActivity;
  14 + private String title;
15 15 private ArrayList<Activity> allActivities;
16 16  
17   - public Activity(String nameOfActivity) {
  17 + public Activity(String title) {
18 18 allActivities = new ArrayList<>();
19   - this.nameOfActivity = nameOfActivity;
  19 + this.title = title;
20 20 uuid = UUID.randomUUID();
21 21 }
22 22  
23 23 public Activity(int id, String name) {
24 24 this.id = id;
25 25 allActivities = new ArrayList<>();
26   - this.nameOfActivity = name;
  26 + this.title = name;
27 27 }
28 28  
29 29 public int getId() {
... ... @@ -34,11 +34,11 @@ public class Activity {
34 34 this.id = id;
35 35 }
36 36  
37   - public String getNameOfActivity() {
38   - return nameOfActivity;
  37 + public String getTitle() {
  38 + return title;
39 39 }
40 40  
41   - public void setNameOfActivity(String nameOfActivity) {
42   - this.nameOfActivity = nameOfActivity;
  41 + public void setTitle(String title) {
  42 + this.title = title;
43 43 }
44 44 }
... ...
Risiko/app/src/main/res/layout/activity_new_incident.xml
... ... @@ -10,6 +10,8 @@
10 10 android:layout_width="match_parent"
11 11 android:layout_height="match_parent"
12 12 android:orientation="vertical"
  13 + android:background="@color/colorWhite"
  14 + android:padding="10dp"
13 15 android:layout_margin="15dp">
14 16  
15 17  
... ...
Risiko/app/src/main/res/layout/activity_sequence_of_activities.xml
... ... @@ -10,6 +10,8 @@
10 10 android:layout_width="match_parent"
11 11 android:layout_height="match_parent"
12 12 android:orientation="vertical"
  13 + android:background="@color/colorWhite"
  14 + android:padding="10dp"
13 15 android:layout_margin="15dp">
14 16  
15 17  
... ...
~$pport - Distribuert server-klient.docx deleted
No preview for this file type