Commit 4c727a7c by pye52

初步实现module及component

parent e4d10df3
...@@ -13,7 +13,10 @@ import com.blankj.utilcode.util.Utils; ...@@ -13,7 +13,10 @@ import com.blankj.utilcode.util.Utils;
import java.io.File; import java.io.File;
public class RootApp extends Application implements Configuration.Provider { import dagger.android.AndroidInjector;
import dagger.android.support.DaggerApplication;
public class RootApp extends DaggerApplication implements Configuration.Provider {
private static final String LOG_PREFIX = "app"; private static final String LOG_PREFIX = "app";
private static final String LOG_DIR = "log"; private static final String LOG_DIR = "log";
// 日志文件保留周期 // 日志文件保留周期
...@@ -40,4 +43,9 @@ public class RootApp extends Application implements Configuration.Provider { ...@@ -40,4 +43,9 @@ public class RootApp extends Application implements Configuration.Provider {
.setExecutor(SCTaskExecutor.getIOThreadExecutor()) .setExecutor(SCTaskExecutor.getIOThreadExecutor())
.build(); .build();
} }
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return null;
}
} }
package com.bgycc.smartcanteen.dagger;
import android.content.Context;
import com.bgycc.smartcanteen.RootApp;
import javax.inject.Singleton;
import dagger.BindsInstance;
import dagger.Component;
import dagger.android.AndroidInjector;
@Singleton
@Component(modules = ApplicationModule.class)
interface ApplicationComponent extends AndroidInjector<RootApp> {
@Component.Factory
interface Factory {
ApplicationComponent create(@BindsInstance Context applicationContext);
}
}
\ No newline at end of file
package com.bgycc.smartcanteen.dagger;
import android.content.Context;
import androidx.room.Room;
import com.bgycc.smartcanteen.data.DatabaseManager;
import com.bgycc.smartcanteen.data.ForceToPayOffline;
import com.bgycc.smartcanteen.data.OldDataCompatible;
import com.bgycc.smartcanteen.data._1To_2Migration;
import com.bgycc.smartcanteen.entity.PayRequest;
import com.bgycc.smartcanteen.entity.typeadapter.PayRequestTypeAdapter;
import com.bgycc.smartcanteen.repository.CommandRepository;
import com.bgycc.smartcanteen.repository.PayDataRepository;
import com.bgycc.smartcanteen.repository.PayResponseRepository;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import kotlin.jvm.JvmStatic;
@Module
public class ApplicationModule {
private static final String DB_NAME = "smart_canteen.db";
@JvmStatic
@Singleton
@Provides
static CommandRepository provideCommandRepository(DatabaseManager database) {
return new CommandRepository(database.getCommandDao());
}
@JvmStatic
@Singleton
@Provides
static PayDataRepository providePayDataRepository(DatabaseManager database) {
return new PayDataRepository(database.getPayDataDao());
}
@JvmStatic
@Singleton
@Provides
static PayResponseRepository providePayResponseRepository(DatabaseManager database) {
return new PayResponseRepository(database.getPayResponseDao());
}
@JvmStatic
@Singleton
@Provides
static DatabaseManager provideDatabase(Context context) {
return Room.databaseBuilder(
context.getApplicationContext(),
DatabaseManager.class,
DB_NAME)
.addCallback(new OldDataCompatible())
.addCallback(new ForceToPayOffline())
.addMigrations(new _1To_2Migration())
// 一旦版本不兼容则清空数据库
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
}
@JvmStatic
@Singleton
@Provides
static Gson provideGson() {
return new GsonBuilder()
.registerTypeAdapter(PayRequest.class, new PayRequestTypeAdapter())
.create();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment