Exemple #1
0
        public Task <User> SearchUser(string TempEmail)
        {
            TaskCompletionSource <User> ResultCompletionSource = new TaskCompletionSource <User>();
            DocumentReference           Path = Conn.Collection(usercollection).Document(TempEmail);

            Path.Get().AddOnCompleteListener(new OnCompleteEventHandleListener((Android.Gms.Tasks.Task
                                                                                obj) =>
            {
                if (obj.IsSuccessful)
                {
                    App.searchResult      = new User();
                    DocumentSnapshot temp = (DocumentSnapshot)obj.Result;
                    if (temp.Exists())
                    {
                        var TempDictionary   = temp.Data;
                        var searchUserResult = new User((string)temp.Id, (string)TempDictionary["username"]);
                        ResultCompletionSource.SetResult(searchUserResult);
                    }
                    else
                    {
                        ResultCompletionSource.SetResult(null);
                    }
                }
            }));

            return(ResultCompletionSource.Task);
        }
Exemple #2
0
 public static IDictionary <string, object> Map(DocumentSnapshot source)
 {
     if (source != null && source.Exists())
     {
         return(source.Data.ToDictionary(pair => pair.Key, pair => pair.Value.ToFieldValue(typeof(object))));
     }
     return(null);
 }
Exemple #3
0
        public static T Map <T>(DocumentSnapshot source, ServerTimestampBehavior?serverTimestampBehavior = null)
        {
            if (source != null && source.Exists())
            {
                IDictionary <string, Java.Lang.Object> data;
                if (serverTimestampBehavior == null)
                {
                    data = source.Data;
                }
                else
                {
                    data = source.GetData(serverTimestampBehavior.Value.ToNative());
                }

                var properties       = typeof(T).GetProperties();
                var idProperties     = properties.Where(p => Attribute.GetCustomAttribute(p, typeof(IdAttribute)) != null);
                var mappedProperties = properties.Select(p => (Property: p, Attribute: Attribute.GetCustomAttribute(p, typeof(MapToAttribute)) as MapToAttribute))
                                       .Where(t => t.Attribute != null)
                                       .ToDictionary(t => t.Attribute.Mapping, t => t.Property);
                var igonoredProperties = properties.Where(p => Attribute.GetCustomAttribute(p, typeof(IgnoredAttribute)) != null);

                var instance = Activator.CreateInstance <T>();

                foreach (var(key, value) in data)
                {
                    try
                    {
                        PropertyInfo property;
                        if (mappedProperties.ContainsKey(key))
                        {
                            property = mappedProperties[key];
                        }
                        else
                        {
                            property = typeof(T).GetProperty(key);
                        }

                        if (property != null && !igonoredProperties.Contains(property))
                        {
                            property.SetValue(instance, value.ToFieldValue(property.PropertyType));
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine($"{key} is invalid: {e.Message}");
                        throw;
                    }
                }

                foreach (var idProperty in idProperties)
                {
                    idProperty.SetValue(instance, source.Id);
                }

                return(instance);
            }
            return(default);
Exemple #4
0
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (snapshot.Exists())
            {
                string fullname = snapshot.Get("fullname").ToString();
                AppDataHelper.SaveFullName(fullname);
            }
        }
 public void OnComplete(Task task)
 {
     if (task.IsSuccessful)
     {
         DocumentSnapshot doc = task.Result as DocumentSnapshot;
         if (doc.Exists())
         {
             Console.WriteLine($"Doc all read OK: {doc.Data}");
         }
     }
 }
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (snapshot.Exists())
            {
                string fullname  = snapshot.Get("fullname").ToString();
                string email     = snapshot.Get("email").ToString();
                string image_url = snapshot.Get("image_id") != null?snapshot.Get("image_id").ToString() : "";

                Helper.SaveFullname(fullname);
                Helper.SaveImageUrl(image_url);
                Helper.SaveEmail(email);
            }
        }
        private void InstantiateUser(Java.Lang.Object result)
        {
            DocumentSnapshot snap = (DocumentSnapshot)result;

            if (snap.Exists())
            {
                thisUser = new User
                {
                    Image_Url = snap.Get("image_id") != null?snap.Get("image_id").ToString() : "",
                                    Fullname = snap.Get("fullname").ToString(),
                                    User_Id  = snap.Id.ToString(),
                                    Email    = snap.Get("email").ToString()
                };
                InvokeEvent();
            }
        }
Exemple #8
0
 public static IDictionary <string, object> Map(DocumentSnapshot source, ServerTimestampBehavior?serverTimestampBehavior = null)
 {
     if (source != null && source.Exists())
     {
         IDictionary <string, Java.Lang.Object> data;
         if (serverTimestampBehavior == null)
         {
             data = source.Data;
         }
         else
         {
             data = source.GetData(serverTimestampBehavior.Value.ToNative());
         }
         return(data.ToDictionary(pair => pair.Key, pair => pair.Value.ToFieldValue(typeof(object))));
     }
     return(null);
 }
Exemple #9
0
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (!snapshot.Exists())
            {
                return;
            }

            DocumentReference likeReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID);

            if (like)
            {
                likeReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true);
            }
            else
            {
                //check for null
                if (snapshot.Get("likes") == null)
                {
                    return;
                }

                var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null;

                if (data != null)
                {
                    var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister);

                    //retrieve our own id
                    string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid;

                    //check if our user ID is contained inside the dictionary
                    if (dictionaryFromHashMap.Contains(uid))
                    {
                        //remove our user ID to unlike the post
                        dictionaryFromHashMap.Remove(uid);

                        //update the hashmap withour our userid included
                        likeReference.Update("likes", dictionaryFromHashMap);
                    }
                }
            }
        }
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (!snapshot.Exists())
            {
                return;
            }

            DocumentReference likesReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID);

            if (Like)
            {
                likesReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true);
            }
            else
            {
                if (snapshot.Get("likes") == null)
                {
                    return;
                }

                var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null;

                if (data != null)
                {
                    var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister);

                    string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid;

                    if (dictionaryFromHashMap.Contains(uid))
                    {
                        dictionaryFromHashMap.Remove(uid);
                        likesReference.Update("likes", dictionaryFromHashMap);
                    }
                }
            }
        }
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snap = (DocumentSnapshot)result;

            Console.WriteLine(snap.Id);
            if (snap.Exists())
            {
                IDictionary <string, Java.Lang.Object> keyValuePairs = snap.Data;
                foreach (KeyValuePair <string, Java.Lang.Object> pair in keyValuePairs)
                {
                    //Console.WriteLine("{0}: {1}", pair.Key.ToString(), pair.Value.ToString());
                    FirebaseBackend.FirebaseBackend
                    .GetFireStore()
                    .Collection("users")
                    .Document(pair.Key.ToString())
                    .Get()
                    .AddOnSuccessListener(listener);
                }
            }
            else
            {
                Console.WriteLine("Not found");
            }
        }
Exemple #12
0
 public void OnComplete(Task task)
 {
     if (task == taskEqualCollection)
     {
         if (task.IsSuccessful)//if we did pull an image url, we put it in the imageview
         {
             DocumentSnapshot ds = (DocumentSnapshot)task.Result;
             //if the user set a profile picture manually already, we add its url to the user's information and input the picture to the imageview
             if (ds.Exists() && ds.Get(Constants.PROFILE_PIC_URL) != null)
             {
                 user.SetUserData(ds);
                 user.TieNum  = ds.Get(Constants.TIE_NUM) != null ? (int)ds.Get(Constants.TIE_NUM) : 0;
                 user.WinNum  = ds.Get(Constants.WIN_NUM) != null ? (int)ds.Get(Constants.WIN_NUM) : 0;
                 user.LossNum = ds.Get(Constants.LOSS_NUM) != null ? (int)ds.Get(Constants.LOSS_NUM) : 0;
                 InitStats();
                 user.ProfilePicture_url = (string)ds.Get(Constants.PROFILE_PIC_URL);
                 sp.SetData(Constants.PROFILE_PIC_URL, user.ProfilePicture_url);
                 sp.SetData(Constants.USERNAME, (string)ds.Get(Constants.USERNAME));
                 sp.SetData(Constants.EMAIL, (string)ds.Get(Constants.EMAIL));
                 sp.SetData(Constants.PASSWORD, (string)ds.Get(Constants.PASSWORD));
                 if (ds.Get(Constants.TIE_NUM) != null)
                 {
                     sp.SetData(Constants.TIE_NUM, (int)ds.Get(Constants.TIE_NUM));
                 }
                 if (ds.Get(Constants.WIN_NUM) != null)
                 {
                     sp.SetData(Constants.WIN_NUM, (int)ds.Get(Constants.WIN_NUM));
                 }
                 if (ds.Get(Constants.LOSS_NUM) != null)
                 {
                     sp.SetData(Constants.LOSS_NUM, (int)ds.Get(Constants.LOSS_NUM));
                 }
                 ImageService.Instance.LoadUrl(user.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(Constants.DOWNSAMPLE_SIZE, Constants.DOWNSAMPLE_SIZE).Into(ivMePic);
             }
             else//incase its a new user
             {
                 HashMap hm = user.SetUserData();
                 fd.AddDocumentToCollection(Constants.FS_USERS_COL, user.UserName, hm); //Init user's information to firebase, document name is user's username
             }
         }
     }
     else if (task == taskFindGame && task.IsSuccessful)
     {
         QuerySnapshot qs           = (QuerySnapshot)task.Result;
         string        gameNum      = "";
         bool          doStart      = false;
         string        opponentName = string.Empty;
         foreach (DocumentSnapshot ds in qs.Documents)//we check to see if someone is looking to start a game (if not we start a game)
         {
             bool isLive = (bool)ds.Get(Constants.ISLIVE_GAME);
             if (!isLive)//we found a game that's not started yet (1 or less players connected)
             {
                 gameNum = (string)ds.Get(Constants.GAMENUM);
                 HashMap hm = new HashMap();
                 if (ds.Get(Constants.HOST_GAME) != null)//check if there is already a player in the game, if yes we join him
                 {
                     opponentName = (string)ds.Get(Constants.HOST_GAME);
                     hm.Put(Constants.GAMENUM, gameNum);
                     hm.Put(Constants.HOST_GAME, opponentName);
                     hm.Put(Constants.PLAYER_GAME, user.UserName);
                     hm.Put(Constants.ISLIVE_GAME, true);                          //we put true to say a game is running
                     fd.AddDocumentToCollection(Constants.GAMES_COL, gameNum, hm); //put the information according to the game number
                     doStart = true;
                     isHost  = false;
                     break;
                 }
             }
         }
         if (doStart)
         {
             StartGame(gameNum, opponentName, game.Subject);
         }
         else
         {
             foreach (DocumentSnapshot ds in qs.Documents)//we know a game hasn't started yet because we looped all the available games
             {
                 bool isLive = (bool)ds.Get(Constants.ISLIVE_GAME);
                 if (!isLive)
                 {
                     gameNum = (string)ds.Get(Constants.GAMENUM);
                     HashMap hm = new HashMap();
                     hm.Put(Constants.GAMENUM, gameNum);
                     hm.Put(Constants.ISLIVE_GAME, false);
                     hm.Put(Constants.HOST_GAME, user.UserName);
                     fd.AddDocumentToCollection(Constants.GAMES_COL, gameNum, hm);         //put the information according to the game number
                     fd.AddSnapShotListenerToDocument(Constants.GAMES_COL, gameNum, this); //add event listener on current game
                     //put loading screen until 2nd joins game
                     ShowProgressDlg();
                     isHost = true;
                     break;//stop checking for other games
                 }
             }
         }
     }
 }