Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gypsy;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.DatePickerDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.NumberPicker;
- import android.widget.Spinner;
- import android.widget.Toast;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import com.google.firebase.storage.FirebaseStorage;
- import com.google.firebase.storage.StorageReference;
- import com.google.firebase.storage.UploadTask;
- import com.google.android.gms.tasks.OnFailureListener;
- import com.google.android.gms.tasks.OnSuccessListener;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- public class BookForm extends AppCompatActivity {
- private Spinner roomPreferenceSpinner;
- private List<String> roomTypeList = new ArrayList<>();
- private EditText fullNameET;
- private EditText contactNumberET;
- private EditText emailET;
- private EditText checkInDateET;
- private EditText checkOutDateET;
- private NumberPicker adultsNumberPicker;
- private NumberPicker childrenNumberPicker;
- private EditText yourGcashNameET;
- private EditText yourGcashNumberET;
- private EditText referenceNumberET;
- private Button submitButton;
- private ImageView attachedImageView;
- private static final int PICK_IMAGE_REQUEST = 1;
- private Uri selectedImageUri; // To store the selected image URI
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_book_form);
- Intent intent = getIntent(); // kunin yung hotelId sa DynamicHotel para sa
- String hotelId = intent.getStringExtra("hotelKey"); // personalized fields nila
- Log.d("BookFormActivity", "hotelId: " + hotelId); // sa book info like roomtypes etc
- DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
- DatabaseReference roomPreferencesRef = databaseReference.child("hotels").child(hotelId).child("roomTypes");
- fullNameET = findViewById(R.id.fullNameET);
- contactNumberET = findViewById(R.id.contactNumberET);
- emailET = findViewById(R.id.emailET);
- submitButton = findViewById(R.id.submitButton);
- roomPreferenceSpinner = findViewById(R.id.roomPreferenceSpinner);
- checkInDateET = findViewById(R.id.checkInDateET);
- checkOutDateET = findViewById(R.id.checkOutDateET);
- yourGcashNameET = findViewById(R.id.yourGcashNameET);
- yourGcashNumberET = findViewById(R.id.yourGcashNumberET);
- referenceNumberET = findViewById(R.id.referenceNumberET);
- attachedImageView = findViewById(R.id.attachedImageView);
- ArrayAdapter<String> roomTypeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, roomTypeList); // list string ng mga items
- roomTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // built in layout ni AS para sa dropdown selection
- roomPreferenceSpinner.setAdapter(roomTypeAdapter);
- adultsNumberPicker = findViewById(R.id.adultsNumberPicker);
- childrenNumberPicker = findViewById(R.id.childrenNumberPicker);
- adultsNumberPicker.setMinValue(0);
- adultsNumberPicker.setMaxValue(10);
- childrenNumberPicker.setMinValue(0);
- childrenNumberPicker.setMaxValue(10);
- checkInDateET.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showDatePickerDialog(checkInDateET);
- }
- });
- checkOutDateET.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showDatePickerDialog(checkOutDateET);
- }
- });
- roomPreferencesRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) { // then loop para mag iterate sa children ng DataSnapshot
- for (DataSnapshot preferenceSnapshot : dataSnapshot.getChildren()) {
- String roomPreference = preferenceSnapshot.getValue(String.class);
- roomTypeList.add(roomPreference);
- }
- roomTypeAdapter.notifyDataSetChanged();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- Log.e("BookForm", "error" + databaseError.getMessage());
- }
- });
- submitButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (allFields()) {
- FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
- if (currentUser != null) {
- String userId = currentUser.getUid();
- String fullName = fullNameET.getText().toString();
- String checkInDate = checkInDateET.getText().toString();
- String checkOutDate = checkOutDateET.getText().toString();
- int adults = adultsNumberPicker.getValue();
- int children = childrenNumberPicker.getValue();
- String contactNumber = contactNumberET.getText().toString();
- String email = emailET.getText().toString();
- String roomPreference = roomPreferenceSpinner.getSelectedItem().toString();
- String yourGcashName = yourGcashNameET.getText().toString();
- String yourGcashNumber = yourGcashNumberET.getText().toString();
- String referenceNumber = referenceNumberET.getText().toString();
- // Save the booking data, including the image
- save(userId, hotelId, fullName, checkInDate, checkOutDate,
- adults, children, contactNumber, email, roomPreference,
- yourGcashName, yourGcashNumber, referenceNumber);
- Toast.makeText(BookForm.this, "Booking submitted.", Toast.LENGTH_SHORT).show();
- } else {
- Log.e("BookForm", "User is not authenticated");
- }
- } else {
- Toast.makeText(BookForm.this, "Please fill in all required fields.", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // Attach an OnClickListener to the "Attach Photo" button
- Button attachPhotoButton = findViewById(R.id.attachPhotoButton);
- attachPhotoButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- openFileChooser();
- }
- });
- }
- // Open the file chooser to select an image
- private void openFileChooser() {
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, PICK_IMAGE_REQUEST);
- }
- // Handle the selected image
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
- selectedImageUri = data.getData();
- attachedImageView.setImageURI(selectedImageUri);
- attachedImageView.setVisibility(View.VISIBLE);
- }
- }
- // Save booking data and the attached image to Firebase
- private void save(String userId, String hotelId, String fullName, String checkInDate, String checkOutDate,
- int adults, int children, String contactNumber, String email, String roomPreference,
- String yourGcashName, String yourGcashNumber, String referenceNumber) {
- FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
- if (currentUser != null) {
- String userType = "Tourist"; // select yung user type which is tourist
- // kukunin yung name for Tourist user type
- String userPath = "users/" + userType + "/" + currentUser.getUid() + "/name";
- DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
- DatabaseReference userRef = databaseReference.child(userPath);
- userRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- String userName = dataSnapshot.getValue(String.class);
- DatabaseReference hotelRef = databaseReference.child("hotels").child(hotelId).child("user_id");
- hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot hotelDataSnapshot) {
- if (hotelDataSnapshot.exists()) {
- String hotelUserId = hotelDataSnapshot.getValue(String.class);
- // Store the image in Firebase Storage (replace "dummy_path" with your actual storage path)
- if (selectedImageUri != null) {
- StorageReference storageReference = FirebaseStorage.getInstance().getReference();
- // replace "dummy_path" with your actual storage path
- StorageReference imageRef = storageReference.child("dummy_path/" + userId + "/" + hotelId + "/image.jpg");
- imageRef.putFile(selectedImageUri)
- .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- // Image uploaded successfully, you can save its download URL if needed
- // Uri downloadUrl = taskSnapshot.getDownloadUrl();
- Log.d("BookForm", "Image uploaded successfully");
- // Continue with saving other booking data to Firebase
- // ...
- // Display a success message to the user
- Toast.makeText(BookForm.this, "Booking submitted.", Toast.LENGTH_SHORT).show();
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Log.e("BookForm", "Image upload failed: " + e.getMessage());
- // Handle image upload failure here
- }
- });
- }
- // store na under booking_info and generate ng unique id
- DatabaseReference bookingsRef = databaseReference.child("booking_info").push();
- String bookingId = bookingsRef.getKey();
- bookingsRef.child("userId").setValue(userId);
- bookingsRef.child("fullName").setValue(fullName);
- bookingsRef.child("checkInDate").setValue(checkInDate);
- bookingsRef.child("checkOutDate").setValue(checkOutDate);
- bookingsRef.child("adults").setValue(adults);
- bookingsRef.child("children").setValue(children);
- bookingsRef.child("contactNumber").setValue(contactNumber);
- bookingsRef.child("email").setValue(email);
- bookingsRef.child("roomPreference").setValue(roomPreference);
- bookingsRef.child("yourGcashName").setValue(yourGcashName);
- bookingsRef.child("yourGcashNumber").setValue(yourGcashNumber);
- bookingsRef.child("referenceNumber").setValue(referenceNumber);
- bookingsRef.child("userName").setValue(userName);
- bookingsRef.child("hotelUserId").setValue(hotelUserId);
- Log.d("BookForm", "Booking data saved");
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError hotelDatabaseError) {
- Log.e("BookForm", "Hotel database error: " + hotelDatabaseError.getMessage());
- }
- });
- } else {
- Log.e("BookForm", "User data does not exist in the database");
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- Log.e("BookForm", "Database error: " + databaseError.getMessage());
- }
- });
- } else {
- Log.e("BookForm", "User is not authenticated");
- }
- }
- private void showDatePickerDialog(final EditText editText) {
- final Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH);
- int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
- DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- // Adjust month index and set the selected date in the EditText
- String selectedDate = (monthOfYear + 1) + "/" + dayOfMonth + "/" + year;
- editText.setText(selectedDate);
- }
- }, year, month, dayOfMonth);
- datePickerDialog.show();
- }
- private boolean allFields() {
- return !fullNameET.getText().toString().isEmpty() &&
- !contactNumberET.getText().toString().isEmpty() &&
- !emailET.getText().toString().isEmpty() &&
- !checkInDateET.getText().toString().isEmpty() &&
- !checkOutDateET.getText().toString().isEmpty() &&
- !roomPreferenceSpinner.getSelectedItem().toString().isEmpty() &&
- !yourGcashNameET.getText().toString().isEmpty() &&
- !yourGcashNumberET.getText().toString().isEmpty() &&
- !referenceNumberET.getText().toString().isEmpty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement