Skip to content

Commit

Permalink
新增 ComponentMonitor 案例
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Mar 24, 2020
1 parent 21f4e63 commit 04a0b24
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 28 deletions.
16 changes: 8 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

### 1.5.x 计划

对齐组件化

修改命名,FlapItem --> Component 等等

1. 移除 LayoutItemFactory
2. 移除 LifecycleItem
3. 重命名 FlapItemFactory --> ComponentProxy
4. 新增 IComponentModel
对齐脑子里的组件化设计思想,改了一堆名字,要挨骂了~

1. 移除 LayoutItemFactory(已废弃)
2. 移除 LifecycleItem(已废弃)
3. 重命名 FlapItemFactory --> ComponentProxy,更加符合设计理念
3. 重命名 FlapItem --> Component,对应"组件"概念
4. 新增 IComponentModel(待定 TODO)
5. 新增 ComponentFlowListener,可以监听关于组件的流程回调事件

### 1.4.1

Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/ComponentMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.yifeiyuan.flapdev;

import android.os.SystemClock;
import android.util.Log;

import me.yifeiyuan.flap.FlapComponent;
import me.yifeiyuan.flap.extensions.ComponentFlowListener;
import me.yifeiyuan.flap.internal.ComponentProxy;
/**
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
*
* @author 程序亦非猿 [Follow me](<a> https://github.com/AlanCheen</a>)
* @since 2020/3/24 11:14 PM
* @since 1.0
*/
public class ComponentMonitor implements ComponentFlowListener {

private static final String TAG = "ComponentMonitor";

private long createTime;
private long bindTime;

@Override
public void onStartCreateComponent(final ComponentProxy factory) {
Log.d(TAG, "开始使用 " + factory.getClass().getSimpleName() + " 创建组件");
createTime = SystemClock.uptimeMillis();
}

@Override
public void onComponentCreated(final ComponentProxy factory, final FlapComponent component) {
long timeCost = SystemClock.uptimeMillis() - createTime;
Log.d(TAG, "组件 " + component.getClass().getSimpleName() + " 创建完毕,耗时 " + timeCost + " 毫秒");
}

@Override
public void onStartBindComponent(final FlapComponent component, final int position, final Object model) {
Log.d(TAG, "开始绑定组件 [" + component.getClass().getSimpleName() + "], position = [" + position + "], model = [" + model + "]");
bindTime = SystemClock.uptimeMillis();
}

@Override
public void onComponentBound(final FlapComponent component, final int position, final Object model) {
Log.d(TAG, "组件绑定完毕,耗时 " + (SystemClock.uptimeMillis() - bindTime) + " 毫秒");
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/DifferActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -39,6 +40,12 @@ public boolean areItemsTheSame(@NonNull final SimpleTextModel simpleTextModel, @
public boolean areContentsTheSame(@NonNull final SimpleTextModel simpleTextModel, @NonNull final SimpleTextModel t1) {
return simpleTextModel.equals(t1);
}

@Nullable
@Override
public Object getChangePayload(@NonNull final SimpleTextModel oldItem, @NonNull final SimpleTextModel newItem) {
return super.getChangePayload(oldItem, newItem);
}
});

List<SimpleTextModel> models = new ArrayList<>();
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ private void initFlap() {
long t1 = System.currentTimeMillis();
//自定义 item view type ; custom item view type
Flap.getDefault().register(new CustomViewTypeComponent.Factory());

long t2 = System.currentTimeMillis();

Log.e("Flap", "Init Flap time cost :" + (t2 - t1));

Flap.getDefault().registerFlowListener(new ComponentMonitor());

// Flap.getDefault().getFlapItemPool().setMaxRecycledViews(new SimpleImageItem.Factory().getItemViewType(null), 8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class Factory implements ComponentProxy<CustomModel, CustomViewTyp

@NonNull
@Override
public CustomViewTypeComponent onCreateComponent(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {
public CustomViewTypeComponent createComponent(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {
return new CustomViewTypeComponent(inflater.inflate(R.layout.flap_item_custom_type, parent, false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnviro
ClassName layoutInflater = ClassName.get("android.view", "LayoutInflater");
ClassName viewGroup = ClassName.get("android.view", "ViewGroup");

MethodSpec onCreateViewHolderMethod = MethodSpec.methodBuilder("onCreateComponent")
MethodSpec onCreateViewHolderMethod = MethodSpec.methodBuilder("createComponent")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(layoutInflater, "inflater")
Expand Down
50 changes: 44 additions & 6 deletions flap/src/main/java/me/yifeiyuan/flap/Flap.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.yifeiyuan.flap.exceptions.ComponentProxyNotFoundException;
import me.yifeiyuan.flap.extensions.ComponentFlowListener;
import me.yifeiyuan.flap.extensions.ComponentPool;
import me.yifeiyuan.flap.internal.ComponentProxy;
import me.yifeiyuan.flap.internal.DefaultComponent;
Expand All @@ -31,6 +34,7 @@ public final class Flap implements IFlap {

private final Map<Class<?>, ComponentProxy<?, ?>> itemFactories;
private final SparseArray<ComponentProxy<?, ?>> factoryMapping;
private final List<ComponentFlowListener> flowListeners = Collections.synchronizedList(new ArrayList<ComponentFlowListener>());

private static final ComponentPool GLOBAL_POOL = new ComponentPool();

Expand Down Expand Up @@ -60,7 +64,6 @@ private Flap(int typeCount) {
}

private void injectFactories(final Flap flap) {

try {
Class<?> flapItemFactoryManager = Class.forName("me.yifeiyuan.flap.apt.manager.ComponentAutoRegister");
Method method = flapItemFactoryManager.getMethod("inject", Flap.class);
Expand Down Expand Up @@ -98,7 +101,6 @@ public ComponentManager clearAll() {

@Override
public int getItemViewType(@NonNull final Object model) {

Class<?> modelClazz = model.getClass();

ComponentProxy factory = itemFactories.get(modelClazz);
Expand All @@ -115,28 +117,54 @@ public int getItemViewType(@NonNull final Object model) {
@NonNull
@Override
public FlapComponent onCreateViewHolder(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {

FlapComponent vh = null;

ComponentProxy<?, ?> factory = factoryMapping.get(viewType);
dispatchBeforeCreateComponentEvent(factory, viewType);
if (factory != null) {
try {
vh = factory.onCreateComponent(inflater, parent, viewType);
vh = factory.createComponent(inflater, parent, viewType);
} catch (Exception e) {
e.printStackTrace();
FlapDebug.throwIfDebugging(e);
}
}
if (vh == null) {
vh = DEFAULT_FACTORY.onCreateComponent(inflater, parent, viewType);
vh = DEFAULT_FACTORY.createComponent(inflater, parent, viewType);
}
dispatchAfterCreateComponentEvent(factory, viewType, vh);
return vh;
}

private void dispatchBeforeCreateComponentEvent(final ComponentProxy<?, ?> factory, final int viewType) {
for (final ComponentFlowListener flowListener : flowListeners) {
flowListener.onStartCreateComponent(factory);
}
}

private void dispatchAfterCreateComponentEvent(final ComponentProxy<?, ?> factory, final int viewType, final FlapComponent vh) {
for (final ComponentFlowListener flowListener : flowListeners) {
flowListener.onComponentCreated(factory, vh);
}
}

@SuppressWarnings("unchecked")
@Override
public void onBindViewHolder(@NonNull final FlapComponent component, final int position, final Object model, @NonNull final List<Object> payloads, @NonNull final FlapAdapter flapAdapter) {
dispatchOnBeforeBindComponent(component, position, model);
component.bind(model, position, payloads, flapAdapter);
dispatchOnComponentBound(component, position, model);
}

private void dispatchOnBeforeBindComponent(@NonNull final FlapComponent component, final int position, final Object model) {
for (final ComponentFlowListener flowListener : flowListeners) {
flowListener.onStartBindComponent(component, position, model);
}
}

private void dispatchOnComponentBound(@NonNull final FlapComponent component, final int position, final Object model) {
for (final ComponentFlowListener flowListener : flowListeners) {
flowListener.onComponentBound(component, position, model);
}
}

@Override
Expand Down Expand Up @@ -166,4 +194,14 @@ public static void setDebug(final boolean isDebugging) {
public ComponentPool getComponentPool() {
return GLOBAL_POOL;
}

@Override
public void registerFlowListener(final ComponentFlowListener componentFlowListener) {
flowListeners.add(componentFlowListener);
}

@Override
public void unregisterFlowListener(final ComponentFlowListener componentFlowListener) {
flowListeners.remove(componentFlowListener);
}
}
3 changes: 2 additions & 1 deletion flap/src/main/java/me/yifeiyuan/flap/FlapComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
@SuppressWarnings({"EmptyMethod", "WeakerAccess", "unused"})
public abstract class FlapComponent<T> extends RecyclerView.ViewHolder implements LifecycleObserver {

@NonNull
protected final Context context;

private boolean isVisible = false;

public FlapComponent(View itemView) {
public FlapComponent(@NonNull View itemView) {
super(itemView);
context = itemView.getContext();
}
Expand Down
4 changes: 4 additions & 0 deletions flap/src/main/java/me/yifeiyuan/flap/IFlap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.yifeiyuan.flap;

import me.yifeiyuan.flap.extensions.ComponentFlowListener;
/**
* IFlap is the core interface that defines what Flap can do and how Flap works.
*
Expand All @@ -9,4 +10,7 @@
*/
interface IFlap extends ComponentManager, AdapterDelegate {

void registerFlowListener(ComponentFlowListener componentFlowListener);

void unregisterFlowListener(ComponentFlowListener componentFlowListener);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.yifeiyuan.flap.extensions;

import me.yifeiyuan.flap.FlapComponent;
import me.yifeiyuan.flap.internal.ComponentProxy;
/**
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
*
Expand All @@ -9,12 +11,37 @@
*/
public interface ComponentFlowListener {

void onBeforeCreate();
/**
* 在创建组件前调用
*
* @param proxy 组件代理
*/
void onStartCreateComponent(final ComponentProxy proxy);

void onAfterCreate();
/**
* 在组件创建完毕后调用
*
* @param proxy
* @param component
*/
void onComponentCreated(final ComponentProxy proxy, final FlapComponent component);

void onBeforeBind();
/**
* 在绑定组件之前调用
*
* @param component
* @param position
* @param model
*/
void onStartBindComponent(final FlapComponent component, final int position, Object model);

void onAfterBind();
/**
* 在组件绑定完毕后回调
*
* @param component
* @param position
* @param model
*/
void onComponentBound(final FlapComponent component, final int position, Object model);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import java.util.List;

import me.yifeiyuan.flap.FlapAdapter;
/**
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
*
Expand All @@ -13,15 +17,29 @@
*/
public class FlapRecyclerView extends RecyclerView {

private FlapAdapter adapter;

public FlapRecyclerView(@NonNull final Context context) {
super(context);
init();
}

public FlapRecyclerView(@NonNull final Context context, @Nullable final AttributeSet attrs) {
super(context, attrs);
init();
}

public FlapRecyclerView(@NonNull final Context context, @Nullable final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
adapter = new FlapAdapter();
setAdapter(adapter);
}

public void setData(List<?> data) {
adapter.setDataAndNotify(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface ComponentProxy<T, VH extends FlapComponent<T>> {
* @return your component
*/
@NonNull
VH onCreateComponent(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, int viewType);
VH createComponent(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, int viewType);

/**
* @param model your model to bind with the component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class Factory implements ComponentProxy {

@NonNull
@Override
public FlapComponent onCreateComponent(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {
public FlapComponent createComponent(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {
View view = new View(parent.getContext());
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
view.setLayoutParams(layoutParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import android.support.annotation.Nullable;
/**
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
*
* todo
* @author 程序亦非猿 [Follow me](<a> https://github.com/AlanCheen</a>)
* @since 2020/3/24 4:35 PM
* @since 1.0
*/
public interface IComponentModel {

boolean areItemsTheSame(@NonNull IComponentModel var1, @NonNull IComponentModel var2);
boolean areItemsTheSame(@NonNull IComponentModel other);

boolean areContentsTheSame(@NonNull IComponentModel var1, @NonNull IComponentModel var2);
boolean areContentsTheSame(@NonNull IComponentModel other);

@Nullable
Object getChangePayload(@NonNull IComponentModel oldItem, @NonNull IComponentModel newItem);
Object getChangePayload(@NonNull IComponentModel newItem);
}

0 comments on commit 04a0b24

Please sign in to comment.