use of de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs 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()));
}
});
});
}
use of de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs in project RSAndroidApp by RailwayStations.
the class DetailsActivity method uploadPhoto.
private void uploadPhoto() {
final UploadBinding uploadBinding = UploadBinding.inflate(getLayoutInflater());
uploadBinding.etComment.setText(upload.getComment());
uploadBinding.spActive.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.active_flag_options)));
if (station != null) {
uploadBinding.spActive.setVisibility(View.GONE);
} else {
if (upload.getActive() == null) {
uploadBinding.spActive.setSelection(0);
} else if (upload.getActive()) {
uploadBinding.spActive.setSelection(1);
} else {
uploadBinding.spActive.setSelection(2);
}
}
final boolean sameChecksum = crc32 != null && crc32.equals(upload.getCrc32());
uploadBinding.cbChecksum.setVisibility(sameChecksum ? View.VISIBLE : View.GONE);
if (station != null) {
uploadBinding.spCountries.setVisibility(View.GONE);
} else {
final List<Country> countryList = baseApplication.getDbAdapter().getAllCountries();
final KeyValueSpinnerItem[] items = new KeyValueSpinnerItem[countryList.size() + 1];
items[0] = new KeyValueSpinnerItem(getString(R.string.chooseCountry), "");
int selected = 0;
for (int i = 0; i < countryList.size(); i++) {
final Country country = countryList.get(i);
items[i + 1] = new KeyValueSpinnerItem(country.getName(), country.getCode());
if (country.getCode().equals(upload.getCountry())) {
selected = i + 1;
}
}
final ArrayAdapter<KeyValueSpinnerItem> countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
uploadBinding.spCountries.setAdapter(countryAdapter);
uploadBinding.spCountries.setSelection(selected);
}
uploadBinding.txtPanorama.setText(Html.fromHtml(getString(R.string.panorama_info), Html.FROM_HTML_MODE_COMPACT));
uploadBinding.txtPanorama.setMovementMethod(LinkMovementMethod.getInstance());
uploadBinding.txtPanorama.setLinkTextColor(Color.parseColor("#c71c4d"));
String overrideLicense = null;
if (station != null) {
final Country country = Country.getCountryByCode(countries, station.getCountry());
overrideLicense = country.getOverrideLicense();
}
if (overrideLicense != null) {
uploadBinding.cbSpecialLicense.setText(getString(R.string.special_license, overrideLicense));
}
uploadBinding.cbSpecialLicense.setVisibility(overrideLicense == null ? View.GONE : View.VISIBLE);
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
builder.setTitle(R.string.photo_upload).setView(uploadBinding.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 -> {
if (uploadBinding.cbSpecialLicense.getText().length() > 0 && !uploadBinding.cbSpecialLicense.isChecked()) {
Toast.makeText(this, R.string.special_license_confirm, Toast.LENGTH_LONG).show();
return;
}
if (sameChecksum && !uploadBinding.cbChecksum.isChecked()) {
Toast.makeText(this, R.string.photo_checksum, Toast.LENGTH_LONG).show();
return;
}
if (station == null) {
if (uploadBinding.spActive.getSelectedItemPosition() == 0) {
Toast.makeText(this, R.string.active_flag_choose, Toast.LENGTH_LONG).show();
return;
}
final KeyValueSpinnerItem selectedCountry = (KeyValueSpinnerItem) uploadBinding.spCountries.getSelectedItem();
upload.setCountry(selectedCountry.getValue());
}
alertDialog.dismiss();
binding.details.progressBar.setVisibility(View.VISIBLE);
String stationTitle = binding.details.etbahnhofname.getText().toString();
String comment = uploadBinding.etComment.getText().toString();
upload.setTitle(stationTitle);
upload.setComment(comment);
try {
stationTitle = URLEncoder.encode(binding.details.etbahnhofname.getText().toString(), String.valueOf(StandardCharsets.UTF_8));
comment = URLEncoder.encode(comment, String.valueOf(StandardCharsets.UTF_8));
} catch (final UnsupportedEncodingException e) {
Log.e(TAG, "Error encoding station title or comment", e);
}
upload.setActive(uploadBinding.spActive.getSelectedItemPosition() == 1);
baseApplication.getDbAdapter().updateUpload(upload);
final File mediaFile = getStoredMediaFile(upload);
assert mediaFile != null;
final RequestBody file = RequestBody.create(mediaFile, MediaType.parse(URLConnection.guessContentTypeFromName(mediaFile.getName())));
rsapiClient.photoUpload(bahnhofId, station != null ? station.getCountry() : upload.getCountry(), stationTitle, latitude, longitude, comment, upload.getActive(), file).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<InboxResponse> call, @NonNull final Response<InboxResponse> response) {
binding.details.progressBar.setVisibility(View.GONE);
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 {
assert response.errorBody() != null;
final Gson gson = new Gson();
inboxResponse = gson.fromJson(response.errorBody().charStream(), InboxResponse.class);
if (inboxResponse.getState() == null) {
inboxResponse.setState(InboxResponse.InboxResponseState.ERROR);
}
}
assert inboxResponse != null;
upload.setRemoteId(inboxResponse.getId());
upload.setInboxUrl(inboxResponse.getInboxUrl());
upload.setUploadState(inboxResponse.getState().getUploadState());
upload.setCrc32(inboxResponse.getCrc32());
baseApplication.getDbAdapter().updateUpload(upload);
if (inboxResponse.getState() == InboxResponse.InboxResponseState.ERROR) {
new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(InboxResponse.InboxResponseState.ERROR.getMessageId()).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 uploading photo", t);
binding.details.progressBar.setVisibility(View.GONE);
new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(InboxResponse.InboxResponseState.ERROR.getMessageId()).toString(), t.getMessage()));
// try to get the upload state again
fetchUploadStatus(upload);
}
});
});
}
use of de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs in project RSAndroidApp by RailwayStations.
the class MyDataActivity method changePassword.
public void changePassword(final View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
final ChangePasswordBinding passwordBinding = ChangePasswordBinding.inflate(getLayoutInflater());
builder.setTitle(R.string.bt_change_password).setView(passwordBinding.getRoot()).setIcon(R.mipmap.ic_launcher).setPositiveButton(android.R.string.ok, null).setNegativeButton(android.R.string.cancel, (dialog, id) -> dialog.cancel());
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
String newPassword = getValidPassword(passwordBinding.password, passwordBinding.passwordRepeat);
if (newPassword == null) {
return;
}
alertDialog.dismiss();
try {
newPassword = URLEncoder.encode(newPassword, String.valueOf(StandardCharsets.UTF_8));
} catch (final UnsupportedEncodingException e) {
Log.e(TAG, "Error encoding new password", e);
}
rsapiClient.changePassword(newPassword).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<Void> call, @NonNull final Response<Void> response) {
switch(response.code()) {
case 200:
Log.i(TAG, "Successfully changed password");
binding.myData.etPassword.setText(passwordBinding.password.getText());
baseApplication.setPassword(passwordBinding.password.getText().toString());
rsapiClient.setCredentials(profile.getEmail(), passwordBinding.password.getText().toString());
new SimpleDialogs().confirm(MyDataActivity.this, R.string.password_changed);
break;
case 401:
rsapiClient.clearCredentials();
new SimpleDialogs().confirm(MyDataActivity.this, R.string.authorization_failed);
break;
default:
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.change_password_failed).toString(), response.code()));
}
}
@Override
public void onFailure(@NonNull final Call<Void> call, @NonNull final Throwable t) {
Log.e(TAG, "Error changing password", t);
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.change_password_failed).toString(), t.getMessage()));
}
});
});
}
use of de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs in project RSAndroidApp by RailwayStations.
the class MyDataActivity method requestEmailVerification.
public void requestEmailVerification(final View view) {
new SimpleDialogs().confirm(this, R.string.requestEmailVerification, (dialogInterface, i) -> rsapiClient.resendEmailVerification().enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<Void> call, @NonNull final Response<Void> response) {
if (response.code() == 200) {
Log.i(TAG, "Successfully requested email verification");
Toast.makeText(MyDataActivity.this, R.string.emailVerificationRequested, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MyDataActivity.this, R.string.emailVerificationRequestFailed, Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(@NonNull final Call<Void> call, @NonNull final Throwable t) {
Log.e(TAG, "Error requesting email verification", t);
Toast.makeText(MyDataActivity.this, R.string.emailVerificationRequestFailed, Toast.LENGTH_LONG).show();
}
}));
}
use of de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs in project RSAndroidApp by RailwayStations.
the class MyDataActivity method resetPassword.
public void resetPassword(final View view) {
rsapiClient.clearCredentials();
final String emailOrNickname = binding.myData.etEmailOrNickname.getText().toString();
if (StringUtils.isBlank(emailOrNickname)) {
new SimpleDialogs().confirm(this, R.string.missing_email_or_nickname);
return;
}
profile.setEmail(emailOrNickname);
saveLocalProfile(profile);
rsapiClient.resetPassword(emailOrNickname).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull final Call<Void> call, @NonNull final Response<Void> response) {
switch(response.code()) {
case 202:
new SimpleDialogs().confirm(MyDataActivity.this, R.string.password_email);
break;
case 400:
new SimpleDialogs().confirm(MyDataActivity.this, R.string.profile_wrong_data);
break;
case 404:
new SimpleDialogs().confirm(MyDataActivity.this, R.string.profile_not_found);
break;
default:
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.request_password_failed).toString(), response.code()));
}
}
@Override
public void onFailure(@NonNull final Call<Void> call, @NonNull final Throwable t) {
Log.e(TAG, "Request new password failed", t);
new SimpleDialogs().confirm(MyDataActivity.this, String.format(getText(R.string.request_password_failed).toString(), t));
}
});
}
Aggregations