SHARE
TWEET

Untitled

a guest Jun 24th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.domo
  2.  
  3. import android.util.Log
  4. import com.google.firebase.firestore.FirebaseFirestore
  5.  
  6. class UserServices
  7. {
  8.  
  9.     private val context=FirebaseFirestore.getInstance()
  10.     private val userDB = context.collection("users")
  11.  
  12.     fun addToDB(user: User) {
  13.         val userRef=HashMap<String,Any>()
  14.         userRef["uid"]       = user.uid
  15.         userRef["email"]     = user.email
  16.         userRef["username"]  = user.username
  17.         userRef["friendsID"] = user.friendsId
  18.         userRef["eventsID"]  = user.eventsId
  19.  
  20.         context.collection("users").document(user.uid).set(user)
  21.     }
  22.     fun updateUser(user:User)
  23.     {
  24.         userDB.document(user.uid).set(user)
  25.     }
  26.  
  27.     fun getUserById(uid : String): User? {
  28.         val userRef=userDB.document(uid).get()
  29.         ///TODO : Find another way to make this work
  30.  
  31.         while(!userRef.isComplete)
  32.         {
  33.             continue
  34.         }
  35.         if(userRef.isSuccessful)
  36.         {
  37.             return User(userRef.result)
  38.         }
  39.         else
  40.         {
  41.             return null
  42.         }
  43.  
  44.  
  45.     }
  46.     fun addFriend(uid1: String,uid2:String) {
  47.         //TODO : Make this work
  48.         if (uid1 != uid2) {
  49.             val user1 = getUserById(uid1)
  50.             val user2 = getUserById(uid2)
  51.             if(!user1?.friendsId?.contains(uid2)!!) {
  52.                 if (user2 != null) {
  53.                     user1.friendsId.add(uid2)
  54.                     user2.friendsId.add(uid1)
  55.                     Log.d("[User Services]", "Users $uid1 and $uid2 became friends")
  56.                     updateUser(user1)
  57.                     updateUser(user2)
  58.                 }
  59.             }
  60.         }
  61.         else
  62.         {
  63.             Log.d("[User Services]","Users $uid1 and $uid2 are already friends")
  64.  
  65.         }
  66.     }
  67.  
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top