/** * A utility method to create a child data context, with its own scope and index from the data * of this data context. * * @param context The proteus context used to evaluate {@link FunctionBinding} to evaluate the scope. * @param scope The scope for the new data context * @param dataIndex The data index to used by the new data context. * @return A new data context with scope evaluated. */ public DataContext createChild(@NonNull ProteusContext context, @NonNull Map<String, Value> scope, int dataIndex) { return create(context, data, dataIndex, scope); }
/** * This is a copy constructor for creating a clone of * another data context. The {@link #hasOwnProperties} * is always {@code false} for a cloned data context. * * @param dataContext the parent data context to clone from. */ public DataContext(DataContext dataContext) { this.data = dataContext.getData(); this.scope = dataContext.getScope(); this.index = dataContext.getIndex(); this.hasOwnProperties = false; }
/** * Utility method to create a new {@link DataContext} without any {@link #scope}. * * @param context The proteus context to resolve {@link FunctionBinding} to evaluate the scope. * @param data The data to be used by the data context. * @param dataIndex The data index to used by the data context. * @return A new data context with scope evaluated. */ public static DataContext create(@NonNull ProteusContext context, @Nullable ObjectValue data, int dataIndex) { DataContext dataContext = new DataContext(null, dataIndex); dataContext.update(context, data); return dataContext; }
private void updateDataContext(ObjectValue data) { if (dataContext.hasOwnProperties()) { dataContext.update(context, data); } else { dataContext.setData(data); } }
dataContext = DataContext.create(context, data, dataIndex); } else { dataContext = parentDataContext.copy(); dataContext = DataContext.create(context, data, dataIndex, map); } else { dataContext = parentDataContext.createChild(context, map, dataIndex);
protected void updateChildren() { if (!hasDataBoundChildren && view instanceof ViewGroup) { ViewGroup parent = (ViewGroup) view; int count = parent.getChildCount(); View child; for (int index = 0; index < count; index++) { child = parent.getChildAt(index); if (child instanceof ProteusView) { ((ProteusView) child).getViewManager().update(dataContext.getData()); } } } } }
/** * Returns a clone of this data context with {@link #hasOwnProperties} set to {@code false}. * * @return a new cloned data context. */ public DataContext copy() { return new DataContext(this); }
@Override public boolean handleChildren(T view, Value children) { ProteusView proteusView = ((ProteusView) view); ProteusView.Manager viewManager = proteusView.getViewManager(); ProteusLayoutInflater layoutInflater = viewManager.getContext().getInflater(); ObjectValue data = viewManager.getDataContext().getData(); int dataIndex = viewManager.getDataContext().getIndex(); if (children.isArray()) { ProteusView child; Iterator<Value> iterator = children.getAsArray().iterator(); Value element; while (iterator.hasNext()) { element = iterator.next(); if (!element.isLayout()) { throw new ProteusInflateException("attribute 'children' must be an array of 'Layout' objects"); } child = layoutInflater.inflate(element.getAsLayout(), data, view, dataIndex); addView(proteusView, child); } } return true; }
@NonNull @Override public SimpleListAdapter create(@NonNull ProteusRecyclerView view, @NonNull ObjectValue config) { Layout layout = config.getAsObject().getAsLayout(ATTRIBUTE_ITEM_LAYOUT); Integer count = config.getAsObject().getAsInteger(ATTRIBUTE_ITEM_COUNT); ObjectValue data = view.getViewManager().getDataContext().getData(); ProteusContext context = (ProteusContext) view.getContext(); return new SimpleListAdapter(context.getInflater(), data, layout, count != null ? count : 0); } };
/** * Utility method to create a new {@link DataContext} with a {@link #scope}. * * @param context The proteus android context to resolve {@link FunctionBinding} to evaluate the scope. * @param data The data to be used by the data context. * @param dataIndex The data index to used by the data context. * @param scope The scope map used to create the {@link #data} of this data context. * @return A new data context with scope evaluated. */ public static DataContext create(@NonNull ProteusContext context, @Nullable ObjectValue data, int dataIndex, @Nullable Map<String, Value> scope) { DataContext dataContext = new DataContext(scope, dataIndex); dataContext.update(context, data); return dataContext; }