Back

[SOLVED] Button Clickable every 24h

  • 0
  • Flutter
  • Android
  • Apple
  • Users
  • Self Hosted
Il BUS di sistema
2 Oct, 2023, 17:06

hii

TL;DR
The user had a question about making a button clickable every 24 hours, and it has been solved. The solution involved checking if the UserID exists in the database and either creating a new document or updating an existing one. The user also asked if it is possible to implement different cooldowns for each user, and it was suggested to use server-side code in an Appwrite Function to correctly send alerts to the users. The code provided shows how to check if the time stored in the document is more than 24 hours ago. If it is, the button can be shown.
Il BUS di sistema
2 Oct, 2023, 17:06

it's me again

Il BUS di sistema
2 Oct, 2023, 17:07

i started taking a look at function but i was wondering if this would work for separate user, supposing the user send an http post/get request at the function this would work for separate user or the same function would work for all the user

Il BUS di sistema
2 Oct, 2023, 17:08

because i want this to be subjective for each user

Tessa
2 Oct, 2023, 17:19

no need to tag the entire company 🙂 We always see your messages here without being tagged 🤗 btw that is amazing you are starting developing early on, you will be very successful because of this :Fire6_Orange:

@D5 thank you for helping this person 🙂

safwan
2 Oct, 2023, 17:31

In your case - as D5 suggested - you should use server-side code in an Appwrite Function to correctly send alerts to the users.

If you are storing the last time at which the user pressed the button in a document inside a database, what you can do is this:

Whenever the user starts the app, run a `databases.getDocument` call from your app and check if the time stored in the document is more than 24 hours ago. Something like this:

TypeScript
Future result = await databases.getDocument(
    databaseId: '[DATABASE_ID]',
    collectionId: '[COLLECTION_ID]',
    documentId: '[DOCUMENT_ID]',
);

final lastUserTime = result.timeByUser;  // timeByUser is the name of the attribute in the database's collection that holds the actual time

bool isMoreThan24HoursAgo(String isoTime) {
    DateTime dateTimeA = DateTime.parse(isoTime).toUtc();
    DateTime currentDateTime = DateTime.now().toUtc();

    Duration difference = currentDateTime.difference(dateTimeA);

    return difference.inHours > 24;
}

This will return true if more than 24 hours have passed, and false if less than 24 hours have passed.

If the value returned by the isMoreThan24HoursAgo function is true, you can show the button.

Il BUS di sistema
2 Oct, 2023, 18:25

@Tessa sorry for the tags and thank you for the support

Il BUS di sistema
2 Oct, 2023, 18:26

is this gonna work if the client move the time on his phone ?

D5
2 Oct, 2023, 18:30

Phone time will not affect

D5
2 Oct, 2023, 18:30

Because this is done server sided

D5
2 Oct, 2023, 18:31

Phone = client side Appwrite = server side

Il BUS di sistema
2 Oct, 2023, 18:33

but this line of code takes the date from the client side?

DateTime currentDateTime = DateTime.now().toUtc();

D5
2 Oct, 2023, 18:34

When you click the button, you create a new database record or update existing. Database API or function will record existing creation/modification time from server side

Il BUS di sistema
2 Oct, 2023, 18:35

i get it

Il BUS di sistema
2 Oct, 2023, 18:35

thank you

Il BUS di sistema
2 Oct, 2023, 21:22

hi, it's me again, do you guys think i can implement this logic to make a different "Cooldown" for every user? in the code below if the parameter UserID (wich i've taken and stored in a variable) is not found on the collection of the database that contains the UserID and the timeByUser, he'll create a new one, otherwhise i will update the exsisting one.

i'm also not sure this is the best practice to do this, but i think can be work good right?

(edit: iv've inplemented this today and when the user clicks the button it checks if there is the UserID already in the document otherwhise he will create a new document with that specific user id, also the GetUserID() is a function to get the id of the user in the current session)

TypeScript
void getuser() async {
  try {
    final userId = await GetUserID();

    Future<bool> userExist() async {
      try {
        final response = await databases.listDocuments(
          databaseId: '651b24f731b2855ab92d',
          collectionId: '651b253f714e3fb0c8a7',
          queries: [Query.equal('UserID', userId)],
        );

        // Verifica se ci sono documenti nel risultato
        return response.documents.isNotEmpty;
      } catch (e) {
        print(e.toString());
        return false;
      }
    }

    if (await userExist()) {
      print('UserID already exists in the collection');
    } else {
      await databases.createDocument(
        databaseId: '651b24f731b2855ab92d',
        collectionId: '651b253f714e3fb0c8a7',
        documentId: ID.unique(),
        data: {
          'UserID': userId, 
          'timeByUser': 'not_setted_yet',
        },
      );
      print("Document created");
    }
  } catch (e) {
    print(e.toString());
  }
}
safwan
3 Oct, 2023, 17:43

This code seems to the job of checking if user exists, and then either creating/updating document correctly

safwan
3 Oct, 2023, 17:43

Is there anything else you want to do?

Il BUS di sistema
3 Oct, 2023, 17:43

not at the moment

Il BUS di sistema
3 Oct, 2023, 17:44

thank you for checking

safwan
3 Oct, 2023, 17:53

If you have no more issues, I'll be marking this question as SOLVED. Feel free to make a new post for any other issue you face <:appwritepeepo:1156975874852270110>

Il BUS di sistema
3 Oct, 2023, 18:29

okk

Il BUS di sistema
3 Oct, 2023, 18:29

thank you

Il BUS di sistema
3 Oct, 2023, 18:29

you guys are awesome !

Drake
15 Oct, 2023, 03:25

[SOLVED] Button Clickable every 24h

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more