Subversion Repository Public Repository

Nextrek

Diff Revisions 751 vs 752 for /Android/SmartCharging/SmartCharging_WP/SmartCharging/Net/SmartChargeAPI.cs

Diff revisions: vs.
  @@ -51,13 +51,41 @@
51 51 if (this.siteTypeList == null)
52 52 {
53 53 List<SiteType> ret = new List<SiteType>();
54 - string toBeParsed = await config.getConfigValueByKey("siteTypes");
55 - string[] split = toBeParsed.Split('|');
56 - foreach (string el in split)
54 +
55 +
56 + /*call the api; if this fails, use the config file*/
57 + string url = await this.GetBaseUrl();
58 +
59 + url += "/get_tipologie_list.php";
60 + string responseString = await this.net.PostRequest(new Uri(url), this.CreateRequestPostData(new { }));
61 +
62 + if (responseString != null && !responseString.Equals(""))
63 + {
64 + JObject parsed = this.tryParseJson(responseString);
65 + JArray types = (JArray)parsed["tipologie"];
66 + JObject type;
67 + for (int i = 0; i < types.Count; i++)
68 + {
69 + type = (JObject)types[i];
70 +
71 + ret.Add(new SiteType() { Id = (string)type["id"], Label = (string)type["tipologia"] });
72 +
73 +
74 + }
75 +
76 + }
77 + else
57 78 {
58 - string[] siteTypeString = el.Split(',');
59 - ret.Add(new SiteType() { Id = siteTypeString[0], Label = siteTypeString[1] });
79 + string toBeParsed = await config.getConfigValueByKey("siteTypes");
80 + string[] split = toBeParsed.Split('|');
81 + foreach (string el in split)
82 + {
83 + string[] siteTypeString = el.Split(',');
84 + ret.Add(new SiteType() { Id = siteTypeString[0], Label = siteTypeString[1] });
85 + }
60 86 }
87 +
88 +
61 89 this.siteTypeList = ret;
62 90 }
63 91
  @@ -213,24 +241,10 @@
213 241
214 242
215 243 string url = await this.GetBaseUrl();
216 - string newAvatarName = "";
217 244
218 - if (avatar != null)
219 - {
220 - //Upload avatar
221 - using (var stream = await avatar.OpenAsync(Windows.Storage.FileAccessMode.Read))
222 - {
223 - // This is where the byteArray to be stored.
224 - Byte[] bytes = new byte[stream.Size];
225 245
226 - // This returns IAsyncOperationWithProgess, so you can add additional progress handling
227 - await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, Windows.Storage.Streams.InputStreamOptions.None);
228 - newAvatarName = await this.UploadImage(bytes, avatar.Name, avatar.FileType);
229 - }
230 - }
231 -
232 246
233 - var obj = new { nome = newUser.Name, cognome = newUser.Surname, cell = newUser.CellphoneNumber, email = newUser.Email, username = username, password = password, avatar = newAvatarName };
247 + var obj = new { nome = newUser.Name, cognome = newUser.Surname, cell = newUser.CellphoneNumber, email = newUser.Email, username = username, password = password, avatar = avatar == null ? "" : avatar.Name };
234 248
235 249 url += "/registrazione_utente.php";
236 250 string responseString = await this.net.PostRequest(new Uri(url), this.CreateRequestPostData(obj));
  @@ -243,6 +257,17 @@
243 257 newUser.Id = ((string)parsed["id"]);
244 258 this.user = newUser;
245 259 this.token = (string)parsed["token"];
260 + if (avatar != null)
261 + {
262 + //Upload avatar
263 + using (var stream = await avatar.OpenAsync(Windows.Storage.FileAccessMode.Read))
264 + {
265 + // This is where the byteArray to be stored.
266 + Byte[] bytes = new byte[stream.Size];
267 + await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, Windows.Storage.Streams.InputStreamOptions.None);
268 + await this.UploadAvatar(bytes, avatar.Name, (string)parsed["avatar_name"], avatar.FileType);
269 + }
270 + }
246 271 return true;
247 272 }
248 273 else
  @@ -274,8 +299,10 @@
274 299 List<Site> response = new List<Site>();
275 300
276 301 par = new Dictionary<string, string>();
277 - var obj = new { lat = location.Latitude, lon = location.Longitude, distance = maxDistance, tipologia = type.Id };
278 302
303 + var obj = new { lat = location.Latitude, lon = location.Longitude, distance = maxDistance, tipologia = type != null ? type.Id : "-1" };
304 +
305 +
279 306
280 307 url += "/get_list_locali.php";
281 308 string responseString = await this.net.PostRequest(new Uri(url), this.CreateRequestPostData(obj));
  @@ -483,7 +510,7 @@
483 510 JObject parsed = this.tryParseJson(responseString);
484 511 if (parsed != null)
485 512 {
486 - return this.CreateReviewFromJson(parsed);
513 + return this.CreateReviewFromJson((JObject)parsed["comment"]);
487 514
488 515 }
489 516 else
  @@ -543,6 +570,11 @@
543 570 return response;
544 571 }
545 572
573 + public async Task<bool> DeleteSiteImage(Site s, string imagePath)
574 + {
575 + return await this.DeleteImage(imagePath, s.Id, "local");
576 + }
577 +
546 578 public async Task GetSiteDetailsAndReviews(Site site, int reviewLimit)
547 579 {
548 580 await this.GetSiteDetails(site);
  @@ -570,7 +602,6 @@
570 602 {
571 603
572 604 JObject parsed = this.tryParseJson(responseString);
573 - //logout ok
574 605 if (parsed != null && ((string)parsed["return"]).Equals("0"))
575 606 {
576 607 result = true;
  @@ -605,7 +636,6 @@
605 636 if (responseString != null && !responseString.Equals(""))
606 637 {
607 638 JObject parsed = this.tryParseJson(responseString);
608 - //logout ok
609 639 if (parsed != null && ((string)parsed["return"]).Equals("0"))
610 640 {
611 641
  @@ -674,6 +704,63 @@
674 704
675 705
676 706 }
707 +
708 + public async Task<bool> EditSite(Site site, List<StorageFile> imagesToAdd, List<string> imagesToBeDeleted)
709 + {
710 +
711 +
712 + string url = await this.GetBaseUrl();
713 +
714 +
715 +
716 + foreach (string img in imagesToBeDeleted)
717 + {
718 + await this.DeleteSiteImage(site, img);
719 + }
720 + await this.AddSiteImages(site, imagesToAdd);
721 +
722 +
723 + var obj = new
724 + {
725 + user_id = this.user.Id,
726 + nomelocale = site.Name,
727 + descrizione = site.Description,
728 + tipologia = site.Type.Id,
729 + lat = site.Position.Position.Latitude,
730 + lon = site.Position.Position.Longitude,
731 + indirizzo = site.Address,
732 + telefono = site.PhoneNumber,
733 + email = site.Email,
734 + sito = site.WebSite,
735 + n_punti_ricarica = site.Chargers,
736 + token = this.token
737 + };
738 +
739 + url += "/edit_locale.php";
740 + string responseString = await this.net.PostRequest(new Uri(url), this.CreateRequestPostData(obj));
741 + if (responseString != null && !responseString.Equals(""))
742 + {
743 + JObject parsed = this.tryParseJson(responseString);
744 + //add site ok
745 + if (parsed != null && ((string)parsed["return"]).Equals("0"))
746 + {
747 + site.Id = ((string)parsed["local_id"]);
748 + return true;
749 + }
750 + else
751 + {
752 + await errorHandler.showErrorMessage("AddSite", ((string)parsed["return"]));
753 + }
754 + }
755 + else
756 + {
757 + await errorHandler.showErrorMessage();
758 + }
759 +
760 + return false;
761 +
762 +
763 + }
677 764 #endregion
678 765
679 766 /*returns a new image name*/
  @@ -682,12 +769,12 @@
682 769 string url = await this.GetBaseUrl();
683 770
684 771 url += "/upload_image.php";
685 -
686 - string responseString = await this.net.PostRequest(new Uri(url), bytes, "image_file", imageName, "image/" + extension.Substring(1));
772 + Dictionary<string, string> parameters = new Dictionary<string, string>();
773 + parameters.Add("image_file", imageName);
774 + string responseString = await this.net.PostRequest(new Uri(url), bytes, parameters, "image/" + extension.Substring(1));
687 775 if (responseString != null && !responseString.Equals(""))
688 776 {
689 777 JObject parsed = this.tryParseJson(responseString);
690 - //logout ok
691 778 if (parsed != null && ((string)parsed["return"]).Equals("0"))
692 779 {
693 780 return (string)parsed["image_file"];
  @@ -705,6 +792,71 @@
705 792 return "";
706 793 }
707 794
795 + private async Task<string> UploadAvatar(Byte[] bytes, string avatarName, string avatarReturnedName, string extension)
796 + {
797 + if (this.user == null)
798 + {
799 + return null;
800 + }
801 + string url = await this.GetBaseUrl();
802 +
803 + url += "/upload_avatar.php";
804 + Dictionary<string, string> parameters = new Dictionary<string, string>();
805 + parameters.Add("avatar_file", avatarName);
806 + parameters.Add("user_id", this.user.Id);
807 + parameters.Add("avatar_name", avatarReturnedName);
808 + parameters.Add("token", this.token);
809 +
810 + string responseString = await this.net.PostRequest(new Uri(url), bytes, parameters, "image/" + extension.Substring(1));
811 + if (responseString != null && !responseString.Equals(""))
812 + {
813 + JObject parsed = this.tryParseJson(responseString);
814 + if (parsed != null && ((string)parsed["return"]).Equals("0"))
815 + {
816 + return (string)parsed["avatar_name"];
817 + }
818 + else
819 + {
820 + await errorHandler.showErrorMessage("UploadAvatar", ((string)parsed["return"]));
821 + }
822 + }
823 + else
824 + {
825 + await errorHandler.showErrorMessage();
826 + }
827 +
828 + return "";
829 + }
830 +
831 + private async Task<bool> DeleteImage(string imagePath, string id, string type)
832 + {
833 + string url = await this.GetBaseUrl();
834 +
835 + var obj = new { id = id, type = type, img = imagePath.Substring(imagePath.LastIndexOf("/")+1), token = this.token };
836 +
837 + url += "/delete_immagine.php";
838 +
839 + string responseString = await this.net.PostRequest(new Uri(url), this.CreateRequestPostData(obj));
840 + if (responseString != null && !responseString.Equals(""))
841 + {
842 + JObject parsed = this.tryParseJson(responseString);
843 + if (parsed != null && ((string)parsed["return"]).Equals("0"))
844 + {
845 + return true;
846 + }
847 + else
848 + {
849 + await errorHandler.showErrorMessage("DeleteImage", ((string)parsed["return"]));
850 + }
851 + }
852 + else
853 + {
854 + await errorHandler.showErrorMessage();
855 + }
856 +
857 + return false;
858 + }
859 +
708 860 private async Task<List<string>> UploadImages(List<StorageFile> images)
709 861 {
710 862