Advertisement
Yonka2019

Untitled

May 6th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. using Android.Content;
  2. using Android.Gms.Extensions;
  3. using Android.Runtime;
  4. using Firebase;
  5. using Firebase.Firestore;
  6. using Java.Util;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10.  
  11. namespace aa
  12. {
  13. internal class Firebase
  14. {
  15. private static FirebaseFirestore DB;
  16.  
  17. public static void Initialize(Context context)
  18. {
  19. FirebaseApp app = FirebaseApp.InitializeApp(context);
  20. if (app == null)
  21. {
  22. FirebaseOptions options = new FirebaseOptions.Builder()
  23. .SetProjectId("")
  24. .SetApplicationId("")
  25. .SetApiKey("")
  26. .SetStorageBucket("")
  27. .Build();
  28.  
  29. app = FirebaseApp.InitializeApp(context, options);
  30.  
  31. DB = FirebaseFirestore.GetInstance(app);
  32. DB.FirestoreSettings = new FirebaseFirestoreSettings.Builder().SetPersistenceEnabled(false).Build();
  33. }
  34. else
  35. {
  36. DB = FirebaseFirestore.GetInstance(app);
  37. }
  38. }
  39.  
  40. public static async Task CreateList(string username, string listName)
  41. {
  42. HashMap newList = new HashMap();
  43.  
  44. newList.Put(listName, new JavaList()); // new list
  45. await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newList, SetOptions.Merge());
  46. }
  47.  
  48. public static async Task RemoveList(string username, string listName)
  49. {
  50. HashMap newList = new HashMap();
  51.  
  52. newList.Put(listName, FieldValue.Delete()); // new list
  53.  
  54. await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newList, SetOptions.Merge()); // remove list in my user
  55.  
  56. HashMap otherUsersNewList = new HashMap();
  57.  
  58. otherUsersNewList.Put($"{listName} ({username})", FieldValue.Delete()); // new list
  59.  
  60. foreach (string user in await GetUsers())
  61. {
  62. await DB.Collection("Users").Document(user).Collection("Information").Document("List").Set(otherUsersNewList, SetOptions.Merge()); // remove list from every user (also not from not me, in the next pattern: 'ListName (owner)' this is because when some user joins the list, he sees this list in THIS specific pattern
  63. }
  64. }
  65.  
  66. public static async Task AddToList(string username, string listName, string item)
  67. {
  68. JavaList data = await GetListData(username, listName);
  69. data.Add(item);
  70.  
  71. HashMap newData = new HashMap();
  72. newData.Put(listName, data);
  73.  
  74. await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newData, SetOptions.Merge());
  75. }
  76.  
  77. public static async Task RemoveFromList(string username, string listName, string item)
  78. {
  79. JavaList removedItemData = await GetListData(username, listName);
  80. removedItemData.Remove(item);
  81.  
  82. HashMap newData = new HashMap();
  83. newData.Put(listName, removedItemData);
  84.  
  85. await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newData); // without merge other items, we are OVERWRITTING them
  86. }
  87.  
  88. public static async Task<List<string>> GetUsers()
  89. {
  90. List<string> usersList = new List<string>();
  91.  
  92. QuerySnapshot querySnapshot = (QuerySnapshot)await DB.Collection("Users").Get();
  93. foreach (DocumentSnapshot document in querySnapshot.Documents)
  94. {
  95. usersList.Add(document.Id);
  96. }
  97.  
  98. return usersList;
  99. }
  100.  
  101. public static async Task<JavaList> GetListData(string username, string listName)
  102. {
  103. DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("List").Get();
  104.  
  105. if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
  106. {
  107. if (snap.Data.ContainsKey(listName))
  108. {
  109. return (JavaList)snap.Data[listName];
  110. }
  111. else
  112. return null;
  113. }
  114. return null;
  115. }
  116.  
  117. public static async Task<List<string>> GetLists(string username)
  118. {
  119. DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("List").Get();
  120.  
  121. if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
  122. return snap.Data.Keys.ToList();
  123. else
  124. return null;
  125. }
  126.  
  127. public static async Task<string> GetPassword(string username)
  128. {
  129. DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("Login").Get();
  130.  
  131. if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
  132. return (string)snap.Data["Password"];
  133. else
  134. return null;
  135. }
  136.  
  137. public static void CreateUser(string username, string password)
  138. {
  139. HashMap data = new HashMap();
  140. data.Put("Created", true);
  141.  
  142. HashMap login = new HashMap();
  143. login.Put("Password", password);
  144.  
  145. DB.Collection("Users").Document(username).Set(data); // in order to create and 'confirm' the user document, we need to add some field to this document (document can't be without any fields)
  146.  
  147. DB.Collection("Users").Document(username).Collection("Information").Document("Login").Set(login);
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement