Search in sources :

Example 1 with Upload

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload in project RSAndroidApp by RailwayStations.

the class DetailsActivity method verifyCurrentPhotoUploadExists.

private void verifyCurrentPhotoUploadExists() {
    if (upload == null || upload.isProblemReport() || upload.isUploaded()) {
        upload = new Upload();
        if (station != null) {
            upload.setCountry(station.getCountry());
            upload.setStationId(station.getId());
        }
        upload.setLat(latitude);
        upload.setLon(longitude);
        upload = baseApplication.getDbAdapter().insertUpload(upload);
    }
}
Also used : Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload)

Example 2 with Upload

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload in project RSAndroidApp by RailwayStations.

the class OutboxActivity method deleteCompletedUploads.

private void deleteCompletedUploads() {
    final List<Upload> uploads = dbAdapter.getCompletedUploads();
    if (uploads.isEmpty()) {
        return;
    }
    new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom)).setIcon(R.mipmap.ic_launcher).setTitle(R.string.confirm_delete_processed_uploads).setPositiveButton(R.string.button_ok_text, (dialog, which) -> {
        for (final Upload upload : uploads) {
            dbAdapter.deleteUpload(upload.getId());
            FileUtils.deleteQuietly(FileUtils.getStoredMediaFile(this, upload.getId()));
        }
        adapter.changeCursor(dbAdapter.getOutbox());
    }).setNegativeButton(R.string.button_cancel_text, null).create().show();
}
Also used : Bundle(android.os.Bundle) FileUtils(de.bahnhoefe.deutschlands.bahnhofsfotos.util.FileUtils) NonNull(androidx.annotation.NonNull) DbAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.DbAdapter) Intent(android.content.Intent) Response(retrofit2.Response) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) OutboxAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.OutboxAdapter) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) AlertDialog(android.app.AlertDialog) InboxStateQuery(de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxStateQuery) List(java.util.List) Nullable(androidx.annotation.Nullable) Callback(retrofit2.Callback) Toast(android.widget.Toast) Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload) ContextThemeWrapper(android.view.ContextThemeWrapper) Menu(android.view.Menu) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) Call(retrofit2.Call) Log(android.util.Log) ActivityOutboxBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityOutboxBinding) ContextThemeWrapper(android.view.ContextThemeWrapper) Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload)

Example 3 with Upload

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload 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 4 with Upload

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload in project RSAndroidApp by RailwayStations.

the class OutboxActivity method onCreate.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final BaseApplication baseApplication = (BaseApplication) getApplication();
    dbAdapter = baseApplication.getDbAdapter();
    final ActivityOutboxBinding binding = ActivityOutboxBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    adapter = new OutboxAdapter(OutboxActivity.this, dbAdapter.getOutbox());
    binding.lstUploads.setAdapter(adapter);
    // item click
    binding.lstUploads.setOnItemClickListener((parent, view, position, id) -> {
        final Upload upload = dbAdapter.getUploadById(id);
        final Intent detailIntent = new Intent(OutboxActivity.this, DetailsActivity.class);
        detailIntent.putExtra(DetailsActivity.EXTRA_UPLOAD, upload);
        startActivity(detailIntent);
    });
    binding.lstUploads.setOnItemLongClickListener((parent, view, position, id) -> {
        final String uploadId = String.valueOf(id);
        new SimpleDialogs().confirm(OutboxActivity.this, getResources().getString(R.string.delete_upload, uploadId), (dialog, which) -> {
            dbAdapter.deleteUpload(id);
            FileUtils.deleteQuietly(FileUtils.getStoredMediaFile(this, id));
            adapter.changeCursor(dbAdapter.getOutbox());
        });
        return true;
    });
    final List<InboxStateQuery> query = new ArrayList<>();
    for (final Upload upload : dbAdapter.getPendingUploads(true)) {
        query.add(new InboxStateQuery(upload.getRemoteId()));
    }
    baseApplication.getRsapiClient().queryUploadState(query).enqueue(new Callback<>() {

        @Override
        public void onResponse(@NonNull final Call<List<InboxStateQuery>> call, @NonNull final Response<List<InboxStateQuery>> response) {
            final List<InboxStateQuery> stateQueries = response.body();
            if (stateQueries != null) {
                dbAdapter.updateUploadStates(stateQueries);
                adapter.changeCursor(dbAdapter.getOutbox());
            } else {
                Log.w(TAG, "Upload states not processable");
            }
        }

        @Override
        public void onFailure(@NonNull final Call<List<InboxStateQuery>> call, @NonNull final Throwable t) {
            Log.e(TAG, "Error retrieving upload state", t);
            Toast.makeText(OutboxActivity.this, R.string.error_retrieving_upload_state, Toast.LENGTH_LONG).show();
        }
    });
}
Also used : InboxStateQuery(de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxStateQuery) ArrayList(java.util.ArrayList) Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload) ActivityOutboxBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityOutboxBinding) Intent(android.content.Intent) OutboxAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.OutboxAdapter) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Upload (de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload)4 SimpleDialogs (de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs)3 ArrayList (java.util.ArrayList)3 Intent (android.content.Intent)2 ContextThemeWrapper (android.view.ContextThemeWrapper)2 ActivityOutboxBinding (de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityOutboxBinding)2 OutboxAdapter (de.bahnhoefe.deutschlands.bahnhofsfotos.db.OutboxAdapter)2 InboxStateQuery (de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxStateQuery)2 List (java.util.List)2 AlertDialog (android.app.AlertDialog)1 TaskStackBuilder (android.app.TaskStackBuilder)1 Point (android.graphics.Point)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 ArrayAdapter (android.widget.ArrayAdapter)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1