Is my project well structured or can you help me with any suggestions?

  android, android-room, kotlin-coroutines, mvvm, viewmodel

I have created a database with ROOM where I verify if the username and password are registered in it, I would like to know if there is any redundancy or what suggestion you could give me.

//Dao

@Dao
interface UserDao {

    @Query("SELECT * FROM user_table WHERE userName = :name AND password =:password")
    fun readAllData(name: String, password: String): LiveData<User>

}

//UserRepository

class UserRepository(private val userDao: UserDao) {

    var loginDatabase: UserDatabase? = null
    var readAllData: LiveData<User>? = null

    fun initializeDB(context: Context): UserDatabase {
        return UserDatabase.getDatabase(context)
    }

    fun getLoginDetails(context: Context, username: String, password: String): LiveData<User>? {
        loginDatabase = initializeDB(context)
        readAllData = loginDatabase!!.userDao().readAllData(username, password)
        return readAllData
    }

}

//UserViewModel

class UserViewModel(application: Application) : AndroidViewModel(application) {

    var readAllData: LiveData<User>? = null
    val repository: UserRepository

    init {
        val userDao = UserDatabase.getDatabase(
                application
        ).userDao()
        repository = UserRepository(userDao)
        readAllData = repository.readAllData
    }

    fun getLoginDetails(context: Context, username: String, password: String): LiveData<User>? {
        readAllData = repository.getLoginDetails(context, username, password)
        return readAllData
    }

}

//MainFragment

mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)

    binding.buttonIngresar.setOnClickListener {

        val name = binding.textInputUser.editText?.text.toString()
        val passWord = binding.textInputPassword.editText?.text.toString()
        context?.let { it1 ->
            mUserViewModel.getLoginDetails(it1, name, passWord)?.observe(viewLifecycleOwner, Observer {

                if (it == null) {
                    Toast.makeText(context, "Faund", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(context, "Not Found", Toast.LENGTH_SHORT).show()
                }

            })

Source: Android Questions

LEAVE A COMMENT