development #3
| @@ -1,16 +1,34 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using System.Text; | ||||
| using UnityEngine; | ||||
| using UnityEngine.Networking; | ||||
| using UnityEngine.UIElements; | ||||
| using Newtonsoft.Json; | ||||
| using System.Collections.Generic; | ||||
| using System.Globalization; | ||||
|  | ||||
| class FurnitureFetcher : MonoBehaviour | ||||
| { | ||||
|     public VisualTreeAsset itemTemplate; | ||||
|     public UIDocument uiDocument; | ||||
|  | ||||
|     public static string API_URL = "http://100.123.31.103:5102/api/v1/FurnitureVariant/GetByIds"; | ||||
|     public static string API_URL = "https://localhost:7223/api/v1/FurnitureVariant/GetAll"; | ||||
|  | ||||
|     private static readonly Dictionary<string, string> CurrencySymbols = new() | ||||
|     { | ||||
|         { "USD", "$" }, | ||||
|         { "EUR", "€" }, | ||||
|         { "GBP", "£" }, | ||||
|         { "JPY", "¥" }, | ||||
|         { "MXN", "$" }, | ||||
|         { "CAD", "$" }, | ||||
|         { "BRL", "R$" }, | ||||
|         { "ARS", "$" }, | ||||
|         { "CLP", "$" }, | ||||
|         { "COP", "$" }, | ||||
|         { "CNY", "¥" }, | ||||
|         { "INR", "₹" } | ||||
|     }; | ||||
|  | ||||
|  | ||||
|     private void Awake() | ||||
|     { | ||||
| @@ -19,38 +37,84 @@ class FurnitureFetcher : MonoBehaviour | ||||
|  | ||||
|     IEnumerator GetFurnitureData() | ||||
|     { | ||||
|         Debug.Log($"API URL: {API_URL}"); | ||||
|         List<string> variantIds = new() | ||||
|         { | ||||
|             "ab6fd51f-81f0-490d-b713-8d4f3de61a58", | ||||
|             "1f3b348f-b3ee-4985-b76f-7f6d61f2c835" | ||||
|         }; | ||||
|  | ||||
|         FurnitureIdsRequest data = new() { ids = variantIds }; | ||||
|         string json = JsonUtility.ToJson(data); | ||||
|  | ||||
|         using UnityWebRequest request = new(API_URL, "POST"); | ||||
|         byte[] bodyRaw = Encoding.UTF8.GetBytes(json); | ||||
|         request.uploadHandler = new UploadHandlerRaw(bodyRaw); | ||||
|         using UnityWebRequest request = UnityWebRequest.Get(API_URL); | ||||
|         request.downloadHandler = new DownloadHandlerBuffer(); | ||||
|         request.SetRequestHeader("Content-Type", "application/json"); | ||||
|  | ||||
|         yield return request.SendWebRequest(); | ||||
|  | ||||
|         string rawResponse = request.downloadHandler.text; | ||||
|         //Debug.Log("📦 RAW RESPONSE:\n" + rawResponse); | ||||
|  | ||||
|         if (request.result == UnityWebRequest.Result.Success) | ||||
|         { | ||||
|             string wrappedJson = "{\"items\":" + request.downloadHandler.text + "}"; | ||||
|             FurnitureVariantList list = JsonUtility.FromJson<FurnitureVariantList>(wrappedJson); | ||||
|             string wrappedJson = "{\"items\":" + rawResponse + "}"; | ||||
|             FurnitureVariantList list; | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 list = JsonConvert.DeserializeObject<FurnitureVariantList>(wrappedJson); | ||||
|             } | ||||
|             catch (System.Exception ex) | ||||
|             { | ||||
|                 Debug.LogError("❌ Error al deserializar JSON: " + ex.Message); | ||||
|                 yield break; | ||||
|             } | ||||
|  | ||||
|             var root = uiDocument.rootVisualElement; | ||||
|             var scrollView = root.Q<ScrollView>("furniture-list"); | ||||
|  | ||||
|             scrollView.Clear(); | ||||
|  | ||||
|             foreach (var variant in list.items) | ||||
|             { | ||||
|                 VisualElement item = itemTemplate.CloneTree(); | ||||
|                 item.Q<Label>("nameLabel").text = $"Name: {variant.name}"; | ||||
|                 item.Q<Label>("colorLabel").text = $"Color: {variant.color}"; | ||||
|                 item.Q<Label>("lineLabel").text = $"Line: {variant.line}"; | ||||
|  | ||||
|                 string currencySymbol = CurrencySymbols.TryGetValue(variant.currency, out string symbol) | ||||
|                     ? symbol | ||||
|                     : variant.currency; | ||||
|  | ||||
|                 string formattedPrice = $"{currencySymbol}{variant.price.ToString("N2", CultureInfo.InvariantCulture)} {variant.currency}"; | ||||
|                 item.Q<Label>("priceLabel").text = $"Price: {formattedPrice}"; | ||||
|  | ||||
|                 item.Q<Label>("stockLabel").text = $"Stock: {variant.stock}"; | ||||
|  | ||||
|                 var attributesContainer = item.Q<VisualElement>("attributesContainer"); | ||||
|                 if (variant.attributes != null) | ||||
|                 { | ||||
|                     string attributesText = "Attributes:\n"; | ||||
|                     foreach (var kvp in variant.attributes) | ||||
|                     { | ||||
|                         attributesText += $"{kvp.Key}: {kvp.Value}\n"; | ||||
|                     } | ||||
|                     //Debug.Log(attributesText); | ||||
|                     //Debug.Log($"--- ATRIBUTOS PARA: {variant.name} ---"); | ||||
|                     //Debug.Log($"Total keys: {variant.attributes.Count}"); | ||||
|  | ||||
|                     int index = 0; | ||||
|                     foreach (var kvp in variant.attributes) | ||||
|                     { | ||||
|                         //Debug.Log($"[{index}] {kvp.Key}: {kvp.Value}"); | ||||
|  | ||||
|                         string formattedKey = FormatKey(kvp.Key); | ||||
|                         Label attrLabel = new Label($"{formattedKey}: {kvp.Value}"); | ||||
|                         attrLabel.AddToClassList("furniture-attribute"); // estilo opcional | ||||
|                         attrLabel.style.whiteSpace = WhiteSpace.Normal; | ||||
|                         attributesContainer.Add(attrLabel); | ||||
|  | ||||
|                         index++; | ||||
|                     } | ||||
|  | ||||
|                     if (index == 0) | ||||
|                         Debug.Log("⚠️ El diccionario está vacío aunque no es null."); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     Debug.Log($"❌ 'attributes' es NULL para {variant.name}"); | ||||
|                 } | ||||
|  | ||||
|                 scrollView.Add(item); | ||||
|             } | ||||
|         } | ||||
| @@ -59,4 +123,16 @@ class FurnitureFetcher : MonoBehaviour | ||||
|             Debug.LogError("Error: " + request.error); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private string FormatKey(string rawKey) | ||||
|     { | ||||
|         if (string.IsNullOrWhiteSpace(rawKey)) | ||||
|             return ""; | ||||
|  | ||||
|         // Inserta espacio antes de mayúsculas | ||||
|         string spaced = System.Text.RegularExpressions.Regex.Replace(rawKey, "([a-z])([A-Z])", "$1 $2"); | ||||
|  | ||||
|         // Capitaliza primera letra | ||||
|         return char.ToUpper(spaced[0]) + spaced.Substring(1); | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| [Serializable] | ||||
| public class FurnitureIdsRequest | ||||
| @@ -14,6 +15,14 @@ public class FurnitureVariant | ||||
|     public string name; | ||||
|     public string color; | ||||
|     public string line; | ||||
|     public int stock; | ||||
|     public float price; | ||||
|     public string currency; | ||||
|     public string categoryId; | ||||
|     public string providerId; | ||||
|  | ||||
|     [JsonProperty("attributes")] | ||||
|     public Dictionary<string, string> attributes; | ||||
|     public string _id; | ||||
|     public string id; | ||||
|     public string createdAt; | ||||
|   | ||||
| @@ -1,8 +1,11 @@ | ||||
| <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||
|     <ui:Style src="Styles/FurnitureItem.uss" /> | ||||
|     <ui:VisualElement class="furniture-item"> | ||||
|         <ui:Label name="nameLabel" class="furniture-title"/> | ||||
|         <ui:Label name="colorLabel" class="furniture-subtitle"/> | ||||
|         <ui:Label name="lineLabel" class="furniture-subtitle"/> | ||||
|         <ui:Label name="nameLabel" class="furniture-title" /> | ||||
|         <ui:Label name="colorLabel" class="furniture-subtitle" /> | ||||
|         <ui:Label name="lineLabel" class="furniture-subtitle" /> | ||||
|         <ui:Label name="priceLabel" class="furniture-subtitle" /> | ||||
|         <ui:Label name="stockLabel" class="furniture-subtitle" /> | ||||
|         <ui:VisualElement name="attributesContainer" class="attributes-container" /> | ||||
|     </ui:VisualElement> | ||||
| </ui:UXML> | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||
|     <ui:Style src="Styles/MainScreen.uss" /> | ||||
| <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" editor-extension-mode="False"> | ||||
|     <Style src="project://database/Assets/UI%20Toolkit/Styles/MainScreen.uss?fileID=7433441132597879392&guid=1993886af6eb1494f9f4a4f3c968807a&type=3#MainScreen" /> | ||||
|     <ui:VisualElement class="main-screen"> | ||||
|         <ui:Label text="Furniture List" class="title-label" /> | ||||
|         <ui:ScrollView name="furniture-list" style="flex-grow: 1" /> | ||||
|         <ui:ScrollView name="furniture-list" vertical-scroller-visibility="Hidden" horizontal-scroller-visibility="Hidden" style="flex-grow: 1;" /> | ||||
|     </ui:VisualElement> | ||||
| </ui:UXML> | ||||
|   | ||||
| @@ -31,3 +31,17 @@ | ||||
| #FurnitureItemDocumentUI-container{ | ||||
| display:none; | ||||
| } | ||||
|  | ||||
| .attributes-container { | ||||
|     margin-top: 8px; | ||||
| } | ||||
| .attribute-line { | ||||
|     font-size: 12px; | ||||
|     color: #aaaaaa; | ||||
| } | ||||
|  | ||||
| .furniture-attribute { | ||||
|     font-size: 12px; | ||||
|     color: #bbbbbb; | ||||
|     margin-left: 10px; | ||||
| } | ||||
| @@ -6,6 +6,7 @@ | ||||
|     "com.unity.device-simulator.devices": "1.0.0", | ||||
|     "com.unity.feature.development": "1.0.1", | ||||
|     "com.unity.mobile.android-logcat": "1.4.5", | ||||
|     "com.unity.nuget.newtonsoft-json": "3.2.1", | ||||
|     "com.unity.textmeshpro": "3.0.9", | ||||
|     "com.unity.timeline": "1.7.6", | ||||
|     "com.unity.ugui": "1.0.0", | ||||
|   | ||||
| @@ -153,7 +153,7 @@ | ||||
|     }, | ||||
|     "com.unity.nuget.newtonsoft-json": { | ||||
|       "version": "3.2.1", | ||||
|       "depth": 1, | ||||
|       "depth": 0, | ||||
|       "source": "registry", | ||||
|       "dependencies": {}, | ||||
|       "url": "https://packages.unity.com" | ||||
|   | ||||
| @@ -149,14 +149,6 @@ PlayerSettings: | ||||
|   - {fileID: 4800000, guid: b02d09082cbc14569ae46c12d9836abc, type: 3} | ||||
|   - {fileID: 4800000, guid: f40a6c94c8e1b494d93d434ae522a14f, type: 3} | ||||
|   - {fileID: 4800000, guid: 0f45334fa661448c886e4f99ba812a7e, type: 3} | ||||
|   - {fileID: 479186601668263405, guid: e04dc9557a6bcec40b5d8576988f74a3, type: 2} | ||||
|   - {fileID: 11400000, guid: 6bfaaf31cb485f64fb956f1d691a0e1d, type: 2} | ||||
|   - {fileID: -7180841881952691092, guid: 228964b93c232624fac8c242b79176e5, type: 2} | ||||
|   - {fileID: 11400000, guid: 911becadf2ac07e4da62b8afa0b9ea7f, type: 2} | ||||
|   - {fileID: 4800000, guid: b1d4c517492046e1a4e4cc49b0b73b42, type: 3} | ||||
|   - {fileID: 4800000, guid: 2b7e3540272684e0b8e12fed2c4609de, type: 3} | ||||
|   - {fileID: 4800000, guid: c9f956787b1d945e7b36e0516201fc76, type: 3} | ||||
|   - {fileID: 4800000, guid: 0945859e5a1034c2cb6dce53cb4fb899, type: 3} | ||||
|   metroInputSource: 0 | ||||
|   wsaTransparentSwapchain: 0 | ||||
|   m_HolographicPauseOnTrackingLoss: 1 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user