-1

I am working on a Booking App ,where there are different time slots in a day(for example 11:30,12:30) in which users can book their ride.But I don't want the booking option to be open for the whole day,it should just be open from a certain time until the next day.I've heard this can be done by cloud functions,if so ,can anyone tell me how?

This is my BookingActivity.java

public class BookingActivity extends AppCompatActivity {

private DatabaseReference mUser;
private DatabaseReference mTime1;
private DatabaseReference mTime2;
private DatabaseReference mCount1;
private DatabaseReference mCount2;
private FirebaseAuth mAuth;
private static final String TAG = "BookingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking);

mAuth = FirebaseAuth.getInstance();

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FirebaseUser fu = mAuth.getCurrentUser();
final User newUser = new User(fu);

mUser = FirebaseDatabase.getInstance().getReference().child("Users");
mTime1 = FirebaseDatabase.getInstance().getReference().child("3:30");
mTime2 = FirebaseDatabase.getInstance().getReference().child("5:30");
mCount1 = FirebaseDatabase.getInstance().getReference().child("Count@3:30");
mCount2 = FirebaseDatabase.getInstance().getReference().child("Count@5:30");

findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Update(mTime1,mCount1,newUser);

    }
});

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Update(mTime2,mCount2,newUser);
    }
});

}


public void Book(DatabaseReference mDatabase,User user) {

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

HashMap<String,String>datamap = new HashMap<>();

if(user!=null) {
    datamap.put("Name", user.getUserName());
    datamap.put("Email", user.getUserEmail());
    datamap.put("timestamp",ts);
    datamap.put("Limit", user.setRating("1"));
}

 mDatabase.push().setValue(datamap);

}

public void Update(final DatabaseReference mDatabase, DatabaseReference mref,final User user) {

    mref.runTransaction(new Transaction.Handler() {
    @NonNull
    @Override
    public Transaction.Result doTransaction(@NonNull MutableData mutableData) {

        Integer CurrentValue = mutableData.getValue(Integer.class);
        if(CurrentValue==null) {
            return Transaction.success(mutableData);
        }
        else if(CurrentValue<5 && user.getRating().equals("0")){
            mutableData.setValue(CurrentValue + 1);
            Book(mDatabase,user);
            runOnUiThread(new Runnable() {
                public void run() {
                    final Toast toast = Toast.makeText(BookingActivity.this,"Booked Successfully",Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
         }
         else{
            runOnUiThread(new Runnable() {
                public void run() {
                    final Toast toast = Toast.makeText(BookingActivity.this,"Maximum Limit Reached",Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
        }
        return Transaction.success(mutableData);

    }

    @Override
    public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {

        Log.d(TAG, "Updating likes count transaction is completed");

    }
});
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case R.id.menuLogout:

        FirebaseAuth.getInstance().signOut();
        finish();
        startActivity(new Intent(this, MainActivity.class));

        break;
}

return true;
}

My database structure looks like this enter image description here

2 Answers2

2

You check the time and enable/disable the booking button based on it when your activity starts. Put a similar check inside the booking activity so they can't launch it directly.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Such feature doesn't exist on Google Cloud Functions. You may be referring to scheduling[1], but that is used to trigger calls to functions.

What you can do is implement that business logic into the function itself, leaving it always available on the HTTP level, but only having it actually book something at certain hours (get the current time with "new Date();").

Cheers,

Miguel

[1] https://medium.com/google-cloud/google-cloud-functions-scheduling-cron-5657c2ae5212