2

I have a button that provides a reward to users. I want users to have the option to press the button up to 2 times every 24 hours. Is it possible to implement this without making users sign in?

One possible way to achieve this is by using Firebase and keeping track of the number of times a user has pressed the button and reset it every 24 hours. I believe this does require users to sign in, though, which is not ideal. Also, I don't think this is a very good way to solve my problem.

EDIT: local storage like shared preference may work out, but if the user clears the app cache, the data stored in shared preferences will be deleted as well. This means that the button press counter and time will be reset, and the user will be able to press the button up to 2 times again.

Is there any other way to achieve this?

Ken White
  • 123,280
  • 14
  • 225
  • 444

1 Answers1

0

You can use the shared_preferences plugin to keep track of different things.

This plugin will persist the data even if the app is closed.

Code example

create a method to save the last time:

Future<void> _saveLastActionTime() async{
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setInt('lastActionTime', DateTime.now().millisecondsSinceEpoch);

}

And now to retrieve it:

Future<int?> _getLastActionTime() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  return prefs.getInt('lastActionTime');
}

To get check whether the last action was more then 24hours ago:

Future<bool> _isLastActionMoreThan24HoursAgo() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int? lastActionTime = prefs.getInt('lastActionTime');
  if (lastActionTime == null) {
    return true;
  }
  DateTime lastActionDateTime = DateTime.fromMillisecondsSinceEpoch(lastActionTime);
  return DateTime.now().difference(lastActionDateTime).inHours > 24;
}
MendelG
  • 14,885
  • 4
  • 25
  • 52
  • I could do this, but couldn't users go to Settings->Applications->Clear Cache and delete it? – lostinparadise Dec 29 '22 at 22:25
  • @lostinparadise 1. I updated my answer with an additional example, 2. I don't think will delete it, but I might be wrong though. you can do a quick test... – MendelG Dec 29 '22 at 22:32
  • Thanks for testing it out. [This answer](https://stackoverflow.com/a/8916939/9718626) made me skeptical, but I guess it doesn't delete it. – lostinparadise Dec 29 '22 at 22:49
  • @lostinparadise So I tested it out. If the user clicks on "clear Cache" then it _won't_ be deleted. But, if the user clicks on "clear storage" then it will be deleted. However, the user gets a dialog warning letting them know before that theyr'e deleting the app database. – MendelG Dec 29 '22 at 22:50
  • I see. In the code, why return true if `lastActionTime` is null? – lostinparadise Dec 30 '22 at 03:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250745/discussion-between-lostinparadise-and-mendelg). – lostinparadise Dec 30 '22 at 03:31