private lateinit var viewmodelAuthentication: ViewModel_Authentication
class Activity_Login : ComponentActivity() {
@SuppressLint("CoroutineCreationDuringComposition")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Share_FreelyTheme {
viewmodelAuthentication = viewModel()
Design_Login()
}
}
}
}
@Composable
fun Design_Login() {
val context = LocalContext.current
val focusManager = LocalFocusManager.current
val interactionSource = remember { MutableInteractionSource() }
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var email by remember { mutableStateOf("mail") }
Box(
modifier = Modifier
.fillMaxSize()
.background(
Brush.linearGradient(
colors = listOf(
colorResource(id = R.color.gradient_color_start),
colorResource(id = R.color.gradient_color_end)
)
)
),
contentAlignment = Alignment.Center
) {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(id = R.drawable.chat),
contentDescription = "",
modifier = Modifier.size((LocalContext.current.resources.displayMetrics.widthPixels / 100 * 12).dp)
)
Spacer(Modifier.size(20.dp))
Text(
text = "Login",
color = colorResource(id = R.color.white),
modifier = Modifier.wrapContentSize(), fontSize = 25.sp
)
Spacer(Modifier.size(10.dp))
Box(
modifier = Modifier
.padding(horizontal = 15.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(15.dp))
.background(colorResource(id = R.color.white).copy(0.1f))
.border(
width = 1.dp,
color = colorResource(id = R.color.white).copy(0.2f),
shape = RoundedCornerShape(15.dp)
)
) {
LazyColumn(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
item {
Spacer(Modifier.size(10.dp))
Textfield_Text(
value = username,
onValueChange = { username = it
if (it != "")
email = search_user(it)
},
"Username",
R.drawable.person_icon, null, true
)
}
item {
Spacer(Modifier.size(10.dp))
Textfield_Password(
value = password,
onValueChange = { password = it }, "Password", false
)
Text(
text = "Forgot My Password?",
color = colorResource(id = R.color.white),
textAlign = TextAlign.Start,
modifier = Modifier.clickable(
interactionSource = interactionSource,
indication = null
) {
val intent =
Intent(context, Activity_Forget_Password::class.java)
context.startActivity(intent)
(context as? ComponentActivity)?.finish()
}
.width((context.resources.displayMetrics.widthPixels / 100 * 30).dp)
)
}
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
OutlinedButton(
onClick = {
if (username.isBlank() && password.isBlank()) {
Toast.makeText(
context,
"Please enter your username and password!",
Toast.LENGTH_LONG
).show()
} else if (username.isBlank()) {
Toast.makeText(
context,
"Please enter your username!",
Toast.LENGTH_LONG
).show()
} else if (password.isBlank()) {
Toast.makeText(
context,
"Please enter your password!",
Toast.LENGTH_LONG
).show()
} else if (password.length < 8) {
Toast.makeText(
context,
"Password Length Cannot Be Less Than 8 Characters!",
Toast.LENGTH_LONG
).show()
} else {
Toast.makeText(
context,
email,
Toast.LENGTH_LONG
).show()
}
focusManager.clearFocus(true)
},
border = BorderStroke(
1.dp,
colorResource(id = R.color.white).copy(.7f)
),
shape = RoundedCornerShape(10.dp),
colors = ButtonDefaults.buttonColors(
containerColor = colorResource(id = R.color.transparent)
)
) {
Text(
text = "Login",
color = colorResource(id = R.color.white)
)
}
}
Spacer(Modifier.size(10.dp))
}
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
Text(
text = "Don't you have an account?",
color = colorResource(id = R.color.white_transparent)
)
Spacer(Modifier.size(15.dp))
Text(
text = "Sign Up",
color = colorResource(id = R.color.white),
fontWeight = FontWeight.Bold,
modifier = Modifier.clickable(
interactionSource = interactionSource,
indication = null
) {
val intent = Intent(context, Activity_SignUp::class.java)
context.startActivity(intent)
(context as? ComponentActivity)?.finish()
}
)
}
Spacer(Modifier.size(10.dp))
}
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun LoginPreview() {
Share_FreelyTheme {
Design_Login()
}
}
private fun search_user(query: String) : String{
val database = FirebaseDatabase.getInstance()
val ref = database.getReference("Users")
var email by mutableStateOf("e")
val question = ref.orderByChild("username").equalTo(query)
question.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
val user = snapshot.children.firstOrNull()?.getValue(Model_User::class.java)
email = user?.email ?: "email no"
} else {
email = "email not found"
}
}
override fun onCancelled(error: DatabaseError) {
Log.e("Firebase", "Searching error: ${error.message}")
}
})
return email
}
Using the code above, I wanted to get data from Firebase using search_user in Textfield in Jetpack Compose, but when I click on the button and want to show the email address in the Toast message, the "mail" message in
var email by mutableStateOf("e")
that I defined in Design_Login() comes and does not get data from Firebase, how can I fix this?
Since I am working with Authentication, I used these in Rules:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
onDataChange
and run the program in the debugger. Does it reach that line? If so, what value do you get when you callsnapshot.getValue()
?