2
\$\begingroup\$

I call remote server to get my result. Result might contain artist,track and album depends on API method but there will be only one response (artist or track or album) at a time. It mean the other two will be null. How can I improve:

  • public void onNext(Result result)
  • public void renderList(List<?> result). Is it okay the way I am assigning values to the adapter?

private void apiTask(final Observable<Result> observer) {
        observer.subscribe(new Subscriber<Result>() {
            @Override
            public void onCompleted() {
                Log.i(TAG , "onComplete" );
            }

            @Override
            public void onError(Throwable e) {
                    Gson gson = new Gson();
                try {
                    ResponseBody response = ((HttpException) e).response().errorBody();
                    if (response != null) {
                        ApiError apiError = gson.fromJson(response.charStream(), ApiError.class);
                        if (apiError != null && apiError.getMessage() != null) {
                            daoManagerResponse.onError(apiError.getMessage());
                        } else {
                            daoManagerResponse.onError(e.getMessage());
                        }
                    }
                }
                catch (RuntimeException exception) {
                    daoManagerResponse.onError(e.getMessage());
                }
            }

            @Override
            public void onNext(Result result) {
                    try {
                        daoManagerResponse.onResponse(result.getMatchItem().getAlbummatches().getAlbum());
                    }
                catch (NullPointerException e) {
                    Log.e(TAG, e.getMessage());
                }
                try {
                    daoManagerResponse.onResponse(result.getMatchItem().getArtists().getArtists());
                } catch (NullPointerException e) {
                    Log.e(TAG, e.getMessage());
                }

                try {
                    daoManagerResponse.onResponse(result.getMatchItem().getTracks().getTracks());
                } catch (NullPointerException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }

2:-

@Override
public void onResponse(List<?> result) {
    if (result.size() > 0) {
        resultActivityView.renderList(result);
        resultActivityView.onSearchComplete();
    } else {
        resultActivityView.noResult();
    }
}

3:-

@Override
public void renderList(List<?> result) {

    if (result != null && result.size() > 0) {
        if (result.get(0) instanceof Track) {
            TrackAdaptor trackAdaptor = new TrackAdaptor();
            recyclerView.setAdapter(trackAdaptor);
            trackAdaptor.setTracks((List<Track>) result);
        }

        if (result.get(0) instanceof Album) {
            AlbumAdaptor albumAdaptor = new AlbumAdaptor();
            recyclerView.setAdapter(albumAdaptor);
            albumAdaptor.setAlbum((List<Album>) result);

        }

        if (result.get(0) instanceof Artist) {
            ArtistAdaptor artistAdaptor = new ArtistAdaptor();
            recyclerView.setAdapter(artistAdaptor);
            artistAdaptor.setArtists((List<Artist>) result);
        }
    }
}

You can have a look here for better understanding

https://github.com/Iqra786/Radio

\$\endgroup\$
  • \$\begingroup\$ Does title make scenes? \$\endgroup\$ – user1154390 Nov 3 '17 at 11:03
  • \$\begingroup\$ Much better :-) \$\endgroup\$ – Mast Nov 3 '17 at 11:03
  • \$\begingroup\$ Welcome to Code Review! I hope you get good answers. \$\endgroup\$ – Phrancis Nov 3 '17 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.