public async void Login_Api(string userName, string pass, string tok_ty, string acc_tok) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tok_ty, acc_tok); client.DefaultRequestHeaders.Add("api-version", "1.0"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var values = new Dictionary <string, string> { { "usuario", userName }, { "contrasena", pass } }; var content = new FormUrlEncodedContent(values); var response = await client.PostAsync("http://aige.sytes.net/ApiRestSAM/api/citasapp/accesousuarios", content); switch (response.StatusCode) { case (System.Net.HttpStatusCode.OK): res_Label_api.Text = "Acceso Exitoso"; var responseString = await response.Content.ReadAsStringAsync(); // var xjson = JsonConvert.DeserializeObject(responseString); var xjson = JsonConvert.DeserializeObject <Root>(responseString); int xid = Convert.ToInt32(xjson.bandera); string id_x = xjson.bandera; var idx = xjson.tablas.Table1[0].IdUsuario; int idid = Convert.ToInt32(xjson.tablas.Table1[0].IdUsuario); if (id_x == "-2") { userTxt.Focus(); passTxt.Focus(); btnAuth.Text = "Intentar de nuevo"; res_Label.Text = "Hubo un problema como los datos de acceso"; res_Label_api.Text = "Usuario o contraseña no validos"; } else { try { member = new Member(); memberdatabase = new MemberDatabase(); member.UserName = userName; member.Pass = pass; member.Token_Type = tok_ty; member.Access_Token = acc_tok; member.ID = idid; memberdatabase.AddMember(member); } catch (Exception ex) { res_Label.Text = "Hubo un problema como los datos de acceso: " + ex.ToString(); res_Label_api.Text = "Usuario o contraseña no validos"; } Application.Current.MainPage = new NavigationPage(new PageNav()); } break; case (System.Net.HttpStatusCode.BadRequest): res_Label_api.Text = "Error 400"; break; case (System.Net.HttpStatusCode.Forbidden): res_Label_api.Text = "Error 403"; break; //500 case (System.Net.HttpStatusCode.InternalServerError): string status = "Nuestros servidores estan en mantenimiento"; res_Label_api.Text = status; break; } waitActIndicator.IsRunning = false; btnAuth.IsEnabled = true; return; }
public Chat(string id, string name, string consumerid, string storeuid, string Customername) { TextContainer = new ObservableCollection <MessageText>(); listView = new ListView(); QueryId = id; QueryName = name; Consumerid = consumerid; Storeuid = storeuid; CustName = Customername; BindingContext = this; InitializeComponent(); CustomNavigationPage.SetGradientColors(this, new Tuple <Color, Color>(Color.FromHex("#17a39d"), Color.FromHex("#17a39d"))); Title = QueryName; // BindData(); // CustomNavigationPage.SetGradientColors(this, new Tuple<Color, Color>(Color.FromHex("#17a39d"), Color.FromHex("#17a39d"))); manager1 = TodoItemManager.DefaultManager; SignalRClient.Start().ContinueWith(task => { if (task.IsFaulted) { DisplayAlert("Error", "An error occurred when trying to connect to SignalR: " + task.Exception.InnerExceptions[0].Message, "OK"); } }); memberDatabase = new MemberDatabase(); var members = memberDatabase.GetUserDetail(); UserDetail userin = (from blog in members select blog).FirstOrDefault(); if (userin != null) { UserNameSentMessage = userin.Name; UsernameTextbox.Text = userin.Name; } // BindData(); // BindFromAzure(); if (CrossConnectivity.Current.IsConnected) { BindFromAWS(); } else { BindData(); } SignalRClient.Start(); //try to reconnect every 10 seconds, just in case Device.StartTimer(TimeSpan.FromSeconds(5), () => { if (!SignalRClient.IsConnectedOrConnecting) { SignalRClient.Start(); } return(true); }); try { MessageSent += async(uploadedFilename) => { try { var tt = DateTime.Now; var text = uploadedFilename; if (text.StartsWith("awsimage")) { var sendMessage = text.Substring(text.LastIndexOf("/") + 1); sendMessage = "awsimage" + sendMessage; SignalRClient.SendMessage(UsernameTextbox.Text, sendMessage); int index = text.IndexOf("awsimage"); uploadedFilename = text.Substring(index + 8); // string images = "https://maudit.elixirct.net/UploadFileToServer/Uploads/" + uploadedFilename; // var images = DependencyService.Get<IFileService>().GetPictureFromDisk(uploadedFilename, getGalleryPath()); TextContainer.Add(new MessageText { imgsource = uploadedFilename, imgheight = "200", imgwidth = "200", Status = "Sent", DateSent = DateTime.Now, StoreNameMessage = UserNameSentMessage }); var last4 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last4, ScrollToPosition.MakeVisible, false); uploadedFilename = uploadedFilename.Substring(uploadedFilename.LastIndexOf("/") + 1); try { MemoryStream ms = new MemoryStream(); member = new Member(); memberDatabase = new MemberDatabase(); member.Text = uploadedFilename; member.Status = "Sent"; member.Datetime = DateTime.Now; member.Query_ID = QueryId; member.Type = "IMG"; if (member != null) { memberDatabase.AddMember(member); } string Inputs = "QueryId=" + QueryId + "&ChatText=" + uploadedFilename + "&StoreUID=" + Storeuid + "&ConumerUniqueID=" + Consumerid + "&ChatType=" + "IMG" + "&IsActive=" + "1" + "&Status=" + "Sent" + "&Rating=" + ""; string resp = await RestService.SaveChat(Inputs); var result = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(resp); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } else { SignalRClient.SendMessage(UsernameTextbox.Text, uploadedFilename); TextContainer.Add(new MessageText { Text = text, imgheight = "10", imgwidth = "10", imgsource = imlocal, Status = "Sent", DateSent = DateTime.Now, StoreNameMessage = UserNameSentMessage }); var last2 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last2, ScrollToPosition.MakeVisible, true); try { string Inputs = "QueryId=" + QueryId + "&ChatText=" + text + "&StoreUID=" + Storeuid + "&ConumerUniqueID=" + Consumerid + "&ChatType=" + "Text" + "&IsActive=" + "1" + "&Status=" + "Sent" + "&Rating=" + ""; string resp = await RestService.SaveChat(Inputs); var result = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(resp); if (result != "0") { MemoryStream ms = new MemoryStream(); member = new Member(); memberDatabase = new MemberDatabase(); member.Text = uploadedFilename; member.Status = "Sent"; member.Datetime = DateTime.Now; member.Query_ID = QueryId; member.Type = "Text"; if (member != null) { memberDatabase.AddMember(member); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } var last = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last, ScrollToPosition.MakeVisible, false); imlocal = null; } catch (Exception ex) { await DisplayAlert("Alert", "Please wait 5 second, The connection has not been established", "Ok"); System.Diagnostics.Debug.WriteLine(ex); } }; SignalRClient.OnMessageReceived += (username, uploadedFilename) => { if (username != UserNameSentMessage) { var cname = username; var cc = uploadedFilename; if (cc.StartsWith("awsimage")) { int index = cc.IndexOf("awsimage"); uploadedFilename = cc.Substring(index + 8); //var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename); //imlocal = ImageSource.FromStream(() => new MemoryStream(imageData)); //var imagestore2 = uploadedFilename + ".jpg"; //Stream stream = new MemoryStream(imageData); //DependencyService.Get<IFileService>().SavePicture(imagestore2, stream, getGalleryPath()); string url = "http://elixirct.in/ShopRConservicePublish/Uploads/" + uploadedFilename; // string url = "http://elixirct.in/ShopRConservicePublish/Uploads" + uploadedFilename; try { DependencyService.Get <IFileService>().DownloadFile(url, getGalleryPath()); var images = DependencyService.Get <IFileService>().GetPictureFromDisk(uploadedFilename, getGalleryPath()); // var client = new HttpClient(); // var uri = new Uri(Uri.EscapeUriString(url)); // byte[] urlContents = await client.GetByteArrayAsync(uri); // var stream1 = new MemoryStream(urlContents); TextContainer.Add(new MessageText { imgsource = images, imgheight = "200", imgwidth = "200", Status = "Received", DateSent = DateTime.Now, CustomerChatName = cname }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } Task.Delay(TimeSpan.FromSeconds(1)); // var last3 = ListView.ItemsSource.Cast<object>().LastOrDefault(); // ListView.ScrollTo(last3, ScrollToPosition.MakeVisible, true); member = new Member(); memberDatabase = new MemberDatabase(); member.Text = uploadedFilename; member.Status = "Received"; member.Datetime = DateTime.Now; member.Query_ID = QueryId; member.Type = "IMG"; if (member != null) { memberDatabase.AddMember(member); } } //else if (cc.StartsWith("pdf")) //{ // int index = cc.IndexOf("pdf"); // uploadedFilename = cc.Substring(index + 3); // var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename); // imlocal = ImageSource.FromStream(() => new MemoryStream(imageData)); // var imagestore2 = uploadedFilename + ".pdf"; // Stream stream = new MemoryStream(imageData); // DependencyService.Get<IFileService>().SavePicture(imagestore2, stream, getGalleryPath()); // var images = DependencyService.Get<IFileService>().GetPictureFromDisk(imagestore2, getGalleryPath()); // // TextContainer.Add(new MessageText { imgsource = images, imgheight = "200", imgwidth = "200", Status = "Sent", DateSent = DateTime.Now }); // TextContainer.Add(new MessageText { Text = images, imgheight = "10", imgwidth = "10", Status = "Sent", DateSent = DateTime.Now }); // var last21 = ListView.ItemsSource.Cast<object>().LastOrDefault(); // ListView.ScrollTo(last21, ScrollToPosition.MakeVisible, true); //} else { TextContainer.Add(new MessageText { Text = cc, imgheight = "10", imgwidth = "10", Status = "Received", DateSent = DateTime.Now, CustomerChatName = cname }); var last2 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last2, ScrollToPosition.MakeVisible, true); member = new Member(); memberDatabase = new MemberDatabase(); member.Text = cc; member.Status = "Received"; member.Datetime = DateTime.Now; member.Query_ID = QueryId; member.Type = "Text"; if (member != null) { memberDatabase.AddMember(member); var last3 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last3, ScrollToPosition.MakeVisible, true); } } var last32 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last32, ScrollToPosition.MakeVisible, true); } var last31 = ListView.ItemsSource.Cast <object>().LastOrDefault(); ListView.ScrollTo(last31, ScrollToPosition.MakeVisible, true); }; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); DisplayAlert("Alert", "Some Network Problem", "OK"); } Messagebox.Completed += (s, e) => { if (CrossConnectivity.Current.IsConnected) { var messageSent = MessageSent; if (messageSent != null) { messageSent(Messagebox.Text); } Messagebox.Text = string.Empty; } else { // DisplayAlert("Alert", "Please check your internet connection", "OK"); } }; }