막무가내 삽질 블로그
안드로이드 mvp + retrofit + php 간단 정리 본문
728x90
이곳저곳에서 보다 이제 어느정도는 정리가 끝났다. 많은 예제들을 짬뽕시키고 내게 필요한 쪽으로 계속 변경중에 있다.
기존에 방식은 view에서 adapter로 다시 넘겨 줬지만 변경된 것은 모든걸 presenter에서 처리를 하였다.
MainContract
public interface MainContract {
// presenter 에서 view를 업데이트 하기 위한 이벤트
interface View {
void showLoading();
void hideLoading();
void onErrorMessage(String message);
void showToast(String name);
}
// view에서 호출할때 이벤트
interface Presenter {
void attachView(View view);
void detachView();
void onRequestData();
void setUserAdapterModel(UserAdapterContract.Model adapterModel);
void setUserAdapterView(UserAdapterContract.View adapterView);
}
}
MainPresenter
public class MainPresenter implements MainContract.Presenter, OnItemClick {
private static final String TAG = "MainPresenter";
private MainContract.View view;
private UserAdapterContract.View adapterView;
private UserAdapterContract.Model adapterModel;
public MainPresenter(MainContract.View view) {
this.view = view;
}
@Override
public void attachView(MainContract.View view) {
this.view = view;
}
@Override
public void detachView() {
view = null;
}
@Override
public void onRequestData() {
view.showLoading();
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<User>> call = apiInterface.getUsers();
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
view.hideLoading();
if (response.isSuccessful() && response.body() != null) {
view.onErrorMessage(response.message()); // 명칭 나중에 바꿀것
adapterModel.setData(response.body());
adapterView.notifyAdapter();
}
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
view.hideLoading();
view.onErrorMessage(t.getLocalizedMessage());
}
});
}
@Override
public void setUserAdapterModel(UserAdapterContract.Model adapterModel) {
this.adapterModel = adapterModel;
}
@Override
public void setUserAdapterView(UserAdapterContract.View adapterView) {
this.adapterView = adapterView;
this.adapterView.setOnClickListener(this);
}
@Override
public void OnItemClick(int position) {
User user = adapterModel.user(position);
view.showToast(user.getName());
}
}
UserAdapterContract
public interface UserAdapterContract {
interface View {
void notifyAdapter();
void setOnClickListener(OnItemClick clickListener);
}
interface Model {
void setData(List<User> userList);
User user(int position);
}
}
UserAdapter
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> implements UserAdapterContract.Model, UserAdapterContract.View {
private static final String TAG = "UserAdapter";
private Context context;
private List<User> userList;
private OnItemClick onItemClick;
public UserAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_main, parent, false);
return new ViewHolder(view, onItemClick);
}
@Override
public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) {
User user = userList.get(position);
holder.name.setText(user.getName());
holder.age.setText(String.valueOf(user.getAge()));
}
@Override
public int getItemCount() {
return userList != null ? userList.size() : 0;
}
@Override
public void notifyAdapter() {
notifyDataSetChanged();
}
@Override
public void setOnClickListener(OnItemClick clickListener) {
this.onItemClick = clickListener;
}
@Override
public void setData(List<User> userList) {
this.userList = userList;
}
@Override
public User user(int position) {
return userList.get(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView age, name;
OnItemClick onItemClick;
public ViewHolder(@NonNull View itemView, final OnItemClick onItemClick) {
super(itemView);
age = itemView.findViewById(R.id.age);
name = itemView.findViewById(R.id.name);
this.onItemClick = onItemClick;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClick.OnItemClick(getAdapterPosition());
}
});
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements MainContract.View {
private static final String TAG = "MainActivity";
private MainPresenter presenter;
private UserAdapter adapter;
private RecyclerView recyclerView;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
recyclerView = findViewById(R.id.recycler_view);
presenter = new MainPresenter(this);
progressDialog = new ProgressDialog(this);
adapter = new UserAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
presenter.attachView(this);
presenter.onRequestData();
presenter.setUserAdapterModel(adapter);
presenter.setUserAdapterView(adapter);
}
@Override
public void showLoading() {
progressDialog.show();
}
@Override
public void hideLoading() {
progressDialog.hide();
}
@Override
public void onErrorMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void showToast(String name) {
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView();
}
}
실행순서
참고https://thdev.tech/androiddev/2016/11/28/Android-MVP-One/ㅊ
참고https://github.com/AndroInfo/Android-MVP-with-Retrofit?source=post_page-----4016253ab3fc----------------------
참고https://www.youtube.com/watch?v=Vh92eSAEu5c
'Android' 카테고리의 다른 글
java.lang.IndexOutOfBoundsException: 에러 난 이유 (0) | 2019.12.17 |
---|---|
android null object reference (0) | 2019.12.15 |
Resources$NotFoundException: String resource ID (0) | 2019.12.07 |
안드로이드 ActivityCompat.finishAffinity 사용 (0) | 2019.12.06 |
안드로이드 레이아웃 인플레이터란? (LayoutInflater) (0) | 2019.12.05 |
Comments