Search in sources :

Example 1 with OutboxAdapter

use of de.bahnhoefe.deutschlands.bahnhofsfotos.db.OutboxAdapter 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

Intent (android.content.Intent)1 ActivityOutboxBinding (de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityOutboxBinding)1 OutboxAdapter (de.bahnhoefe.deutschlands.bahnhofsfotos.db.OutboxAdapter)1 SimpleDialogs (de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs)1 InboxStateQuery (de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxStateQuery)1 Upload (de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1