Fetch for blocks
You should use MBurgerTasks static methods to retrieve data from MBurger dashboard.
This class has public static functions that will call asynchronously MBurger API.
You have 2 ways to retrieve data from API:
    Listener
    Actions

Listener

The Listener approach is the easiest one.
With every method of the MBurgerTasks class you should pass the corresponding listener to return data from the function to your control.
All the listeners are inside the MBApiResultsListeners and all of them are simple interfaces that will return the MBurger object you asked for or an error message.
For example, you should use a MBApiProjectResultListener (implementing it to your Activity or Fragment or creating a new one runtime) to obtain Project basic data, so the class will implement these methods:
1
@Override
2
fun onProjectApiResult(project: MBProject) {
3
/*API call went well*/
4
}
5
6
@Override
7
fun onProjectApiError(error: String) {
8
/*There was an error, you should show it*/
9
}
Copied!

Actions

The Action approach is more complicated but more flexible.
It uses LocalBroadcastMessages to send data from the API to your Activity/Fragment using an Action which your Activity/Fragment is listening to.
You can also use this method also if you are doing your own API calls to your server. First you must implement MBGenericApiResultListener which will automatically implement the method.Java
1
@Override
2
fun onApiResult(response: MBAPIResponse) {
3
}
Copied!
For example, to obtain the blocks and the project you have to follow these few steps:
    Create a BroadcastReceiver.
    Initialize the BroadcastReceiver in your onResume method with the MBurgerApiActionInitializer class specific function.
    Pause the BroadcastReceiver in your onPause method with the MBurgerApiActionInitializer class specific function.
    Call the task to send the blocks’ request according to the parameters given in this sample.
    Fetch the blocks from the response variable of onApiResult.
The code will result:
1
override fun onResume() {
2
super.onResume();
3
val actions = arrayOf(MBAPIConstants.ACTION_GET_BLOCKS, MBAPIConstants.ACTION_GET_PROJECT)
4
bRec = MBurgerApiActionInitializer.initializeNookoReceiverCustom(this, this, actions)
5
MBurgerTasks.askForProject(this)
6
7
/**Add custom filters to the API call or leave it null**/
8
val arrayOfFilters: ArrayList<Any> = null
9
10
/**Inside the blocks returned the “sections” field will be valorized**/
11
val getSections = true
12
13
/**Inside the sections the “elements” field will not be valorized (null)**/
14
val getElements = false
15
MBurgerTasks.askForBlocks(this, arrayOfFilters, getSections, getElements)
16
MBurgerTasks.askForProject(this)
17
}
18
19
override fun onPause() {
20
super.onPause()
21
MBurgerApiActionInitializer.pauseNookoReceiver(this, bRec)
22
}
23
24
override fun onApiResult(MBAPIResponse response) {
25
/** Check ApiAction to distinguish the responses**/
26
if (response.getApiAction().equals(MBAPIConstants.ACTION_GET_BLOCKS)) {
27
if (response.getResult()) {
28
blocks = (ArrayList<MBBlock>)response.getPayload().get(MBApiPayloadKeys.key_blocks)
29
/** Once you received the project’s blocks you can draw your menu**/
30
} else {
31
Log.e(“ERROR RESPONSE API”, “THERE WAS AN ERROR DURING BLOCKS’ LOADING”)
32
}
33
}
34
35
if(response.getApiAction().equals(MBAPIConstants.ACTION_GET_PROJECT)){
36
if (response.getResult()) {
37
project = (MBProject)response.getPayload().get(MBApiPayloadKeys.key_project)
38
/** Your code**/
39
} else {
40
Log.e(“ERROR RESPONSE API”, “THERE WAS AN ERROR DURING BLOCKS’ LOADING”)
41
}
42
}
43
}
Copied!
Note that you can check the ApiAction to distinguish the different responses, and you will find all responses inside the MBAPIConstants class.
In case you previously initialized a BroadcastReceiver only for blocks this check is useless.
This is an example of a single-action BroadcastReceiver initialization (for blocks)
Single action initializationJava
1
override fun onResume() {
2
super.onResume();
3
bRec = MBurgerApiActionInitializer.initializeNookoReceiverForBlocks(this, this);
4
}
Copied!
Last modified 1yr ago
Copy link