1

I try to develop "Reservation" application. It means: When the user clicks "Reserve" button it adds to database as "+1" of total reservation. But when the same user click "Unreserve" it subtracts from database as "-1". But the problem is that it works properly only when the internet connection is turn on.

In the second case, when internet is turn off and user click "Reserve" it show "1" in database(after turn on the internet) -> it means it not add value to database but just replace with this "1".

So:

(Online)

Database: "9" -> click "Reserve" value is increasing "+1".
Database after "Reserve": "10"
Database after "Unreserve": "9".

When user try to reserve when the internet is turn off it looks like:

(Offline)

Database: "9" -> click "Reserve" value is replacing with "+1".
Database after turn on internet "Reserve": "1"
Database after "Unreserve": "-1"

To resolve this situation in properly way, should I set that "Buttons" work only when the internet connection is turn on? Is this the easiest way to resolve this problem?

I read about onDisconnect() method but I didn't see any case how to set the firebase to work only with the user is online without any

I would appreciate if someone could give me a tip what should I implement to work this with any troubles.

gryzek
  • 537
  • 9
  • 25
  • Features like reservation would work properly only if the user is online, isn't it?. If the user is offline, let the user click the button but show toast, snackbar or dialog stating that the user needs to be connected to Internet in order to use this feature. Hope this help :) – Ye Min Htut Dec 08 '16 at 16:31
  • @YeMinHtut I thought about it but it doesn't resolve this problem. Because even if the user see this dialog, he can click this button accidentally and this will change values in database in wrong way. Solution, which I'm looking for is how to disable this button or another function when user is offline to prevent modifying of data. – gryzek Dec 08 '16 at 16:48
  • @YeMinHtut answer is correct.When the user clicks reserve button, first thing you do is check whether the user is connected to internet. (Here is the snippet to check if device is connected to internet - http://stackoverflow.com/questions/32547006/connectivitymanager-getnetworkinfoint-deprecated/40279055#40279055). If yes, then only process the reserve method. Else, show him a toast ! – Nishanth Sreedhara Dec 08 '16 at 17:47
  • @NishanthSreedhara Your solution work, Sir :) But could you tell me what could I set in your code, because when I turn on the internet I still can't click "Reserve", after reload activity with those fragment (your code is using in fragment class) again I can do this. Is there another way to set this, when automatically internet is turned on, I can immediately click "Reserve"? Thank you for your help! – gryzek Dec 08 '16 at 22:44

1 Answers1

-1
reserveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(Utils.isNetworkAvailable) {
                //process reservation
            } else {
                Toast.makeText(getApplicationContext(), "Your device is not connected to internet!", Toast.LENGTH_SHORT).show();
            }
        }
    });

Assuming, you are using the isNetworkAvailable method from the comments. This should work. Don't disable the button.

Nishanth Sreedhara
  • 1,266
  • 1
  • 19
  • 34