# Barber **Repository Path**: baijuncheng-open-source/barber ## Basic Information - **Project Name**: Barber - **Description**: 一个自定义视图样式库。提供了一个简单的基于自定义注释@StyledAttr的style接口来定义视图样式 - **Primary Language**: Java - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-09-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Barber Barber is your personal custom component stylist. * Simply annotate your fields using the `@StyledAttr` or `@OhosAttr` annotations * Call the appropriate `Barber.style(this...)` variant * Let `Barber` take care of all the boilerplate for you. * Profit This library is heavily influenced by Jake Wharton's Butter Knife library, and was actually suggested to me by the man himself. ### PreView ![](img/barber.jpg) Usage ----- Barber has two main annotations that you use: `@StyledAttr` and `@OhosAttr`. These can be used on fields or methods (e.g. setters). `StyledAttr` is used for retrieving custom attrs for custom views. `@OhosAttr` is used for retrieving values for attributes in the ohos namespace. The Barber class has 3 overloaded `style()` methods, so you can call the appropriate one from whichever constructor you prefer. Annotated fields or methods *cannot* be private, and must at least be package accessible. This is because Barber will generate a `**$$Barbershop` class in the same package as the target class. #### StyledAttr and Required Declare your styled attributes in your `layout.xml`, like you normally would. For example: ``` ``` ``` @StyledAttr(value = ResourceTable.String_stripeColor, kind = Kind.COLOR) int stripeColor; @StyledAttr(ResourceTable.String_stripeCount) int stripeCount = 3; @Required @StyledAttr(value = ResourceTable.String_poleWidth, kind = Kind.DIMEN_PIXEL_SIZE) int poleWidth; private final Paint paint = new Paint(); public BarberView(Context context) { super(context); } ``` ### OhosAttr Ohos mainly sets annotations on the properties of Ohos itself, and it is used in the same way as StyledAttr, as shown below ``` @OhosAttr("top_margin") public String mTop_margin; private final Paint paint = new Paint(); public BarberView(Context context) { super(context); } public BarberView(Context context, AttrSet attrs) { super(context, attrs); Barber.style(this, attrs); addDrawTask(this::onDraw); Logger.getLogger(TAG).severe("mTop_margin = : " + mTop_margin); } ``` By default, Barber will resolve which `attrset` method to use based on the type of the target. That is, if you declare it on an `int`, then Barber will generate code that calls `attrset.getInt(...)`. Now, if a component is inflated without specifying this attribute, its generated `$$Barbershop` class will throw an IllegalStateException looking like this: `Missing required attribute 'requiredString' while styling 'com.io.sweers.barber.sample.testing.RequiredTestView'` ``` dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) implementation project(path: ':api_annotation') implementation project(path: ':api') testImplementation 'junit:junit:4.13' ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.100' annotationProcessor project(path: ':api_annotation') annotationProcessor project(path: ':compile') } ``` ``` The lib is available on Maven Central, you can find it with [Gradle, please] dependencies { implementation ‘com.gitee.baijuncheng-open-source:barber:1.0.0’ } ``` Proguard -------- If you use Proguard, [consumer proguard rules](https://github.com/hzsweers/barber/blob/master/api/consumer-proguard-rules.pro) are packaged in the `api` module AAR. License ------- Copyright 2015 Henri Z. Sweers Licensed 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.