2015年12月9日 星期三

Android 開發(一百零七) Android Studio 2.0 hotkey 筆記



 .settext -> .setColor  可以用tab不要用enter


Bitmap b =  null;
想要看更多可能的提示  control + shift + space 可能可以看到更多有用的提示


alt + or   可以協助選取


alt + enter 可以將constructor直接建立field 並且sign




也可以直接將instanceof 直接轉型




fori =>  live template
list.fori  => for(int i = 0; i< list.size() ; i++)

logi => Log.i(TAG, "liveTemplate: 123");
lost => private static final String TAG = "MainActivity";
MainActivity 是依照class name

logm =>  method 參數印log 

public void send(String p1, String p2){
    Log.d(TAG, "send() called with: " + "p1 = [" + p1 + "], p2 = [" + p2 + "]");
}

logr => return value 印出來
wtf => Log.wtf(TAG,"msg",new Exception);

command + shift + A => action name 快捷鍵提示視窗
選擇 replace structure  可以使用regex架構的 replace

shrinkResource = true
可以減少resource的數量,但是會減慢app的build速度

其他快捷鍵
Find symbol: OPT+CMD+O
Find Commands : Shift + CMD + A
View implementation of symbol: CMD+B
View implementation : OPT+CMD+B
recently used files: CRT+TAB


debug:
Evaluate 快速debug



condition debug break 例如recycler view你不會想針對每個一個一個看,你只會想要看某一個的時候,例如可以針對position==3
condition debug -> more info 可以做到增加log
可以直接在debug - >  console裡面看到,記得要將Suspend的勾勾取消選取



Android 開發(一百零六) 利用retrofit & gradle flavor來建立測試環境

retrofit 是個非常方便開發的library,利用它我們可以快速開發,而且快速測試,但是該怎麼做? 今天就要來稍微說明一下,2015 Android Dev Summit提到的做法.

在說明如何測試之前,必須先說明一下gradle flavor,如果不知道gradle flavor的可以參考一下之前的文章
Gradle Flavor ,flavor 的好處是,假設今天我們有兩種flavor,
normalMode & mockMode,當我需要建置正常的版本時,我只需要使用normalMode flavor就好,當我需要建置debug mode時,我只需要使用mockMode就好了.

不過這樣有什麼好處?

讓我們繼續看下去….

同樣一個getUsers()的method
在normalMode底下回傳的資料是真正api 打到server的資料
而在mockMode時則是回傳我們自己偽造的資料

以這種idea去設計的話,我們可以想像出一種架構

interface IApiService{
    Users getUser();
}

然後在normalMode時

NormalService implements IApiService{
    public User getUser(){
        //do api call and get User Model
        //....

        return user;
    }
}

然後在mockMode時則是

MockService implements IApiService{
    public User getUser(){
        //mock test data 
        User user = new User();
        user.name = "ted";
        return user;
    }
}

所以依照上面的範例,我們只需要分別在normal and mock 的folder裡將這兩個檔案放入就完成了,之後只要利用flavor的切換就可以切換成正式資料或測試資料了.

那…..講了這些又跟retrofit有什麼關係?

利用retrofit 我們可以很輕易地做到剛剛這些事情

舉個例子

SimpleService.GitHub github = Injection.getInjection();

    // Create a call instance for looking up Retrofit contributors.
    Observable<List<SimpleService.Contributor>> call = github.contributors("square", "retrofit");

    // Fetch and print a list of the contributors to the library.
    Log.d("Ted","fire");
    call.observeOn(Schedulers.io())
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Action1<List<SimpleService.Contributor>>() {
                @Override
                public void call(List<SimpleService.Contributor> contributors) {
                    for (SimpleService.Contributor contributor : contributors) {
                        Log.d("Ted",contributor.login + " (" + contributor.contributions + ")");
                    }
                }
            });

稍微講解一下上面的code

  1. 首先我們先取得可以call github api 的service
  2. 接著call api github.contributors();
  3. 接著等api 回應,並且顯示log

為了達到 更換資料的
所以我們必須想辦法讓 Injection.getInjection()丟出不同的service

是不是跟第一個sample的概念很像?

所以我們只需要在normalMode讓api真的去打server

public class Injection {

public static SimpleService.GitHub getInjection(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(SimpleService.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    return retrofit.create(SimpleService.GitHub.class);
}
}

然後在 mockMode 的時候製造假的資料

    public static SimpleService.GitHub getInjection() {

    // Create the Behavior object which manages the fake behavior and the background executor.
    NetworkBehavior behavior = NetworkBehavior.create();
    // Create the mock implementation and use MockRetrofit to apply the behavior to it.
    NetworkBehavior.Adapter<?> adapter = RxJavaBehaviorAdapter.create();
    MockRetrofit mockRetrofit = new MockRetrofit(behavior, adapter);
    MockGitHub mockGitHub = new MockGitHub();
    SimpleService.GitHub gitHub = mockRetrofit.create(SimpleService.GitHub.class, mockGitHub);
    return gitHub;
}

這樣就行了,之後只需要在開發的時候切換到mockMode就可以使用假的資料去做開發了,是不是很方便啊!!

順便提一下retrofit 有提供mock的lib 讓我們可以方便的mock假的rx資料以及call<>資料
compile ‘com.squareup.retrofit:retrofit-mock:2.0.0-beta2’
compile ‘com.squareup.retrofit:adapter-rxjava-mock:2.0.0-beta2’
而且還可以利用NetworkBehavior的setDelay來延遲api回傳的時間,真的非常的方便.

最後

還是要附一下開發的連結retrofit_with_gradle_flavor

大家快點去嘗試看看吧!!