Advertisement
kitlolz012

DynamicHotel only

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