Reputation: 115
How can I compare the value from the room database with the text field? I want to compare the user input value with the credential view model but I have no idea how to do it. I have already set the query in the credentialDao so the value fetched should be inside the view model. But I just could not compare it. CredentialDao
@Dao
interface CredentialDao {
//if user name same ignore
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(credential: Credential)
@Update
suspend fun updateUser(credential: Credential)
@Query("SELECT * FROM credential_table WHERE userName LIKE :userName AND password LIKE :password")
fun readAllData(userName: String, password: String): Credential
}
CredentialDatabase
@Database(entities = [Credential::class], version = 1, exportSchema = false)
@TypeConverters(dateConverter::class)
abstract class CredentialDatabase: RoomDatabase() {
abstract fun credentialDao(): CredentialDao
companion object{
@Volatile
private var INSTANCE: CredentialDatabase? = null
val migration_1_2: Migration = object: Migration(2,1){
override fun migrate(database: SupportSQLiteDatabase){
}
}
fun getDatabase(context: Context): CredentialDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
CredentialDatabase::class.java,
"credential_database"
).fallbackToDestructiveMigration()
.build()
INSTANCE = instance
return instance
}
}
}
}
Credential Repository
class CredentialRepository(private val credentialDao: CredentialDao) {
suspend fun addCredential(credential: Credential){
credentialDao.addUser(credential)
}
suspend fun updateCredential(credential: Credential){
credentialDao.updateUser(credential)
}
fun readAllData(username: String,password:String) {
credentialDao.readAllData(username,password)
}
}
CredentialViewModel
class CredentialViewModel(application: Application): AndroidViewModel(application) {
private val repository: CredentialRepository
init{
val credentialDao = CredentialDatabase.getDatabase(application).credentialDao()
repository = CredentialRepository(credentialDao)
}
fun addCredential(credential: Credential){
viewModelScope.launch(Dispatchers.IO){
repository.addCredential(credential)
}
}
fun updateCredential(credential: Credential){
viewModelScope.launch(Dispatchers.IO){
repository.updateCredential(credential)
}
}
fun readAllData(userName: String, password: String){
viewModelScope.launch(Dispatchers.IO){
repository.readAllData(userName, password)
}
}
}
LoginActivity
class LoginActivity : AppCompatActivity() {
private lateinit var mUserViewModel: CredentialViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
// Inflate the layout for this fragment
mUserViewModel = ViewModelProvider(this).get(CredentialViewModel::class.java)
val login = findViewById<Button>(R.id.login_btn)
login?.setOnClickListener {
val userName = username_txt.text.toString().trim();
val password = password_txt.text.toString().trim();
if(userName.isEmpty()||password.isEmpty()){
Toast.makeText(
applicationContext,
"Please fill out all field!",
Toast.LENGTH_SHORT
).show()
}else{
mUserViewModel.readAllData(userName, password)
if(user!=null){
Toast.makeText(applicationContext, "Successfully Login!", Toast.LENGTH_SHORT).show()
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.putExtra("Username",userName)
startActivity(intent)
}else{
Toast.makeText(applicationContext, "Wrong Password!", Toast.LENGTH_SHORT).show()
}
}
}
}
}
Upvotes: 0
Views: 168
Reputation: 941
I think the readAllData()
method in your CredentialRepository
is incorrect, because this function needs to return the query result to CredentialViewModel
. Your correct approach is to pass the query result to ViewModel
.
When trying to log in, you should also call the method defined in ViewModel
and pass username
and password
to this method for retrieval. The final result defines a LiveData<Boolean>
to represent whether you log in successfully.
class CredentialViewModel(application: Application): AndroidViewModel(application) {
private val mResult = MutableLiveData<Credential>()
val resultLiveData: LiveData<Credential> get() = mResult
private val repository: CredentialRepository
init{
val credentialDao = CredentialDatabase.getDatabase(application).credentialDao()
repository = CredentialRepository(credentialDao)
}
fun addCredential(credential: Credential){
viewModelScope.launch(Dispatchers.IO){
repository.addCredential(credential)
}
}
fun updateCredential(credential: Credential){
viewModelScope.launch(Dispatchers.IO){
repository.updateCredential(credential)
}
}
fun readAllData(userName: String, password: String){
viewModelScope.launch(Dispatchers.IO){
mResult.postValue(repository.readAllData(userName, password))
}
}
}
class LoginActivity : AppCompatActivity() {
private lateinit var mUserViewModel: CredentialViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
// Inflate the layout for this fragment
mUserViewModel = ViewModelProvider(this).get(CredentialViewModel::class.java)
val login = findViewById<Button>(R.id.login_btn)
mUserViewModel.resultLiveData.observe(this@LoginActivity, { credential ->
if(credential!=null){
Toast.makeText(applicationContext, "Successfully Login!", Toast.LENGTH_SHORT).show()
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.putExtra("Username",userName)
startActivity(intent)
}else{
Toast.makeText(applicationContext, "Wrong Password!", Toast.LENGTH_SHORT).show()
}
})
login?.setOnClickListener {
val userName = username_txt.text.toString().trim();
val password = password_txt.text.toString().trim();
if (userName.isEmpty() || password.isEmpty()) {
Toast.makeText(
applicationContext,
"Please fill out all field!",
Toast.LENGTH_SHORT
).show()
} else {
mUserViewModel.readAllData(userName, password)
}
}
}
}
modify Repository
class CredentialRepository(private val credentialDao: CredentialDao) {
suspend fun addCredential(credential: Credential){
credentialDao.addUser(credential)
}
suspend fun updateCredential(credential: Credential){
credentialDao.updateUser(credential)
}
fun readAllData(username: String,password:String) :Credential{
return credentialDao.readAllData(username,password)
}
}
Upvotes: 1