Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add todos-with-dagger sample #23

Merged
merged 2 commits into from
Nov 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':droidux', ':droidux-processor', ':middlewares:droidux-thunk', ':examples:todomvc', ':examples:todos-with-undo'
include ':droidux', ':droidux-processor', ':middlewares:droidux-thunk', ':examples:todomvc', ':examples:todos-with-undo', ':todos-with-dagger'
1 change: 1 addition & 0 deletions todos-with-dagger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
44 changes: 44 additions & 0 deletions todos-with-dagger/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "info.izumin.android.droidux.example.todoswithdagger"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'io.reactivex:rxjava:1.0.15'
compile 'io.reactivex:rxandroid:1.0.1'
compile project(':droidux')
apt project(':droidux-processor')
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
compile 'com.google.code.gson:gson:2.4'
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
}
17 changes: 17 additions & 0 deletions todos-with-dagger/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package info.izumin.android.droidux.example.todoswithdagger;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
21 changes: 21 additions & 0 deletions todos-with-dagger/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.izumin.android.droidux.example.todoswithdagger">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".App">
<activity android:name=".module.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package info.izumin.android.droidux.example.todoswithdagger;

import android.app.Application;

import info.izumin.android.droidux.example.todoswithdagger.entity.TodoList;
import info.izumin.android.droidux.example.todoswithdagger.reducer.TodoListReducer;

/**
* Created by izumin on 11/4/15.
*/
public class App extends Application {
public static final String TAG = App.class.getSimpleName();

private RootStore store;
private AppComponent component;

@Override
public void onCreate() {
super.onCreate();
setupStore();
setupGraph();
}

public RootStore getStore() {
return store;
}

public AppComponent getComponent() {
return component;
}

private void setupStore() {
store = new DroiduxRootStore.Builder()
.setInitialState(new TodoList())
.setReducer(new TodoListReducer())
.build();
}

private void setupGraph() {
component = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package info.izumin.android.droidux.example.todoswithdagger;

import javax.inject.Singleton;

import dagger.Component;
import info.izumin.android.droidux.example.todoswithdagger.module.main.MainActivityComponent;
import info.izumin.android.droidux.example.todoswithdagger.module.main.MainActivityModule;

/**
* Created by izumin on 11/29/15.
*/
@Singleton
@Component(
modules = AppModule.class
)
public interface AppComponent {
MainActivityComponent createMainActivityComponent(MainActivityModule module);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package info.izumin.android.droidux.example.todoswithdagger;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

/**
* Created by izumin on 11/29/15.
*/
@Module
public class AppModule {

private final App app;

public AppModule(App app) {
this.app = app;
}

@Provides
@Singleton
RootStore provideRootStore() {
return app.getStore();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package info.izumin.android.droidux.example.todoswithdagger;

import info.izumin.android.droidux.Action;
import info.izumin.android.droidux.annotation.Store;
import info.izumin.android.droidux.example.todoswithdagger.entity.TodoList;
import info.izumin.android.droidux.example.todoswithdagger.reducer.TodoListReducer;
import rx.Observable;

/**
* Created by izumin on 11/29/15.
*/
@Store(TodoListReducer.class)
public interface RootStore {
TodoList todoList();
Observable<TodoList> observeTodoList();
Observable<Action> dispatch(Action action);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package info.izumin.android.droidux.example.todoswithdagger.action;

import com.google.gson.Gson;

import info.izumin.android.droidux.Action;

/**
* Created by izumin on 11/4/15.
*/
public class AddTodoAction implements Action {
public static final String TAG = AddTodoAction.class.getSimpleName();

private final String text;

public AddTodoAction(String text) {
this.text = text;
}

public String getText() {
return text;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package info.izumin.android.droidux.example.todoswithdagger.action;

import info.izumin.android.droidux.Action;

/**
* Created by izumin on 11/5/15.
*/
public class ClearCompletedTodoAction implements Action {
public static final String TAG = ClearCompletedTodoAction.class.getSimpleName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package info.izumin.android.droidux.example.todoswithdagger.action;

import com.google.gson.Gson;

import info.izumin.android.droidux.Action;

/**
* Created by izumin on 11/5/15.
*/
public class DeleteTodoAction implements Action {
public static final String TAG = DeleteTodoAction.class.getSimpleName();

private final long id;

public DeleteTodoAction(long id) {
this.id = id;
}

public long getId() {
return id;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package info.izumin.android.droidux.example.todoswithdagger.action;

import com.google.gson.Gson;

import info.izumin.android.droidux.Action;

/**
* Created by izumin on 11/5/15.
*/
public class ToggleCompletedTodoAction implements Action {
public static final String TAG = ToggleCompletedTodoAction.class.getSimpleName();

private final int id;

public ToggleCompletedTodoAction(int id) {
this.id = id;
}

public int getId() {
return id;
}

@Override
public String toString() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package info.izumin.android.droidux.example.todoswithdagger.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import info.izumin.android.droidux.example.todoswithdagger.App;
import info.izumin.android.droidux.example.todoswithdagger.R;
import info.izumin.android.droidux.example.todoswithdagger.RootStore;
import info.izumin.android.droidux.example.todoswithdagger.databinding.ListItemTodoBinding;
import info.izumin.android.droidux.example.todoswithdagger.entity.TodoList;

/**
* Created by izumin on 11/4/15.
*/
public class TodoListAdapter extends BaseAdapter {
public static final String TAG = TodoListAdapter.class.getSimpleName();

private static final int LAYOUT_RES_ID = R.layout.list_item_todo;

private final LayoutInflater inflater;
private final RootStore store;

public TodoListAdapter(Context context) {
super();
this.inflater = LayoutInflater.from(context);
this.store = ((App) context.getApplicationContext()).getStore();
this.store.observeTodoList().subscribe(todoList -> notifyDataSetChanged());
}

@Override
public int getCount() {
return store.todoList().size();
}

@Override
public TodoList.Todo getItem(int position) {
return store.todoList().get(position);
}

@Override
public long getItemId(int position) {
return getItem(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListItemTodoBinding binding;
if (convertView == null) {
binding = DataBindingUtil.inflate(inflater, LAYOUT_RES_ID, parent, false);
convertView = binding.getRoot();
convertView.setTag(binding);
} else {
binding = (ListItemTodoBinding) convertView.getTag();
}

binding.setTodo(getItem(position));

return convertView;
}
}
Loading