ограничить нажатие кнопки до 3 раз в неделю - PullRequest
0 голосов
/ 15 ноября 2018

У меня есть кнопка, которую я хочу ограничить нажатием до 3 раз в неделю.

Как я могу отследить это и убедиться, что после достижения максимального значения нажатие кнопки не произойдет?

Вот мой основной код для нажатия кнопки и подсчета.

int count = 0;
limit_ButtonPress()

Button btn = findViewById(R.id.comp)
btn.setOnClickListener(new OnClickListener){
    (View v){
    count++
}
private void limit_ButtonPress(){
    if(count> 0  && count< 5){
    btn.setVisabilty(View.Invisable)
} 

как теперь можно ограничить 3 нажатия в неделю? Спасибо, ребята

1 Ответ

0 голосов
/ 15 ноября 2018

посмотрите на это

, вы хотите, чтобы эту кнопку можно было нажимать только трижды в неделю, не более того.поэтому у нас есть такая структура базы данных firebase:

"root/" : {
    "users" : {
        "user_01" : {
                //some profile data about the user like name email etc.
                "data" : "val"
                //this is important
                "signUpTime" : 1563245126 //this is the timestamp of when the user signed up
            },
        ...
    }
   "activity" : {
       "user_01" : {
            random_id_01 : {
                "clickTime" : 156845164 //timestamp of click
            },
            ...
       }
   }
}

теперь она отвечает за определение доступа пользователей к базе данных для чтения и записи.

теперь идет часть java / android

private void initUI(){
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("activity").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    Query query = databaseReference.orderByChild("clickTime").limitToLast(3);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                //now count the children in dataSnapshot
                int totalChildren = (int) dataSnapshot.getChildrenCount();
                if(totalChildren > 2){
                    //now check for this person properly
                    int milliSecInAWeek = 7*24*60*60*1000; //perhaps i m right ;)
                    //now check if the last of the three click was within this week 
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                        //well this is actully a loop kind of thing
                        //but we are just conserned about the first most click so we will call return just on the first child itteration :)
                        UserActivity userActivity = snapshot.getValue(UserActivity.class);

                        if(userActivity.getClickTime() - System.currentTimeMillis() < milliSecInAWeek){
                            //this person has clicked thrre times in this week disable the button buddy ;)
                        }else{
                            //this person has got chance(s) to click. let the button be enables :) 
                        }
                        return;
                    }
                }else{
                    //this user has clicked less then 3 times so the let the button be clickable for the user
                }
            }else{
                //there is no activity by this user let the button be clickable
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private class UserActivity{

    private float clickTime;

    public UserActivity() {

    }

    public UserActivity(float clickTime) {
        this.clickTime = clickTime;
    }

    public float getClickTime() {
        return clickTime;
    }

    public void setClickTime(float clickTime) {
        this.clickTime = clickTime;
    }
}

надеюсь, что это поможет вам, и если он просто примет ответ, это немного поможет мне:)

счастливое кодирование !!!

...