void OnAddChatroom(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(chatroomEntry.Text))
            {
                //logic to add chat room
                ChatroomModel cr = new ChatroomModel();
                cr.Id      = chatroomEntry.Text;
                cr.Title   = chatroomEntry.Text;
                cr.UserIds = new List <string>()
                {
                    DependencyService.Get <FirebaseAuthService>().GetUid()
                };

                Action <bool> onChatroomCreatedSuccess = (bool obj) =>
                {
                    UserService.Instance.User.AddChatroom(cr.Id);
                    UserService.Instance.SaveUserToServer();

                    InitChatroomStackLayout();

                    chatroomEntry.Text = null;
                };

                ChatService.Instance.CreateChatroom(cr, onChatroomCreatedSuccess);
            }
        }
Ejemplo n.º 2
0
        public void CreateChatroom(ChatroomModel chatroomDataToAdd, Action <bool> onSuccess = null, Action <string> onError = null)
        {
            Action <ChatroomModel> onValueEvent = (ChatroomModel chatroomDataExisting) =>
            {
                Action onSetValueSuccess = () =>
                {
                    if (onSuccess != null)
                    {
                        onSuccess(true);
                    }
                };

                Action <string> onSetValueError = (string errorDesc) =>
                {
                    if (onError != null)
                    {
                        onError(errorDesc);
                    }
                };

                if (chatroomDataExisting == null)
                {
                    fbDatabaseService.SetValue(ROOMS_URL_PREFIX + chatroomDataToAdd.Id, chatroomDataToAdd, onSetValueSuccess, onSetValueError);
                }
                else
                {
                    //Room already exists just add users
                    foreach (string userId in chatroomDataToAdd.UserIds)
                    {
                        if (!chatroomDataExisting.UserIds.Contains(userId))
                        {
                            chatroomDataExisting.UserIds.Add(userId);
                        }
                    }

                    fbDatabaseService.SetValue(ROOMS_URL_PREFIX + chatroomDataToAdd.Id + USER_IDS_URL_SUFFIX, chatroomDataExisting.UserIds, onSetValueSuccess, onSetValueError);
                }
            };

            fbDatabaseService.AddSingleValueEvent(ROOMS_URL_PREFIX + chatroomDataToAdd.Id, onValueEvent);
        }