発車予定表示機能のホーム画面を:feature:depart:homeモジュールに作成します。
共通UIモジュールの作成と同様に、トップディレクトリで:feature:depart:homeを作成し、javaディレクトリ名の変更、および、サンプルテストクラスの削除を行います。
発車予定表示機能は画面の表示データがありますのでViewModelを使用します。
Hiltと連携してViewModelを使用するためhilt-navigation-composeを導入します。
※navigation-composeは必要ありません。
バージョンカタログファイルにhilt-navigation-composeの定義を追記します。
[versions]
:
hilt-navigation-compose = "1.3.0" # https://mvnrepository.com/artifact/androidx.hilt/hilt-navigation-compose
:
lifecycleRuntimeKtx = "2.9.4"
lifecycle = "2.9.4"
:
[libraries]
:
hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hilt-navigation-compose" }
:
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle" }
lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycle" }
lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" }
:
追記後、『Sync Now』で内容をプロジェクトに反映させます。
エイリアス名を変更したため、:appモジュールのbuild.gradle.ktsを変更します。
: dependencies { implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.lifecycle.runtime.ktx) :
:core:uiモジュールで作成したLibraryConfigurePlugin.ktを使用してもいいのですが、画面が複数ありますので、画面用のビルドプラグインを作成することにします。
:build-logicモジュール内のpluginsパッケージに画面ライブラリモジュールのbuild.gradle.ktsで使用するビルドプラグインFeatureConfigurePlugin.ktを作成します。
package jp.co.progress_llc.portal.buildlogic.plugins import com.android.build.api.dsl.LibraryExtension import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies import jp.co.progress_llc.portal.buildlogic.core.libs import jp.co.progress_llc.portal.buildlogic.core.configureCommonExtension class FeatureConfigurePlugin: Plugin<Project> { override fun apply(project: Project) = with(project) { with(pluginManager) { apply("com.android.library") apply("org.jetbrains.kotlin.android") apply("com.google.devtools.ksp") apply("com.google.dagger.hilt.android") apply("org.jetbrains.kotlin.plugin.compose") } extensions.configure<LibraryExtension> { configureCommonExtension(this) defaultConfig.consumerProguardFiles("consumer-rules.pro") buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } buildFeatures.compose = true } dependencies { add("implementation", project.libs().findLibrary("androidx-core-ktx").get()) add("implementation", project.libs().findLibrary("androidx-appcompat").get()) add("implementation", project.libs().findLibrary("material").get()) add("implementation", project.libs().findLibrary("hilt-android").get()) add("implementation", project.libs().findLibrary("hilt-navigation-compose").get()) add("implementation", project.libs().findLibrary("lifecycle-runtime-ktx").get()) add("implementation", project.libs().findLibrary("lifecycle-viewmodel-compose").get()) add("implementation", platform(project.libs().findLibrary("compose-bom").get())) add("implementation", project.libs().findLibrary("compose-ui").get()) add("implementation", project.libs().findLibrary("compose-material").get()) add("implementation", project.libs().findLibrary("androidx-ui-tooling-preview").get()) add("ksp", project.libs().findLibrary("hilt-compiler").get()) add("debugImplementation", project.libs().findLibrary("androidx-ui-tooling").get()) } } }
作成したビルドプラグインを各モジュールのbuild.gradle.ktsで使用できるようにbuild-logic直下のbuild.gradle.ktsに追記します。
: gradlePlugin { : plugins { register("FeatureConfigurePlugin") { id = "build.logic.feature.configure" implementationClass = "jp.co.progress_llc.portal.buildlogic.plugins.FeatureConfigurePlugin" } } } dependencies { :
追記後、『Sync Now』で内容をプロジェクトに反映させます。
:feature:depart:homeモジュールのbuild.gradle.ktsに作成したビルドプラグインを適用します。
plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) id("build.logic.feature.configure") } android { namespace = "jp.co.progress_llc.portal.feature.depart.home" compileSdk { version = release(36) } defaultConfig { minSdk = 28 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") } buildTypes { release { isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) implementation(project(":core:ui")) }
適用後、『Sync Now』で内容をプロジェクトに反映させます。
:feature:depart:homeモジュールにpresentationディレクトリ(パッケージ)を追加し、直下にDepartHomeScreen.ktを作成します。
※暫定的にViewModelのない画面として作成します。
package jp.co.progress_llc.portal.feature.depart.home.presentation import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import jp.co.progress_llc.portal.core.ui.theme.AppTheme import jp.co.progress_llc.portal.core.ui.components.AppTitleBox import jp.co.progress_llc.portal.core.ui.components.CustomExposedDropdownMenuBox import kotlin.String @Composable fun DepartHomeScreen() { Scaffold( topBar = { AppTitleBox() } ) { innerPadding -> Column( modifier = Modifier .padding(innerPadding) .fillMaxSize() .background(MaterialTheme.colorScheme.background) .padding(horizontal = 8.dp), verticalArrangement = Arrangement.spacedBy(2.dp) ) { Text( text = "発車予定", style = MaterialTheme.typography.titleLarge, ) var selected by remember { mutableStateOf<String?>(null) } CustomExposedDropdownMenuBox( label = "経路を選択", options = emptyList(), selectedOption = selected, onOptionSelected = { }, showAdditionalItem = true, additionalItemText = "編集/新規経路...", onAdditionalItemClick = { } ) } } } @Preview(showBackground = true) @Composable fun DepartHomeScreenPreview() { AppTheme { DepartHomeScreen() } }