LoginSignup
5
7

More than 5 years have passed since last update.

Kotlin+Dagger2+RxJava2+CleanArchitectureのメモ

Posted at

はじめに

Kotlin初心者がKotlin+Dagger2+RxJava2+CleanArchitectureで開発するときのTips。
といっても、「Javaだとこう書いてたけどKotlinだとどうなるの?」という部分がほとんど。
Kotlinの文法が解ってれば不要な内容でもあるのでほぼ自分用のメモ

追記する可能性がないこともない

@Componentで複数のModuleを指定する書き方

{}のところはarrayOf(~)になる
XXX.classXXX::classになる

RootComponent.java
@Singleton
@Component(modules = {ApplicationModules.class, RepositoryModules.class } )
public interface RootComponent {
    Context getContext();
}
RootComponent.kt
@Singleton
@Component(modules = arrayOf(ApplicationModule::class, RepositoryModules::class))
interface RootComponent {
    val context: Context
}

コンストラクタでの@Injectの書き方

プライマリコンストラクタで指定する
その時には、constructorの記載が必要

SomeRepository.java

public class SomeRepository implements SomeDataSource {
    final Context mContext;

    @Inject
    public SomeRepository(Context context) {mContext = context;}

}
SomeRepository.kt
class SomeRepository @Inject constructor(val context:Context): SomeDataSource {
}

RxJavaのZip系で出て来るFunctionあたりの書き方

BiFunctionになる。
型推論してくれるかなぁと思ったけどしてくれなかった(ひょっとしたら書き方があるかもしれないけど)

SomeClass.java(retroRambda使用)

Observable<Pair<String,int>> someMethod(String data1, int data2) {
   return Observable.zip(Observable.just(data1), Observable.just(data2), (e1, e2) -> new Pair(e1, e2));
}
SomeClass.kt
fun someMethod(val data1:String, val data2:Int): Observable<Pair<String,int>> {
   return Observable.zip(Observable.just(data1), Observable.just(data2), BiFunction<String, Int, Pair<String, Int>> {e1, e2 -> Pair(e1, e2)})
}
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7