配置项目初始化参数

配置步骤

  • 为项目取名,例如 Apple 作为项目的名称
  • 设置要初始化的参数的枚举类,方便存取
  • 设置类似建造者模式的增删的接口

代码实例

  • ConfigType.java 枚举类,枚举所有需要初始化的参数,大概包含三类。

    public enum ConfigType {
    
        /* 1. 原生开发必需的常见参数
        * */
        CONFIG_READY,               //是否完成项目初始化
        APPLICATION_CONTEXT,        //全局 context
        API_HOST,                   //网络请求的基本 Host
        ICON,                       //应用中需要使用的文字图标
        ACTIVITY,                   //若为单 Activity 的模式,则在 Activity 创建时存储一个 Activity 实例
        HANDLER,                    //不是特殊应用,单个全局 Handler 可以满足需求
    
        /* 2. 第三方 SDK 接口接入必需的常见参数
        * */
        WE_CHAT_APP_ID,             //微信登录、支付需要的 ID
        WE_CHAT_APP_SECRET,        //微信登录、支付需要的 SECRET
    
        /* 3. 混合开发需要的拦截器及常用设置的参数
        * */
        WEB_HOST,                   //混合开发时网页数据可能来自不同的 Host 地址,这里可以单独设置
        JAVASCRIPT_INTERFACE,       //设置网页 JS 与原生应用交互的关键字
        INTERCEPTORS                //对应 Web 访问的拦截器
    
    }
    
  • Configrator.java

    /**
     * 使用 Interceptor 需要导入 okhttp 包
     */
    public final class Configurator {
    
        private static final HashMap<Object, Object> PROJECT_CONFIGS = new HashMap<>();
        private static final Handler HANDLER = new Handler();
        private static final ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();
    
        // 1. 构造方法中初始化 项目标志位 和 Handler
        private Configurator() {
            PROJECT_CONFIGS.put(ConfigType.CONFIG_READY, false);
            PROJECT_CONFIGS.put(ConfigType.HANDLER, HANDLER);
        }
    
        /*
         * 2. 静态内部类的方式返回 Configurator 实例
         * */
        private static class Holder {
            private static final Configurator INSTANCE = new Configurator();
        }
    
        static Configurator getInstance() {
            return Holder.INSTANCE;
        }
    
        /*
        *  3. 返回项目初始化前的参数配置表 HashMap ,项目初始化的时候调用
        * */
        public static HashMap<Object, Object> getConfigsBeforeInit() {
            return PROJECT_CONFIGS;
        }
    
        /*
         * 4.返回项目初始化完成后的参数配置表,项目运行时调用
         * */
        final HashMap<Object, Object> getConfigsAfterInit() {
            checkConfigration();
            return PROJECT_CONFIGS;
        }
    
        /*
        * 5. 从参数配置表中获取指定 key 的 value 值,并且强制转型为对应实例
        * */
        @SuppressWarnings("unchecked")
        final <T> T getSingleValueAfterInit(Enum<ConfigType> key) {
            checkConfigration();
            final Object value = PROJECT_CONFIGS.get(key);
            if (value == null) {
                throw new NullPointerException(key.toString() + "is null,please check your key");
            } else {
                return (T) value;
            }
        }
    
        /*
         * 6. 修改项目初始化标志位的方法,表明项目参数配置初始化完成
         * */
        public final void configure() {
            PROJECT_CONFIGS.put(ConfigType.CONFIG_READY, true);
        }
    
        /*
        * 检查项目参数是否已经初始化
        */
        private void checkConfigration() {
            final boolean isReady = (boolean) PROJECT_CONFIGS.get(ConfigType.CONFIG_READY);
            if (!isReady) {
                throw new RuntimeException("Config is not ready,call configure");
            }
        }
    
    
        /*
         *  -------------------------------- 后面一系列初始化参数的方法 --------------------------------------------
         */
        public final Configurator withApiHost(String host) {
            PROJECT_CONFIGS.put(ConfigType.API_HOST, host);
            return this;//将原对象添加属性后,重新赋值回去
        }
    
        public final Configurator withInterceptor(Interceptor interceptor) {
            INTERCEPTORS.add(interceptor);
            PROJECT_CONFIGS.put(ConfigType.INTERCEPTORS, INTERCEPTORS);
            return this;
        }
    
        public final Configurator withJavascriptInterface(@NonNull String name) {
            PROJECT_CONFIGS.put(ConfigType.JAVASCRIPT_INTERFACE, name);
            return this;
        }
    
        //浏览器 WebView  加载的 Host
        public final Configurator withWebHost(String webHost) {
            PROJECT_CONFIGS.put(ConfigType.WEB_HOST, webHost);
            return this;
        }
    
        public final Configurator withInterceptors(ArrayList<Interceptor> interceptors) {
            INTERCEPTORS.addAll(interceptors);
            PROJECT_CONFIGS.put(ConfigType.INTERCEPTORS, INTERCEPTORS);
            return this;
        }
    
        public final Configurator withWeChatAppId(String appId) {
            PROJECT_CONFIGS.put(ConfigType.WE_CHAT_APP_ID, appId);
            return this;
        }
    
        public final Configurator withWeChatAppSecret(String appSecret) {
            PROJECT_CONFIGS.put(ConfigType.WE_CHAT_APP_SECRET, appSecret);
            return this;
        }
    
        public final Configurator withActivity(Activity activity) {
            PROJECT_CONFIGS.put(ConfigType.ACTIVITY, activity);
            return this;
        }
    }
    
  • Apple.java 提供项目初始化配置的入口方法,且返回项目运行时需要的参数的一些方法

    public final class Apple {
    
        /*
         * 1. 初始化参数配置的入口方法,获取全局 Context 并存储
         * */
        public static Configurator init(Context context) {
            getConfiguratorInstance().getConfigsBeforeInit().put(ConfigType.APPLICATION_CONTEXT, context.getApplicationContext());
            return getConfiguratorInstance();
        }
    
        /*
        * 2. 返回配置参数列表,一个 HashMap 实例
        * */
        public static HashMap<Object, Object> getAllConfigs() {
            return getConfiguratorInstance().getConfigsAfterInit();
        }
    
        /*
        * 3. 根据传入的 Key ,返回对应 value 的对象
        * */
        public static <T> T getConfigByKey(Enum<ConfigType> key) {
            return getConfiguratorInstance().getSingleValueAfterInit(key);
        }
    
        /*
        * 4. 返回 Configurator 实例
        * */
        public static Configurator getConfiguratorInstance() {
            return Configurator.getInstance();
        }
    
        /*
        * 5. 返回项目的 Context
        * */
        public static Context getApplicationContext() {
            return (Context) getConfigByKey(ConfigType.APPLICATION_CONTEXT);
        }
    
        /*
        * 6. 返回静态全局的 Handler 实例
        * */
        public static Handler getHandler() {
            return (Handler) getConfigByKey(ConfigType.HANDLER);
        }
    }
    

因此,在 Application 中做初始化就相对简单了。在自己的 Application 中的 onCreate 方法中做初始化操作,示例代码如下:

    Apple.init(getApplicationContext())
            .withIcon(new FrontIconModule())
            .withIcon(new FontAwesomeModule())
            .withApiHost("http://pcclv6hgi.bkt.clouddn.com/")
            .withWeChatAppId("your WeChat_APPID")
            .withWeChatAppSecret("your WeChat_APP_Secret")
            .withInterceptor(new DebugInterceptor("index", R.raw.test))
            .withInterceptor(new AddCookieInterceptor())//添加 cookie 同步拦截器
            .withWebHost("https//www.baidu.com/")
            .withJavascriptInterface("apple")
            .withWebEvent("test", new TestEvent())
            .configure();

至此,整个项目的初始化参数都已经配置完成了,可以开始搭建项目框架咯….

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值