Search in sources :

Example 1 with InboxResponse

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse 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()));
            }
        });
    });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) TaskStackBuilder(android.app.TaskStackBuilder) ArrayList(java.util.ArrayList) Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload) Gson(com.google.gson.Gson) Point(android.graphics.Point) ProblemReport(de.bahnhoefe.deutschlands.bahnhofsfotos.model.ProblemReport) ContextThemeWrapper(android.view.ContextThemeWrapper) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) ProblemType(de.bahnhoefe.deutschlands.bahnhofsfotos.model.ProblemType) InboxResponse(de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse) ArrayAdapter(android.widget.ArrayAdapter)

Example 2 with InboxResponse

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse 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);
            }
        });
    });
}
Also used : KeyValueSpinnerItem(de.bahnhoefe.deutschlands.bahnhofsfotos.util.KeyValueSpinnerItem) AlertDialog(androidx.appcompat.app.AlertDialog) TaskStackBuilder(android.app.TaskStackBuilder) Gson(com.google.gson.Gson) UploadBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.UploadBinding) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) RequestBody(okhttp3.RequestBody) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Point(android.graphics.Point) ContextThemeWrapper(android.view.ContextThemeWrapper) Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) InboxResponse(de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse) File(java.io.File) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

TaskStackBuilder (android.app.TaskStackBuilder)2 Point (android.graphics.Point)2 ContextThemeWrapper (android.view.ContextThemeWrapper)2 ArrayAdapter (android.widget.ArrayAdapter)2 AlertDialog (androidx.appcompat.app.AlertDialog)2 Gson (com.google.gson.Gson)2 SimpleDialogs (de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs)2 InboxResponse (de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse)2 UploadBinding (de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.UploadBinding)1 Country (de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country)1 ProblemReport (de.bahnhoefe.deutschlands.bahnhofsfotos.model.ProblemReport)1 ProblemType (de.bahnhoefe.deutschlands.bahnhofsfotos.model.ProblemType)1 Upload (de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload)1 KeyValueSpinnerItem (de.bahnhoefe.deutschlands.bahnhofsfotos.util.KeyValueSpinnerItem)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 RequestBody (okhttp3.RequestBody)1