I am developing an application and I encountered a problem. My project works fine in the emulator. On phones, the score saved in the database always appears as 0.00, so after watching an advertisement, it becomes 0.01 and it gets stuck like that. I use firebase as the database. (It works without any problems in the virtual machine)
<code>watchBut.setOnClickListener {
FirebaseAuth.getInstance().currentUser?.let { user ->
val userId = user.uid
Log.d("Firestore", "Current user ID: $userId")
rewardedAd?.show(this) {
val currentWatchCount = watchCount.text.toString().toDoubleOrNull() ?: 0.00
Log.d("WatchCount", "Current watch count: $currentWatchCount")
val newWatchCount = roundDouble(currentWatchCount + 0.01, 2)
Log.d("WatchCount", "New watch count: $newWatchCount")
watchCount.text = String.format("%.2f", newWatchCount)
updateWatchCount(userId, newWatchCount)
} ?: run {
Log.e("RewardedAd", "No ad available to show.")
}
} ?: run {
Log.e("Firestore", "No authenticated user found.")
}
}
}
fun updateWatchCount(userId: String, watchCount: Double) {
val db = Firebase.firestore
val userCollection = db.collection("users")
userCollection.document(userId).set(mapOf("watchCount" to watchCount), SetOptions.merge())
.addOnSuccessListener {
Log.d("Firestore", "Watch count successfully updated for user: $userId.")
}
.addOnFailureListener { e ->
Log.e("Firestore", "Failed to update watch count for user: $userId: ${e.message}",e)
}
}
</code>
<code>watchBut.setOnClickListener {
FirebaseAuth.getInstance().currentUser?.let { user ->
val userId = user.uid
Log.d("Firestore", "Current user ID: $userId")
rewardedAd?.show(this) {
val currentWatchCount = watchCount.text.toString().toDoubleOrNull() ?: 0.00
Log.d("WatchCount", "Current watch count: $currentWatchCount")
val newWatchCount = roundDouble(currentWatchCount + 0.01, 2)
Log.d("WatchCount", "New watch count: $newWatchCount")
watchCount.text = String.format("%.2f", newWatchCount)
updateWatchCount(userId, newWatchCount)
} ?: run {
Log.e("RewardedAd", "No ad available to show.")
}
} ?: run {
Log.e("Firestore", "No authenticated user found.")
}
}
}
fun updateWatchCount(userId: String, watchCount: Double) {
val db = Firebase.firestore
val userCollection = db.collection("users")
userCollection.document(userId).set(mapOf("watchCount" to watchCount), SetOptions.merge())
.addOnSuccessListener {
Log.d("Firestore", "Watch count successfully updated for user: $userId.")
}
.addOnFailureListener { e ->
Log.e("Firestore", "Failed to update watch count for user: $userId: ${e.message}",e)
}
}
</code>
watchBut.setOnClickListener {
FirebaseAuth.getInstance().currentUser?.let { user ->
val userId = user.uid
Log.d("Firestore", "Current user ID: $userId")
rewardedAd?.show(this) {
val currentWatchCount = watchCount.text.toString().toDoubleOrNull() ?: 0.00
Log.d("WatchCount", "Current watch count: $currentWatchCount")
val newWatchCount = roundDouble(currentWatchCount + 0.01, 2)
Log.d("WatchCount", "New watch count: $newWatchCount")
watchCount.text = String.format("%.2f", newWatchCount)
updateWatchCount(userId, newWatchCount)
} ?: run {
Log.e("RewardedAd", "No ad available to show.")
}
} ?: run {
Log.e("Firestore", "No authenticated user found.")
}
}
}
fun updateWatchCount(userId: String, watchCount: Double) {
val db = Firebase.firestore
val userCollection = db.collection("users")
userCollection.document(userId).set(mapOf("watchCount" to watchCount), SetOptions.merge())
.addOnSuccessListener {
Log.d("Firestore", "Watch count successfully updated for user: $userId.")
}
.addOnFailureListener { e ->
Log.e("Firestore", "Failed to update watch count for user: $userId: ${e.message}",e)
}
}
I changed the rules of the database. I checked the ad tracking code I tried int instead of double then it works. But I need double
8