Dagger2AndBuildVariants

8928b5277aa7234dbca4ff782952343a?s=47 funnelbit
February 24, 2016

 Dagger2AndBuildVariants

8928b5277aa7234dbca4ff782952343a?s=128

funnelbit

February 24, 2016
Tweet

Transcript

  1. 3.
  2. 8.
  3. 11.

    "QQ$PNQPOFOU#BTF public interface AppComponentBase {
 // App
 void inject(App app);


    // Activity
 void inject(MainActivity mainActivity);
 }
  4. 12.

    "QJ$MJFOU public interface ApiClient {
 void request(String query, Callback callback);


    
 class Response {
 public final String body;
 public Response(String body) { this.body = body; }
 }
 
 interface Callback {
 void onSuccess(Response response);
 void onFail(Exception e);
 }
 }
  5. 13.

    &OUSZ.BOBHFS public class EntryManager {
 final ApiClient mApiClient;
 @Inject EntryManager(ApiClient

    apiClient) {
 mApiClient = apiClient;
 }
 
 public void create() {
 mApiClient.request("create query", new ApiClient.Callback() { …
 });
 }
 
 public void delete() {
 mApiClient.request("delete query", new ApiClient.Callback() {
 …
 });
 }
 }
  6. 14.

    .BJO"DUJWJUZ public class MainActivity extends AppCompatActivity {
 @Inject EntryManager mEntryManager;


    
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ((App) getApplication()).getAppComponent().inject(this);
 setContentView(R.layout.activity_main);
 
 mEntryManager.create();
 mEntryManager.delete();
 }
 }
  7. 18.

    "QQ public class App extends Application {
 private AppComponent mAppComponent;


    @Override
 public void onCreate() {
 super.onCreate();
 mAppComponent = DaggerAppComponent.builder()
 .appModule(new AppModule())
 .build();
 }
 
 public AppComponent getAppComponent() {
 return mAppComponent;
 }
 }
  8. 19.

    "QQ$MJFOU*NQM public class AppClientImpl implements ApiClient {
 @Override
 public void

    request(String query, Callback callback) {
 callback.onSuccess(new Response("production response"));
 }
 }
  9. 23.

    "QQ public class App extends Application {
 private AppComponent mAppComponent;


    @Inject LogUtil mLogUtil;
 @Override
 public void onCreate() {
 super.onCreate();
 mAppComponent = DaggerAppComponent.builder()
 .stubAppModule(new StubAppModule())
 .debugModule(new DebugModule())
 .build();
 
 mAppComponent.inject(this);
 mLogUtil.debugLog("͜Μʹͪ͸");
 }
 public AppComponent getAppComponent() {
 return mAppComponent;
 }
 }