如何在我的样本中正确应用MVP模式

时间:2016-11-07 17:08:55

标签: java android mvp

我决定学习MVP模式,在浏览了一些文章之后,我想用我当前的项目来尝试。

我选择了一项活动,并开始考虑如何根据MVP规则将其解耦。最终我不知道该怎么做。这似乎不是一项复杂的活动,但我不知道

可以请某人告诉我我必须开始的事情吗?

演示者必须使用哪些方法,必须在当前活动中留下女巫视图,并且必须在界面中使用whitch方法?

告诉我应该开始的人。

这是我的班级

public final class ActivityUserDataScreen extends AppCompatActivity implements InterfaceActivityUserDataScreen{

private static String gender;
private static int inputHeight;
private static int inputWeight;
private TextInputLayout tilUserName;
private int backPressedQ = 0;
private String avatarName;

private static final String MEN = "men";
private static final String WOMEN = "men";

private Context context;
private PresenterActivityUserDataScreen presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());
    setContentView(R.layout.activity_user_data_screen);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setSupportActionBar((Toolbar) findViewById(R.id.tool_bar));

    context = getApplicationContext();
    initNumberPicker();
    initVar();
    presenter = new PresenterActivityUserDataScreen(this);
}

private void initNumberPicker() {
    NumberPicker pickerHeight = (NumberPicker) findViewById(R.id.pickerHeight);
    UtilClass.setDividerColor(pickerHeight, UtilClass.getMyColor(context, R.color.ntz_color_yellow));
    pickerHeight.setOnValueChangedListener(changeListener);
    pickerHeight.setMaxValue(220);
    pickerHeight.setMinValue(130);
    pickerHeight.setValue(States.HEIGHT_DEFAULT);

    NumberPicker pickerWeight = (NumberPicker) findViewById(R.id.pickerWeight);
    UtilClass.setDividerColor(pickerWeight, UtilClass.getMyColor(context, R.color.ntz_color_yellow));
    pickerWeight.setOnValueChangedListener(changeListener);
    pickerWeight.setMaxValue(120);
    pickerWeight.setMinValue(35);
    pickerWeight.setValue(States.WEIGHT_DEFAULT);
}

private void initVar() {
    tilUserName = (TextInputLayout) findViewById(R.id.tilUserName);

    SwitchButton switchButton = (SwitchButton) findViewById(R.id.sb_custom);
    switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                gender = WOMEN;
            }else {
                gender = MEN;
            }
        }
    });

    EditText etAvatarName = (EditText) findViewById(R.id.etAvatarName);
    etAvatarName.setText(getResources().getString(R.string.avatar));
}

private NumberPicker.OnValueChangeListener changeListener = new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.pickerHeight:
                inputHeight = newVal;
                break;
            case R.id.pickerWeight:
                inputWeight = newVal;
                break;
        }
    }
};

@Override
public final void onBackPressed() {
    UtilClass.processClick(context);

    if (backPressedQ == 1) {
        backPressedQ = 0;
        super.onBackPressed();
        overridePendingTransition(R.anim.open_main, R.anim.close_next);

    } else {
        backPressedQ++;
        Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
    }

    //Обнуление счётчика через 5 секунд
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            backPressedQ = 0;
        }
    }, 5000);
}

public final void goNext(View view) {
    UtilClass.processClick(context);
    EditText editText = tilUserName.getEditText();
    Editable editable = null;
    if (editText != null) {
        editable = editText.getText();
    }
    if (editable != null) {
        avatarName = editable.toString();
    }
    if (!isValidAvatarName()) return;

    saveUserData();
    MetadataSaver saver = new MetadataSaver(context);
    saver.saveFirstUserInfo();
    saver.saveDeviceInfo();
    PreferencesHelper.savePref(context, States.STILL_NOT_FINISH, true);
    UtilClass.goToNextActivity(ActivityUserDataScreen.this, ActivityVideo.class);
}

private void saveUserData(){
    saveAvatarGender();
    saveAvatarHeight();
    saveAvatarWeight();
    saveAvatarName();
}

private void saveAvatarGender(){
    if (gender == null){
        gender = MEN;
    }
    PreferencesHelper.savePref(context, States.AVATAR_GENDER, gender);
}

private boolean isValidAvatarName() {
    if (UtilClass.isTextEmpty(avatarName)) {
        tilUserName.setErrorEnabled(true);
        tilUserName.setError(getResources().getString(R.string.fill_your_avatar_name));
        return false;
    }

    if (avatarName.contains(" ")) {
        avatarName = avatarName.replace(" ", "");
    }

    if (!UtilClass.isLatinAlphabet(avatarName)) {
        tilUserName.setErrorEnabled(true);
        tilUserName.setError(getResources().getString(R.string.avatar_name_in_english));
        return false;
    }

    if (!UtilClass.isNameFree(context, avatarName)) {
        tilUserName.setErrorEnabled(true);
        tilUserName.setError(getResources().getString(R.string.avatar_name_already_in_use));
        return false;
    }

    return true;
}

private void saveAvatarHeight() {
    int result;
    if (inputHeight == 0) {
        result = States.HEIGHT_DEFAULT;
    } else {
        result = inputHeight;
    }

    PreferencesHelper.savePref(context, States.AVATAR_HEIGHT, result);
}

private void saveAvatarWeight() {
    int result;
    if (inputWeight == 0) {
        result = States.WEIGHT_DEFAULT;
    } else {
        result = inputWeight;
    }
    PreferencesHelper.savePref(context, States.AVATAR_WEIGHT, result);
}

private void saveAvatarName() {
    PreferencesHelper.savePref(context, States.AVATAR_NAME, avatarName);
}

public final void switchManWoman(View view) {
    UtilClass.processClick(context);
}
}

提前致谢!

3 个答案:

答案 0 :(得分:1)

要考虑的事项是:

  • 视图需要尽可能愚蠢。可以将其视为演示者给出的命令的执行者,并向演示者介绍UI上发生的所有事情。界面应提供类似"显示此文本"和/或调用演示者的方法,例如"单击按钮"。

  • 演示者是指挥官。它驱动您的视图行为并对来自视图本身的输入作出反应。理想情况下,它应该从与Android相关的任何内容中抽象出来,这样就可以测试vanilla测试中的行为。

答案 1 :(得分:1)

Google发布了a collection of samples来讨论和展示Android应用的不同架构工具和模式。

首先,了解how this one works非常有用。并适应您的样本。

[...]此示例是许多变体的基础。它展示了Model-View-Presenter模式的简单实现,没有架构框架。它使用手动依赖注入来提供具有本地和远程数据源的存储库。使用回调[...]

处理异步任务

enter image description here

答案 2 :(得分:0)

我强烈建议您在媒体上阅读这篇文章:https://medium.com/@tinmegali/model-view-presenter-mvp-in-android-part-1-441bfd7998fe#.f4yiylrwa

从本质上讲,与android SDK相关的所有内容都应该放在你的“视图”中(有时候是你的模型),这通常是一个片段或活动。找出模型和演示者之间的差异将取决于您,但是,您可以将演示者视为根据应用程序的输入做出程序逻辑决策的事情。通常,在Android开发中使用mvp模式来尝试绕过旋转和活动娱乐问题,因此您可以使用静态演示器来运行小型示例应用程序。

祝你好运!