lifecycle event
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/c4662c64 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/c4662c64 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/c4662c64 Branch: refs/heads/0.16-dev Commit: c4662c648e2b74a65019b4074de8687ba8c893bd Parents: 9ed73cb Author: jianbai.gbj <jianbai....@alibaba-inc.com> Authored: Tue Sep 19 16:15:47 2017 +0800 Committer: jianbai.gbj <jianbai....@alibaba-inc.com> Committed: Tue Sep 19 16:15:47 2017 +0800 ---------------------------------------------------------------------- .../java/com/taobao/weex/common/Constants.java | 7 + .../main/java/com/taobao/weex/dom/WXEvent.java | 22 + .../com/taobao/weex/dom/action/Actions.java | 2 +- .../weex/dom/action/RemoveEventAction.java | 5 +- .../list/template/LifecycleManager.java | 188 + .../list/template/WXRecyclerTemplateList.java | 46 +- pre-build/native-bundle-main.js | 45196 ++++++++--------- 7 files changed, 22812 insertions(+), 22654 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/common/Constants.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java index 1aff022..d5611d9 100644 --- a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java +++ b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java @@ -282,6 +282,13 @@ public class Constants { String UNSTICKY = "unsticky"; String STICKY = "sticky"; + interface Recycler{ + String CREATE = "recycler-create"; + String ATTACH = "recycler-attach"; + String DETACH = "recycler-detach"; + String DESTORY = "recycler-destroy"; + } + } public interface PSEUDO { http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java b/android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java index 985d720..942979f 100644 --- a/android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java +++ b/android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java @@ -61,6 +61,17 @@ public class WXEvent extends ArrayList<String> implements Serializable, Cloneabl super.clear(); } + + public boolean remove(String o) { + if(mEventBindingArgs != null){ + mEventBindingArgs.remove(o); + } + if(mEventBindingArgsValues != null){ + mEventBindingArgsValues.remove(o); + } + return super.remove(o); + } + /** * can by null * */ @@ -90,6 +101,17 @@ public class WXEvent extends ArrayList<String> implements Serializable, Cloneabl } } + public static String getEventName(Object event){ + if(event instanceof CharSequence){ + return event.toString(); + }else if(event instanceof JSONObject){ + JSONObject bindings = (JSONObject) event; + String eventName = bindings.getString(WXEvent.EVENT_KEY_TYPE); + return eventName; + } + return event.toString(); + } + public void putEventBindingArgs(String event, Object args){ if(!contains(event)){ add(event); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/dom/action/Actions.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/action/Actions.java b/android/sdk/src/main/java/com/taobao/weex/dom/action/Actions.java index 08c1902..c284997 100644 --- a/android/sdk/src/main/java/com/taobao/weex/dom/action/Actions.java +++ b/android/sdk/src/main/java/com/taobao/weex/dom/action/Actions.java @@ -92,7 +92,7 @@ public class Actions { if (args == null) { return null; } - return new RemoveEventAction(args.getString(0),args.getString(1)); + return new RemoveEventAction(args.getString(0),args.get(1)); case CREATE_FINISH: return new CreateFinishAction(); case REFRESH_FINISH: http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/dom/action/RemoveEventAction.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/action/RemoveEventAction.java b/android/sdk/src/main/java/com/taobao/weex/dom/action/RemoveEventAction.java index c79bba9..1ae132c 100644 --- a/android/sdk/src/main/java/com/taobao/weex/dom/action/RemoveEventAction.java +++ b/android/sdk/src/main/java/com/taobao/weex/dom/action/RemoveEventAction.java @@ -27,6 +27,7 @@ import com.taobao.weex.dom.DOMActionContext; import com.taobao.weex.dom.RenderAction; import com.taobao.weex.dom.RenderActionContext; import com.taobao.weex.dom.WXDomObject; +import com.taobao.weex.dom.WXEvent; import com.taobao.weex.ui.IWXRenderTask; import com.taobao.weex.ui.component.WXComponent; @@ -39,9 +40,9 @@ class RemoveEventAction implements DOMAction, RenderAction { private WXDomObject mUpdatedDomObject; - RemoveEventAction(String ref, String event) { + RemoveEventAction(String ref, Object event) { this.mRef = ref; - this.mEvent = event; + this.mEvent = WXEvent.getEventName(event); } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/LifecycleManager.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/LifecycleManager.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/LifecycleManager.java new file mode 100644 index 0000000..bfb3fed --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/LifecycleManager.java @@ -0,0 +1,188 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.component.list.template; + +import android.support.v4.util.ArrayMap; + +import com.taobao.weex.common.Constants; +import com.taobao.weex.dom.WXEvent; +import com.taobao.weex.ui.component.WXComponent; +import com.taobao.weex.ui.component.WXVContainer; +import com.taobao.weex.ui.component.list.WXCell; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * cell-slot lifecycle manager + * Created by furture on 2017/9/19. + */ +public class LifecycleManager { + + private static final String[] eventNames = { + Constants.Event.Recycler.CREATE, + Constants.Event.Recycler.ATTACH, + Constants.Event.Recycler.DETACH, + Constants.Event.Recycler.DESTORY + }; + + + private WXRecyclerTemplateList recyclerTemplateList; + private Map<String,Map<String,List<String>>> eventSlotWatchRefs; + + + private Map<String,List<String>> slotWatchCreateRefs; + private Map<String,List<String>> slotWatchAttachRefs; + private Map<String,List<String>> slotWatchDetachRefs; + private Map<String,List<String>> slotWatchDestroyRefs; + private List<String> createEvent; + private Map<String, Object> params; + public LifecycleManager(WXRecyclerTemplateList recyclerTemplateList) { + this.recyclerTemplateList = recyclerTemplateList; + this.eventSlotWatchRefs = new HashMap<>(); + this.slotWatchCreateRefs = new ArrayMap<>(); + this.slotWatchAttachRefs = new ArrayMap<>(); + this.slotWatchDetachRefs = new ArrayMap<>(); + this.slotWatchDestroyRefs = new ArrayMap<>(); + this.createEvent = new ArrayList<>(); + this.params = new HashMap<>(); + } + + + + public void updateListData(){ + Map<String, WXCell> templates = recyclerTemplateList.getTemplates(); + Set<Map.Entry<String, WXCell>> entries = templates.entrySet(); + for(Map.Entry<String, WXCell> entry : entries){ + entry.getValue(); + } + } + + public void updateSlotLifecycle(WXCell cell, WXComponent component){ + WXEvent wxEvent = component.getDomObject().getEvents(); + for(String event : eventNames){ + if(wxEvent.contains(event)){ + Map<String,List<String>> slotWatchRefs = eventSlotWatchRefs.get(event); + if(slotWatchRefs == null){ + slotWatchRefs = new HashMap<>(); + eventSlotWatchRefs.put(event, slotWatchRefs); + } + List<String> refs = slotWatchRefs.get(cell.getRef()); + if(refs == null){ + refs = new ArrayList<>(8); + slotWatchRefs.put(cell.getRef(), refs); + } + if(refs.contains(component.getRef())){ + refs.add(component.getRef()); + } + } + } + if(component instanceof WXVContainer){ + WXVContainer container = (WXVContainer) component; + for(int k=0; k<container.getChildCount();){ + WXComponent next = container.getChild(k); + updateSlotLifecycle(cell, next); + } + } + } + + /** + * create event + * */ + public void create(int position){ + String key = recyclerTemplateList.getTemplateKey(position); + if(slotWatchCreateRefs.get(key) == null + || slotWatchCreateRefs.get(key).size() == 0){ + return; + } + if(createEvent.size() >= position){ + if(createEvent.contains(key + position)){ + return; + } + } + WXCell cell = recyclerTemplateList.getTemplates().get(key); + if(cell == null){ + return; + } + params.clear(); + params.put("position", position); + cell.fireEvent(Constants.Event.Recycler.CREATE, params); + createEvent.add(key + position); + } + + + /** + * attach event + * */ + public void attach(int position, WXCell cell){ + if(cell == null || position < 0){ + return; + } + if(slotWatchAttachRefs.get(cell.getRef()) == null + || slotWatchAttachRefs.get(cell.getRef()).size() == 0){ + return; + } + params.clear(); + params.put("position", position); + cell.fireEvent(Constants.Event.Recycler.ATTACH, params); + } + + /** + * detach event + * */ + public void detach(int position, WXCell cell){ + if(cell == null || position < 0){ + return; + } + if(slotWatchDetachRefs.get(cell.getRef()) == null + || slotWatchDetachRefs.get(cell.getRef()).size() == 0){ + return; + } + params.clear(); + params.put("position", position); + cell.fireEvent(Constants.Event.Recycler.DETACH, params); + } + + /** + * destory event + * */ + public void destory(){ + if(slotWatchDestroyRefs.size() == 0){ + return; + } + int size = recyclerTemplateList.getItemCount(); + for(int position=0; position<size; position++){ + String key = recyclerTemplateList.getTemplateKey(position); + if(slotWatchDestroyRefs.get(key) == null + || slotWatchDestroyRefs.get(key).size() == 0){ + return; + } + WXCell cell = recyclerTemplateList.getTemplates().get(key); + if(cell == null){ + return; + } + params.clear(); + params.put("position", position); + cell.fireEvent(Constants.Event.Recycler.DESTORY, params); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c4662c64/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/WXRecyclerTemplateList.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/WXRecyclerTemplateList.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/WXRecyclerTemplateList.java index 865754e..52423e4 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/WXRecyclerTemplateList.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/list/template/WXRecyclerTemplateList.java @@ -86,7 +86,7 @@ import java.util.Set; import static com.taobao.weex.common.Constants.Name.LOADMOREOFFSET; /** - * weex template list supported + * weex template list supported, high performance recycler-list * https://github.com/Hanks10100/weex-native-directive * Created by jianbai.gbj on 2017/8/17. */ @@ -129,6 +129,9 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp private String listDataItemKey = null; private String listDataIndexKey = null; private ArrayMap<String, Integer> mTemplateViewTypes; + + + private Map<String, WXCell> mTemplates; private String listDataTemplateKey = Constants.Name.Recycler.SLOT_TEMPLATE_TYPE; private Runnable listUpdateRunnable; @@ -275,6 +278,7 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp } } } + notifyWatchCreateEvent(); if(getHostView() != null && getHostView().getRecyclerViewBaseAdapter() != null){ getHostView().getRecyclerViewBaseAdapter().notifyDataSetChanged(); } @@ -283,6 +287,18 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp return bounceRecyclerView; } + /** + * notify watch event + * */ + private void notifyWatchCreateEvent() { + if(listData != null){ + return; + } + + + + } + @Override protected void onHostViewInitialized(BounceRecyclerView host) { @@ -406,6 +422,26 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp setAppearanceWatch(component, AppearanceHelper.DISAPPEAR, false); } + + @JSMethod + public void scrollTo(int position, Map<String, Object> options){ + if (position >= 0) { + boolean smooth = true; + if(options != null) { + smooth = WXUtils.getBoolean(options.get(Constants.Name.ANIMATED), true); + } + + final int pos = position; + BounceRecyclerView bounceRecyclerView = getHostView(); + if (bounceRecyclerView == null) { + return; + } + final WXRecyclerView view = bounceRecyclerView.getInnerView(); + view.scrollTo(smooth, pos, 0, getOrientation()); + } + } + + @Override public void scrollTo(WXComponent component, Map<String, Object> options) { float offsetFloat = 0; @@ -1061,7 +1097,7 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp /** * return tepmlate key for position * */ - private String getTemplateKey(int position){ + public String getTemplateKey(int position){ JSONObject data = safeGetListData(position); String template = data.getString(listDataTemplateKey); if(TextUtils.isEmpty(template)){ @@ -1078,6 +1114,8 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp return mTemplates.get(template); } + + /** * get template key from cell; 0 for default type * */ @@ -1408,4 +1446,8 @@ public class WXRecyclerTemplateList extends WXVContainer<BounceRecyclerView> imp } return componentList; } + + public Map<String, WXCell> getTemplates() { + return mTemplates; + } }