Browse Source

ClientAlreadyExistConfirmation vue + requêtes

master
art.dambrine 5 years ago
parent
commit
f019e34af2
  1. 1
      app/src/main/AndroidManifest.xml
  2. 179
      app/src/main/java/com/speculos/myapplicationoc/ClientAlreadyExistConfirmation.java
  3. 2
      app/src/main/java/com/speculos/myapplicationoc/MyApplication.java
  4. 9
      app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java
  5. 131
      app/src/main/res/layout/activity_client_exist_confirmation.xml

1
app/src/main/AndroidManifest.xml

@ -25,6 +25,7 @@
<activity android:name=".TagDetectedActivity" />
<activity android:name=".TagDetectedActivityAdmin" />
<activity android:name=".PaymentActivity" />
<activity android:name=".ClientAlreadyExistConfirmation" />
</application>
</manifest>

179
app/src/main/java/com/speculos/myapplicationoc/ClientAlreadyExistConfirmation.java

@ -0,0 +1,179 @@
package com.speculos.myapplicationoc;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ClientAlreadyExistConfirmation extends Activity {
String hexa_id, mail, srvURL;
float solde, idClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Tweak pour contourner l'erreur android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// recuperation de MyApplication contenant les variables globales
MyApplication myApplication = (MyApplication) getApplication();
srvURL = myApplication.getServerURL();
setContentView(R.layout.activity_client_exist_confirmation);
hexa_id = getIntent().getStringExtra("hexa_id");
if (hexa_id == null) {
// TODO: afficher une erreur manque hexa_id
Toast.makeText(ClientAlreadyExistConfirmation.this, "Hexa_id non trouvé", Toast.LENGTH_LONG).show();
finish();
}
mail = getIntent().getStringExtra("mail");
/* Initialisation des info client*/
// Call API info client à partir du bracelet
String stringAPI = "";
try {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(2000, TimeUnit.MILLISECONDS)
.callTimeout(2000, TimeUnit.MILLISECONDS)
.build();
Request request = new Request.Builder()
.url(srvURL + "/api/clients.json?email=" + mail)
.method("GET", null)
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
String in = response.body().string();
in = in.substring(1, in.length() - 1); // on retire le [] autour de notre json premier et dernier char
Log.d("API-debug-in", in);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(in);
Log.d("API-debug", "json: "+ jsonObject.toString());
// jsonObject = new JSONObject(jsonObject.get("client").toString()); // recup client embed dans l'objet parent
} catch (JSONException e) {
Log.d("API-debug", e.getMessage());
}
String mail = null;
mail = jsonObject.getString("email");
solde = Float.parseFloat(jsonObject.getString("solde"));
idClient = Float.parseFloat(jsonObject.getString("id"));
TextView value_mail = findViewById(R.id.value_mail);
value_mail.setText(mail);
JSONArray jsonBracelets = new JSONArray(jsonObject.get("bracelets").toString());
Log.d("API-debug", "NBBRACELETS: " + jsonBracelets.length());
int nbBracelets = jsonBracelets.length();
if (nbBracelets > 0) {
TextView textNbBracelets = findViewById(R.id.textNbBracelets);
// 1 bracelet
textNbBracelets.setText("avec " + nbBracelets + " bracelet");
// plusieurs bracelets
if (nbBracelets > 1)
textNbBracelets.setText("avec " + nbBracelets + " bracelets");
} else {
TextView textNbBracelets = findViewById(R.id.textNbBracelets);
textNbBracelets.setText("sans bracelet");
}
} catch (IOException | JSONException e) {
e.printStackTrace();
Log.d("API-debug-exception", e.getMessage());
Toast.makeText(ClientAlreadyExistConfirmation.this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
TextView value_solde = findViewById(R.id.value_solde);
value_solde.setText(solde + " €");
// Bouton de confirmation d'enregistrement
Button button_enregistrer = findViewById(R.id.button_enregistrer);
button_enregistrer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Requête ajout du bracelet au compte
// On peut maintenant link le bracelet grace à l'hexa_id et l'idClient
try {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"nfcId\": \"" + hexa_id + "\",\"client\": \"/api/clients/" + idClient + "\",\"state\": 0}");
Request request = new Request.Builder()
.url(srvURL + "/api/bracelets")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response responseHttp = client.newCall(request).execute();
if (responseHttp.code() == 400) {
try {
JSONObject jsonReader = null;
jsonReader = new JSONObject(responseHttp.body().string());
Log.d("JSON-MSG", "violations: " + jsonReader.getJSONArray("violations").getString(0));
Toast.makeText(ClientAlreadyExistConfirmation.this, "violations: " + jsonReader.getJSONArray("violations").getString(0), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
finish();
}
});
// Bouton de retour
Button button_retour = findViewById(R.id.button_non);
button_retour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}

2
app/src/main/java/com/speculos/myapplicationoc/MyApplication.java

@ -4,7 +4,7 @@ import android.app.Application;
public class MyApplication extends Application {
private String serverURL = "http://art-dev:8080";
private String serverURL = "http://192.168.1.72:8080";
public String getServerURL(){
return serverURL;

9
app/src/main/java/com/speculos/myapplicationoc/TagDetectedActivityAdmin.java

@ -1,6 +1,7 @@
package com.speculos.myapplicationoc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
@ -181,7 +182,12 @@ public class TagDetectedActivityAdmin extends Activity {
} else {
// Le mail est déjà present en base
Toast.makeText(TagDetectedActivityAdmin.this, "Le mail est déjà présent en base. Souhaitez vous ajouter ce bracelet au compte: " + mail, Toast.LENGTH_LONG).show();
// TODO: rediriger vers une vue de confirmation
// Toast.makeText(TagDetectedActivityAdmin.this, "Le mail est déjà présent en base. Souhaitez vous ajouter ce bracelet au compte: " + mail, Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(TagDetectedActivityAdmin.this, ClientAlreadyExistConfirmation.class);
myIntent.putExtra("hexa_id", hexa_id);
myIntent.putExtra("mail",mail);
startActivity(myIntent);
}
@ -202,6 +208,5 @@ public class TagDetectedActivityAdmin extends Activity {
}
});
}
}

131
app/src/main/res/layout/activity_client_exist_confirmation.xml

@ -0,0 +1,131 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Un compte client existe"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/textNbBracelets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="avec ? bracelets"
android:textSize="20sp"
android:textStyle="bold"/>
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/layout_mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/label_mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Mail :"
android:textSize="30sp" />
<TextView
android:id="@+id/value_mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.7"
android:gravity="center"
android:text="-"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_solde"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/label_solde"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Solde :"
android:textSize="30sp" />
<TextView
android:id="@+id/value_solde"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.7"
android:gravity="center"
android:text="-"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button_enregistrer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Ajouter ce bracelet au compte" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<Button
android:id="@+id/button_non"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="retour" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Loading…
Cancel
Save