feat: seconde partie faite

This commit is contained in:
Azerothwav 2025-01-20 16:15:20 +01:00
parent ed84be2a46
commit 02fb881274
12 changed files with 247 additions and 10 deletions

View File

@ -36,7 +36,6 @@ android {
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
@ -45,4 +44,5 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation("com.android.volley:volley:1.2.1")
}

View File

@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -11,7 +14,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DepannTout"
tools:targetApi="31">
tools:targetApi="31"
android:usesCleartextTraffic="true"
>
<activity
android:name=".ClientDetails"
android:exported="false" />

View File

@ -7,6 +7,9 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject
class Home : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -30,5 +33,32 @@ class Home : AppCompatActivity() {
val intent = Intent(this, ListeInterventions::class.java)
startActivity(intent)
}
val buttonImport = findViewById<Button>(R.id.btnImport)
buttonImport.setOnClickListener() {
val queue = Volley.newRequestQueue(this)
val url = "http://192.168.48.185:8000/intervention"
val jsonObjectRequest = object : JsonObjectRequest(
Method.POST, url, null,
{ },
{ }) {
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
return headers
}
override fun getBody(): ByteArray {
val jsonObject = JSONObject()
jsonObject.put("date", "2022-01-05")
jsonObject.put("observation", "Ceci est un test")
jsonObject.put("idClient", 1)
return jsonObject.toString().toByteArray()
}
}
queue.add(jsonObjectRequest)
}
}
}

View File

@ -0,0 +1,86 @@
package com.example.depanntout
import android.content.Context
import android.util.Log
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.example.depanntout.bdd.Intervention
import org.json.JSONArray
class Importation {
fun importInterventionsFromServer(context: Context, callback: (List<Intervention>) -> Unit) {
val url = "http://192.168.48.185:8000/interventions"
val queue = Volley.newRequestQueue(context)
val interventionsList = mutableListOf<Intervention>()
val jsonObjectRequest = JsonArrayRequest(
Request.Method.GET,
url,
null,
{ response ->
val interventionsArray = response as JSONArray
for (i in 0 until interventionsArray.length()) {
val interventionJson = interventionsArray.getJSONObject(i)
val intervention = Intervention(
idIntervention = interventionJson.getInt("idIntervention"),
datetime = interventionJson.getString("date"),
observation = interventionJson.getString("observation"),
idClient = interventionJson.getInt("idClient")
)
interventionsList.add(intervention)
}
Toast.makeText(context, "Server response: ${interventionsList.size} interventions loaded", Toast.LENGTH_LONG).show()
callback(interventionsList)
},
{ error ->
Toast.makeText(context, "Server error: ${error.message}", Toast.LENGTH_SHORT).show()
callback(emptyList())
}
)
queue.add(jsonObjectRequest)
}
fun importClientsFromServer(context: Context, callback: (List<Client>) -> Unit) {
val url = "http://192.168.48.185:8000/clients"
val queue = Volley.newRequestQueue(context)
val clientsList = mutableListOf<Client>()
val jsonObjectRequest = JsonArrayRequest(
Request.Method.GET,
url,
null,
{ response ->
val clientsArray = response as JSONArray
for (i in 0 until clientsArray.length()) {
val clientJson = clientsArray.getJSONObject(i)
val client = Client(
idClient = clientJson.getInt("idClient"),
fistname = clientJson.getString("nom"),
lastname = clientJson.getString("prenom"),
email = clientJson.getString("email"),
tel = clientJson.getString("telephone"),
adresse = clientJson.getString("adresse"),
)
clientsList.add(client)
}
Toast.makeText(context, "Server response: ${clientsList.size} clients loaded", Toast.LENGTH_LONG).show()
callback(clientsList)
},
{ error ->
Toast.makeText(context, "Server error: ${error.message}", Toast.LENGTH_SHORT).show()
callback(emptyList())
}
)
queue.add(jsonObjectRequest)
}
}

View File

@ -52,11 +52,24 @@ class ListeClients : AppCompatActivity() {
val createBdDepannTout: CreateBdDepannTout = CreateBdDepannTout(this)
val dao: DAO = DAO(this, createBdDepannTout)
val clientDAO : ClientDAO = ClientDAO(dao)
val clients: List<Client> = clientDAO.getClients()
Log.d("bdd", clients.toString())
var clients: List<Client> = clientDAO.getClients()
val adapter = clientAdapter(clients, this)
recyclerView.adapter = adapter
val buttonImport = findViewById<Button>(R.id.btnImport)
buttonImport.setOnClickListener() {
Importation().importClientsFromServer(this) { clientsServer ->
if (clientsServer.isNotEmpty()) {
for (client: Client in clientsServer) {
clientDAO.createClient(client)
Log.d("Client server importer :", client.fistname + " " + client.lastname)
}
clients = clients.plus(clientsServer)
}
val adapter = clientAdapter(clients, this)
recyclerView.adapter = adapter
}
}
}
}

View File

@ -44,11 +44,24 @@ class ListeInterventions : AppCompatActivity() {
val createBdDepannTout: CreateBdDepannTout = CreateBdDepannTout(this)
val dao: DAO = DAO(this, createBdDepannTout)
val interventionDAO : InterventionDAO = InterventionDAO(dao)
val interventions: List<Intervention> = interventionDAO.getInterventions()
Log.d("bdd", interventions.toString())
var interventions: List<Intervention> = interventionDAO.getInterventions()
val adapter = interventionAdapter(interventions, this)
recyclerView.adapter = adapter
val buttonImport = findViewById<Button>(R.id.btnImport)
buttonImport.setOnClickListener() {
Importation().importInterventionsFromServer(this) { interventionsServer ->
if (interventionsServer.isNotEmpty()) {
for (intervention: Intervention in interventionsServer) {
interventionDAO.createIntervention(intervention)
Log.d("Intervention server importer :", intervention.idIntervention.toString())
}
interventions = interventions.plus(interventionsServer)
}
val adapter = interventionAdapter(interventions, this)
recyclerView.adapter = adapter
}
}
}
}

View File

@ -1,6 +1,8 @@
package com.example.depanntout.bdd
import android.util.Log
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.depanntout.Client
data class Intervention(

View File

@ -9,9 +9,12 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.example.depanntout.bdd.ClientDAO
import com.example.depanntout.bdd.CreateBdDepannTout
import com.example.depanntout.bdd.DAO
import org.json.JSONObject
class createClient : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -45,6 +48,32 @@ class createClient : AppCompatActivity() {
startActivity(intent)
Toast.makeText(this, "Client ajouter", Toast.LENGTH_SHORT).show()
finish()
val queue = Volley.newRequestQueue(this)
val url = "http://192.168.48.185:8000/client"
val jsonObjectRequest = object : JsonObjectRequest(
Method.POST, url, null,
{ },
{ }) {
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
return headers
}
override fun getBody(): ByteArray {
val jsonObject = JSONObject()
jsonObject.put("prenom", newClient.fistname)
jsonObject.put("nom", newClient.lastname)
jsonObject.put("email", newClient.email)
jsonObject.put("telephone", newClient.tel)
jsonObject.put("adresse", newClient.adresse)
return jsonObject.toString().toByteArray()
}
}
queue.add(jsonObjectRequest)
}
}
}

View File

@ -9,11 +9,16 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.depanntout.bdd.ClientDAO
import com.example.depanntout.bdd.CreateBdDepannTout
import com.example.depanntout.bdd.DAO
import com.example.depanntout.bdd.Intervention
import com.example.depanntout.bdd.InterventionDAO
import org.json.JSONObject
import java.lang.reflect.Method
class createIntervention: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -45,6 +50,30 @@ class createIntervention: AppCompatActivity() {
startActivity(intent)
Toast.makeText(this, "Intervention ajouter", Toast.LENGTH_SHORT).show()
finish()
val queue = Volley.newRequestQueue(this)
val url = "http://192.168.48.185:8000/intervention"
val jsonObjectRequest = object : JsonObjectRequest(
Method.POST, url, null,
{ },
{ }) {
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
return headers
}
override fun getBody(): ByteArray {
val jsonObject = JSONObject()
jsonObject.put("date", newIntervention.datetime)
jsonObject.put("observation", newIntervention.observation)
jsonObject.put("idClient", newIntervention.idClient)
return jsonObject.toString().toByteArray()
}
}
queue.add(jsonObjectRequest)
}
}
}

View File

@ -36,4 +36,14 @@
android:textSize="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnImport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="152dp"
android:layout_marginTop="380dp"
android:text="Importer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -20,7 +20,7 @@
android:id="@+id/btnQuit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="206dp"
android:layout_marginStart="112dp"
android:layout_marginTop="680dp"
android:text="Quitter"
app:layout_constraintStart_toStartOf="parent"
@ -46,4 +46,14 @@
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnImport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="204dp"
android:layout_marginTop="680dp"
android:text="Importer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -20,7 +20,7 @@
android:id="@+id/btnQuit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="206dp"
android:layout_marginStart="108dp"
android:layout_marginTop="680dp"
android:text="Quitter"
app:layout_constraintStart_toStartOf="parent"
@ -46,4 +46,14 @@
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnImport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="204dp"
android:layout_marginTop="680dp"
android:text="Importer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>