MVVM で Hilt のパターン化 💉

シンプルなものにして整理、定型化しておきたい。

「どこに」←「どれを」インジェクトしてるか、とそれの記述をパターン化。

 

💉 View ← ViewModel

MVVM でいうところの View であるこれらに、ViewModel をインジェクトする場合。

コンストラクタからインジェクトできないいわゆる「Androidクラス」。
以下のものがこれに当てはまる。

- Activity
- Fragment
- View
- Service
- BroadcastReceiver

それぞれのクラスに付ける2つのアノテーションと ktx による記述でプロパティにインジェクトする。


@AndroidEntryPoint
class MainFragment : Fragment() {
  private val viewModel: MainViewModel by viewModels()


@HiltViewModel
class MainViewModel @Inject constructor(
  repository: MainRepository
) : ViewModel() {

 


💉 ViewModel ← Repository

Module を使った ViewModel コンストラクタへのインジェクト。

インターフェースの型でリンクする。


@HiltViewModel
class MainViewModel @Inject constructor(
  repository: MainRepository
) : ViewModel() {

Hilt 備え付けの Component でライフサイクルを考慮して、実装型でインジェクトする。

abstract、@Binds は、インターフェースを介す場合の、 @InstallIn( ViewModelComponent::class)、 @ViewModelScoped は、 ViewModel に対してのインジェクトのパターン。


@Module
@InstallIn(ViewModelComponent::class)
abstract class RepositoryModule {

  @Binds
  @ViewModelScoped
  abstract fun bindRepository(impl: DefaultRepository): MainRepository
}

 

💉 Repository ← DataSource

Androidクラスのようなプロパティへのインジェクトとは違う @Module、@Provides を使ったコンストラクタインジェクトの Hilt基本的パターン。

SingletonComponent は、旧 ApplcationComponent で生存期間最長。


class DefaultRepository @Inject constructor(
  private val localDao: LocalDao,
  private val contentResolver: ContentResolver,
  private val sharedPreferences: SharedPreferences
) : MainRepository {


@Module
@InstallIn(SingletonComponent::class)
object DataSourceModule {

  @Provides
  @Singleton
  fun provideLocalDao(
    @ApplicationContext context: Context
  ): LocalDao {
    return LocalDao(context)
  }

 

💉 DataSource ← Context

Context は、 @ApplicationContext と @ActivityContext がすでに用意されているので区別しながらアノテーション1つのみで注入できる。これ便利。


@Module
@InstallIn(SingletonComponent::class)
object DataSourceModule {

  @Provides
  @Singleton
  fun provideLocalDao(
    @ApplicationContext context: Context
  ): LocalDao {
    return LocalDao(context)
  }

しかし、どの位置でも自在にインジェクトしようとするとコケる。


[Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.

@Module 直下の @Provides メソッドの引数で使うものですか。

 

build.gradle


buildscript {
  ext.versions = [
    "hilt"       : "2.41",
  ]
  dependencies {
    classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.hilt}"
  }
}

apply plugin: "org.jetbrains.kotlin.kapt"
apply plugin: "dagger.hilt.android.plugin"

dependencies {
  implementation "com.google.dagger:hilt-android:${versions.hilt}"
  kapt "com.google.dagger:hilt-android-compiler:${versions.hilt}"

  androidTestImplementation  "com.google.dagger:hilt-android-testing:${versions.hilt}"
  kaptAndroidTest "com.google.dagger:hilt-compiler:${versions.hilt}"
  testImplementation "com.google.dagger:hilt-android-testing:${versions.hilt}"
  kaptTest "com.google.dagger:hilt-compiler:${versions.hilt}"
}

👉 Gradle Build Setup 

 

💉 まとめ

多少の流儀はあるけど、 Component 記述がなくなっただけでも記述量は大幅に削減できます。

alpha らしく、まだコンポーネント名など変わるんかもしれんが、ここも適宜修正させてもらいます。

👉 【MVVM】 Kotlin Flow で使える5つの利用パターン 
👉 skydoves/Pokedex: 🗡️ Android Pokedex using Hilt, Motion, Coroutines, Flow, Jetpack (Room, ViewModel) based on MVVM architecture. 
👉 Hilt and Dagger annotations cheat sheet - Android Developers - Medium 



画面上部のLED点滅は一体なんだったのか【Android12 / Pixel5】

ロック画面時に謎だったのですが、



このアプリを入れてみて分かりました。

👉 センサーツールボックス - Google Play のアプリ 



「近接センサー稼働中」

の明示ですね。

で、

ロック画面では、どの機能がLEDを点滅させているか?

ということですが、

How to disable the annoying flashing dot on your Pixel 5

[設定] - [システム] - [ジェスチャー] からの

「ふせるだけでサイレントモードをオン」

のようです。

👉 How to disable the annoying flashing dot on your Pixel 5 - 9to5Google 


Android12でアイコンを四角にする方法

Android11 では、フォントやアイコン形状などはシステムUIのテーマとしてカスタマイズできていました。

Android11 custom theme icons

👉 Android 12 is missing font and icon shape customization 

しかし、Android12では、

android12 dynamic theme

フォント変更
→ できない。

アイコンの形状変更
→ できない。

👉 Android 12 Beta 3 Changelog: All the new features and changes! 

ということで、

Android12ではアイコンは丸型だけ。四角には変更できない。

ということになります。

このことについては、上記XDAデベロッパーサイトのコメントやGoogle Issue Tracker でも悲しみのコメントが多く寄せられています。

👉 Custom Styles missing from Wallpaper and Style menu [193775634] - Visible to Public - Issue Tracker 

しかし、Google 社員がコメントしているように、

“The custom style features (font, icon shape, icon pack, and accent color) in R are being replaced by the new dynamic theming feature that we are introducing in S. We see the new dynamic theming feature as more modern and intelligent. A simple and delightful experience that we hope all users can get to enjoy.”

「カスタムスタイル機能は、Android12ではダイナミックテーマ機能に置き換えられています。よりモダンでインテリジェントなものと見なされています。すべてのユーザーにシンプルで楽しい体験をお届けします。」

と、新しいダイナミックテーマ機能は、

- モダン
- インテリジェント
- シンプル
- 楽しい

ということなので心配ないようです。

使いづらくなっただけかと思ってびっくりしていましたが、

- モダン
- インテリジェント
- シンプル
- 楽しい

ということです。

よろしくおねがいいたします。

👉 アイコンの形は四角のほうが使いやすい