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

The front line of Android app development at Ha...

Avatar for funnelbit funnelbit
April 14, 2015
55

The front line of Android app development at Hatena

Avatar for funnelbit

funnelbit

April 14, 2015
Tweet

Transcript

  1. %*

  2. खಈ%*ͷ໰୊ w ґଘؔ܎ΛखಈͰ؅ཧ͢Δ͜ͱʹͳΔ w ґଘΛ"DUJWJUZ͔Β"DUJWJUZ΁ؾΛ͚ͭͯ౉͢ ࣄʹͳΔ Intent intent = new

    Intent(context, BookmarkActivity.class);
 intent.putExtra(Intent.EXTRA_API_CLIENT, mAPIClient);
 startActivity(intent);
  3. ௨৴෦෼ public class BkumaFormalServiceProvider implements BkumaServiceProvider {
 …
 @Override
 public

    BkumaService getService() {
 OkHttpClient client = new OkHttpClient();
 
 RestAdapter restAdapter = new RestAdapter.Builder()
 .setClient(new OkClient(client))
 .setEndpoint(BkumaService.END_POINT)
 .build();
 return restAdapter.create(BkumaService.class);
 } …
  4. .PEVMF͔Β*OKFDU @Module
 public class BkumaModule {
 private final BkumaService mBkumaService;


    public BkumaModule(BkumaGirlsApplication application) {
 mBkumaService = new BkumaFormalServiceProvider(application .getApplicationContext()).getService();
 
 @Provides @Singleton
 EntryManager provideEntryManager() {
 return new EntryManager(mBkumaService);
 } … public class MainActivity extends ActionBarActivity { @Inject EntryManager mEntryManager; @Override
 protected void onCreate(Bundle savedInstanceState) {
 … ((BkumaGirlsApplication)getApplication()).component() .inject(this); } …
  5. $PNQPOFOUΛ࡞Δ public class BKUMAGirlsApplication extends Application {
 @Singleton
 @Component(modules =

    BkumaModule.class)
 public interface ApplicationComponent {
 void inject(MainActivity mainActivity);
 }
 
 private ApplicationComponent mComponent;
 
 @Override
 public void onCreate() {
 mComponent = DaggerBKUMAGirlsApplication_ApplicationComponent.builder()
 .bkumaModule(new BkumaModule(this))
 .build();
 }
 
 public ApplicationComponent component() {
 return mComponent;
 }
 }
  6. ௨৴෦෼ʢςετʣ @Override
 public BkumaService getService() {
 RestAdapter restAdapter = new

    RestAdapter
 .Builder()
 .setEndpoint(BkumaService.END_POINT)
 .build();
 
 MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
 mockRestAdapter.setDelay(0); …(ଓ͖·͢)
  7. ௨৴෦෼ʢςετʣ …
 BkumaService bkumaService = new BkumaService() {
 @Override
 public

    void getTopEntries(@Query("limit") int limit, Callback<List<EntryResponse>> callback) {
 callback.success( TestUtils.createDummyEntry(mContext, limit),
 createDummyJsonResponse()
 );
 } }); return mockRestAdapter.create( BkumaService.class, bkumaService );
  8. .PEVMF ςετ @Module
 public class BkumaTestModule {
 private final BkumaService

    mBkumaService; …
 public BkumaTestModule(InstrumentationTestCase instrumentationTestCase) { mBkumaService = new BkumaMockServiceProvider(mInstrumentationTestCase .getInstrumentation().getContext()).getService();
 }
 
 @Provides
 EntryManager provideEntryManager() {
 return new EntryManager(mBkumaService);
 } …
  9. *OKFDUΛ͢Δ ςετ public class BkumaEntryManagerTest extends InstrumentationTestCase {
 …
 @Inject


    EntryManager mEntryManager;
 … public testGetEntries() { assertEquals( "ΤϯτϦऔಘͰ͖͍ͯΔ", false, mEntryManager.getEntries().isEmpty()); }
 }
  10. &OUSZ.BOBHFSͷΩϟογϡ public class EntryManagerProvider {
 private final int MAX_SIZE =

    4;
 private final LruCache<String, EntryManager> mCache = new LruCache<>(MAX_SIZE); … }
  11. &OUSZ.BOBHFSͷΩϟογϡ public EntryManager getEntryManager(@NonNull String key) {
 EntryManager entryManager;
 entryManager

    = mCache.get(key);
 if (entryManager == null) {
 entryManager = new EntryManager(mBkumaService);
 mCache.put(key, entryManager);
 }
 return entryManager;
 }