Commit 50f6be2d authored by RACODE977's avatar RACODE977

show free slots android page

parent 6d4c9f01
package com.example.vehicleparkingsystem;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.vehicleparkingsystem.Models.BaseURL;
import com.example.vehicleparkingsystem.SQLite.Sqlitedb;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class ParkingSlots extends AppCompatActivity {
LinearLayout L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12;
Button button;
String l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12;
ArrayList<Map<String, String>> SavedData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking_slots);
L1 = findViewById(R.id.l1);
L2 = findViewById(R.id.l2);
L3 = findViewById(R.id.l3);
L4 = findViewById(R.id.l4);
L5 = findViewById(R.id.l5);
L6 = findViewById(R.id.l6);
L7 = findViewById(R.id.l7);
L8 = findViewById(R.id.l8);
L9 = findViewById(R.id.l9);
L10 = findViewById(R.id.l10);
L11 = findViewById(R.id.l11);
L12 = findViewById(R.id.l12);
button = findViewById(R.id.availability);
L1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L1","1", L1);
}
});
L2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L2","1", L2);
}
});
L3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L3","1", L3);
}
});
L4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L4","1", L4);
}
});
L5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L5","10", L5);
}
});
L6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L6","1", L6);
}
});
L7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L7","1", L7);
}
});
L8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L8","1", L8);
}
});
L9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L9","1", L9);
}
});
L10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L10","1", L10);
}
});
L11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L11","1", L11);
}
});
L12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirmAlert("L12","1", L12);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SlotsIdentify().execute();
}
});
}
private class SlotsIdentify extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// SavedData = Sqlitedb.Search(ParkingSlots.this,"SELECT * FROM ParkingSlots",3);
// System.out.println(SavedData);
//
// if(!SavedData.isEmpty()){
// l1 = SavedData.get(0).get("2");
// l2 = SavedData.get(0).get("2");
// l3 = SavedData.get(0).get("2");
// l4 = SavedData.get(0).get("2");
// l5 = SavedData.get(0).get("2");
// l6 = SavedData.get(0).get("2");
// l7 = SavedData.get(0).get("2");
// l8 = SavedData.get(0).get("2");
// l9 = SavedData.get(0).get("2");
// l10 = SavedData.get(0).get("2");
// l11 = SavedData.get(0).get("2");
// l12 = SavedData.get(0).get("2");
//
//
// if(Objects.equals(l1, "1")){
// L1.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l2, "1")){
// L2.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l3, "1")){
// L3.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l4, "1")){
// L4.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l5, "1")){
// L5.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l6, "1")){
// L6.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l7, "1")){
// L7.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l8, "1")){
// L8.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l9, "1")){
// L9.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l10, "1")){
// L10.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l11, "1")){
// L11.setBackgroundColor(Color.RED);
// }else if(Objects.equals(l12, "1")){
// L12.setBackgroundColor(Color.RED);
// }
// }
}
@Override
protected void onPostExecute(String s) {
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... strings) {
try {
String Url = BaseURL.get_url() + "slot_availability";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(Url)
.get()
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody body = response.body();
JSONArray json = new JSONArray(body.string());
System.out.println(json);
for (int i = 0; i < json.length(); i++) {
l1 = json.getJSONObject(0).getString("1");
l2 = json.getJSONObject(0).getString("2");
l3 = json.getJSONObject(0).getString("3");
l4 = json.getJSONObject(0).getString("4");
l5 = json.getJSONObject(0).getString("5");
l6 = json.getJSONObject(0).getString("6");
l7 = json.getJSONObject(0).getString("7");
l8 = json.getJSONObject(0).getString("8");
l9 = json.getJSONObject(0).getString("9");
l10 = json.getJSONObject(0).getString("10");
l11 = json.getJSONObject(0).getString("11");
l12 = json.getJSONObject(0).getString("12");
}
if(Objects.equals(l1, "1")){
L1.setBackgroundColor(Color.RED);
}if(Objects.equals(l2, "1")){
L2.setBackgroundColor(Color.RED);
}if(Objects.equals(l3, "1")){
L3.setBackgroundColor(Color.RED);
}if(Objects.equals(l4, "1")){
L4.setBackgroundColor(Color.RED);
}if(Objects.equals(l5, "1")){
L5.setBackgroundColor(Color.RED);
}if(Objects.equals(l6, "1")){
L6.setBackgroundColor(Color.RED);
}if(Objects.equals(l7, "1")){
L7.setBackgroundColor(Color.RED);
}if(Objects.equals(l8, "1")){
L8.setBackgroundColor(Color.RED);
}if(Objects.equals(l9, "1")){
L9.setBackgroundColor(Color.RED);
}if(Objects.equals(l10, "1")){
L10.setBackgroundColor(Color.RED);
}if(Objects.equals(l11, "1")){
L11.setBackgroundColor(Color.RED);
}if(Objects.equals(l12, "1")){
L12.setBackgroundColor(Color.RED);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
} finally {
}
}
}
private void confirmAlert(String slot, String status, LinearLayout layout) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(ParkingSlots.this);
builder1.setMessage("Do you want to reserve this parking slot?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Sqlitedb.InsertData(ParkingSlots.this, "ParkingSlots",
"slot,status",
"'" + slot + "','" + status + "'");
layout.setBackgroundColor(Color.RED);
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment