Advertisement
kitlolz012

DynamicHotel

Nov 17th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.26 KB | Source Code | 0 0
  1.  
  2. package com.example.gypsy;
  3.  
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13.  
  14. import androidx.annotation.NonNull;
  15. import androidx.appcompat.app.AppCompatActivity;
  16. import androidx.viewpager2.widget.ViewPager2;
  17.  
  18. import com.google.android.material.appbar.CollapsingToolbarLayout;
  19. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  20. import com.google.firebase.auth.FirebaseAuth;
  21. import com.google.firebase.auth.FirebaseUser;
  22. import com.google.firebase.database.DataSnapshot;
  23. import com.google.firebase.database.DatabaseError;
  24. import com.google.firebase.database.DatabaseReference;
  25. import com.google.firebase.database.FirebaseDatabase;
  26. import com.google.firebase.database.Query;
  27. import com.google.firebase.database.ValueEventListener;
  28. import com.squareup.picasso.Picasso;
  29.  
  30. import java.util.ArrayList;
  31. import java.util.List;
  32.  
  33. public class DynamicHotel extends AppCompatActivity {
  34.  
  35.     private ViewPager2 imageSliderViewPager;
  36.     private HotelImageSliderAdapter imageSliderAdapter;
  37.     private ArrayList<Uri> imageUris = new ArrayList<>();
  38.     private LinearLayout sliderDotsPanel;
  39.     private Button bookButton;
  40.     private FloatingActionButton messageButton;
  41.     private String recepientName;
  42.     private String hotelUserId;
  43.  
  44.     private String currentUserName;
  45.     private FirebaseAuth mAuth;
  46.     private String currentUserId;
  47.  
  48.  
  49.     @Override
  50.     protected void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.activity_dynamic_hotel);
  53.  
  54.         // retrieve intent galing sa hotel fragment
  55.         Intent intent = getIntent();
  56.         String hotelId = intent.getStringExtra("hotelKey");
  57.         Log.d("DynamicHotel", "hotelId: " + hotelId);
  58.  
  59.         mAuth = FirebaseAuth.getInstance();
  60.         FirebaseUser currentUser = mAuth.getCurrentUser();
  61.         if (currentUser != null) {
  62.             currentUserId = currentUser.getUid();
  63.             Log.d("DynamicHotel", "currentUserId: " + currentUserId);
  64.         }
  65.  
  66.         bookButton = findViewById(R.id.book_button);
  67.         messageButton = findViewById(R.id.messageButton);
  68.  
  69.         imageSliderViewPager = findViewById(R.id.imageSliderViewPager);
  70.         sliderDotsPanel = findViewById(R.id.dotsLayout2);
  71.  
  72.         imageSliderAdapter = new HotelImageSliderAdapter(this, imageUris, imageSliderViewPager, sliderDotsPanel);
  73.         imageSliderViewPager.setAdapter(imageSliderAdapter);
  74.  
  75.         imageSliderAdapter.updateDotsIndicator(0);
  76.         imageSliderViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
  77.             @Override
  78.             public void onPageSelected(int position) {
  79.                 super.onPageSelected(position);
  80.                 imageSliderAdapter.updateDotsIndicator(position);
  81.             }
  82.         });
  83.  
  84.         bookButton.setOnClickListener(new View.OnClickListener() {
  85.             @Override
  86.             public void onClick(View v) {
  87.                 Intent intent = new Intent(DynamicHotel.this, BookForm.class);
  88.                 intent.putExtra("hotelKey", hotelId);
  89.                 Log.d("bookButton", "hotelId: " + hotelId);
  90.                 startActivity(intent);
  91.             }
  92.         });
  93.  
  94.         Button mapButton = findViewById(R.id.map);
  95.  
  96.         mapButton.setOnClickListener(new View.OnClickListener() {
  97.             @Override
  98.             public void onClick(View v) {
  99.                 Intent intent = new Intent(DynamicHotel.this, MainActivity5.class);
  100.                 intent.putExtra("hotelId", hotelId); // Pass the specific hotelId
  101.                 startActivity(intent);
  102.             }
  103.         });
  104.  
  105.         queryHotelUserId(hotelId);
  106.         retrieveHotelUserName(hotelId);
  107.         retrieveCurrentUserName(currentUserId);
  108.  
  109.         messageButton.setOnClickListener(new View.OnClickListener() {
  110.             @Override
  111.             public void onClick(View v) {
  112.  
  113.                             Intent intent = new Intent(DynamicHotel.this, TouristHotelChat.class);
  114.                             intent.putExtra("recipientUserId", hotelUserId);
  115.                             intent.putExtra("recipientUserName", recepientName);
  116.                             intent.putExtra("currentUserName", currentUserName);
  117.  
  118.                             Log.d("DynamicHotel messageButton", "recipientUserId : " + hotelUserId);
  119.                             Log.d("DynamicHotel messageButton", "recipientName : " + recepientName);
  120.                             Log.d("DynamicHotel messageButton", "currentUserName: " + currentUserName);
  121.                             startActivity(intent);
  122.                         }
  123.                     });
  124.  
  125.         retrieve(hotelId);
  126.     }
  127.  
  128.     public void retrieve(String hotelId){
  129.  
  130.         TextView hotelNameTV = findViewById(R.id.nameTextView);
  131.         TextView hotelDescriptionTV = findViewById(R.id.descriptionTextView);
  132.         TextView hotelAddressTV = findViewById(R.id.addressTextView);
  133.         TextView hotelContactTV = findViewById(R.id.contactTextView);
  134.         TextView hotelEmailTV = findViewById(R.id.emailTextView);
  135.         TextView cancellationPolicyTV = findViewById(R.id.cancellationPolicyTextView);
  136.  
  137.         DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
  138.         DatabaseReference hotelRef = databaseReference.child("hotels").child(hotelId);
  139.  
  140.         hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
  141.             @Override
  142.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  143.                 Log.d("DynamicHotel", "onDataChange called");
  144.                 if (dataSnapshot.exists()) {
  145.                     String title = dataSnapshot.child("name").getValue(String.class);
  146.                     String hotelAddress = dataSnapshot.child("address").getValue(String.class);
  147.                     String description = dataSnapshot.child("description").getValue(String.class);
  148.                     String hotelContact = dataSnapshot.child("contact").getValue(String.class);
  149.                     String hotelEmail = dataSnapshot.child("email").getValue(String.class);
  150.                     String cancellationPolicy = dataSnapshot.child("cancellationPolicy").getValue(String.class);
  151.  
  152.                     hotelNameTV.setText(title);
  153.                     hotelDescriptionTV.setText(description);
  154.                     hotelAddressTV.setText(hotelAddress);
  155.                     hotelContactTV.setText(hotelContact);
  156.                     hotelEmailTV.setText(hotelEmail);
  157.                     cancellationPolicyTV.setText(cancellationPolicy);
  158.  
  159.                     imageUris.clear();
  160.                     DataSnapshot imagesSnapshot = dataSnapshot.child("images");
  161.                     for (DataSnapshot imageSnapshot : imagesSnapshot.getChildren()) {
  162.                         String imageUrl = imageSnapshot.getValue(String.class);
  163.                         Uri uri = Uri.parse(imageUrl);
  164.                         imageUris.add(uri);
  165.                     }
  166.  
  167.                     imageSliderAdapter.setImageUris(imageUris);
  168.                     imageSliderAdapter.notifyDataSetChanged();
  169.  
  170.                     // kahit wag na lagyan ng title
  171.                     //CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.Collapsing_toolbar);
  172.                     //collapsingToolbar.setTitle(title);
  173.  
  174.                 }
  175.             }
  176.             @Override
  177.             public void onCancelled(@NonNull DatabaseError databaseError) {
  178.                 Log.e("DynamicHotel", "Failed to retrieve hotel data: " + databaseError.getMessage());
  179.             }
  180.         });
  181.     }
  182.  
  183.     private void queryHotelUserId(String hotelId) {
  184.         DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
  185.         hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
  186.             @Override
  187.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  188.                 if (dataSnapshot.exists()) {
  189.                     hotelUserId = dataSnapshot.child("user_id").getValue(String.class);
  190.                     Log.d("DynamicHotel", "hotelUserId / recipientId: " + hotelUserId);
  191.  
  192.                     // You can perform any additional actions here after obtaining hotelUserId
  193.                 }
  194.             }
  195.  
  196.             @Override
  197.             public void onCancelled(@NonNull DatabaseError databaseError) {
  198.                 Log.e("DynamicHotel", "Database error: " + databaseError.getMessage());
  199.             }
  200.         });
  201.     }
  202.     private void retrieveHotelUserName(String hotelId) {
  203.         DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
  204.         hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
  205.             @Override
  206.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  207.                 if (dataSnapshot.exists()) {
  208.                     hotelUserId = dataSnapshot.child("user_id").getValue(String.class);
  209.                     Log.d("RetrieveHotelUserName", "hotelUserId: " + hotelUserId);
  210.                     retrieveHotelName(hotelUserId);
  211.                 }
  212.             }
  213.  
  214.             @Override
  215.             public void onCancelled(@NonNull DatabaseError databaseError) {
  216.                 Log.e("DynamicHotel", "Hotel user id retrieval cancelled: " + databaseError.getMessage());
  217.             }
  218.         });
  219.     }
  220.     private void retrieveHotelName(String hotelUserId) {
  221.         DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users").child("Hotel").child(hotelUserId);
  222.         usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
  223.             @Override
  224.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  225.                 if (dataSnapshot.exists()) {
  226.                     recepientName = dataSnapshot.child("name").getValue(String.class);
  227.                     Log.d("retrieveHotelName", "recepientName: " + recepientName);
  228.                 }
  229.             }
  230.  
  231.             @Override
  232.             public void onCancelled(@NonNull DatabaseError databaseError) {
  233.                 Log.e("DynamicHotel", "Hotel user name retrieval cancelled: " + databaseError.getMessage());
  234.             }
  235.         });
  236.     }
  237.     private void retrieveCurrentUserName(String currentUserId) {
  238.         DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users").child("Tourist").child(currentUserId);
  239.         Log.d("DynamicHotel", " User ref: " + usersRef);
  240.         usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
  241.             @Override
  242.             public void onDataChange(DataSnapshot dataSnapshot) {
  243.                 if (dataSnapshot.exists()) {
  244.                     currentUserName = dataSnapshot.child("name").getValue(String.class);
  245.                     Log.d("DynamicHotel", "Current User Name: " + currentUserName);
  246.                 }
  247.             }
  248.  
  249.             @Override
  250.             public void onCancelled(DatabaseError databaseError) {
  251.                 Log.e("BookingDetail", "Current user name retrieval cancelled: " + databaseError.getMessage());
  252.             }
  253.         });
  254.  
  255.     }
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement