Android生成Mvp模板

1. 新建一个intellij idea的插件工程

2. 添加2个类

Mvp.java

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Mvp extends AnAction {

    String ACTIVITY = "Activity.java";
    String FRAGMENT = "Fragment.java";

    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (project == null || editor == null) {
            return;
        }
        PsiFile currentEditorFile = PsiUtilBase.getPsiFileInEditor(editor, project);
        if (currentEditorFile == null) {
            return;
        }

        String currentEditorFileName = currentEditorFile.getName();
        String modelName = currentEditorFileName;
        if (currentEditorFileName.endsWith(ACTIVITY)) {
            modelName = currentEditorFileName.replace(ACTIVITY, "");
        } else if (currentEditorFileName.endsWith(FRAGMENT)) {
            modelName = currentEditorFileName.replace(FRAGMENT, "");
        }

        PsiDirectory directory = currentEditorFile.getParent();


        List<String> list = new ArrayList<>();
        while (directory != null) {
            list.add(directory.getName());
            if (directory.getName().equals("com")) {
                break;
            }
            directory = directory.getParent();
        }
        if (list.size() == 0) {
            return;
        }
        Collections.reverse(list);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            stringBuilder.append(list.get(i));
            if (i < list.size() - 1) {
                stringBuilder.append(".");
            }
        }
        String basePackage = stringBuilder.toString();

        String basePath = getCurrentPath(e);

        try {
            createPresenterClass(basePackage, basePath, modelName);
            createModelClass(basePackage, basePath, modelName);
            createMvpClass(basePackage, basePath, modelName);
        } catch (IOException e1) {
            Messages.showMessageDialog("create file failed", "Error", Messages.getErrorIcon());
            return;
        }
        Messages.showMessageDialog("created success! please wait a moment", "Success", Messages.getInformationIcon());
        refreshProject(e);
    }


    private void refreshProject(AnActionEvent e) {
        e.getProject().getBaseDir().refresh(false, true);
    }

    private void createMvpClass(String basePackage, String path, String modelName) throws IOException {
        String filename = "I" + modelName + "Mvp.java";
        File file = new File(path, filename);
        file.createNewFile();
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        String content = String.format(MvpTemplate.MVP_TEMPLATE, basePackage, modelName);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    private void createModelClass(String basePackage, String path, String modelName) throws IOException {
        String filename = modelName + "ModelImpl.java";
        File file = new File(path, filename);
        file.createNewFile();
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        String content = String.format(MvpTemplate.MODEL_TEMPLATE, basePackage, modelName);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    private void createPresenterClass(String basePackage, String path, String modelName) throws IOException {
        String filename = modelName + "PresenterImpl.java";
        File file = new File(path, filename);
        file.createNewFile();
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        String content = String.format(MvpTemplate.PRESENTER_TEMPLATE, basePackage, modelName);
        writer.write(content);
        writer.flush();
        writer.close();
    }


    private String getCurrentPath(AnActionEvent e) {
        VirtualFile currentFile = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
        if (currentFile != null) {
            return currentFile.getParent().getPath();
        }
        return null;
    }

}

MvpTemplate.java
interface MvpTemplate {

    String MVP_TEMPLATE =
            "package %1$s;\n" +
                    "\n" +
                    "interface I%2$sMvp {\n" +
                    "interface I%2$sModel extends IMvp.IModel {}\n" +
                    "interface I%2$sPresenter extends IMvp.IPresenter {}\n" +
                    "interface I%2$sView extends IMvp.IView {}\n" +
                    "}";

    String PRESENTER_TEMPLATE =
            "package %1$s;\n" +
                    "import android.content.Context;\n" +
                    "import java.lang.Override;\n" +
                    "class %2$sPresenterImpl extends BasePresenter implements I%2$sMvp.I%2$sPresenter{\n" +
                    "private Context mContext;\n" +
                    "private I%2$sMvp.I%2$sView mView;\n" +
                    "private I%2$sMvp.I%2$sModel mModel;\n" +
                    "%2$sPresenterImpl(Context context, I%2$sMvp.I%2$sView view) {\n" +
                    "mContext = context;\n" +
                    "mView = view;\n" +
                    "mModel = new %2$sModelImpl(context);\n" +
                    "}\n" +
                    "@Override\n" +
                    "public void start() {}\n" +
                    "@Override\n" +
                    "public void destroyView() {mView = null;}" +
                    "}";

    String MODEL_TEMPLATE =
            "package %1$s;\n" +
                    "import android.content.Context;\n" +
                    "class %2$sModelImpl implements I%2$sMvp.I%2$sModel {\n" +
                    "private Context mContext;\n" +
                    "%2$sModelImpl(Context context) {\n" +
                    "mContext = context;\n" +
                    "}\n" +
                    "}";
}

3. plugin.xml文件<actions></actions>中添加

<action id="mvp_generator" class="Mvp" text="mvp" description="generator mvp">
      <add-to-group group-id="GenerateGroup" anchor="last"/>
    </action>

4. 打包 Build-Prepare Plugin Module

5. AndroidStudio 安装插件

6. 新建一个Activity或Fragment,文件名Activity.java或Fragment.java

7. 按Alt+Insert快捷键选择mvp

8. 在同文件夹下生成了其他的个文件

mvp1.png
interface IMainMvp {
    interface IMainModel extends IMvp.IModel {
    }
    interface IMainPresenter extends IMvp.IPresenter {
    }
    interface IMainView extends IMvp.IView {
    }
}
class MainModelImpl implements IMainMvp.IMainModel {
    private Context mContext;

    MainModelImpl(Context context) {
        mContext = context;
    }
}
class MainPresenterImpl extends BasePresenter implements IMainMvp.IMainPresenter {
    private Context mContext;
    private IMainMvp.IMainView mView;
    private IMainMvp.IMainModel mModel;

    MainPresenterImpl(Context context, IMainMvp.IMainView view) {
        mContext = context;
        mView = view;
        mModel = new MainModelImpl(context);
    }

    @Override
    public void start() {
    }

    @Override
    public void destroyView() {
        mView = null;
    }
}

9. 加入Mvp.java ,BasePresenter.java

Mvp.java
public interface IMvp {
    interface IModel {
    }
    interface IPresenter {
        void start();
        void destroy();
    }
    interface IView {
    }
}

BasePresenter.java
public abstract class BasePresenter implements IMvp.IPresenter {
    public abstract void destroyView();
    @CallSuper
    @Override
    public void destroy() {
         destroyView();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容