O
O
Oleg Mikhailov2017-08-05 00:54:47
Android
Oleg Mikhailov, 2017-08-05 00:54:47

How to create proper android MVP structure?

Hello everyone!)
Need help understanding/explaining MVP design.
I've been at war with this thing for two days now and I just can't get to the bottom of it! I read articles: this one, this one, this one, and even this one, but everything there is somehow rather complicated examples. Those. I'm not writing because I'm too lazy to google something, I just want to understand) The
question is this:
I have a previously implemented fragment of an application that needs to be redone according to MVP. There are two activities in total: LoginActivity (screen below), MainActivity.
The interaction logic is as follows:
1. The Login screen has a TextView and a Sign In button (Gmail API).
2. By clicking on the Sign In button, a connection to Google is made, then a window appears with a choice of gmail accounts, and then the transition to MainActivity is performed.
3. When the MainActivity is created, the name of the Toolbar is filled with the email received after the request was sent and forwarded from the LoginActivity to the MainActivity using putExtra to the launch intent of the MainActivity;
That's it, then a list is displayed on MainActivity and access to the local database, there are no problems, it seems.
But how to properly design interfaces for LoginActivity?
How I did it:
I planned to make separate Model, View, Presenter for each of the activities. Those. ModelLogin, ModelMain, ViewLogin, ViewMain, etc. But it turned out, honestly, garbage is still at the stage of creating an MVP for Login.
I just couldn't figure out what should be implemented in the IViewLogin and IModelLogin interfaces, while IPresenterLogin was full of almost all the implementation...

The code
public interface IPresenterLogin {
//соединение к google
  void connectionGoogle(FragmentActivity fragment, Context context,
      OnConnectionFailedListener error);
//задаёт размер и цвет кнопке
  void setBtmLogin(SignInButton signInButton);
//авторизация через акк
  Intent signIn();
//тут передаём результат авторизации и метод возвращает Intent, 
//который потом внутри активити LoginView вызывается через startActivity(presenter.startActivityFromResult)
  Intent startActivityFromResult(Context context, GoogleSignInResult result);
//просто вывод всплывающего сообщения
  void getToast(Context context, String msg);


}

LoginActivity screenshot, this is so, a rough template))
3c2dbd63b39a49e8ab46359a5cf0242a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Kovalev, 2017-08-05
@Olegatorapp

public interface  LoginPresenter{
 void clickOnSignInBtn();  //презентер после вызова этого метода дергает методы модели, выполняющие подключеник к апи и вход, и вызывает  showSelectionWindow() c названиями аккаунтов, полученных из модели.
 void accountSelected(String s); // после того как юзер выбрал аккаунт, вьюшка дергает этот метод с параметром, какой аккаунт выбран, тут не обязательно должна быть строка, после этого презентер дергает соответствующий метод из вашей модели, выполняющий авторизацию уже определенного юзера, если все хорошо - вызывает
//openMainActivity() и передает туда инфу для интента, если нет - showError()
}

public interface LoginView {
void showSelectionWindow(List<String> accountsNames);  //показать меню выбора.
void openMainActivity(String arg); // В реализации вьюшка кладет в интент аргумент и запускает новую активити.
void showError(String msg); 
/* Сюда можно добавить другие нужные вам методы, аля 
        showProgress(), showMessage()
*/

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question