Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dagger2AndBuildVariants

funnelbit
February 24, 2016

 Dagger2AndBuildVariants

funnelbit

February 24, 2016
Tweet

More Decks by funnelbit

Other Decks in Technology

Transcript

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


    // Activity
 void inject(MainActivity mainActivity);
 }
  2. "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);
 }
 }
  3. &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() {
 …
 });
 }
 }
  4. .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();
 }
 }
  5. "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;
 }
 }
  6. "QQ$MJFOU*NQM public class AppClientImpl implements ApiClient {
 @Override
 public void

    request(String query, Callback callback) {
 callback.onSuccess(new Response("production response"));
 }
 }
  7. "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;
 }
 }