くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

AndroidStudioで開発をはじめるときにすること~パッケージ構成&共通クラス編~

いろいろやっているとAndroidアプリを開発しはじめるときにいつもやることを忘れるので、その備忘録。その2

環境
  1. Android Studio: 2.3.3
  2. Gradle: 3.3
  3. Android Plugin for Gradle: 2.3.3
  4. Java8

パッケージ構成はこんな感じ

<root_package>/
├── activity              ... Activity
│   └── MainActivity.java
├── di                    ... DI関係。DaggerのComponentとか
├── dialog                ... DialogFragment
├── entity                ... OrmaのModels
├── fragment              ... Fragment
├── repository            ... OrmaのHelper
├── util                  ... Utitly関係
├── widget                ... UI部品
└── AppApplication.java   ... Applicationクラス
一括作成bashコマンド
mkdir -p activity di dialog entity fragment repository util widget

共通クラスはこんな感じ

BaseActivity

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    private AppComponent appComponent;

    protected static void start(@NonNull AppCompatActivity activity, @NonNull Intent intent) {
        activity.startActivity(intent);
        // activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
    }

    @NonNull
    protected AppComponent getComponent() {
        if (appComponent == null) {
            AppApplication appApplication = (AppApplication) getApplication();
            appComponent = appApplication.getAppComponent();
        }

        return appComponent;
    }

    @LayoutRes
    protected abstract int layoutId();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutId());
        ButterKnife.bind(this);
    }
}

BaseFragment

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {

    @LayoutRes
    protected abstract int layoutId();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(layoutId(), container, false);
        ButterKnife.bind(this, view);
        return view;
    }
}

AppApplication

import lombok.Getter;
public class AppApplication extends Application {

    @Getter
    AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        // Dagger
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }
}

Dagger関連

di.AppModule

@Module
public class AppModule {

    private Context context;

    public AppModule(Application app) {
        context = app;
    }

    @Provides
    public Context provideContext() {
        return context;
    }

    @Singleton
    @Provides
    public OrmaDatabase provideOrmaDatabase(Context context) {
        return OrmaDatabase.builder(context)
                .writeOnMainThread(AccessThreadConstraint.WARNING)
                .build();
    }

    @Singleton
    @Provides
    public MyRepository provideMyRepository(OrmaDatabase orma) {
        return new MyRepositoryImpl(orma);
    }
}

di.AppComponent

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    void inject(MainActivity activity);
}

Orma関連

MyRepository

@Singleton
public class MyRepositoryImpl implements MyRepository {

    OrmaDatabase orma;

    @Inject
    public PersonRepositoryImpl(OrmaDatabase orma) {
        this.orma = orma;
    }

    @NonNull
    @Override
    public List<PersonEntity> findAll() {
        return orma.selectFromPersonEntity().toList();
    }
}

サンプル

MainActivity

public class MainActivity extends BaseActivity {
    @Inject
    MyRepository myRepo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
    }
}