1

I'm using dagger-hilt

I want to set a password to Room. I know that I can do it like this:

@Provides
@Singleton
fun provideDatabase(application: Application): MyDatabase {
    val passphrase: ByteArray = SQLiteDatabase.getBytes("my_password".toCharArray())
    val factory = SupportFactory(passphrase)

    return Room.databaseBuilder(application, MyDatabase::class.java, "my_database")
        .openHelperFactory(factory)
        .allowMainThreadQueries()
        .build()
}

But if the password is wrong I want to show a compose form with input to try another password.

How can I do it?

EoinKanro
  • 105
  • 1
  • 7

1 Answers1

1

So, my solution is:

  • migrate to dagger2
  • check password on startup
  • if something is wrong - show login content and restart app if the new password is correct
  • else - init dagger and set normal content

What helped me:

My onCreate:

 class MainActivity : ComponentActivity() {   
 .... 
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val loggedIn = isDatabasePasswordCorrect(this@MainActivity, getSavedPassword())
    if (!loggedIn) {
        val content: @Composable () -> Unit = { Login() }
        setAppContent(content)
    } else {
        initDagger()

        val content: @Composable () -> Unit = { App() }
        setAppContent(content)
    }
}
....
}
EoinKanro
  • 105
  • 1
  • 7