Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.Content;
- using Android.Gms.Extensions;
- using Android.Runtime;
- using Firebase;
- using Firebase.Firestore;
- using Java.Util;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace aa
- {
- internal class Firebase
- {
- private static FirebaseFirestore DB;
- public static void Initialize(Context context)
- {
- FirebaseApp app = FirebaseApp.InitializeApp(context);
- if (app == null)
- {
- FirebaseOptions options = new FirebaseOptions.Builder()
- .SetProjectId("")
- .SetApplicationId("")
- .SetApiKey("")
- .SetStorageBucket("")
- .Build();
- app = FirebaseApp.InitializeApp(context, options);
- DB = FirebaseFirestore.GetInstance(app);
- DB.FirestoreSettings = new FirebaseFirestoreSettings.Builder().SetPersistenceEnabled(false).Build();
- }
- else
- {
- DB = FirebaseFirestore.GetInstance(app);
- }
- }
- public static async Task CreateList(string username, string listName)
- {
- HashMap newList = new HashMap();
- newList.Put(listName, new JavaList()); // new list
- await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newList, SetOptions.Merge());
- }
- public static async Task RemoveList(string username, string listName)
- {
- HashMap newList = new HashMap();
- newList.Put(listName, FieldValue.Delete()); // new list
- await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newList, SetOptions.Merge()); // remove list in my user
- HashMap otherUsersNewList = new HashMap();
- otherUsersNewList.Put($"{listName} ({username})", FieldValue.Delete()); // new list
- foreach (string user in await GetUsers())
- {
- 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
- }
- }
- public static async Task AddToList(string username, string listName, string item)
- {
- JavaList data = await GetListData(username, listName);
- data.Add(item);
- HashMap newData = new HashMap();
- newData.Put(listName, data);
- await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newData, SetOptions.Merge());
- }
- public static async Task RemoveFromList(string username, string listName, string item)
- {
- JavaList removedItemData = await GetListData(username, listName);
- removedItemData.Remove(item);
- HashMap newData = new HashMap();
- newData.Put(listName, removedItemData);
- await DB.Collection("Users").Document(username).Collection("Information").Document("List").Set(newData); // without merge other items, we are OVERWRITTING them
- }
- public static async Task<List<string>> GetUsers()
- {
- List<string> usersList = new List<string>();
- QuerySnapshot querySnapshot = (QuerySnapshot)await DB.Collection("Users").Get();
- foreach (DocumentSnapshot document in querySnapshot.Documents)
- {
- usersList.Add(document.Id);
- }
- return usersList;
- }
- public static async Task<JavaList> GetListData(string username, string listName)
- {
- DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("List").Get();
- if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
- {
- if (snap.Data.ContainsKey(listName))
- {
- return (JavaList)snap.Data[listName];
- }
- else
- return null;
- }
- return null;
- }
- public static async Task<List<string>> GetLists(string username)
- {
- DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("List").Get();
- if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
- return snap.Data.Keys.ToList();
- else
- return null;
- }
- public static async Task<string> GetPassword(string username)
- {
- DocumentSnapshot snap = (DocumentSnapshot)await DB.Collection("Users").Document(username).Collection("Information").Document("Login").Get();
- if (snap != null && snap.Exists()) // check if username exists (or it will return that document doesn't exists)
- return (string)snap.Data["Password"];
- else
- return null;
- }
- public static void CreateUser(string username, string password)
- {
- HashMap data = new HashMap();
- data.Put("Created", true);
- HashMap login = new HashMap();
- login.Put("Password", password);
- 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)
- DB.Collection("Users").Document(username).Collection("Information").Document("Login").Set(login);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement