예제 #1
0
        private void OnRefreshWallsCompleted(RequestUser userRequest, ResponseJson response)
        {
            if (response.value.Equals("success"))
            {
                //reset text error
                wallView.ErrorText = "";

                //save new user data in model
                string selectedWallName = wallView.SelectedWall;
                Wall selectedWallChanges = dataModel.MergeAndReturnWallChanges(response.user, selectedWallName);

                //add new messages on the selected wall if any
                foreach (Message mess in selectedWallChanges.messages)
                {
                    wallView.AddMessageToSelectedWall(mess.content, mess.author, mess.date);
                }

                //create new notifications
                ManageNewMessagesNotification(response.user);
            }
            else if (response.value.Equals("fail"))
            {
                wallView.ErrorText = response.details;
            }
            else
            {
                wallView.ErrorText = "Unexpected error";
            }
        }
예제 #2
0
        private void OnLoginUserCompleted(RequestUser userRequest, ResponseJson response)
        {
            if (response.value.Equals("success"))
            {
                //reset text error
                loginUserView.ErrorText = "";

                //create login user - response request doesn't contain usernames or passwords
                User loginUser = response.user;
                loginUser.username = userRequest.username;
                loginUser.password = userRequest.password;

                //save new user data in model
                dataModel.User = loginUser;

                //set new user data to wallView
                PopulateWallView();

                //load wallView
                parentView.CurrentChildView = wallView;
                wallView.AutoRefreshWorking = true;
            }
            else if (response.value.Equals("fail"))
            {
                loginUserView.ErrorText = response.details;
            }
            else
            {
                loginUserView.ErrorText = "Unexpected error";
            }
        }
예제 #3
0
        private void OnAddMessageCompleted(RequestUser userRequest, ResponseJson response)
        {
            if (response.value.Equals("success"))
            {
                //reset text error
                wallView.ErrorText = "";

                //save new user data in model
                dataModel.MergeUserModel(response.user);

                if (userRequest.name.Equals(wallView.SelectedWall))
                {
                    if (response.user.walls != null && response.user.walls.Count > 0 && response.user.walls[0].messages != null && response.user.walls[0].messages.Count > 0)
                    {
                        Message mess = response.user.walls[0].messages[0];
                        wallView.AddMessageToSelectedWall(mess.content, mess.author, mess.date);
                    }
                }

                wallView.NewMessageText = "";
            }
            else if (response.value.Equals("fail"))
            {
                wallView.ErrorText = response.details;
            }
            else
            {
                wallView.ErrorText = "Unexpected error";
            }
        }
예제 #4
0
        private void OnAddWallCompleted(RequestUser userRequest, ResponseJson response)
        {
            if (response.value.Equals("success"))
            {
                //reset text error
                wallView.ErrorText = "";

                //save new user data in model
                dataModel.MergeUserModel(response.user);

                if (response.user.walls != null && response.user.walls.Count > 0)
                {
                    wallView.AddNewWallName(response.user.walls[0].name);
                    wallView.SelectedWall = response.user.walls[0].name;
                }

                //wallView.NewWallNameText = "";
            }
            else if (response.value.Equals("fail"))
            {
                wallView.ErrorText = response.details;
            }
            else
            {
                wallView.ErrorText = "Unexpected error";
            }
        }
예제 #5
0
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Tuple<MyRequestMethod, RequestUser, OnRequestCompletedMethod> param = e.Argument as Tuple<MyRequestMethod, RequestUser, OnRequestCompletedMethod>;
            ResponseJson responseJson;

            try
            {
                string json;

                if (!JsonSerializer.Serialize(param.Item2, out json))
                {
                    responseJson = new ResponseJson();
                    responseJson.value = "fail";
                    responseJson.details = "Error while serializing request input json";
                    e.Result = Tuple.Create<RequestUser, ResponseJson, OnRequestCompletedMethod>(param.Item2, responseJson, param.Item3);
                    return;
                }

                string serverAdress = GetRequestAdress(param.Item1);
                HttpWebRequest webRequest = null;
                webRequest = (HttpWebRequest)WebRequest.Create(serverAdress);
                webRequest.ContentType = GetRequestContentType();
                webRequest.ContentLength = json.Length;
                webRequest.Method = GetRequestMethod(param.Item1);

                using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var webResponse = (HttpWebResponse)webRequest.GetResponse();
                    using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
                    {
                        string response = streamReader.ReadToEnd();
                        if (!JsonSerializer.Deserialize<ResponseJson>(response, out responseJson))
                        {
                            responseJson = new ResponseJson();
                            responseJson.value = "fail";
                            responseJson.details = "Error while deserializing response output json";
                            e.Result = Tuple.Create<RequestUser, ResponseJson, OnRequestCompletedMethod>(param.Item2, responseJson, param.Item3);
                            return;
                        }

                        if (!responseJson.value.Equals("success") && !responseJson.value.Equals("fail"))
                        {
                            responseJson = new ResponseJson();
                            responseJson.value = "fail";
                            responseJson.details = "Wrong response json received from server";
                            e.Result = Tuple.Create<RequestUser, ResponseJson, OnRequestCompletedMethod>(param.Item2, responseJson, param.Item3);
                            return;
                        }

                        e.Result = Tuple.Create<RequestUser, ResponseJson, OnRequestCompletedMethod>(param.Item2, responseJson, param.Item3);
                        return;
                    }
                }
            }
            catch (Exception exception)
            {
                string error = exception.ToString();
                int pos = error.IndexOf("--->");
                if (pos != -1)
                {
                    error = error.Substring(0, pos);
                }

                responseJson = new ResponseJson();
                responseJson.value = "fail";
                responseJson.details = error;
                e.Result = Tuple.Create<RequestUser, ResponseJson, OnRequestCompletedMethod>(param.Item2, responseJson, param.Item3);
                return;
            }
        }