如何在应用程序中仅在特定时间段内启用某些功能?

时间:2018-07-20 13:30:04

标签: java android firebase firebase-realtime-database google-cloud-functions

我正在使用预订应用程序,该应用程序在一天中有不同的时间段(例如11:30,12:30),用户可以在其中预订他们的行程。但我不希望预订选项是全天开放,应该只是从某个时间开放到第二天。我听说这可以通过云功能来完成,如果可以的话,有人可以告诉我如何吗?

这是我的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;
}

我的数据库结构如下所示 enter image description here

2 个答案:

答案 0 :(得分:2)

您可以检查时间并在活动开始时基于它启用/禁用预订按钮。在预订活动中放入类似的支票,这样他们就不能直接启动它。

答案 1 :(得分:0)

Google Cloud Functions不具备该功能。您可能指的是schedule [1],但这是用来触发对函数的调用的。

您可以做的是将业务逻辑实现到函数本身中,使其始终在HTTP级别上可用,但实际上只能在某些小时预订一些东西(使用“ new Date();”获取当前时间)。

干杯

米格尔

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