#[SOLVED] Button Clickable every 24h

44 messages · Page 1 of 1 (latest)

turbid ether
#

The most recommended or ideal way for this is not sending the code like you're doing currently (client sided) but server sided with a function

#

That way you're 100% sure 24h has passed from last time the button was clicked and the user can't "hack" the app to override the timer

frozen fiber
#

how can I get started with function ?

turbid ether
#

For that you will need an appwrite function with database that checks latest time the user clicked the button and if it was after 24h from latest time, you send the command

turbid ether
#

New docs have improved information

frozen fiber
#

thx, if I ever need help I can write here ?

turbid ether
#

Yes, of course!

frozen fiber
#

aight

turbid ether
#

If you face any errors or have doubts, don't hesitate to write here 😁

frozen fiber
#

thank you man

#

last thing

#

before starting that topics what do you suggest me to learn or have a basic knowledge

#

like http request like i see in the docs or more

turbid ether
#

You're doing this in flutter, right?

#

If so, you will need to learn (in the following order):

  • Auth for flutter
  • Databases API (in your case for flutter SDK)
  • Functions

It looks difficult, at least it appeared to me when I started, mainly functions, but believe me that everything is a lot easier than what looks to be

#

You don't need to learn another programming language or similar if going with flutter since Appwrite functions can be easily written with Dart (the programming language Flutter uses) unlike other Appwrite competitors

frozen fiber
#

thank you

#

you helped me

frozen fiber
#

hii

#

it's me again

#

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

#

because i want this to be subjective for each user

delicate aspen
civic vigil
# frozen fiber because i want this to be subjective for each user

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:

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.

The Databases service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access permissions.

All data returned by the Databases service are represented as structured JSON documents.

The Databases service can contain multiple databases, each database can co...

frozen fiber
frozen fiber
turbid ether
#

Because this is done server sided

#

Phone = client side
Appwrite = server side

frozen fiber
#

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

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

turbid ether
frozen fiber
#

i get it

#

thank you

frozen fiber
#

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)

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());
  }
}

civic vigil
#

Is there anything else you want to do?

frozen fiber
#

not at the moment

#

thank you for checking

civic vigil
#

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

frozen fiber
#

okk

#

thank you

#

you guys are awesome !