メインコンテンツまでスキップ

Android - Jetpack Compose

このチュートリアルでは、Androidアプリケーションを作成し、Koinの依存性注入を使用してコンポーネントを取得する方法を説明します。 チュートリアルの所要時間は約__10分__です。

注記

更新 - 2024-10-21

コードを入手する

Gradleの設定

以下のようにKoin Androidの依存関係を追加します。

dependencies {

// Koin for Android
implementation "io.insert-koin:koin-androidx-compose:$koin_version"
}

アプリケーションの概要

このアプリケーションのアイデアは、ユーザーのリストを管理し、PresenterまたはViewModelを使用してMainActivityクラスに表示することです。

Users -> UserRepository -> (Presenter or ViewModel) -> Composable

"User"データ

ユーザーのコレクションを管理します。データクラスは次のとおりです。

data class User(val name : String)

ユーザーのリストを管理する(ユーザーを追加したり、名前で検索したりする)ための"Repository"コンポーネントを作成します。以下は、UserRepositoryインターフェースとその実装です。

interface UserRepository {
fun findUser(name : String): User?
fun addUsers(users : List<User>)
}

class UserRepositoryImpl : UserRepository {

private val _users = arrayListOf<User>()

override fun findUser(name: String): User? {
return _users.firstOrNull { it.name == name }
}

override fun addUsers(users : List<User>) {
_users.addAll(users)
}
}

Koinモジュール

module関数を使用してKoinモジュールを宣言します。 Koinモジュールは、注入されるすべてのコンポーネントを定義する場所です。

val appModule = module {

}

最初のコンポーネントを宣言しましょう。 UserRepositoryImplのインスタンスを作成して、UserRepositoryのシングルトンが必要です。

val appModule = module {
singleOf(::UserRepositoryImpl) bind UserRepository::class
}

UserViewModelを使用したユーザーの表示

UserViewModelクラス

ユーザーを表示するためのViewModelコンポーネントを作成しましょう。

class UserViewModel(private val repository: UserRepository) : ViewModel() {

fun sayHello(name : String) : String{
val foundUser = repository.findUser(name)
return foundUser?.let { "Hello '$it' from $this" } ?: "User '$name' not found!"
}
}

UserRepositoryはUserViewModelのコンストラクタで参照されています

KoinモジュールでUserViewModelを宣言します。 メモリにインスタンスを保持しないように(Androidライフサイクルでのリークを回避するため)、viewModelOf定義として宣言します。

val appModule = module {
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
viewModelOf(::UserViewModel)
}

get()関数を使用すると、Koinに必要な依存関係を解決するように依頼できます。

ComposeでのViewModelの注入

UserViewModelコンポーネントが作成され、UserRepositoryインスタンスが解決されます。 Activityに取得するには、koinViewModel()関数を使用して注入しましょう。

@Composable
fun ViewModelInject(userName : String, viewModel: UserViewModel = koinViewModel()){
Text(text = viewModel.sayHello(userName), modifier = Modifier.padding(8.dp))
}
備考

koinViewModel関数を使用すると、ViewModelインスタンスを取得し、関連するViewModel Factoryを作成して、ライフサイクルにバインドできます

UserStateHolderを使用したユーザーの表示

UserStateHolderクラス

ユーザーを表示するためのState holderコンポーネントを作成しましょう。

class UserStateHolder(private val repository: UserRepository) {

fun sayHello(name : String) : String{
val foundUser = repository.findUser(name)
return foundUser?.let { "Hello '$it' from $this" } ?: "User '$name' not found!"
}
}

UserRepositoryはUserViewModelのコンストラクタで参照されています

KoinモジュールでUserStateHolderを宣言します。 メモリにインスタンスを保持しないように(Androidライフサイクルでのリークを回避するため)、factoryOf定義として宣言します。

val appModule = module {
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
factoryOf(::UserStateHolder)
}

ComposeでのUserStateHolderの注入

UserStateHolderコンポーネントが作成され、UserRepositoryインスタンスが解決されます。 Activityに取得するには、koinInject()関数を使用して注入しましょう。

@Composable
fun FactoryInject(userName : String, presenter: UserStateHolder = koinInject()){
Text(text = presenter.sayHello(userName), modifier = Modifier.padding(8.dp))
}
備考

koinInject関数を使用すると、ViewModelインスタンスを取得し、関連するViewModel Factoryを作成して、ライフサイクルにバインドできます

Koinの開始

AndroidアプリケーションでKoinを開始する必要があります。 アプリケーションのメインエントリポイントであるMainApplicationクラスで、startKoin()関数を呼び出すだけです。

class MainApplication : Application(){
override fun onCreate() {
super.onCreate()

startKoin{
androidLogger()
androidContext(this@MainApplication)
modules(appModule)
}
}
}
備考

startKoinmodules()関数は、指定されたモジュールのリストをロードします

Composeアプリケーションを開始するときは、KoinAndroidContextを使用して、Koinを現在のComposeアプリケーションにリンクする必要があります。

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
KoinAndroidContext {
App()
}
}
}
}
}

Koinモジュール:クラシックまたはコンストラクタDSL?

アプリのKoinモジュールの宣言を次に示します。

val appModule = module {
single<HelloRepository> { HelloRepositoryImpl() }
viewModel { MyViewModel(get()) }
}

コンストラクタを使用すると、よりコンパクトな方法で記述できます。

val appModule = module {
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
viewModelOf(::UserViewModel)
}

アプリの検証!

Koin構成が適切であることを確認するために、簡単なJUnitテストでKoin構成を検証できます。

Gradleの設定

以下のようにKoin Androidの依存関係を追加します。

dependencies {

// Koin for Tests
testImplementation "io.insert-koin:koin-test-junit4:$koin_version"
}

モジュールの確認

verify()関数を使用すると、指定されたKoinモジュールを検証できます。

class CheckModulesTest : KoinTest {

@Test
fun checkAllModules() {
appModule.verify()
}
}

JUnitテストだけで、定義の構成に不足がないことを確認できます。