├── .gitignore ├── .idea ├── .name ├── checkstyle-idea.xml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── Dagger2Example.iml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── drarter │ │ └── dagger2 │ │ └── example │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── drarter │ │ └── dagger2 │ │ └── example │ │ ├── ExampleApplication.java │ │ ├── base │ │ ├── activity │ │ │ └── BaseActivity.java │ │ ├── constant │ │ │ └── Constants.java │ │ ├── database │ │ │ ├── Db.java │ │ │ └── DbOpenHelper.java │ │ ├── fragment │ │ │ └── BaseFragment.java │ │ └── model │ │ │ ├── Bike.java │ │ │ ├── ListsItem.java │ │ │ ├── Motor.java │ │ │ ├── TodoItem.java │ │ │ ├── TodoList.java │ │ │ └── Vehicle.java │ │ ├── internal │ │ └── di │ │ │ ├── ApplicationContext.java │ │ │ ├── PerActivity.java │ │ │ ├── PerApp.java │ │ │ ├── component │ │ │ ├── ActivityComponent.java │ │ │ ├── ApplicationComponent.java │ │ │ └── HasComponent.java │ │ │ └── module │ │ │ ├── ActivityModule.java │ │ │ ├── ApplicationModule.java │ │ │ ├── BikeModule.java │ │ │ ├── DataBaseModule.java │ │ │ ├── MotorModule.java │ │ │ └── VehicleModule.java │ │ └── ui │ │ ├── main │ │ ├── MainActivity.java │ │ ├── MainActivityComponent.java │ │ └── MainActivityModule.java │ │ ├── menu │ │ └── MenuActivity.java │ │ └── sql │ │ ├── SqlbriteActivity.java │ │ ├── SqlbriteActivityComponent.java │ │ ├── SqlbriteActivityModule.java │ │ ├── adapter │ │ ├── ItemsAdapter2.java │ │ └── ListsAdapter2.java │ │ └── fragment │ │ ├── ItemsFragment.java │ │ ├── ListsFragment.java │ │ ├── NewItemFragment.java │ │ └── NewListFragment.java │ └── res │ ├── anim │ ├── slide_in_left.xml │ ├── slide_in_right.xml │ ├── slide_out_left.xml │ └── slide_out_right.xml │ ├── layout │ ├── activity_main.xml │ ├── activity_menu.xml │ ├── activity_sqlbrite.xml │ ├── items.xml │ ├── lists.xml │ ├── new_item.xml │ └── new_list.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Dagger2Example -------------------------------------------------------------------------------- /.idea/checkstyle-idea.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 26 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Dagger2Example.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dagger2Example 2 | Dagger2Example -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | buildscript { 5 | repositories { 6 | mavenCentral() 7 | } 8 | 9 | dependencies { 10 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' 11 | } 12 | } 13 | 14 | android { 15 | compileSdkVersion 23 16 | buildToolsVersion "23.0.2" 17 | 18 | defaultConfig { 19 | applicationId "com.drarter.dagger2.example" 20 | minSdkVersion 11 21 | targetSdkVersion 23 22 | versionCode 1 23 | versionName "1.0" 24 | } 25 | buildTypes { 26 | release { 27 | minifyEnabled false 28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 29 | } 30 | } 31 | 32 | dependencies { 33 | compile fileTree(dir: 'libs', include: ['*.jar']) 34 | compile 'com.android.support:appcompat-v7:23.1.1' 35 | compile 'com.android.support:support-annotations:23.1.1' 36 | 37 | compile 'com.jakewharton:butterknife:7.0.1' 38 | 39 | provided 'javax.annotation:jsr250-api:1.0' 40 | apt 'com.google.dagger:dagger-compiler:2.0' 41 | compile 'com.google.dagger:dagger:2.0' 42 | 43 | compile 'com.squareup.sqlbrite:sqlbrite:0.4.1' 44 | 45 | compile 'io.reactivex:rxandroid:1.0.1' 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Daniel/SDK/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/drarter/dagger2/example/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example; 2 | 3 | import android.app.Application; 4 | 5 | import com.drarter.dagger2.example.internal.di.component.ApplicationComponent; 6 | import com.drarter.dagger2.example.internal.di.component.DaggerApplicationComponent; 7 | import com.drarter.dagger2.example.internal.di.module.ApplicationModule; 8 | import com.drarter.dagger2.example.internal.di.module.DataBaseModule; 9 | 10 | public class ExampleApplication extends Application { 11 | 12 | private ApplicationComponent applicationComponent; 13 | 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | initApplicationComponent(); 18 | } 19 | 20 | private void initApplicationComponent() { 21 | this.applicationComponent = DaggerApplicationComponent.builder() 22 | .applicationModule(new ApplicationModule(this)) 23 | .dataBaseModule(new DataBaseModule(this)) 24 | .build(); 25 | } 26 | 27 | public ApplicationComponent getApplicationComponent() { 28 | return this.applicationComponent; 29 | } 30 | 31 | @Override 32 | public void onTerminate() { 33 | super.onTerminate(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/activity/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | 7 | import com.drarter.dagger2.example.ExampleApplication; 8 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 9 | import com.drarter.dagger2.example.internal.di.component.ApplicationComponent; 10 | import com.drarter.dagger2.example.internal.di.component.HasComponent; 11 | 12 | import butterknife.ButterKnife; 13 | 14 | public abstract class BaseActivity extends AppCompatActivity implements HasComponent { 15 | 16 | protected abstract int getContentViewResource(); 17 | 18 | protected abstract ActivityComponent getInitializeCompoent(); 19 | 20 | protected abstract void onInject(@Nullable ActivityComponent component); 21 | 22 | protected ActivityComponent component; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(getContentViewResource()); 28 | 29 | ButterKnife.bind(this); 30 | 31 | this.component = getInitializeCompoent(); 32 | if (this.component != null) { 33 | onInject(this.component); 34 | } 35 | } 36 | 37 | protected ApplicationComponent getApplicationComponent() { 38 | return ((ExampleApplication) getApplication()).getApplicationComponent(); 39 | } 40 | 41 | @Override 42 | protected void onDestroy() { 43 | ButterKnife.unbind(this); 44 | super.onDestroy(); 45 | } 46 | 47 | @Override 48 | public ActivityComponent getComponent() { 49 | return this.component; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/constant/Constants.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.constant; 2 | 3 | public class Constants { 4 | 5 | public class DataBase { 6 | public static final String DATABASE_NAME = "test.db"; 7 | public static final int DATABASE_VERSION = 1; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/database/Db.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.drarter.dagger2.example.base.database; 17 | 18 | import android.database.Cursor; 19 | 20 | public final class Db { 21 | public static final int BOOLEAN_FALSE = 0; 22 | public static final int BOOLEAN_TRUE = 1; 23 | 24 | public static String getString(Cursor cursor, String columnName) { 25 | return cursor.getString(cursor.getColumnIndexOrThrow(columnName)); 26 | } 27 | 28 | public static boolean getBoolean(Cursor cursor, String columnName) { 29 | return getInt(cursor, columnName) == BOOLEAN_TRUE; 30 | } 31 | 32 | public static long getLong(Cursor cursor, String columnName) { 33 | return cursor.getLong(cursor.getColumnIndexOrThrow(columnName)); 34 | } 35 | 36 | public static int getInt(Cursor cursor, String columnName) { 37 | return cursor.getInt(cursor.getColumnIndexOrThrow(columnName)); 38 | } 39 | 40 | private Db() { 41 | throw new AssertionError("No instances."); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/database/DbOpenHelper.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.database; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | 7 | import com.drarter.dagger2.example.base.constant.Constants; 8 | import com.drarter.dagger2.example.base.model.TodoItem; 9 | import com.drarter.dagger2.example.base.model.TodoList; 10 | 11 | public class DbOpenHelper extends SQLiteOpenHelper { 12 | 13 | private static final String CREATE_LIST = "" 14 | + "CREATE TABLE " + TodoList.TABLE + "(" 15 | + TodoList.ID + " INTEGER NOT NULL PRIMARY KEY," 16 | + TodoList.NAME + " TEXT NOT NULL," 17 | + TodoList.ARCHIVED + " INTEGER NOT NULL DEFAULT 0" 18 | + ")"; 19 | private static final String CREATE_ITEM = "" 20 | + "CREATE TABLE " + TodoItem.TABLE + "(" 21 | + TodoItem.ID + " INTEGER NOT NULL PRIMARY KEY," 22 | + TodoItem.LIST_ID + " INTEGER NOT NULL REFERENCES " + TodoList.TABLE + "(" + TodoList.ID + ")," 23 | + TodoItem.DESCRIPTION + " TEXT NOT NULL," 24 | + TodoItem.COMPLETE + " INTEGER NOT NULL DEFAULT 0" 25 | + ")"; 26 | private static final String CREATE_ITEM_LIST_ID_INDEX = 27 | "CREATE INDEX item_list_id ON " + TodoItem.TABLE + " (" + TodoItem.LIST_ID + ")"; 28 | 29 | public DbOpenHelper(Context context) { 30 | super(context, Constants.DataBase.DATABASE_NAME, null, Constants.DataBase.DATABASE_VERSION); 31 | } 32 | 33 | @Override 34 | public void onCreate(SQLiteDatabase db) { 35 | db.execSQL(CREATE_LIST); 36 | db.execSQL(CREATE_ITEM); 37 | db.execSQL(CREATE_ITEM_LIST_ID_INDEX); 38 | 39 | long groceryListId = db.insert(TodoList.TABLE, null, new TodoList.Builder() 40 | .name("Grocery List") 41 | .build()); 42 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 43 | .listId(groceryListId) 44 | .description("Beer") 45 | .build()); 46 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 47 | .listId(groceryListId) 48 | .description("Point Break on DVD") 49 | .build()); 50 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 51 | .listId(groceryListId) 52 | .description("Bad Boys 2 on DVD") 53 | .build()); 54 | 55 | long holidayPresentsListId = db.insert(TodoList.TABLE, null, new TodoList.Builder() 56 | .name("Holiday Presents") 57 | .build()); 58 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 59 | .listId(holidayPresentsListId) 60 | .description("Pogo Stick for Jake W.") 61 | .build()); 62 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 63 | .listId(holidayPresentsListId) 64 | .description("Jack-in-the-box for Alec S.") 65 | .build()); 66 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 67 | .listId(holidayPresentsListId) 68 | .description("Pogs for Matt P.") 69 | .build()); 70 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 71 | .listId(holidayPresentsListId) 72 | .description("Coal for Jesse W.") 73 | .build()); 74 | 75 | long workListId = db.insert(TodoList.TABLE, null, new TodoList.Builder() 76 | .name("Work Items") 77 | .build()); 78 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 79 | .listId(workListId) 80 | .description("Finish SqlBrite library") 81 | .complete(true) 82 | .build()); 83 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 84 | .listId(workListId) 85 | .description("Finish SqlBrite sample app") 86 | .build()); 87 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder() 88 | .listId(workListId) 89 | .description("Publish SqlBrite to GitHub") 90 | .build()); 91 | 92 | long birthdayPresentsListId = db.insert(TodoList.TABLE, null, new TodoList.Builder() 93 | .name("Birthday Presents") 94 | .archived(true) 95 | .build()); 96 | db.insert(TodoItem.TABLE, null, new TodoItem.Builder().listId(birthdayPresentsListId) 97 | .description("New car") 98 | .complete(true) 99 | .build()); 100 | } 101 | 102 | @Override 103 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/fragment/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.LayoutRes; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | 11 | import com.drarter.dagger2.example.internal.di.component.HasComponent; 12 | 13 | import butterknife.ButterKnife; 14 | 15 | public abstract class BaseFragment extends Fragment { 16 | 17 | @LayoutRes 18 | protected abstract int getLayoutResId(); 19 | 20 | @Nullable 21 | @Override 22 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 23 | View view = inflater.inflate(getLayoutResId(), container, false); 24 | ButterKnife.bind(this, view); 25 | return view; 26 | } 27 | 28 | @SuppressWarnings("unchecked") 29 | protected C getComponent(Class componentType) { 30 | return componentType.cast(((HasComponent) getActivity()).getComponent()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/Bike.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.model; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | public class Bike { 6 | 7 | private Motor motor; 8 | 9 | public Bike(@NonNull Motor motor) { 10 | this.motor = motor; 11 | } 12 | 13 | public void increaseSpped() { 14 | this.motor.setSpeed(this.motor.getSpeed() + 1); 15 | } 16 | 17 | public void decreaseSpeed() { 18 | if (this.motor.getSpeed() > 0) { 19 | this.motor.setSpeed(this.motor.getSpeed() - 1); 20 | } 21 | } 22 | 23 | public int getSpeed() { 24 | return this.motor.getSpeed(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/ListsItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.drarter.dagger2.example.base.model; 17 | 18 | import android.database.Cursor; 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | import android.support.annotation.NonNull; 22 | 23 | import com.drarter.dagger2.example.base.database.Db; 24 | 25 | import java.util.Arrays; 26 | import java.util.Collection; 27 | 28 | import rx.functions.Func1; 29 | 30 | public class ListsItem implements Parcelable { 31 | private static String ALIAS_LIST = "list"; 32 | private static String ALIAS_ITEM = "item"; 33 | 34 | private static String LIST_ID = ALIAS_LIST + "." + TodoList.ID; 35 | private static String LIST_NAME = ALIAS_LIST + "." + TodoList.NAME; 36 | private static String ITEM_COUNT = "item_count"; 37 | private static String ITEM_ID = ALIAS_ITEM + "." + TodoItem.ID; 38 | private static String ITEM_LIST_ID = ALIAS_ITEM + "." + TodoItem.LIST_ID; 39 | 40 | public static Collection TABLES = Arrays.asList(TodoList.TABLE, TodoItem.TABLE); 41 | public static String QUERY = "" 42 | + "SELECT " + LIST_ID + ", " + LIST_NAME + ", COUNT(" + ITEM_ID + ") as " + ITEM_COUNT 43 | + " FROM " + TodoList.TABLE + " AS " + ALIAS_LIST 44 | + " LEFT OUTER JOIN " + TodoItem.TABLE + " AS " + ALIAS_ITEM + " ON " + LIST_ID + " = " + ITEM_LIST_ID 45 | + " GROUP BY " + LIST_ID; 46 | 47 | private long id; 48 | private String name; 49 | private int itemCount; 50 | 51 | public ListsItem(long id, String name, int itemCount) { 52 | this.id = id; 53 | this.name = name; 54 | this.itemCount = itemCount; 55 | } 56 | 57 | static Func1 MAPPER = new Func1() { 58 | @Override 59 | public ListsItem call(Cursor cursor) { 60 | long id = Db.getLong(cursor, TodoList.ID); 61 | String name = Db.getString(cursor, TodoList.NAME); 62 | int itemCount = Db.getInt(cursor, ITEM_COUNT); 63 | return new ListsItem(id, name, itemCount); 64 | } 65 | }; 66 | 67 | @Override 68 | public int describeContents() { 69 | return 0; 70 | } 71 | 72 | @Override 73 | public void writeToParcel(Parcel dest, int flags) { 74 | dest.writeLong(this.id); 75 | dest.writeString(this.name); 76 | dest.writeInt(this.itemCount); 77 | } 78 | 79 | protected ListsItem(Parcel in) { 80 | this.id = in.readLong(); 81 | this.name = in.readString(); 82 | this.itemCount = in.readInt(); 83 | } 84 | 85 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 86 | public ListsItem createFromParcel(Parcel source) { 87 | return new ListsItem(source); 88 | } 89 | 90 | public ListsItem[] newArray(int size) { 91 | return new ListsItem[size]; 92 | } 93 | }; 94 | 95 | public long getId() { 96 | return id; 97 | } 98 | 99 | public void setId(long id) { 100 | this.id = id; 101 | } 102 | 103 | public String getName() { 104 | return name; 105 | } 106 | 107 | public void setName(String name) { 108 | this.name = name; 109 | } 110 | 111 | public int getItemCount() { 112 | return itemCount; 113 | } 114 | 115 | public void setItemCount(int itemCount) { 116 | this.itemCount = itemCount; 117 | } 118 | 119 | public static ListsItem newInstance(@NonNull Cursor cursor) { 120 | long id = Db.getLong(cursor, TodoList.ID); 121 | String name = Db.getString(cursor, TodoList.NAME); 122 | int itemCount = Db.getInt(cursor, ITEM_COUNT); 123 | return new ListsItem(id, name, itemCount); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/Motor.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.model; 2 | 3 | public class Motor { 4 | 5 | private int speed; 6 | 7 | public Motor() { 8 | this.speed = 0; 9 | } 10 | 11 | public int getSpeed() { 12 | return speed; 13 | } 14 | 15 | public void accelerate(int value) { 16 | speed = speed + value; 17 | } 18 | 19 | public void setSpeed(int value) { 20 | speed = value; 21 | } 22 | 23 | public void brake() { 24 | speed = 0; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/TodoItem.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.model; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.os.Parcel; 6 | import android.os.Parcelable; 7 | import android.support.annotation.NonNull; 8 | 9 | import com.drarter.dagger2.example.base.database.Db; 10 | 11 | import rx.functions.Func1; 12 | 13 | public class TodoItem implements Parcelable { 14 | public static final String TABLE = "todo_item"; 15 | 16 | public static final String ID = "_id"; 17 | public static final String LIST_ID = "todo_list_id"; 18 | public static final String DESCRIPTION = "description"; 19 | public static final String COMPLETE = "complete"; 20 | 21 | private long id; 22 | private long listId; 23 | private String description; 24 | private boolean isComplete; 25 | 26 | public TodoItem(long id, long listId, String description, boolean isComplete) { 27 | this.id = id; 28 | this.listId = listId; 29 | this.description = description; 30 | this.isComplete = isComplete; 31 | } 32 | 33 | public static final Func1 MAPPER = new Func1() { 34 | @Override 35 | public TodoItem call(Cursor cursor) { 36 | long id = Db.getLong(cursor, ID); 37 | long listId = Db.getLong(cursor, LIST_ID); 38 | String description = Db.getString(cursor, DESCRIPTION); 39 | boolean complete = Db.getBoolean(cursor, COMPLETE); 40 | 41 | return new TodoItem(id, listId, description, complete); 42 | } 43 | }; 44 | 45 | public long getId() { 46 | return id; 47 | } 48 | 49 | public void setId(long id) { 50 | this.id = id; 51 | } 52 | 53 | public long getListId() { 54 | return listId; 55 | } 56 | 57 | public void setListId(long listId) { 58 | this.listId = listId; 59 | } 60 | 61 | public String getDescription() { 62 | return description; 63 | } 64 | 65 | public void setDescription(String description) { 66 | this.description = description; 67 | } 68 | 69 | public boolean isComplete() { 70 | return isComplete; 71 | } 72 | 73 | public void setIsComplete(boolean isComplete) { 74 | this.isComplete = isComplete; 75 | } 76 | 77 | public static TodoItem createInstance(@NonNull Cursor cursor) { 78 | long id = Db.getLong(cursor, ID); 79 | long listId = Db.getLong(cursor, LIST_ID); 80 | String description = Db.getString(cursor, DESCRIPTION); 81 | boolean complete = Db.getBoolean(cursor, COMPLETE); 82 | 83 | return new TodoItem(id, listId, description, complete); 84 | } 85 | 86 | public static final class Builder { 87 | private final ContentValues values = new ContentValues(); 88 | 89 | public Builder id(long id) { 90 | values.put(ID, id); 91 | return this; 92 | } 93 | 94 | public Builder listId(long listId) { 95 | values.put(LIST_ID, listId); 96 | return this; 97 | } 98 | 99 | public Builder description(String description) { 100 | values.put(DESCRIPTION, description); 101 | return this; 102 | } 103 | 104 | public Builder complete(boolean complete) { 105 | values.put(COMPLETE, complete); 106 | return this; 107 | } 108 | 109 | public ContentValues build() { 110 | return values; // TODO defensive copy? 111 | } 112 | } 113 | 114 | @Override 115 | public int describeContents() { 116 | return 0; 117 | } 118 | 119 | @Override 120 | public void writeToParcel(Parcel dest, int flags) { 121 | dest.writeLong(this.id); 122 | dest.writeLong(this.listId); 123 | dest.writeString(this.description); 124 | dest.writeByte(isComplete ? (byte) 1 : (byte) 0); 125 | } 126 | 127 | protected TodoItem(Parcel in) { 128 | this.id = in.readLong(); 129 | this.listId = in.readLong(); 130 | this.description = in.readString(); 131 | this.isComplete = in.readByte() != 0; 132 | } 133 | 134 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 135 | public TodoItem createFromParcel(Parcel source) { 136 | return new TodoItem(source); 137 | } 138 | 139 | public TodoItem[] newArray(int size) { 140 | return new TodoItem[size]; 141 | } 142 | }; 143 | } 144 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/TodoList.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.model; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.os.Parcel; 6 | import android.os.Parcelable; 7 | 8 | import com.drarter.dagger2.example.base.database.Db; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | import rx.functions.Func1; 14 | 15 | public class TodoList implements Parcelable { 16 | public static final String TABLE = "todo_list"; 17 | 18 | public static final String ID = "_id"; 19 | public static final String NAME = "name"; 20 | public static final String ARCHIVED = "archived"; 21 | 22 | private long id; 23 | private String name; 24 | private boolean isArchived; 25 | 26 | public TodoList(long id, String name, boolean isArchived) { 27 | this.id = id; 28 | this.name = name; 29 | this.isArchived = isArchived; 30 | } 31 | 32 | public static Func1> MAP = new Func1>() { 33 | @Override 34 | public List call(final Cursor cursor) { 35 | try { 36 | List values = new ArrayList<>(cursor.getCount()); 37 | 38 | while (cursor.moveToNext()) { 39 | long id = Db.getLong(cursor, ID); 40 | String name = Db.getString(cursor, NAME); 41 | boolean archived = Db.getBoolean(cursor, ARCHIVED); 42 | values.add(new TodoList(id, name, archived)); 43 | } 44 | return values; 45 | } finally { 46 | cursor.close(); 47 | } 48 | } 49 | }; 50 | 51 | public static final class Builder { 52 | private final ContentValues values = new ContentValues(); 53 | 54 | public Builder id(long id) { 55 | values.put(ID, id); 56 | return this; 57 | } 58 | 59 | public Builder name(String name) { 60 | values.put(NAME, name); 61 | return this; 62 | } 63 | 64 | public Builder archived(boolean archived) { 65 | values.put(ARCHIVED, archived); 66 | return this; 67 | } 68 | 69 | public ContentValues build() { 70 | return values; // TODO defensive copy? 71 | } 72 | } 73 | 74 | @Override 75 | public int describeContents() { 76 | return 0; 77 | } 78 | 79 | @Override 80 | public void writeToParcel(Parcel dest, int flags) { 81 | dest.writeLong(this.id); 82 | dest.writeString(this.name); 83 | dest.writeByte(isArchived ? (byte) 1 : (byte) 0); 84 | } 85 | 86 | protected TodoList(Parcel in) { 87 | this.id = in.readLong(); 88 | this.name = in.readString(); 89 | this.isArchived = in.readByte() != 0; 90 | } 91 | 92 | public static final Creator CREATOR = new Creator() { 93 | public TodoList createFromParcel(Parcel source) { 94 | return new TodoList(source); 95 | } 96 | 97 | public TodoList[] newArray(int size) { 98 | return new TodoList[size]; 99 | } 100 | }; 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/base/model/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.base.model; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | public class Vehicle { 6 | 7 | private Motor motor; 8 | 9 | public Vehicle(@NonNull Motor motor) { 10 | this.motor = motor; 11 | } 12 | 13 | public void increaseSpeed(int value) { 14 | motor.accelerate(value); 15 | } 16 | 17 | public void stop() { 18 | motor.brake(); 19 | } 20 | 21 | public int getSpeed() { 22 | return motor.getSpeed(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/ApplicationContext.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di; 2 | 3 | import java.lang.annotation.Retention; 4 | 5 | import javax.inject.Qualifier; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Qualifier 10 | @Retention(RUNTIME) 11 | public @interface ApplicationContext { 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/PerActivity.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di; 2 | 3 | import java.lang.annotation.Retention; 4 | 5 | import javax.inject.Scope; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Scope 10 | @Retention(RUNTIME) 11 | public @interface PerActivity { 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/PerApp.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di; 2 | 3 | import java.lang.annotation.Retention; 4 | 5 | import javax.inject.Qualifier; 6 | import javax.inject.Scope; 7 | 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | @Scope 11 | @Retention(RUNTIME) 12 | public @interface PerApp { 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/component/ActivityComponent.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.component; 2 | 3 | import com.drarter.dagger2.example.internal.di.PerActivity; 4 | import com.drarter.dagger2.example.internal.di.module.ActivityModule; 5 | 6 | import dagger.Component; 7 | 8 | @PerActivity 9 | @Component( 10 | modules = ActivityModule.class 11 | ) 12 | public interface ActivityComponent { 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/component/ApplicationComponent.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.component; 2 | 3 | import com.drarter.dagger2.example.base.model.Vehicle; 4 | import com.drarter.dagger2.example.internal.di.module.ApplicationModule; 5 | import com.drarter.dagger2.example.internal.di.module.DataBaseModule; 6 | import com.drarter.dagger2.example.internal.di.module.VehicleModule; 7 | import com.squareup.sqlbrite.BriteDatabase; 8 | 9 | import javax.inject.Singleton; 10 | 11 | import dagger.Component; 12 | 13 | @Singleton 14 | @Component( 15 | modules = { 16 | ApplicationModule.class, 17 | VehicleModule.class, 18 | DataBaseModule.class 19 | 20 | } 21 | ) 22 | public interface ApplicationComponent { 23 | Vehicle getVehicle(); 24 | BriteDatabase getBriteDataBase(); 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/component/HasComponent.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.component; 2 | 3 | public interface HasComponent { 4 | C getComponent(); 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/ActivityModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import android.app.Activity; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.drarter.dagger2.example.internal.di.PerActivity; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | @Module 12 | public class ActivityModule { 13 | 14 | private Activity activity; 15 | 16 | public ActivityModule(@NonNull Activity activity) { 17 | this.activity = activity; 18 | } 19 | 20 | @PerActivity 21 | @Provides 22 | Activity provideActivity() { 23 | return this.activity; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/ApplicationModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.drarter.dagger2.example.ExampleApplication; 7 | import com.drarter.dagger2.example.internal.di.ApplicationContext; 8 | import com.drarter.dagger2.example.internal.di.PerApp; 9 | 10 | import javax.inject.Singleton; 11 | 12 | import dagger.Module; 13 | import dagger.Provides; 14 | 15 | @PerApp 16 | @Module 17 | public class ApplicationModule { 18 | 19 | private ExampleApplication billyApplication; 20 | 21 | public ApplicationModule(@NonNull ExampleApplication billyApplication) { 22 | 23 | this.billyApplication = billyApplication; 24 | } 25 | 26 | @Singleton 27 | @ApplicationContext 28 | @Provides 29 | Context provideContext() { 30 | return this.billyApplication.getApplicationContext(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/BikeModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import com.drarter.dagger2.example.base.model.Bike; 4 | import com.drarter.dagger2.example.base.model.Motor; 5 | 6 | import javax.inject.Singleton; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | @Module( 12 | includes = MotorModule.class 13 | ) 14 | public class BikeModule { 15 | 16 | @Provides 17 | Bike provideBike(Motor motor) { 18 | return new Bike(motor); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/DataBaseModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import com.drarter.dagger2.example.base.database.DbOpenHelper; 4 | import com.squareup.sqlbrite.BriteDatabase; 5 | import com.squareup.sqlbrite.SqlBrite; 6 | 7 | import android.content.Context; 8 | import android.support.annotation.NonNull; 9 | 10 | import javax.inject.Singleton; 11 | 12 | import dagger.Module; 13 | import dagger.Provides; 14 | 15 | @Module 16 | public class DataBaseModule { 17 | 18 | private Context context; 19 | 20 | public DataBaseModule(@NonNull Context context) { 21 | 22 | this.context = context; 23 | } 24 | 25 | @Singleton 26 | @Provides 27 | public DbOpenHelper provideDataBaseHelper() { 28 | return new DbOpenHelper(this.context); 29 | } 30 | 31 | @Singleton 32 | @Provides 33 | public SqlBrite provideSqlBrite() { 34 | return SqlBrite.create(); 35 | } 36 | 37 | @Singleton 38 | @Provides 39 | public BriteDatabase provideBriteDataBase(SqlBrite sqlBrite, DbOpenHelper helper) { 40 | return sqlBrite.wrapDatabaseHelper(helper); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/MotorModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import com.drarter.dagger2.example.base.model.Motor; 4 | 5 | import dagger.Module; 6 | import dagger.Provides; 7 | 8 | @Module 9 | public class MotorModule { 10 | 11 | @Provides 12 | Motor provideMotor() { 13 | return new Motor(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/internal/di/module/VehicleModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.internal.di.module; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.drarter.dagger2.example.base.model.Motor; 6 | import com.drarter.dagger2.example.base.model.Vehicle; 7 | 8 | import javax.inject.Singleton; 9 | 10 | import dagger.Module; 11 | import dagger.Provides; 12 | 13 | @Module( 14 | includes = { 15 | MotorModule.class 16 | } 17 | ) 18 | public class VehicleModule { 19 | 20 | @Provides 21 | Vehicle privdeVehicle(@NonNull Motor motor) { 22 | return new Vehicle(motor); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/main/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.main; 2 | 3 | import android.support.annotation.Nullable; 4 | import android.view.Menu; 5 | import android.view.MenuItem; 6 | import android.widget.TextView; 7 | 8 | import com.drarter.dagger2.example.R; 9 | import com.drarter.dagger2.example.base.activity.BaseActivity; 10 | import com.drarter.dagger2.example.base.model.Bike; 11 | import com.drarter.dagger2.example.base.model.Vehicle; 12 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 13 | import com.drarter.dagger2.example.internal.di.module.BikeModule; 14 | 15 | import javax.inject.Inject; 16 | 17 | import butterknife.Bind; 18 | import butterknife.OnClick; 19 | 20 | public class MainActivity extends BaseActivity { 21 | 22 | @Bind(R.id.text_vehicle_result) 23 | TextView textVehicleResult; 24 | 25 | @Bind(R.id.text_bike_result) 26 | TextView textBikeResult; 27 | 28 | @Inject 29 | Vehicle vehicle; 30 | 31 | @Inject 32 | Bike bike; 33 | 34 | @Override 35 | protected int getContentViewResource() { 36 | return R.layout.activity_main; 37 | } 38 | 39 | @Override 40 | protected ActivityComponent getInitializeCompoent() { 41 | return DaggerMainActivityComponent.builder() 42 | .applicationComponent(getApplicationComponent()) 43 | .mainActivityModule(new MainActivityModule(this)) 44 | .bikeModule(new BikeModule()) 45 | .build(); 46 | } 47 | 48 | @Override 49 | protected void onInject(@Nullable ActivityComponent component) { 50 | if (component != null) { 51 | ((MainActivityComponent) component).inject(this); 52 | } 53 | } 54 | 55 | @Override 56 | public boolean onCreateOptionsMenu(Menu menu) { 57 | getMenuInflater().inflate(R.menu.menu_main, menu); 58 | return true; 59 | } 60 | 61 | @Override 62 | public boolean onOptionsItemSelected(MenuItem item) { 63 | int id = item.getItemId(); 64 | 65 | if (id == R.id.action_settings) { 66 | return true; 67 | } 68 | 69 | return super.onOptionsItemSelected(item); 70 | } 71 | 72 | @SuppressWarnings("unused") 73 | @OnClick(R.id.button_vehicle_increase_speed) 74 | public void onSpeedIncreaseClick() { 75 | this.vehicle.increaseSpeed(10); 76 | this.textVehicleResult.setText("vehicle speed : " + vehicle.getSpeed()); 77 | } 78 | 79 | @SuppressWarnings("unused") 80 | @OnClick(R.id.button_vehicle_brake) 81 | public void onVehicleBrake() { 82 | this.vehicle.stop(); 83 | this.textVehicleResult.setText("vehicle speed : " + vehicle.getSpeed()); 84 | } 85 | 86 | @SuppressWarnings("unused") 87 | @OnClick(R.id.button_bike_increase_speed) 88 | public void onBikeIncreaseSpeed() { 89 | this.bike.increaseSpped(); 90 | this.textBikeResult.setText("bike speed : " + bike.getSpeed()); 91 | } 92 | 93 | @SuppressWarnings("unused") 94 | @OnClick(R.id.button_bike_decrease_speed) 95 | public void onBikeDecreaseSpeed() { 96 | this.bike.decreaseSpeed(); 97 | this.textBikeResult.setText("bike speed : " + bike.getSpeed()); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/main/MainActivityComponent.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.main; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.drarter.dagger2.example.internal.di.PerActivity; 6 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 7 | import com.drarter.dagger2.example.internal.di.component.ApplicationComponent; 8 | import com.drarter.dagger2.example.internal.di.module.BikeModule; 9 | import com.drarter.dagger2.example.ui.main.MainActivityModule; 10 | import com.drarter.dagger2.example.ui.main.MainActivity; 11 | 12 | import dagger.Component; 13 | 14 | @PerActivity 15 | @Component( 16 | dependencies = ApplicationComponent.class, 17 | modules = { 18 | MainActivityModule.class, 19 | BikeModule.class 20 | } 21 | ) 22 | public interface MainActivityComponent extends ActivityComponent { 23 | void inject(@NonNull MainActivity mainActivity); 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/main/MainActivityModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.main; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.drarter.dagger2.example.ui.main.MainActivity; 6 | 7 | import java.lang.ref.WeakReference; 8 | 9 | import dagger.Module; 10 | 11 | @Module 12 | public class MainActivityModule { 13 | 14 | private WeakReference activityWeakReference; 15 | 16 | public MainActivityModule(@NonNull MainActivity mainActivity) { 17 | this.activityWeakReference = new WeakReference(mainActivity); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/menu/MenuActivity.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.menu; 2 | 3 | import android.content.Intent; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.drarter.dagger2.example.R; 7 | import com.drarter.dagger2.example.base.activity.BaseActivity; 8 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 9 | import com.drarter.dagger2.example.ui.main.MainActivity; 10 | import com.drarter.dagger2.example.ui.sql.SqlbriteActivity; 11 | 12 | import butterknife.OnClick; 13 | 14 | public class MenuActivity extends BaseActivity { 15 | 16 | @Override 17 | protected int getContentViewResource() { 18 | return R.layout.activity_menu; 19 | } 20 | 21 | @Override 22 | protected ActivityComponent getInitializeCompoent() { 23 | return null; 24 | } 25 | 26 | @Override 27 | protected void onInject(@Nullable ActivityComponent component) { 28 | 29 | } 30 | 31 | @SuppressWarnings("unused") 32 | @OnClick(R.id.button_dagger_ex) 33 | public void onDagger2Exclick() { 34 | onStartActivity(MainActivity.class); 35 | } 36 | 37 | @SuppressWarnings("unused") 38 | @OnClick(R.id.button_sqlbrite_ex) 39 | public void onSqlbriteExClick() { 40 | onStartActivity(SqlbriteActivity.class); 41 | } 42 | 43 | private void onStartActivity(Class cls) { 44 | Intent intent = new Intent(this, cls); 45 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 46 | startActivity(intent); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/SqlbriteActivity.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.drarter.dagger2.example.R; 7 | import com.drarter.dagger2.example.base.activity.BaseActivity; 8 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 9 | import com.drarter.dagger2.example.ui.sql.fragment.ItemsFragment; 10 | import com.drarter.dagger2.example.ui.sql.fragment.ListsFragment; 11 | import com.drarter.dagger2.example.ui.sql.fragment.NewItemFragment; 12 | import com.drarter.dagger2.example.ui.sql.fragment.NewListFragment; 13 | import com.squareup.sqlbrite.BriteDatabase; 14 | 15 | import javax.inject.Inject; 16 | 17 | public class SqlbriteActivity extends BaseActivity implements ListsFragment.OnListsFragmentListener, ItemsFragment.OnItemsFragmentListener { 18 | 19 | @Inject 20 | BriteDatabase db; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | 26 | if (savedInstanceState == null) { 27 | getSupportFragmentManager().beginTransaction() 28 | .add(R.id.frame_container, ListsFragment.newInstance()) 29 | .commit(); 30 | } 31 | } 32 | 33 | @Override 34 | protected int getContentViewResource() { 35 | return R.layout.activity_sqlbrite; 36 | } 37 | 38 | @Override 39 | protected ActivityComponent getInitializeCompoent() { 40 | return DaggerSqlbriteActivityComponent.builder() 41 | .applicationComponent(getApplicationComponent()) 42 | .sqlbriteActivityModule(new SqlbriteActivityModule()) 43 | .build(); 44 | } 45 | 46 | @Override 47 | protected void onInject(@Nullable ActivityComponent component) { 48 | if (component != null) { 49 | ((SqlbriteActivityComponent) component).inject(this); 50 | } 51 | } 52 | 53 | @Override 54 | public void onListClicked(long id) { 55 | getSupportFragmentManager().beginTransaction() 56 | .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, 57 | R.anim.slide_out_right) 58 | .replace(android.R.id.content, ItemsFragment.newInstance(id)) 59 | .addToBackStack(null) 60 | .commit(); 61 | } 62 | 63 | @Override 64 | public void onNewListClicked() { 65 | NewListFragment.newInstance().show(getSupportFragmentManager(), "new-list"); 66 | } 67 | 68 | @Override 69 | public void onNewItemClicked(long listId) { 70 | NewItemFragment.newInstance(listId).show(getSupportFragmentManager(), "new-item"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/SqlbriteActivityComponent.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.drarter.dagger2.example.internal.di.PerActivity; 6 | import com.drarter.dagger2.example.internal.di.component.ActivityComponent; 7 | import com.drarter.dagger2.example.internal.di.component.ApplicationComponent; 8 | import com.drarter.dagger2.example.ui.sql.fragment.ItemsFragment; 9 | import com.drarter.dagger2.example.ui.sql.fragment.ListsFragment; 10 | import com.drarter.dagger2.example.ui.sql.fragment.NewItemFragment; 11 | 12 | import dagger.Component; 13 | 14 | @PerActivity 15 | @Component( 16 | dependencies = ApplicationComponent.class, 17 | modules = { 18 | SqlbriteActivityModule.class 19 | } 20 | ) 21 | public interface SqlbriteActivityComponent extends ActivityComponent { 22 | 23 | void inject(@NonNull SqlbriteActivity sqlbriteActivity); 24 | 25 | void inject(@NonNull ListsFragment fragment); 26 | 27 | void inject(@NonNull NewItemFragment newItemFragment); 28 | 29 | void inject(@NonNull ItemsFragment itemsFragment); 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/SqlbriteActivityModule.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql; 2 | 3 | import dagger.Module; 4 | 5 | @Module 6 | public class SqlbriteActivityModule { 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/adapter/ItemsAdapter2.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.adapter; 2 | 3 | import android.content.Context; 4 | import android.database.Cursor; 5 | import android.text.SpannableString; 6 | import android.text.style.StrikethroughSpan; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.CheckedTextView; 11 | import android.widget.CursorAdapter; 12 | 13 | import com.drarter.dagger2.example.base.model.TodoItem; 14 | 15 | import rx.functions.Action1; 16 | 17 | public class ItemsAdapter2 extends CursorAdapter implements Action1 { 18 | 19 | public ItemsAdapter2(Context context, Cursor c, boolean autoRequery) { 20 | super(context, c, autoRequery); 21 | } 22 | 23 | public ItemsAdapter2(Context context, Cursor c, int flags) { 24 | super(context, c, flags); 25 | } 26 | 27 | @Override 28 | public View newView(Context context, Cursor cursor, ViewGroup parent) { 29 | return LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_multiple_choice, parent, false); 30 | } 31 | 32 | @Override 33 | public void bindView(View view, Context context, Cursor cursor) { 34 | TodoItem item = TodoItem.createInstance(cursor); 35 | 36 | CheckedTextView textView = (CheckedTextView) view; 37 | textView.setChecked(item.isComplete()); 38 | 39 | CharSequence description = item.getDescription(); 40 | if (item.isComplete()) { 41 | SpannableString spannable = new SpannableString(description); 42 | spannable.setSpan(new StrikethroughSpan(), 0, description.length(), 0); 43 | description = spannable; 44 | } 45 | 46 | textView.setText(description); 47 | } 48 | 49 | @Override 50 | public void call(Cursor cursor) { 51 | changeCursor(cursor); 52 | notifyDataSetChanged(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/adapter/ListsAdapter2.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.adapter; 2 | 3 | import android.content.Context; 4 | import android.database.Cursor; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.CursorAdapter; 9 | import android.widget.TextView; 10 | 11 | import com.drarter.dagger2.example.base.model.ListsItem; 12 | 13 | import rx.functions.Action1; 14 | 15 | public class ListsAdapter2 extends CursorAdapter implements Action1 { 16 | 17 | public ListsAdapter2(Context context, Cursor c, boolean autoRequery) { 18 | super(context, c, autoRequery); 19 | } 20 | 21 | public ListsAdapter2(Context context, Cursor c, int flags) { 22 | super(context, c, flags); 23 | } 24 | 25 | @Override 26 | public View newView(Context context, Cursor cursor, ViewGroup parent) { 27 | return LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false); 28 | } 29 | 30 | @Override 31 | public void bindView(View view, Context context, Cursor cursor) { 32 | ListsItem item = ListsItem.newInstance(cursor); 33 | ((TextView) view).setText(item.getName() + " (" + item.getItemCount() + ")"); 34 | } 35 | 36 | @Override 37 | public void call(Cursor cursor) { 38 | changeCursor(cursor); 39 | notifyDataSetChanged(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/fragment/ItemsFragment.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.fragment; 2 | 3 | import android.app.Activity; 4 | import android.database.Cursor; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.view.MenuItemCompat; 8 | import android.view.Menu; 9 | import android.view.MenuInflater; 10 | import android.view.MenuItem; 11 | import android.view.View; 12 | import android.widget.AdapterView; 13 | import android.widget.ListView; 14 | 15 | import com.drarter.dagger2.example.R; 16 | import com.drarter.dagger2.example.base.database.Db; 17 | import com.drarter.dagger2.example.base.model.TodoItem; 18 | import com.drarter.dagger2.example.base.model.TodoList; 19 | import com.drarter.dagger2.example.base.fragment.BaseFragment; 20 | import com.drarter.dagger2.example.ui.sql.SqlbriteActivityComponent; 21 | import com.drarter.dagger2.example.ui.sql.adapter.ItemsAdapter2; 22 | import com.squareup.sqlbrite.BriteDatabase; 23 | 24 | import javax.inject.Inject; 25 | 26 | import butterknife.Bind; 27 | import rx.Observable; 28 | import rx.android.schedulers.AndroidSchedulers; 29 | import rx.functions.Action1; 30 | import rx.functions.Func1; 31 | import rx.functions.Func2; 32 | import rx.schedulers.Schedulers; 33 | import rx.subscriptions.CompositeSubscription; 34 | 35 | import static android.support.v4.view.MenuItemCompat.SHOW_AS_ACTION_IF_ROOM; 36 | import static android.support.v4.view.MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT; 37 | import static com.squareup.sqlbrite.SqlBrite.Query; 38 | 39 | public final class ItemsFragment extends BaseFragment { 40 | private static final String KEY_LIST_ID = "list_id"; 41 | private static final String LIST_QUERY = "SELECT * FROM " 42 | + TodoItem.TABLE 43 | + " WHERE " 44 | + TodoItem.LIST_ID 45 | + " = ? ORDER BY " 46 | + TodoItem.COMPLETE 47 | 48 | + " ASC"; 49 | private static final String COUNT_QUERY = "SELECT COUNT(*) FROM " 50 | + TodoItem.TABLE 51 | + " WHERE " 52 | + TodoItem.COMPLETE 53 | + " = " 54 | + Db.BOOLEAN_FALSE 55 | + " AND " 56 | + TodoItem.LIST_ID 57 | + " = ?"; 58 | private static final String TITLE_QUERY = 59 | "SELECT " + TodoList.NAME + " FROM " + TodoList.TABLE + " WHERE " + TodoList.ID + " = ?"; 60 | 61 | public interface OnItemsFragmentListener { 62 | void onNewItemClicked(long listId); 63 | } 64 | 65 | public static ItemsFragment newInstance(long listId) { 66 | Bundle arguments = new Bundle(); 67 | arguments.putLong(KEY_LIST_ID, listId); 68 | 69 | ItemsFragment fragment = new ItemsFragment(); 70 | fragment.setArguments(arguments); 71 | return fragment; 72 | } 73 | 74 | @Inject 75 | BriteDatabase db; 76 | 77 | @Bind(android.R.id.list) 78 | ListView listView; 79 | @Bind(android.R.id.empty) 80 | View emptyView; 81 | 82 | private OnItemsFragmentListener onItemsFragmentListener; 83 | // private ItemsAdapter adapter; 84 | private ItemsAdapter2 adapter; 85 | private CompositeSubscription subscriptions; 86 | 87 | private long getListId() { 88 | return getArguments().getLong(KEY_LIST_ID); 89 | } 90 | 91 | @Override 92 | public void onAttach(Activity activity) { 93 | if (!(activity instanceof OnItemsFragmentListener)) { 94 | throw new IllegalStateException("Activity must implement fragment Listener."); 95 | } 96 | 97 | super.onAttach(activity); 98 | 99 | getComponent(SqlbriteActivityComponent.class).inject(this); 100 | 101 | setHasOptionsMenu(true); 102 | 103 | onItemsFragmentListener = (OnItemsFragmentListener) activity; 104 | // adapter = new ItemsAdapter(activity); 105 | adapter = new ItemsAdapter2(activity, null, true); 106 | } 107 | 108 | @Override 109 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 110 | super.onCreateOptionsMenu(menu, inflater); 111 | 112 | MenuItem item = menu.add(R.string.new_item) 113 | .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 114 | @Override 115 | public boolean onMenuItemClick(MenuItem item) { 116 | onItemsFragmentListener.onNewItemClicked(getListId()); 117 | return true; 118 | } 119 | }); 120 | MenuItemCompat.setShowAsAction(item, SHOW_AS_ACTION_IF_ROOM | SHOW_AS_ACTION_WITH_TEXT); 121 | } 122 | 123 | @Override 124 | protected int getLayoutResId() { 125 | return R.layout.items; 126 | } 127 | 128 | @Override 129 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 130 | super.onViewCreated(view, savedInstanceState); 131 | listView.setEmptyView(emptyView); 132 | listView.setAdapter(adapter); 133 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 134 | @Override 135 | public void onItemClick(AdapterView parent, View view, int position, long id) { 136 | Cursor cursor = (Cursor) adapter.getItem(position); 137 | if (cursor != null) { 138 | TodoItem item = TodoItem.createInstance(cursor); 139 | boolean newValue = !item.isComplete(); 140 | db.update(TodoItem.TABLE, new TodoItem.Builder().complete(newValue).build(), 141 | TodoItem.ID + " = ?", String.valueOf(item.getId())); 142 | } 143 | } 144 | }); 145 | } 146 | 147 | @Override 148 | public void onResume() { 149 | super.onResume(); 150 | String listId = String.valueOf(getListId()); 151 | 152 | subscriptions = new CompositeSubscription(); 153 | 154 | Observable itemCount = db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId) 155 | .map(new Func1() { 156 | @Override 157 | public Integer call(Query query) { 158 | Cursor cursor = query.run(); 159 | try { 160 | if (!cursor.moveToNext()) { 161 | throw new AssertionError("No rows"); 162 | } 163 | return cursor.getInt(0); 164 | } finally { 165 | cursor.close(); 166 | } 167 | } 168 | }); 169 | Observable listName = 170 | db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Func1() { 171 | @Override 172 | public String call(Query query) { 173 | Cursor cursor = query.run(); 174 | try { 175 | if (!cursor.moveToNext()) { 176 | throw new AssertionError("No rows"); 177 | } 178 | return cursor.getString(0); 179 | } finally { 180 | cursor.close(); 181 | } 182 | } 183 | }); 184 | subscriptions.add( 185 | Observable.combineLatest(listName, itemCount, new Func2() { 186 | @Override 187 | public String call(String listName, Integer itemCount) { 188 | return listName + " (" + itemCount + ")"; 189 | } 190 | }) 191 | .subscribeOn(Schedulers.io()) 192 | .observeOn(AndroidSchedulers.mainThread()) 193 | .subscribe(new Action1() { 194 | @Override 195 | public void call(String title) { 196 | getActivity().setTitle(title); 197 | } 198 | })); 199 | subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId) 200 | .map(new Func1() { 201 | @Override 202 | public Cursor call(Query query) { 203 | return query.run(); 204 | } 205 | }) 206 | .subscribeOn(Schedulers.io()) 207 | .observeOn(AndroidSchedulers.mainThread()) 208 | .subscribe(adapter)); 209 | } 210 | 211 | @Override 212 | public void onPause() { 213 | super.onPause(); 214 | subscriptions.unsubscribe(); 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/fragment/ListsFragment.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.fragment; 2 | 3 | import android.app.Activity; 4 | import android.database.Cursor; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.view.MenuItemCompat; 8 | import android.view.Menu; 9 | import android.view.MenuInflater; 10 | import android.view.MenuItem; 11 | import android.view.View; 12 | import android.widget.ListView; 13 | 14 | import com.drarter.dagger2.example.R; 15 | import com.drarter.dagger2.example.base.fragment.BaseFragment; 16 | import com.drarter.dagger2.example.base.model.ListsItem; 17 | import com.drarter.dagger2.example.ui.sql.SqlbriteActivityComponent; 18 | import com.drarter.dagger2.example.ui.sql.adapter.ListsAdapter2; 19 | import com.squareup.sqlbrite.BriteDatabase; 20 | import com.squareup.sqlbrite.SqlBrite; 21 | 22 | import javax.inject.Inject; 23 | 24 | import butterknife.Bind; 25 | import butterknife.ButterKnife; 26 | import butterknife.OnItemClick; 27 | import rx.Subscription; 28 | import rx.android.schedulers.AndroidSchedulers; 29 | import rx.functions.Func1; 30 | import rx.schedulers.Schedulers; 31 | 32 | import static android.support.v4.view.MenuItemCompat.SHOW_AS_ACTION_IF_ROOM; 33 | import static android.support.v4.view.MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT; 34 | 35 | public final class ListsFragment extends BaseFragment { 36 | 37 | public interface OnListsFragmentListener { 38 | void onListClicked(long id); 39 | 40 | void onNewListClicked(); 41 | } 42 | 43 | public static ListsFragment newInstance() { 44 | return new ListsFragment(); 45 | } 46 | 47 | @Inject 48 | BriteDatabase db; 49 | 50 | @Bind(android.R.id.list) 51 | ListView listView; 52 | @Bind(android.R.id.empty) 53 | View emptyView; 54 | 55 | private OnListsFragmentListener onListsFragmentListener; 56 | private ListsAdapter2 adapter; 57 | private Subscription subscription; 58 | 59 | @Override 60 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 61 | super.onActivityCreated(savedInstanceState); 62 | getComponent(SqlbriteActivityComponent.class).inject(this); 63 | } 64 | 65 | @Override 66 | public void onAttach(Activity activity) { 67 | if (!(activity instanceof OnListsFragmentListener)) { 68 | throw new IllegalStateException("Activity must implement fragment Listener."); 69 | } 70 | 71 | super.onAttach(activity); 72 | setHasOptionsMenu(true); 73 | 74 | onListsFragmentListener = (OnListsFragmentListener) activity; 75 | adapter = new ListsAdapter2(activity, null, true); 76 | } 77 | 78 | @Override 79 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 80 | super.onCreateOptionsMenu(menu, inflater); 81 | 82 | MenuItem item = menu.add(R.string.new_list) 83 | .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 84 | @Override 85 | public boolean onMenuItemClick(MenuItem item) { 86 | onListsFragmentListener.onNewListClicked(); 87 | return true; 88 | } 89 | }); 90 | MenuItemCompat.setShowAsAction(item, SHOW_AS_ACTION_IF_ROOM | SHOW_AS_ACTION_WITH_TEXT); 91 | } 92 | 93 | @Override 94 | protected int getLayoutResId() { 95 | return R.layout.lists; 96 | } 97 | 98 | @Override 99 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 100 | super.onViewCreated(view, savedInstanceState); 101 | ButterKnife.bind(this, view); 102 | listView.setEmptyView(emptyView); 103 | listView.setAdapter(adapter); 104 | } 105 | 106 | @OnItemClick(android.R.id.list) 107 | void listClicked(long listId) { 108 | onListsFragmentListener.onListClicked(listId); 109 | } 110 | 111 | @Override 112 | public void onResume() { 113 | super.onResume(); 114 | 115 | getActivity().setTitle("To-Do"); 116 | 117 | subscription = db.createQuery(ListsItem.TABLES, ListsItem.QUERY) 118 | .map(new Func1() { 119 | @Override 120 | public Cursor call(SqlBrite.Query query) { 121 | return query.run(); 122 | } 123 | }) 124 | .subscribeOn(Schedulers.io()) 125 | .observeOn(AndroidSchedulers.mainThread()) 126 | .subscribe(adapter); 127 | } 128 | 129 | @Override 130 | public void onPause() { 131 | super.onPause(); 132 | subscription.unsubscribe(); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/fragment/NewItemFragment.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.fragment; 2 | 3 | import android.app.AlertDialog; 4 | import android.app.Dialog; 5 | import android.content.Context; 6 | import android.content.DialogInterface; 7 | import android.os.Bundle; 8 | import android.support.annotation.NonNull; 9 | import android.support.v4.app.DialogFragment; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.widget.EditText; 13 | 14 | import com.drarter.dagger2.example.R; 15 | import com.drarter.dagger2.example.base.model.TodoItem; 16 | import com.drarter.dagger2.example.internal.di.component.HasComponent; 17 | import com.drarter.dagger2.example.ui.sql.SqlbriteActivityComponent; 18 | import com.squareup.sqlbrite.BriteDatabase; 19 | 20 | import javax.inject.Inject; 21 | 22 | import static butterknife.ButterKnife.findById; 23 | 24 | public final class NewItemFragment extends DialogFragment { 25 | private static final String KEY_LIST_ID = "list_id"; 26 | 27 | public static NewItemFragment newInstance(long listId) { 28 | Bundle arguments = new Bundle(); 29 | arguments.putLong(KEY_LIST_ID, listId); 30 | 31 | NewItemFragment fragment = new NewItemFragment(); 32 | fragment.setArguments(arguments); 33 | return fragment; 34 | } 35 | 36 | @Inject 37 | BriteDatabase db; 38 | 39 | @Override 40 | public void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | getComponent(SqlbriteActivityComponent.class).inject(this); 43 | } 44 | 45 | @SuppressWarnings("unchecked") 46 | protected C getComponent(Class componentType) { 47 | return componentType.cast(((HasComponent) getActivity()).getComponent()); 48 | } 49 | 50 | private long getListId() { 51 | return getArguments().getLong(KEY_LIST_ID); 52 | } 53 | 54 | @NonNull 55 | @Override 56 | public Dialog onCreateDialog(Bundle savedInstanceState) { 57 | final Context context = getActivity(); 58 | View view = LayoutInflater.from(context).inflate(R.layout.new_item, null); 59 | 60 | final EditText name = findById(view, android.R.id.input); 61 | 62 | return new AlertDialog.Builder(context) // 63 | .setTitle(R.string.new_item) 64 | .setView(view) 65 | .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { 66 | @Override 67 | public void onClick(DialogInterface dialog, int which) { 68 | // createClicked.onNext("clicked"); 69 | 70 | db.insert(TodoItem.TABLE, 71 | new TodoItem.Builder().listId(getListId()).description(name.getText().toString()).build()); 72 | 73 | } 74 | }) 75 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 76 | @Override 77 | public void onClick(@NonNull DialogInterface dialog, int which) { 78 | } 79 | }) 80 | .create(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/drarter/dagger2/example/ui/sql/fragment/NewListFragment.java: -------------------------------------------------------------------------------- 1 | package com.drarter.dagger2.example.ui.sql.fragment; 2 | 3 | import android.app.AlertDialog; 4 | import android.app.Dialog; 5 | import android.content.Context; 6 | import android.content.DialogInterface; 7 | import android.os.Bundle; 8 | import android.support.annotation.NonNull; 9 | import android.support.v4.app.DialogFragment; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.widget.EditText; 13 | 14 | import com.drarter.dagger2.example.R; 15 | import com.drarter.dagger2.example.base.model.TodoList; 16 | import com.squareup.sqlbrite.BriteDatabase; 17 | 18 | import javax.inject.Inject; 19 | 20 | import static butterknife.ButterKnife.findById; 21 | 22 | public final class NewListFragment extends DialogFragment { 23 | public static NewListFragment newInstance() { 24 | return new NewListFragment(); 25 | } 26 | 27 | @Inject 28 | BriteDatabase db; 29 | 30 | @NonNull 31 | @Override 32 | public Dialog onCreateDialog(Bundle savedInstanceState) { 33 | final Context context = getActivity(); 34 | View view = LayoutInflater.from(context).inflate(R.layout.new_list, null); 35 | final EditText name = findById(view, android.R.id.input); 36 | return new AlertDialog.Builder(context) // 37 | .setTitle(R.string.new_list) 38 | .setView(view) 39 | .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() { 40 | @Override 41 | public void onClick(DialogInterface dialog, int which) { 42 | db.insert(TodoList.TABLE, new TodoList.Builder().name(name.getText().toString()).build()); 43 | } 44 | }) 45 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 46 | @Override 47 | public void onClick(@NonNull DialogInterface dialog, int which) { 48 | } 49 | }) 50 | .create(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_in_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_in_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_out_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_out_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 16 | 17 | 22 | 23 | 29 | 30 |