private static @Nullable Unbinder parseBindString(Object target, Field field, View source) { BindString bindString = field.getAnnotation(BindString.class); if (bindString == null) { return null; } validateMember(field); int id = bindString.value(); Context context = source.getContext(); Class<?> fieldType = field.getType(); Object value; if (fieldType == String.class) { value = context.getString(id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindBitmap(Object target, Field field, View source) { BindBitmap bindBitmap = field.getAnnotation(BindBitmap.class); if (bindBitmap == null) { return null; } validateMember(field); int id = bindBitmap.value(); Resources resources = source.getContext().getResources(); Object value; Class<?> fieldType = field.getType(); if (fieldType == Bitmap.class) { value = BitmapFactory.decodeResource(resources, id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindBool(Object target, Field field, View source) { BindBool bindBool = field.getAnnotation(BindBool.class); if (bindBool == null) { return null; } validateMember(field); int id = bindBool.value(); Resources resources = source.getContext().getResources(); Object value; Class<?> fieldType = field.getType(); if (fieldType == boolean.class) { value = resources.getBoolean(id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindInt(Object target, Field field, View source) { BindInt bindInt = field.getAnnotation(BindInt.class); if (bindInt == null) { return null; } validateMember(field); int id = bindInt.value(); Resources resources = source.getContext().getResources(); Class<?> fieldType = field.getType(); Object value; if (fieldType == int.class) { value = resources.getInteger(id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindDimen(Object target, Field field, View source) { BindDimen bindDimen = field.getAnnotation(BindDimen.class); if (bindDimen == null) { return null; } validateMember(field); int id = bindDimen.value(); Resources resources = source.getContext().getResources(); Class<?> fieldType = field.getType(); Object value; if (fieldType == int.class) { value = resources.getDimensionPixelSize(id); } else if (fieldType == float.class) { value = resources.getDimension(id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindAnim(Object target, Field field, View source) { BindAnim bindAnim = field.getAnnotation(BindAnim.class); if (bindAnim == null) { return null; } validateMember(field); int id = bindAnim.value(); Resources resources = source.getContext().getResources(); Object value; Class<?> fieldType = field.getType(); if (fieldType == Animation.class) { value = resources.getAnimation(id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindFloat(Object target, Field field, View source) { BindFloat bindInt = field.getAnnotation(BindFloat.class); if (bindInt == null) { return null; } validateMember(field); int id = bindInt.value(); Context context = source.getContext(); Class<?> fieldType = field.getType(); Object value; if (fieldType == float.class) { value = Utils.getFloat(context, id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindColor(Object target, Field field, View source) { BindColor bindColor = field.getAnnotation(BindColor.class); if (bindColor == null) { return null; } validateMember(field); int id = bindColor.value(); Context context = source.getContext(); Object value; Class<?> fieldType = field.getType(); if (fieldType == int.class) { value = ContextCompat.getColor(context, id); } else if (fieldType == ColorStateList.class) { value = ContextCompat.getColorStateList(context, id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindArray(Object target, Field field, View source) { BindArray bindArray = field.getAnnotation(BindArray.class); if (bindArray == null) { return null; } validateMember(field); int id = bindArray.value(); Resources resources = source.getContext().getResources(); Object value; Class<?> fieldType = field.getType(); if (fieldType == TypedArray.class) { value = resources.obtainTypedArray(id); } else if (fieldType.isArray()) { Class<?> componentType = fieldType.getComponentType(); if (componentType == String.class) { value = resources.getStringArray(id); } else if (componentType == int.class) { value = resources.getIntArray(id); } else if (componentType == CharSequence.class) { value = resources.getTextArray(id); } else { throw new IllegalStateException(); // TODO } } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindDrawable(Object target, Field field, View source) { BindDrawable bindDrawable = field.getAnnotation(BindDrawable.class); if (bindDrawable == null) { return null; } validateMember(field); int id = bindDrawable.value(); int tint = bindDrawable.tint(); Context context = source.getContext(); Class<?> fieldType = field.getType(); Object value; if (fieldType == Drawable.class) { value = tint != Constants.NO_RES_ID ? Utils.getTintedDrawable(context, id, tint) : ContextCompat.getDrawable(context, id); } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindFont(Object target, Field field, View source) { BindFont bindFont = field.getAnnotation(BindFont.class); if (bindFont == null) { return null; } validateMember(field); int id = bindFont.value(); int style = bindFont.style(); Context context = source.getContext(); Class<?> fieldType = field.getType(); Object value; if (fieldType == Typeface.class) { Typeface font = ResourcesCompat.getFont(context, id); value = style != Typeface.NORMAL ? Typeface.create(font, style) : font; } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private static @Nullable Unbinder parseBindView(Object target, Field field, View source) { BindView bindView = field.getAnnotation(BindView.class); if (bindView == null) { return null; } validateMember(field); int id = bindView.value(); boolean isRequired = isRequired(field); Class<?> viewClass = field.getType(); String who = "field '" + field.getName() + "'"; Object view; if (isRequired) { view = Utils.findRequiredViewAsType(source, id, who, viewClass); } else { view = Utils.findOptionalViewAsType(source, id, who, viewClass); } trySet(field, target, view); return new FieldUnbinder(target, field); }
private static @Nullable Unbinder parseOnClick(final Object target, final Method method, View source) { OnClick onClick = method.getAnnotation(OnClick.class); if (onClick == null) { return null; } validateMember(method); validateReturnType(method, void.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_CLICK_TYPES); List<View> views = findViews(source, onClick.value(), isRequired(method), method.getName(), View.class); ViewCollections.set(views, ON_CLICK, v -> tryInvoke(method, target, argumentTransformer.transform(v))); return new ListenerUnbinder<>(views, ON_CLICK); }
private static @Nullable Unbinder parseOnFocusChange(final Object target, final Method method, View source) { OnFocusChange onFocusChange = method.getAnnotation(OnFocusChange.class); if (onFocusChange == null) { return null; } validateMember(method); validateReturnType(method, void.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_FOCUS_CHANGE_TYPES); List<View> views = findViews(source, onFocusChange.value(), isRequired(method), method.getName(), View.class); ViewCollections.set(views, ON_FOCUS_CHANGE, (v, hasFocus) -> tryInvoke(method, target, argumentTransformer.transform(v, hasFocus))); return new ListenerUnbinder<>(views, ON_FOCUS_CHANGE); }
private static @Nullable Unbinder parseOnCheckedChanged(final Object target, final Method method, View source) { OnCheckedChanged onCheckedChanged = method.getAnnotation(OnCheckedChanged.class); if (onCheckedChanged == null) { return null; } validateMember(method); validateReturnType(method, void.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_CHECKED_CHANGED_TYPES); List<CompoundButton> views = findViews(source, onCheckedChanged.value(), isRequired(method), method.getName(), CompoundButton.class); ViewCollections.set(views, ON_CHECKED_CHANGE, (buttonView, isChecked) -> tryInvoke(method, target, argumentTransformer.transform(buttonView, isChecked))); return new ListenerUnbinder<>(views, ON_CHECKED_CHANGE); }
private static @Nullable Unbinder parseOnItemClick(final Object target, final Method method, View source) { OnItemClick onItemClick = method.getAnnotation(OnItemClick.class); if (onItemClick == null) { return null; } validateMember(method); validateReturnType(method, void.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_ITEM_CLICK_TYPES); List<AdapterView<?>> views = findViews(source, onItemClick.value(), isRequired(method), method.getName(), AdapterView.class); ViewCollections.set(views, ON_ITEM_CLICK, (parent, view, position, id) -> tryInvoke(method, target, argumentTransformer.transform(parent, view, position, id))); return new ListenerUnbinder<>(views, ON_ITEM_CLICK); }
private static @Nullable Unbinder parseOnEditorAction(final Object target, final Method method, View source) { OnEditorAction onEditorAction = method.getAnnotation(OnEditorAction.class); if (onEditorAction == null) { return null; } validateMember(method); final boolean propagateReturn = validateReturnType(method, boolean.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_EDITOR_ACTION_TYPES); List<TextView> views = findViews(source, onEditorAction.value(), isRequired(method), method.getName(), TextView.class); ViewCollections.set(views, ON_EDITOR_ACTION, (v, actionId, event) -> { Object value = tryInvoke(method, target, argumentTransformer.transform(v, actionId, event)); //noinspection SimplifiableConditionalExpression return propagateReturn ? (boolean) value : false; }); return new ListenerUnbinder<>(views, ON_EDITOR_ACTION); }
private static @Nullable Unbinder parseOnTouch(final Object target, final Method method, View source) { OnTouch onTouch = method.getAnnotation(OnTouch.class); if (onTouch == null) { return null; } validateMember(method); final boolean propagateReturn = validateReturnType(method, boolean.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_TOUCH_TYPES); List<View> views = findViews(source, onTouch.value(), isRequired(method), method.getName(), View.class); ViewCollections.set(views, ON_TOUCH, (v, event) -> { Object returnValue = tryInvoke(method, target, argumentTransformer.transform(v)); //noinspection SimplifiableConditionalExpression return propagateReturn ? (boolean) returnValue : false; }); return new ListenerUnbinder<>(views, ON_TOUCH); }
private static @Nullable Unbinder parseOnLongClick(final Object target, final Method method, View source) { OnLongClick onLongClick = method.getAnnotation(OnLongClick.class); if (onLongClick == null) { return null; } validateMember(method); final boolean propagateReturn = validateReturnType(method, boolean.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_LONG_CLICK_TYPES); List<View> views = findViews(source, onLongClick.value(), isRequired(method), method.getName(), View.class); ViewCollections.set(views, ON_LONG_CLICK, v -> { Object returnValue = tryInvoke(method, target, argumentTransformer.transform(v)); //noinspection SimplifiableConditionalExpression return propagateReturn ? (boolean) returnValue : false; }); return new ListenerUnbinder<>(views, ON_LONG_CLICK); }
private static @Nullable Unbinder parseOnItemLongClick(final Object target, final Method method, View source) { OnItemLongClick onItemLongClick = method.getAnnotation(OnItemLongClick.class); if (onItemLongClick == null) { return null; } validateMember(method); final boolean propagateReturn = validateReturnType(method, boolean.class); final ArgumentTransformer argumentTransformer = createArgumentTransformer(method, ON_ITEM_LONG_CLICK_TYPES); List<AdapterView<?>> views = findViews(source, onItemLongClick.value(), isRequired(method), method.getName(), AdapterView.class); ViewCollections.set(views, ON_ITEM_LONG_CLICK, (parent, view, position, id) -> { Object value = tryInvoke(method, target, argumentTransformer.transform(parent, view, position, id)); //noinspection SimplifiableConditionalExpression return propagateReturn ? (boolean) value : false; }); return new ListenerUnbinder<>(views, ON_ITEM_LONG_CLICK); }