Last active
May 23, 2017 11:15
-
-
Save maomaobug/474f90c71bdcbaf8a56d166da80a9d85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module //使用 @Module 标记的 class,就成为 Dagger 2的一个 Module | |
public class AppModule { | |
private final DemoApplication mDemoApplication; // extends android.app.Application | |
// Module 的构造函数即普通的 Java class 构造函数,可以有任意个, | |
// 也可以包含任意构造参数。一个 Module 的构造函数调用, | |
// 即一个模块的初始化,这样说有点含糊不清,先放着继续往下看 | |
public AppModule(DemoApplication demoApplication) { | |
mDemoApplication = demoApplication; | |
} | |
// AppInfo 是我们项目中一个类似于 Meta 信息的类,包含诸如版本号, | |
// 渠道号,系统版本。。。业务无关的信息,显然事实上这应该是一个单例。 | |
// 但是,我们并不需要在 AppInfo.java 的代码中去写单例模式, | |
// @Singleton 会保证这个类只会被实例化一次(为了简单,不完全正确)。 | |
// 注意这个 @Provides 后面会解释它是干嘛用的 | |
@Provides | |
@Singleton | |
AppInfo provideAppInfo() { | |
// 这里我们的 AppInfo 依赖 android.app.Application, | |
// 这在模块初始化的时候已经通过构造参数得到了 | |
return new AppInfo(mTLApplication); | |
} | |
/** | |
* Other provide methods | |
*/ | |
@Provides | |
@Singleton | |
FileStorage provideFileStorage() { | |
return new FileStorage(mTLApplication); | |
} | |
/** | |
* 这里就用到了上面的 @Provides 了,我们用一个场景来解释: | |
* 当某个地方需要用到 UserDataStore, 且没有已经实例化 | |
* 的(@Singleton)的时候,Dagger2 会在各个 Module 中 | |
* 搜索被 @Provides 注解并且返回值类型是 UserDataStore | |
* 的方法,也就是这个方法,来构造一个实例(作为单例)。 | |
* | |
* OK,这个方法要被调用了,但是这个方法有个参数 FileStorage, | |
* 这个参数从哪来呢? 递归之前的过程,搜索被 @Provides | |
* 注解并返回值类型是 FileStoreage 的方法,这样就搜到上面 | |
* 的↑方法 provideFileStorage(), 调用它获得我们需要的 | |
* FileStorage 参数(当然不一定真的调用,因为 | |
* provideFileStorage()被注解为@Singleton,会优先用已 | |
* 经实例化的单例),然后我们的 UserDataStore 就实例化好了。 | |
*/ | |
@Provides | |
@Singleton | |
public UserDataStore provideUserDataStore(FileStorage fileStorage) { | |
return new UserDataStore(fileStorage); | |
} | |
/** | |
* 过程同 provideUserDataStore | |
*/ | |
@Provides | |
@Singleton | |
public UserManager2 provideUserManager(UserDataStore userDataStore, | |
UserComponent.Builder builder, | |
TLTokenInterceptor tokenInterceptor) { | |
return new UserManager2(userDataStore, builder, tokenInterceptor); | |
} | |
@Provides | |
@Singleton | |
Gson provideGson() { | |
return new GsonBuilder() | |
.addDeserializationExclusionStrategy(new IgnoreDeserializationStrategy()) | |
.addSerializationExclusionStrategy(new IgnoreSerializationStrategy()) | |
.create(); | |
} | |
@Provides | |
@Singleton | |
RealmConfiguration provideRealmConfiguration(FileStorage fileStorage) { | |
File realmDir = new File(fileStorage.getInternalBaseDir(), FilePathConstants | |
.REALM_DIR_INTERNAL_RELATIVE); | |
return new RealmConfiguration.Builder() | |
.directory(realmDir) | |
.deleteRealmIfMigrationNeeded() | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment