Commit 4ab2377e authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Add the possibility to use the user info or the id given for the API

Edit interact function for API
Add useUsername in commands
parent b14efd35
...@@ -864,7 +864,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -864,7 +864,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
if (apiResponse.getPositiveCommand() != null && apiResponse.getPositiveCommand().getCommand() != null) if (apiResponse.getPositiveCommand() != null && apiResponse.getPositiveCommand().getCommand() != null)
startCommand(api, apiResponse.getId(), apiResponse.getPositiveCommand(), gingerResponse); startCommand(api, apiResponse.getId(), apiResponse.getUsername(), apiResponse.getPositiveCommand(), gingerResponse);
else else
resumeReading(); resumeReading();
} }
...@@ -883,7 +883,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -883,7 +883,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
alertDialogBuilder.setNegativeButton(apiResponse.getNegativeCommand() == null || apiResponse.getNegativeCommand().getName() == null ? getString(R.string.cancel) : apiResponse.getNegativeCommand().getName(), new DialogInterface.OnClickListener() { alertDialogBuilder.setNegativeButton(apiResponse.getNegativeCommand() == null || apiResponse.getNegativeCommand().getName() == null ? getString(R.string.cancel) : apiResponse.getNegativeCommand().getName(), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
startCommand(api, apiResponse.getId(), apiResponse.getNegativeCommand(), gingerResponse); startCommand(api, apiResponse.getId(), apiResponse.getUsername(), apiResponse.getNegativeCommand(), gingerResponse);
} }
}); });
else if (apiResponse.getPositiveCommand() != null && apiResponse.getPositiveCommand().getCommand() != null) // Si le bouton ok est déjà défini et qu'on ne défini pas le bouton négatif, alors il s'agira du simple bouton ok else if (apiResponse.getPositiveCommand() != null && apiResponse.getPositiveCommand().getCommand() != null) // Si le bouton ok est déjà défini et qu'on ne défini pas le bouton négatif, alors il s'agira du simple bouton ok
...@@ -898,7 +898,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -898,7 +898,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
alertDialogBuilder.setNeutralButton(apiResponse.getNeutralCommand().getName() == null ? getString(R.string.more) : apiResponse.getNeutralCommand().getName(), new DialogInterface.OnClickListener() { alertDialogBuilder.setNeutralButton(apiResponse.getNeutralCommand().getName() == null ? getString(R.string.more) : apiResponse.getNeutralCommand().getName(), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
startCommand(api, apiResponse.getId(), apiResponse.getNeutralCommand(), gingerResponse); startCommand(api, apiResponse.getId(), apiResponse.getUsername(), apiResponse.getNeutralCommand(), gingerResponse);
} }
}); });
...@@ -944,13 +944,16 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -944,13 +944,16 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
if (apiResponse.getPositiveCommand().getArguments() == null) if (apiResponse.getPositiveCommand().getArguments() == null)
apiCommand.setArguments(new HashMap<String, String>(){{ apiCommand.setArguments(new HashMap<String, String>(){{
put("paid", "true"); put("paid", "1");
}}); }});
else else
apiCommand.setArguments(apiResponse.getPositiveCommand().getArguments()); apiCommand.setArguments(apiResponse.getPositiveCommand().getArguments());
if (apiResponse.getPositiveCommand().getUseUsername() != null)
apiCommand.setUseUsername(apiResponse.getPositiveCommand().getUseUsername());
} }
startCommand(api, apiResponse.getId(), apiCommand, gingerResponse); startCommand(api, apiResponse.getId(), apiResponse.getUsername(), apiCommand, gingerResponse);
} }
}); });
} catch (final Exception e) { } catch (final Exception e) {
...@@ -995,7 +998,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -995,7 +998,7 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
}.start(); }.start();
} }
protected void startCommand(final API api, final String id, final APICommand apiCommand, final GingerResponse gingerResponse) { protected void startCommand(final API api, final String id, final String username, final APICommand apiCommand, final GingerResponse gingerResponse) {
if (apiCommand == null) { if (apiCommand == null) {
resumeReading(); resumeReading();
...@@ -1009,9 +1012,9 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result ...@@ -1009,9 +1012,9 @@ public class APIActivity extends BaseActivity implements ZXingScannerView.Result
public void run() { public void run() {
try { try {
if (apiCommand.getArguments() == null) if (apiCommand.getArguments() == null)
api.interact(id, apiCommand.getCommand()); api.interact(apiCommand.getUseUsername() == null || apiCommand.getUseUsername() == false ? id : ("user/" + username), apiCommand.getCommand());
else else
api.interact(id, apiCommand.getCommand(), mapArgs(apiCommand.getArguments())); api.interact(apiCommand.getUseUsername() == null || apiCommand.getUseUsername() == false ? id : ("user/" + username), apiCommand.getCommand(), mapArgs(apiCommand.getArguments()));
Thread.sleep(100); Thread.sleep(100);
final APIResponse apiResponse = new ObjectMapper().readValue(api.getRequest().getResponse(), APIResponse.class); final APIResponse apiResponse = new ObjectMapper().readValue(api.getRequest().getResponse(), APIResponse.class);
......
...@@ -16,14 +16,18 @@ public class APICommand { ...@@ -16,14 +16,18 @@ public class APICommand {
protected String name; protected String name;
protected String description; protected String description;
protected Map<String, String> arguments; protected Map<String, String> arguments;
// Si on souhaite utiliser le username plutôt que l'id pour la command
protected Boolean useUsername;
public String getCommand() { return command; } public String getCommand() { return command; }
public String getName() { return name; } public String getName() { return name; }
public String getDescription() { return description; } public String getDescription() { return description; }
public Map<String, String> getArguments() { return arguments; } public Map<String, String> getArguments() { return arguments; }
public Boolean getUseUsername() { return useUsername; }
public void setCommand(String command) { this.command = command; } public void setCommand(String command) { this.command = command; }
public void setName(String name) { this.name = name; } public void setName(String name) { this.name = name; }
public void setDescription(String description) { this.description = description; } public void setDescription(String description) { this.description = description; }
public void setArguments(Map<String, String> arguments) { this.arguments = arguments; } public void setArguments(Map<String, String> arguments) { this.arguments = arguments; }
public void setUseUsername(Boolean useUsername) { this.useUsername = useUsername; }
} }
...@@ -51,14 +51,14 @@ public class API { ...@@ -51,14 +51,14 @@ public class API {
public HTTPRequest getRequest() { return this.request; } public HTTPRequest getRequest() { return this.request; }
public int interact(final String id, final String command) throws Exception { public int interact(final String before, final String command) throws Exception {
return request( return request(
id + (command == null || command.isEmpty() ? "" : ("/" + command)) before + (before == null || before.isEmpty() || command == null || command.isEmpty() ? "" : "/") + command
); );
} }
public int interact(final String id, final String command, Map<String, Object> postArgs) throws Exception { public int interact(final String before, final String command, Map<String, Object> postArgs) throws Exception {
return request( return request(
id + "/" + command, before + (before == null || before.isEmpty() || command == null || command.isEmpty() ? "" : "/") + command,
postArgs postArgs
); );
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment