Firebase Cloud Firestore
3 min readFeb 7, 2023
Set Cloud Firestore
dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:31.2.0')
// Declare the dependency for the Cloud Firestore library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-firestore-ktx'
}
- Select a starting mode for your Cloud Firestore Security Rules: To get started with the Android SDK, select test mode. Good for getting started with mobile libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Secure your data section.
- When you enable Cloud Firestore, it also enables the API in the Cloud API Manager.
Firestore user Entity Insert
- java > models > User.kt class
data class User(
val uid: String,
val displayName: String? = "",
val imageUrl: String? = ""
)
- java > daos> UserDao.kt class
Firebase.firestore
and FirebaseFirestore.getInstance()
are the same. They both return the same singleton instance of the FirebaseFirestore
class, which provides the entry point for all server-side Firestore operations.
class UserDao {
// Access a Cloud Firestore instance from your Activity
private val db = FirebaseFirestore.getInstance()
/* Or
val db = Firebase.firestore*/
private val userCollection = db.collection("users")
fun allUser(user: User?){
user?.let {
//
GlobalScope.launch(Dispatchers.IO) {
userCollection.document(user.uid).set(it)
}
}
}
}
- Add User Firebase (Activity)
private fun updateUI(firebaseUser: FirebaseUser?) {
if(firebaseUser!=null) {
/*Add User in Firebase*/
val user = User(firebaseUser.uid, firebaseUser.displayName, firebaseUser.photoUrl.toString())
val userDao = UserDao()
userDao.allUser(user)
/*Go to another Activity*/
activity?.let { Navigation.findNavController(it, R.id.fragmentContainerView) }
?.navigate(R.id.homeFragment)
}
}
- Result
Firestore user post Entity Insert
- java > models > Post. kt class
data class Post(
val text: String = "",
val createdBy: User = User(),
val createdAt: Long = 0L,
val likeBy: ArrayList<String> = ArrayList()
)
- java > daos> UserDao.kt class (Some modify)
class UserDao {
private val db = FirebaseFirestore.getInstance()
private val userCollection = db.collection("users")
fun allUser(user: User?){
user?.let {
GlobalScope.launch(Dispatchers.IO) {
userCollection.document(user.uid).set(it)
}
}
}
/* add this getUserById()*/
fun getUserById(uID: String): Task<DocumentSnapshot> {
return userCollection.document(uID).get()
}
}
- java > daos> PostDao.kt class
class PostDao{
private val auth = Firebase.auth
private val db = FirebaseFirestore.getInstance()
private val posCollections = db.collection("posts")
fun addPost(text: String){
val currentUser = auth.currentUser!!.uid
GlobalScope.launch {
val userDao = UserDao()
val user = userDao.getUserById(currentUser).await().toObject(User::class.java)!!
val currentTime = System.currentTimeMillis()
val post = Post(text, user, currentTime)
posCollections.document().set(post)
}
}
}
- XML Design
<EditText
android:id="@+id/postEt"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="12dp"
android:background="@drawable/edit_text_round_shape"
android:gravity="top"
android:hint="What do you want to talk about?"
android:padding="12dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/postBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="Post"
android:background="@drawable/edit_text_round_shape"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/postEt" />
- Activity
private lateinit var postDao: PostDao
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
postDao = PostDao()
binding.postBtn.setOnClickListener {
val postText = binding.postEt.text.toString().trim()
if(postText.isNotEmpty()){
postDao.addPost(postText)
}
}
}
- Result