android AutoBundle 像Retrofit一樣構(gòu)建Bundle

前言

android.os.Bundle 相信每一個Android 開發(fā)者都在使用,構(gòu)建bundle和獲取其中value時如果參數(shù)很多寫起來代碼很呆板材义,多次調(diào)用的話 key值容易寫錯酪碘,不易管理麸塞,AutoBundle的產(chǎn)生就是為了解決這些問題笨奠。AutoBundle采用動態(tài)代理的方式創(chuàng)建Bundle,采用編譯時注解的方式為屬性賦值管呵。

使用

  1. 成員屬性 和 方法參數(shù) 注解解析
  2. 創(chuàng)建單例
  3. Activity中如何使用
  4. 定義接口方法
  5. 動態(tài)代理創(chuàng)建Bundle

1. 注解

In your class which has state from Bundle
(Activity, BroadcastReceiver, Service, Fragment or others),

@Box

在接口方法的參數(shù)上添加此注解 自動方法的值put進(jìn)bundle

@Unbox

在Activity梳毙、Fragment 或者任意類中的成員屬性添加此注解,自動獲取其值

@Required

如果添加此注解捐下,當(dāng)接口方法傳入或從Bundle中獲取的值為null時會crash

@BundleFlag

用于標(biāo)記創(chuàng)建Bundle的接口方法账锹,可以在回調(diào)函數(shù)中通過判斷此注解的value做二次修改替換等

2. 創(chuàng)建單例

 
 AutoBundle.builder().debug(true)//allow print debug message
                //check all create method before use
                .validateEagerly(true)
                .addOnBundleListener(new OnBundleListener() {
                    /**
                     * @param flag  flag is @BundFlag value
                     */
                    @Override
                    public void onBundling(int flag, String key, @Nullable Object value, boolean required) {
                        Log.e("print", "key:" + key + " flag:" + flag);
                    }

                    @Override
                    public void onCompleted(int flag, @NonNull Bundle bundle) {
                        Log.e("print", bundle.toString());
                        //replace or add
                        // for example
                        if (flag = xxx) {
                            bundle.put("xxx", xxx);
                        }
                    }
                })
                .installDefault();

3.Activity 中使用

public class MyActivity extends Activity {
    // field must not be private/protected.
    @Required
    @Unbox("loginName")
    String loginName;
    @Required
    @Unbox("password")
    String password;

    @Unbox("int")
    int intValue;

    @Unbox("string")
    String stringValue;

    @Unbox("intArray")
    int[] intArrayValue;

    @Unbox("stringArray")
    String[] stringArrayValue;

    @Unbox("parcelable")
    Parcelable ParcelableValue;

    @Unbox("parcelableArray")
    Parcelable[] parcelableArrayValue;

    @Unbox("sparseParcelableArray")
    SparseArray<? extends Parcelable> sparseParcelableArrayValue;

    @Unbox("stringArrayList")
    List<String> stringArrayListValue;

    @Unbox("parcelableArrayList")
    List<? extends Parcelable> parcelableArrayListValue;

    @Unbox("serializable")
    Serializable serializableValue;
}
如何給屬性自動賦值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //auto bind the field from bundle
        AutoBundle.getDefault().bind(this);
    }

4.像Retrofit創(chuàng)建Call一樣創(chuàng)建Bundle

public interface BundleService {
    @BundleFlag(0)
    Bundle getLogin(@Required @Box("loginName") String loginName,
                    @Required @Box("password") String password);

    @BundleFlag(1)
    Bundle getInt(@Box("int") int value);

    @BundleFlag(2)
    Bundle getString(@Box("string") String value);

    @BundleFlag(3)
    Bundle getIntArray(@Box("intArray") int[] value);

    Bundle getStringArray(@Box("stringArray") String[] value);

    Bundle getParcelable(@Box("parcelable") Parcelable value);

    Bundle getParcelableArray(@Box("parcelableArray") Parcelable[] value);

    Bundle getSparseParcelableArray(@Box("sparseParcelableArray")
                                            SparseArray<? extends Parcelable> value);

    Bundle getStringArrayList(@Box("stringArrayList") ArrayList<String> value);

    Bundle getParcelableArrayList(@Box("parcelableArrayList") ArrayList<? extends Parcelable> value);

    Bundle getSerializable(@Box("serializable") Serializable value);
}

5.接口方法調(diào)用創(chuàng)建Bundle

//second step: you can crate Bundle like retrofit
   Bundle loginBundle = AutoBundle.getDefault()
                .create(BundleService.class)
                .getDefault("JackWharton","123456");

  Bundle intBundle = AutoBundle.getDefault()
                .create(BundleService.class)
                .getInt(1228);



In target class, Call binding method in onCreate.

  • bind(Object target, Bundle bundle)
  • bind(Activity target) (equals to bind(activity, activity.getIntent().getExtras()))

Download

dependencies {
    implementation 'com.xcheng:autobundle-api:1.2.3'
    annotationProcessor 'com.xcheng:autobundle-compiler:1.2.3'
}

GitHub 地址: AutoBundle

Thanks

License

Copyright 2019 xchengDroid

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.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市坷襟,隨后出現(xiàn)的幾起案子奸柬,更是在濱河造成了極大的恐慌,老刑警劉巖婴程,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廓奕,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)懂从,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門授段,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人番甩,你說我怎么就攤上這事侵贵。” “怎么了缘薛?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵窍育,是天一觀的道長。 經(jīng)常有香客問我宴胧,道長漱抓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任恕齐,我火速辦了婚禮乞娄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘显歧。我一直安慰自己仪或,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布士骤。 她就那樣靜靜地躺著范删,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拷肌。 梳的紋絲不亂的頭發(fā)上到旦,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天,我揣著相機(jī)與錄音巨缘,去河邊找鬼添忘。 笑死,一個胖子當(dāng)著我的面吹牛若锁,可吹牛的內(nèi)容都是我干的搁骑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拴清,長吁一口氣:“原來是場噩夢啊……” “哼靶病!你這毒婦竟也來了会通?” 一聲冷哼從身側(cè)響起口予,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎涕侈,沒想到半個月后沪停,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年木张,在試婚紗的時候發(fā)現(xiàn)自己被綠了众辨。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡舷礼,死狀恐怖鹃彻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情妻献,我是刑警寧澤蛛株,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站育拨,受9級特大地震影響谨履,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜熬丧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一笋粟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧析蝴,春花似錦害捕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至腾啥,卻和暖如春东涡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背倘待。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工疮跑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人凸舵。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓祖娘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親啊奄。 傳聞我的和親對象是個殘疾皇子渐苏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容