TMVP框架(简单的)

Tutils   tmvp工具类

public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass()
                    .getGenericSuperclass())).getActualTypeArguments()[i])
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

RetrofitManager   创建retrofit对象

public class RetrofitManager {
   //地址
    private String BaseURL = Contents.BASEURL;




    private static OkHttpClient mOkHttpClient;
    private Retrofit mRetrofit;

    /**
     * 构建Retrofit
     *
     * @return
     */
    public Retrofit build() {
        initOkHttpClient();
        initRetrofit();
        return mRetrofit;
    }

    public RetrofitManager() {

    }

    protected void initRetrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BaseURL)
                .client(mOkHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .build();
    }

    private void initOkHttpClient() {
        //拦截器,用于输出网络请求和结果的 Log
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //初始化OkHttpClient
        mOkHttpClient = new OkHttpClient.Builder()
                            .addInterceptor(interceptor)
                            .retryOnConnectionFailure(true)
                            .connectTimeout(15, TimeUnit.SECONDS)
                            .build();


    }



}

LoginContract(契约类),有了契约类后自动生成相关的base类

 

public class LoginContract {

    public interface View extends BaseView {
      void loginSucc(String str);
      void loginFail(String result);
    }

    public interface Model  extends BaseModel {
        rx.Observable<Bean> loginByPsw(String phone, String code);
    }


    public abstract static class Presenter extends BasePresenter<Model, View> {
      abstract void phoneLogin(String phone,String psw);

        
    }
}

BaseModel初始化retrofit对象

public interface BaseModel {
    public RetrofitManager mRetrofitManager=new RetrofitManager();


}

BasePresenter 初始化presenter对象

 

public class BasePresenter<M, V> {

    public M mModel;
    public V mView;

    public void  setMV(M m,V v){
        this.mModel=m;
        this.mView=v;
    }

}

BaseActivity 

public abstract class BaseActivity <M extends BaseModel,P extends BasePresenter> extends AppCompatActivity {
    public M model;
    public P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        model= TUtils.getT(this,0);
        presenter=TUtils.getT(this,1);

        if(this instanceof BaseView){
            presenter.setMV(model,this);
        }

        setContentView(getLayoutId());
        initView();
    }
    public abstract int getLayoutId();
    public abstract void initView();
}

BaseFragment 

public abstract class BaseFragment <M extends BaseModel,P extends BasePresenter> extends Fragment {

    public M model;
    public P presenter;
    public View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       view= inflater.inflate(getLayoutId(),container,false);
        model=TUtil.getT(this,0);
        presenter=TUtil.getT(this,1);
        if (this instanceof BaseView){
            presenter.setM(model,this);
        }
        
    
        return view;
    }




    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    protected abstract void initData();

    protected abstract void initView(View view);

    protected abstract int getLayoutId();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值