Beispiel #1
0
        public static async Task Login(ListsViewController view, string inpEmail, string inpPassword)
        {
            var done = false;

            AppData.auth.SignInWithPassword(inpEmail, inpPassword, async(user, error) =>
            {
                if (error != null)
                {
                    CustomAlert.Alert(view, "Error Signing In", error.UserInfo.Description);
                    done = true;
                    return;
                }

                if (user != null)
                {
                    SetLocalUser(new User
                    {
                        Name  = user.User.DisplayName,
                        Uid   = user.User.Uid,
                        Email = user.User.Email
                    });

                    await view.ReloadData();
                    CustomAlert.Alert(view, "Login Was Successful", "Welcome back " + user.User.DisplayName);
                }

                done = true;
            });

            while (!done)
            {
                await Task.Delay(50);
            }
        }
Beispiel #2
0
        public static void Invite(UIViewController view, GroceryList list, string inviteeEmail)
        {
            User inviteeUser = null;
            var  inviterUser = AppData.currentUser;
            var  listName    = list.ListName;

            AppData.UsersNode.ObserveSingleEvent(DataEventType.Value, snapshot =>
            {
                var children      = snapshot.Children;
                var childSnapShot = children.NextObject() as DataSnapshot;

                while (childSnapShot != null)
                {
                    var childDict = childSnapShot.GetValue <NSDictionary>();

                    if (childDict.ValueForKey((NSString)"email").ToString() == inviteeEmail)
                    {
                        // user exist
                        inviteeUser = new User
                        {
                            Name  = childDict.ValueForKey((NSString)"name").ToString(),
                            Email = childDict.ValueForKey((NSString)"email").ToString(),
                            Uid   = childDict.ValueForKey((NSString)"uid").ToString()
                        };
                        break;
                    }
                    childSnapShot = children.NextObject() as DataSnapshot;
                }


                if (inviteeUser == null)
                {
                    CustomAlert.Alert(view,
                                      "No Such User",
                                      "Such user doesn't have an account with us");
                    return;
                }

                var invitationTitle = inviterUser.Uid + "|" + listName;

                object[] ownerKeys   = { "ownerUid", "ownerEmail", "ownerName" };
                object[] ownerValues = { inviterUser.Uid, inviterUser.Email, inviterUser.Name };
                var ownerDict        = NSDictionary.FromObjectsAndKeys(ownerValues, ownerKeys);

                object[] inviteeKeys   = { "listName", "owner" };
                object[] inviteeValues = { listName, ownerDict };
                var inviteeDict        = NSDictionary.FromObjectsAndKeys(inviteeValues, inviteeKeys);

                var inviteeNode = AppData.UsersNode.GetChild(inviteeUser.Uid);
                inviteeNode.GetChild("myInvitations")
                .GetChild(invitationTitle)
                .SetValue(inviteeDict);

                CustomAlert.Alert(view,
                                  "Invitation Sent",
                                  "You have successfully invited " + inviteeUser.Name + " to this list.");
            });
        }
Beispiel #3
0
        public static async Task RegisterUser(ListsViewController view, string name, string email, string password)
        {
            var done = false;

            AppData.auth.CreateUser(email, password, (user, error) => {
                if (error != null)
                {
                    CustomAlert.Alert(view, "Error creating user", error.UserInfo.Description);
                    return;
                }

                if (user != null)
                {
                    var changeRequest         = user.User.ProfileChangeRequest();
                    changeRequest.DisplayName = name;
                    changeRequest.CommitChanges(async profileError => {
                        if (profileError != null)
                        {
                            CustomAlert.Alert(view, "Error updating user profile", profileError.UserInfo.Description);
                            return;
                        }
                        //store locally
                        SetLocalUser(new User {
                            Name  = user.User.DisplayName,
                            Email = user.User.Email,
                            Uid   = user.User.Uid
                        });

                        object[] userKeys   = { "name", "email", "uid" };
                        object[] userValues = { user.User.DisplayName, user.User.Email, user.User.Uid };
                        var userDict        = NSDictionary.FromObjectsAndKeys(userValues, userKeys);
                        //store online
                        AppData.UsersNode.GetChild(user.User.Uid).SetValue(userDict);

                        foreach (var list in AppData.currentList.Where(list => list.ListOwner.Uid == AppData.currentUser.Uid))
                        {
                            CloudFunctions.SaveList(list);
                        }

                        await view.ReloadData();
                        CustomAlert.Alert(view, "Success", "You are now registered and can share lists with your friends.");

                        done = true;
                    });
                }
            });

            while (!done)
            {
                await Task.Delay(50);
            }
        }
Beispiel #4
0
        public static async void Logout(ListsViewController view)
        {
            var signedOut = AppData.auth.SignOut(out var error);

            if (signedOut)
            {
                CustomAlert.Alert(view, "Logged Out", "You can still work offline!");

                await view.ReloadData();

                return;
            }

            CustomAlert.Alert(view, "Error on Logout", error?.ToString());
        }