Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
9.9 kB
1
Indexable
package com.example.paymenttesting;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

public class parkingLot extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "parkingLot";
    ImageView lot1,lot5,lot8;
    Dialog dialog;
    Button yes,no,reset;
    private boolean lot1Status,lot5Status,lot8Status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_lot);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        lot1 = findViewById(R.id.l1);
        lot5 = findViewById(R.id.l5);
        lot8 = findViewById(R.id.l8);


        dialog = new Dialog(parkingLot.this);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.background));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setCancelable(false);

        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;


        yes = dialog.findViewById(R.id.btn_yes);
        no = dialog.findViewById(R.id.btn_no);
        reset = findViewById(R.id.reset_btn);



        lot1.setOnClickListener(this);
        lot5.setOnClickListener(this);
        lot8.setOnClickListener(this);

//        final MediaPlayer mp1 = MediaPlayer.create(this,R.raw.click);
//        final MediaPlayer mp2 = MediaPlayer.create(this,R.raw.clicktwo);




        // In onCreate() method
        SharedPreferences sharedPref = getSharedPreferences("parkingLotStatus", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

        editor.apply();




        // In onCreate() method after storing parking lot status in shared preferences
        sharedPref = getSharedPreferences("parkingLotStatus", Context.MODE_PRIVATE);
         lot1Status = sharedPref.getBoolean("lot1", false);
         lot5Status = sharedPref.getBoolean("lot5", false);
         lot8Status = sharedPref.getBoolean("lot8", false);
        if (lot1Status) {
            lot1.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot1, getApplicationContext().getTheme()));
        }
        if (lot5Status) {
            lot5.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot5, getApplicationContext().getTheme()));
        }
        if (lot8Status) {
            lot8.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot8, getApplicationContext().getTheme()));
        }

        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                lot1.setImageDrawable(getResources().getDrawable(R.drawable.lot1, getApplicationContext().getTheme()));
                editor.putBoolean("lot1", false);
                lot1Status = false;
                //editor.apply();

                lot5.setImageDrawable(getResources().getDrawable(R.drawable.lot5, getApplicationContext().getTheme()));
                editor.putBoolean("lot5", false);
                lot5Status = false;
               // editor.apply();

                lot8.setImageDrawable(getResources().getDrawable(R.drawable.lot8, getApplicationContext().getTheme()));
                editor.putBoolean("lot8", false);
                lot8Status = false;
                editor.apply();



            }
        });



    }

    @Override
    public void onClick(View view) {

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        FirebaseAuth mAuth = FirebaseAuth.getInstance();

        DocumentReference userRef = db.collection("Users").document(mAuth.getCurrentUser().getUid());




        // In onClick() method
        SharedPreferences sharedPref = getSharedPreferences("parkingLotStatus", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        switch (view.getId()) {

            case R.id.l1:
                if (lot1Status) {

                    Toast.makeText(this, "Already booked", Toast.LENGTH_SHORT).show();
                    // lot1 is already booked
                    
                } else {
                    dialog.show();
                    yes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            lot1.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot1, getApplicationContext().getTheme()));
                            Toast.makeText(parkingLot.this, "Lot1 Booked!", Toast.LENGTH_SHORT).show();
                            editor.putBoolean("lot1", true);
                            editor.apply();
                            dialog.dismiss();


                            userRef.update("booked", "true")
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Log.d(TAG, "New field added for user");
                                            Toast.makeText(parkingLot.this, "Success", Toast.LENGTH_SHORT).show();
                                        }
                                    })
                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.w(TAG, "Error adding new field for user", e);
                                            Toast.makeText(parkingLot.this, "Not Success", Toast.LENGTH_SHORT).show();

                                        }
                                    });

                        }
                    });
                    no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(parkingLot.this, "Lot1 Cancelled", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
                }
                break;
            // handle lot5 and lot8 bookings in a similar way


            case R.id.l5:
                if (lot5Status) {

                    Toast.makeText(this, "Already booked", Toast.LENGTH_SHORT).show();


                } else {
                    dialog.show();
                    yes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            lot5.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot5, getApplicationContext().getTheme()));
                            Toast.makeText(parkingLot.this, "Lot5 Booked!", Toast.LENGTH_SHORT).show();
                            editor.putBoolean("lot5", true);
                            editor.apply();
                            dialog.dismiss();
                        }
                    });
                    no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(parkingLot.this, "Lot5 Cancelled", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
                }
                break;

            case R.id.l8:
                if (lot8Status) {

                    Toast.makeText(this, "Already booked", Toast.LENGTH_SHORT).show();

                    // lot8 is already booked
                   
                } else {
                    dialog.show();
                    yes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            lot8.setImageDrawable(getResources().getDrawable(R.drawable.bookedlot8, getApplicationContext().getTheme()));
                            Toast.makeText(parkingLot.this, "Lot8 Booked!", Toast.LENGTH_SHORT).show();
                            editor.putBoolean("lot8", true);
                            editor.apply();
                            dialog.dismiss();
                        }
                    });
                    no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(parkingLot.this, "Lot8 Cancelled", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
                }
                break;
        }

    }

}