Search in sources :

Example 1 with KeyValueSpinnerItem

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