- 模块化开发
- List item
- 代码生成器model
从注解获取信息,通过annotationProcessor或apt生成代码
家里项目使用annotationProcesso - 核心model
-
分别建立各模块
atte-annotations
latte-compiler 编译模块
latte-core 核心库
latte-ec 电商模块 -
添加依赖
latte-core依赖于latte-annotations
latte-ec 依赖于latte-core
app依赖于latte-ec,latte-compiler -
一:开始编写项目 :项目架构初始化
1、latte-core
笔记:
WeakHashMap:里面的键值对在不使用的时候会自动回收,而且非常及时,避免内存爆满
代码:
- Configurator.class
/**
* Created by liusihui on 2019/11/05
* 模块功能:配置文件的存储以及获取
**/
package com.lsh.latte_core.app;
import java.util.WeakHashMap;
public class Configurator {
private static final WeakHashMap<String,Object> LATTE_CONFIGS=new WeakHashMap<>();
private Configurator(){
LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(),false); //配置开始了,但是没有完成
}
//线程安全的懒汉模式
public static Configurator getInstance(){
return Holder.INSTANCE;
}
//直接返回
final WeakHashMap<String,Object> getLatteConfigs(){
return LATTE_CONFIGS;
}
//静态内部类单例模式的初始化
private static class Holder{
private static final Configurator INSTANCE=new Configurator();
}
public final void configure(){
LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(),true);//配置开始了,并且已经完成
}
//开始实例化API—HOST
public final Configurator withApiHost(String host){
LATTE_CONFIGS.put(ConfigType.API_HOST.name(),host);
return this;
}
/*
*
* 检查配置是否正确
* 设置为不可更改
*
*
* 在应用程序中获取调用
* */
private void checkConfiguration(){
final boolean isReady=(boolean)LATTE_CONFIGS.get(ConfigType.CONFIG_READY.name());
//如果配置并没有完成,抛出一个运行时异常
if (!isReady){
throw new RuntimeException("Configuration id not ready,call configure");
}
}
/*
* @SuppressWarnings("unchecked") 没有检测过的
* */
@SuppressWarnings("unchecked")
final <T> T getConfiguration(Enum<ConfigType> key){
checkConfiguration();
return (T) LATTE_CONFIGS.get(key);
}
}
- ConfigType
package com.lsh.latte_core.app;
/*
* 枚举类:在整个应用程序里是唯一的单例,也只能初始化一次
*
* 要在进行多线程操作的时候,完全可以用枚举类惰性单例的初始化(线程安全的懒汉模式哈哈哈哈)
* */
public enum ConfigType {
API_HOST, //网络请求域名
APPLICATION_CONTEXT, //全局的上下文
CONFIG_READY, //控制初始化或配置是否完成
ICON //存储自己的初始化项目
}
- Latte
/**
- Created by liusihui on 2019/11/05
- 模块功能:
**/
package com.lsh.latte_core.app;
import android.content.Context;
import java.util.WeakHashMap;
public final class Latte {
public static Configurator init(Context context){
getConfigurations().put(ConfigType.APPLICATION_CONTEXT.name(),context.getApplicationContext());
return Configurator.getInstance();
}
private static WeakHashMap<String,Object> getConfigurations(){
return Configurator.getInstance().getLatteConfigs();
}
}
2、APP
- ExampleApp
/**
* Created by liusihui on 2019/11/05
* 模块功能:
**/
package com.lsh.fastec;
import com.lsh.latte_core.app.Latte;
import android.app.Application;
public class ExampleApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Latte.init(this)
.withApiHost("http://localhost:8080")
.configure();
}
}
- 二:集成第三方字体图标库iconify
由于项目中会用到很多图片,图片过多或者过大会导致项目较大,哈哈哈哈但是!iconify可以解决这个问题!那么甩链接
Github地址:添加链接描述
代码:
1、添加依赖
//字体图标
implementation 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2' // (v4.5)
implementation 'com.joanzapata.iconify:android-iconify-entypo:2.2.2' // (v3,2015)
implementation 'com.joanzapata.iconify:android-iconify-typicons:2.2.2' // (v2.0.7)
implementation 'com.joanzapata.iconify:android-iconify-material:2.2.2' // (v2.0.0)
implementation 'com.joanzapata.iconify:android-iconify-material-community:2.2.2' // (v1.4.57)
implementation 'com.joanzapata.iconify:android-iconify-meteocons:2.2.2' // (latest)
implementation 'com.joanzapata.iconify:android-iconify-weathericons:2.2.2' // (v2.0)
implementation 'com.joanzapata.iconify:android-iconify-simplelineicons:2.2.2' // (v1.0.0)
implementation 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2' // (v2.0.1)
2、在package com.lsh.latte_core.app.Configurator初始化
//封装下字体图标iconify
private static final ArrayList<IconFontDescriptor> ICONS=new ArrayList<>();
//初始化字体图标
private void initIcons(){
if (ICONS.size()>0){
final Iconify.IconifyInitializer initializer=Iconify.with(ICONS.get(0));
for (int i = 0; i <ICONS.size() ; i++) {
initializer.with(ICONS.get(i));
}
}
}
/*
* 加入自己的字体图标
* */
public final Configurator withIcon(IconFontDescriptor descriptor){
ICONS.add(descriptor);
return this;
}
3、在APP中调用
public void onCreate() {
super.onCreate();
Latte.init(this)
.withIcon(new FontAwesomeModule())
.withApiHost("http://10.10.110.38/")
.configure();
}
4、定义自己的图标字体:如阿里巴巴矢量图标
操作步骤:
然后在latte-ec中创建assets,将iconfont.ttf放入
代码:
1、package com.lsh.latte.ec.icon.FontRcMoudle
/**
- Created by liusihui on 2019/11/05
- 模块功能:
**/
package com.lsh.latte.ec.icon;
import com.joanzapata.iconify.Icon;
import com.joanzapata.iconify.IconFontDescriptor;
public class FontRcMoudle implements IconFontDescriptor {
@Override
public String ttfFileName() {
return "iconfont.ttf";
}
@Override
public Icon[] characters() {
return EcIcons.values();
}
}
2、创建枚举类EcIcons
/**
* Created by liusihui on 2019/11/05
* 模块功能:
**/
package com.lsh.latte.ec.icon;
import com.joanzapata.iconify.Icon;
public enum EcIcons implements Icon {
//扫描二维码和支付的字体
icon_scan('\ue606'),
icon_ali_pay('\ue606');
private char character;
EcIcons(char character){
this.character=character;
}
@Override
public String key() {
return name().replace('_','-');
}
@Override
public char character() {
return character;
}
}
3、Activity初始化
/**
* Created by liusihui on 2019/11/05
* 模块功能:
**/
package com.lsh.fastec;
import com.joanzapata.iconify.fonts.FontAwesomeModule;
import com.lsh.latte.ec.icon.FontRcMoudle;
import com.lsh.latte_core.app.Latte;
import android.app.Application;
public class ExampleApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Latte.init(this)
.withIcon(new FontAwesomeModule())
.withIcon(new FontRcMoudle())
.withApiHost("http://10.10.110.38/")
.configure();
}
}
- 集成fragment依赖、butterkniffer黄油刀
//fragment依赖
implementation 'me.yokeyword:fragmentation:0.10.1'
implementation 'me.yokeyword:fragmentation-swipeback:0.7.9'
代码实现:
1、package com.lsh.latte_core.delegates;BaseDelegate
/**
- Created by liusihui on 2019/11/05
- 模块功能:基础的Delegate
- 不希望以后的使用者创造实例,所以abstract整一个抽象类
**/
package com.lsh.latte_core.delegates;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
public abstract class BaseDelegate extends SwipeBackFragment {
/*
* Unbinder 是黄油刀的一个类型
* */
private Unbinder mUnbinder=null;
public abstract void onBindView(@Nullable Bundle savedInstanceState,View rootView);
/*
* 下面是写框架的技巧
* */
public abstract Object setLayout();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView=null;
//那么 现在就要开始我们的框架了!!!
if (setLayout() instanceof Integer){ //如果返回的是setLayout的id
rootView=inflater.inflate((Integer) setLayout(),container,false);
}else if (setLayout() instanceof View){
rootView=(View) setLayout();
}
if (rootView!=null){
mUnbinder= ButterKnife.bind(this,rootView);
onBindView(savedInstanceState,rootView);
}
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mUnbinder!=null){
mUnbinder.unbind(); //解除绑定
}
}
}
2、package com.lsh.fastec.ExanpleDelegate
/**
* Created by liusihui on 2019/11/05
* 模块功能:
**/
package com.lsh.fastec;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.lsh.latte_core.delegates.LatteDelegate;
public class ExanpleDelegate extends LatteDelegate {
@Override
public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
}
@Override
public Object setLayout() {
return R.layout.delegate_example;
}
}
3、ExampleActivity
package com.lsh.fastec;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.lsh.latte_core.activities.ProxyActivity;
import com.lsh.latte_core.delegates.LatteDelegate;
public class ExampleActivity extends ProxyActivity {
@Override
public LatteDelegate setRootDelegate() {
return new ExanpleDelegate();
}
}
4、delegate_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="example delegate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
运行结果: