Refactoring in MeetU — My Final Blog :(

Stephen Jaya
MeetU Engineering
Published in
3 min readMay 15, 2018

This is my final blog for MeetU, before signing out. Basically i will tell what changed in the this month prior to the closing of Software Engineering Project course (PPL).

Refactoring View-Presenter with Contract

In my previous blog, I demonstrate an View-Presenter using listeners, and implement it in the View. Turns out this will be a problematic mess just by creating listeners for the view and implement them all. What if the view just need a GET api call but we need a POST api call in another view having the same activity ?

A few listeners on a view

Well that’s a mess. There’s some empty methods in the view

Because we implement one listener-presenter for an activity. When we’re dealing with fragment, we’ll have to implement all of the listener, even the unused ones in the fragment.

Another thing is, we validate the input in the View. Thus makes more mess.

boolean isValidInput() {
boolean isValid = true;

if ("".equals(scheduleName.getText().toString())) {
isValid = false;
scheduleNameText.setError(getText(R.string.error_form_empty));
}

if ("".equals(scheduleDate.getText().toString())) {
isValid = false;
scheduleDateText.setError(getText(R.string.error_form_empty));
}
return isValid;
}

We realized before its to late, we need to refactor.

Dumb View — Smart Presenter

We need to make the View dumb, it only concerns is to inflate and creating a view, setting a adapter, and listens to a specific actions by presenter. We need to move all of the bussiness logic to the presenter, thus removing validations and all of the logic stuffs.

public class GroupListFragment extends Fragment implements GroupListContract.View {

@BindView(R.id.recyclerView)
RecyclerView recyclerView;

private GroupAdapter groupAdapter;
private GroupListContract.Presenter presenter;
private GroupActivityContract.View activity;

@OnClick(R.id.create_group)
public void onCreateGroupPressed(View v) {
activity.onShowCreateGroup(null);
}

public static GroupListFragment newInstance(GroupActivityContract.View activity) {
GroupListFragment fragment = new GroupListFragment();
Bundle args = new Bundle();

fragment.setArguments(args);
fragment.presenter = new GroupListPresenter(fragment);
fragment.activity = activity;
return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_groups, container, false);

ButterKnife.bind(this, v);
recyclerView.setLayoutManager(new LinearLayoutManager(v.getContext()));
recyclerView.setHasFixedSize(true);

List<GroupDetail> groupList = new ArrayList<>();
groupAdapter = new GroupAdapter(v.getContext(), groupList, new RecyclerViewClickListener() {
@Override
public void onItemClick(View v, int position) {
int groupId = groupAdapter.getGroupList().get(position).getId();
activity.onShowGroupMembers(groupId);
}
});
recyclerView.setAdapter(groupAdapter);

activity.changeActionText("List Group");

presenter.getGroups();

return v;
}

@Override
public void onGetSuccess(List<GroupDetail> groups) {
groupAdapter.setGroupList(groups);
}

@Override
public void onError(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}
}

See ? View need not to do all of the stuff, it only jobs it to handle listeners (by actions or presenters).

View-Presenter Contract

This is one of the interesting part, instead of making a listener for an activity only, now we’ll making a contract for a specific view and presenters

This allows a neat way to know what the view and the presenter does, and to have a clear knowledge of what they should do

import id.meetu.meetu.module.group.model.GroupMembers;

public interface CreateGroupContract {
interface View {
void resetAllError();

void setGroupNameError(String error);

void onCreateSuccess(String message);

void onCreateError(String message);
}

interface Presenter {
void submit(GroupMembers groupMembers);
}
}

With this, now all of the view have their own contract and presenter to deal with, eleminating a bunch of useless code :)

This makes a hella fast development, one of the productive sprints MeetU have gone through

Fix code, Fix UI

Other interesting thing that i found was testing the UI, the quality. Sometimes we can code what we know, turns out during production, anything can happen and we must handle it. For example, some display in our View have many list of items, we could not scroll it ! Turns out we need to add <ScrollView> to our layout

I realized all of this little things is important, as we need to make a viable product appealing to the users, in terms of UI and the behavior, which is preety important. That’s why we need a QA tester to test all of the behavior. So developer won’t miss a thing

Some behavior needs to be fix, even a wording

Conclusion

Many things happen during this sprint and during the courses, from zero knowledge in App development, I manage to make a pretty simple App with the team, thanks to those guys :)

Thank you for the opportunity, love all of the experience, ups and downs. Cheers to all of the MeetU Engineering members !

--

--