Ich habe Probleme, Daten von Firebase mit Jetpack Compose zu ziehen?Android

Forum für diejenigen, die für Android programmieren
Anonymous
 Ich habe Probleme, Daten von Firebase mit Jetpack Compose zu ziehen?

Post by Anonymous »

Code: Select all

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
}
< /code>
Verwenden des obigen Codevar email by mutableStateOf("e")
< /code>
, die ich in Design_login () definiert habe und keine Daten von Firebase erhält. Wie kann ich dies beheben?{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post