Often times in interfaces we have this use case .
...
where we have a datasource
...
that will give us information with a sequence of incrementing IDs. For instance the IMAP email protocol is an example of this.
It’s nice to control where you start from and keep incrementing the value as we go forward. The COUNT library solves this problem.
Expand |
---|
So I need an easy way to begin there and then have my component keep track of what ones it has already and get the next ones etc.title | We need to have a convenient way to set the ID we want to begin from but we need to keep on incrementing it |
---|
| So for my email I would like to begin at say message 10000 and then progressively keep moving forward from there.
| Import the COUNT library into your project. |
---|
|
See Import Library if you need a refresher on how to do it. |
Expand |
---|
title | Create a custom number field called 'NextId' and set it to the value 1 in your card. |
---|
|
See Custom Fields |
Expand |
---|
title | The COUNT library solves this problem by giving a convenient interface to GET/SET a custom field value |
---|
|
Remember Custom Fields the values for these are stored on disc in JSON. This means we can load and save them programmatically. This is what this library does. |
Expand |
---|
title | local NextId = COUNTget("NextMailIdNextId") is how we fetch the data |
---|
|
This is how we get the data from a custom field called NextMailId.NextId. Code Block |
---|
local NextId = COUNTget("NextId") |
|
Expand |
---|
title | COUNTset("NextMailIdNextId", NextId) is how we set it. |
---|
|
So this saves the new value of the custom field in the custom field value file. Code Block |
---|
COUNTset("NextId", NextId) |
|
Expand |
---|
title | It means we can see the NextMailId NextId in the card view of the component - and we can set it too! |
---|
| Image Removed |
|
Expand |
---|
title | This is a complete little program to increment a custom number field |
---|
|
Code Block |
---|
require "COUNT.COUNT"
function main(Data)
--Poll the component every 5 seconds
component.setTimer{delay=5000}
local NextId = COUNTget("NextId")
NextId = NextId+1
COUNTset('NextId', NextId)
end |
|
This is a very useful pattern for lots of ‘feeder’ interfaces.