1
\$\begingroup\$

I am implementing MVP architecture, code works fine, I wanted your suggestions if I there can be improvements done or in any areas where I am not following the pattern correctly.

It is a simple application using RecyclerViews to list games in a Fragment.

FGames - Fragment which displays the list of Games

public class FGames extends Fragment implements GamesView {

    @BindView(R.id.rv_games)
    RecyclerView rvGames;
    private GamesAdapter gamesAdapter;
    private GamesPresenterImpl presenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.games_layout, container, false);
        ButterKnife.bind(this, view);
        presenter = new GamesPresenterImpl(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter.initUi();
    }

    @Override
    public void initRecyclerView(List<GameEntity> gameEntities) {
        gamesAdapter = new GamesAdapter(getActivity(), gameEntities);
        rvGames.setAdapter(gamesAdapter);
        rvGames.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
}

Interface - GamesView

  public interface GamesView {

        /**
         * Initialise the recycler view to list Games data
         * @param gameEntities
         */
        void initRecyclerView(List<GameEntity> gameEntities);
    }

GamesPresenterImpl - Presenter implementation

    public class GamesPresenterImpl implements GamesPresenter {

    GamesView gamesView;

    public GamesPresenterImpl(GamesView gamesView) {
        this.gamesView = gamesView;
    }

    @Override
    public void initUi() {
        gamesView.initRecyclerView(getGamesData());
    }

    public List<GameEntity> getGamesData() {
        List<GameEntity> gameEntities = new ArrayList<>();
        int counter = 0;
        for(int i = 0; i<10; i++){
            GameEntity gameEntity = new GameEntity();
            counter ++;
            gameEntity.setName("Test name " + counter);
            gameEntity.setJackpot(100000055);
            gameEntities.add(gameEntity);
        }
        return gameEntities;
    }
}

Interface - GamesPresenter

public interface GamesPresenter {
    void initUi();
}
\$\endgroup\$
  • \$\begingroup\$ Hello, I have the same question, I know this is an older question, but did you find an answer or was this enough for the separation of concerns and tests? Thank you. \$\endgroup\$ – user1140656 Aug 6 '18 at 11:13

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.