use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.ProblemType in project RSAndroidApp by RailwayStations.
the class DetailsActivity method reportProblem.
void reportProblem() {
if (isNotLoggedIn()) {
Toast.makeText(this, R.string.please_login, Toast.LENGTH_LONG).show();
return;
}
final var reportProblemBinding = ReportProblemBinding.inflate(getLayoutInflater());
if (upload != null && upload.isProblemReport()) {
reportProblemBinding.etProblemComment.setText(upload.getComment());
}
final List<String> problemTypes = new ArrayList<>();
problemTypes.add(getString(R.string.problem_please_specify));
int selected = -1;
for (final ProblemType type : ProblemType.values()) {
problemTypes.add(getString(type.getMessageId()));
if (upload != null && upload.getProblemType() == type) {
selected = problemTypes.size() - 1;
}
}
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, problemTypes);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportProblemBinding.problemType.setAdapter(adapter);
if (selected > -1) {
reportProblemBinding.problemType.setSelection(selected);
}
final var builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
builder.setTitle(R.string.report_problem).setView(reportProblemBinding.getRoot()).setIcon(R.drawable.ic_bullhorn_48px).setPositiveButton(android.R.string.ok, null).setNegativeButton(android.R.string.cancel, (dialog, id1) -> dialog.cancel());
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
final int selectedType = reportProblemBinding.problemType.getSelectedItemPosition();
if (selectedType == 0) {
Toast.makeText(getApplicationContext(), getString(R.string.problem_please_specify), Toast.LENGTH_LONG).show();
return;
}
final ProblemType type = ProblemType.values()[selectedType - 1];
final String comment = reportProblemBinding.etProblemComment.getText().toString();
if (StringUtils.isBlank(comment)) {
Toast.makeText(getApplicationContext(), getString(R.string.problem_please_comment), Toast.LENGTH_LONG).show();
return;
}
alertDialog.dismiss();
if (upload == null || !upload.isProblemReport() || upload.isUploaded()) {
upload = new Upload();
upload.setCountry(station.getCountry());
upload.setStationId(station.getId());
upload.setProblemType(type);
upload.setComment(comment);
upload = baseApplication.getDbAdapter().insertUpload(upload);
} else {
upload.setProblemType(type);
upload.setComment(comment);
baseApplication.getDbAdapter().updateUpload(upload);
}
rsapiClient.reportProblem(new ProblemReport(station.getCountry(), bahnhofId, comment, type)).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<InboxResponse> call, @NonNull final Response<InboxResponse> response) {
final InboxResponse inboxResponse;
if (response.isSuccessful()) {
inboxResponse = response.body();
} else if (response.code() == 401) {
new SimpleDialogs().confirm(DetailsActivity.this, R.string.authorization_failed);
return;
} else {
final Gson gson = new Gson();
inboxResponse = gson.fromJson(response.errorBody().charStream(), InboxResponse.class);
if (inboxResponse.getState() == null) {
inboxResponse.setState(InboxResponse.InboxResponseState.ERROR);
}
}
upload.setRemoteId(inboxResponse.getId());
upload.setUploadState(inboxResponse.getState().getUploadState());
baseApplication.getDbAdapter().updateUpload(upload);
if (inboxResponse.getState() == InboxResponse.InboxResponseState.ERROR) {
new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(R.string.problem_report_failed).toString(), response.message()));
// try to get the upload state again
fetchUploadStatus(upload);
} else {
new SimpleDialogs().confirm(DetailsActivity.this, inboxResponse.getState().getMessageId());
}
}
@Override
public void onFailure(@NonNull final Call<InboxResponse> call, @NonNull final Throwable t) {
Log.e(TAG, "Error reporting problem", t);
new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(R.string.problem_report_failed).toString(), t.getMessage()));
}
});
});
}
Aggregations