S
S
Sergey Semenko2017-01-04 16:25:28
Android
Sergey Semenko, 2017-01-04 16:25:28

What can you say about this code?

Here is the code to send a request to perform a simple action - removing/adding the "favorite" tag:

protected void toggleFavorite() {
        if (favoriteRequest != null && favoriteRequest.executing()) {
            favoriteRequest.cancel();
        }

        if (favoriteRequest == null) {
            favoriteRequest = new BouquetRequest(getApplicationContext());

            favoriteRequest.setListener(new BouquetRequest.Listener()
            {
                @Override
                public void success(String message, @Nullable Object data) {
                    try {
                        if (data == null) {
                            favorites.delete(favorite);
                            favorite = null;
                        } else {
                            favorite = Favorite.fromJson((JSONObject) data);
                            favorites.create(favorite);
                        }
                    } catch (IOException|SQLException e) {
                        Toast.makeText(getApplicationContext(), R.string.unknown_error,
                                Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void error(String error, String code, @Nullable Object data) {
                    Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
                }
            });

            favoriteRequest.before(new BouquetRequest.BeforeCallback()
            {
                @Override
                public void run(StringRequest request) {
                    mFavoriteButton.setEnabled(false);
                }
            });

            favoriteRequest.after(new BouquetRequest.AfterCallback()
            {
                @Override
                public void run(@Nullable NetworkResponse response) {
                    mFavoriteButton.setEnabled(true);
                }
            });
        }

        favoriteRequest
                .setMethod(favorite == null ? BouquetRequest.POST : BouquetRequest.DELETE)
                .setAction(favorite == null ? "favorite" : "favorite/:id");

        if (favorite != null) {
            favoriteRequest.bindParam("id", favorite.id);
        }

        favoriteRequest.execute();
    }

The fact is that in one Activity / Fragment there can be up to 10 such actions, as a result, you will get quite a bit of code for requests alone.
BouquetRequest - a wrapper over Volley, I did it with the hope of simplifying the sending of requests, but somehow it didn't work out very efficiently.
Actually the question is: if you got a project with such code for revision, what would be the thoughts about the previous programmer? And would you change something in the logic of building queries?
PS I want to decide on an approach at an early stage of development, so that later it would be easier to find a replacement person.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-01-04
@abler98

Terrible, I would throw it all away, take Retrofit, RxJava, take it from the fragment / activity to the presenter and live happily ever after.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question