//Coroutine controlling the list generating (animates if it is an animated list)
    private IEnumerator GenerateListRoutine(JSONNode json, DataBinder prefabOverride)
    {
        IsGenerating = true;

        if (HasContent)
        {
            yield return(StartCoroutine(ClearListRoutine()));
        }

        if (m_isAnimatedList)
        {
            while (Binders.Count > 0)
            {
                yield return(null);
            }
        }

        JSONNode jsonStructure;

        JsonNodeBuilder.GetCompleteNodeStructure(json, m_nodeStructure, out jsonStructure, 0);
        int numberToGenerate = 0;

        if (m_isCapped)
        {
            numberToGenerate = m_maxCount > jsonStructure.Count ? jsonStructure.Count : m_maxCount;
        }

        if (prefabOverride != null)
        {
            m_databinderPrefabToGenerate = prefabOverride;
        }

        Binders      = Generator.GenerateListFromData(jsonStructure, m_databinderPrefabToGenerate, m_holder, numberToGenerate);
        IsGenerating = false;

        if (m_isAnimatedList)
        {
            if (m_databinderPrefabToGenerate.GetComponent <Animator>() == null)
            {
                Debug.LogError($"The prefab {m_databinderPrefabToGenerate.name} is missing an animator and you are trying to animate it");
                yield break;
            }

            if (m_animationTriggerIn.HasValue())
            {
                foreach (DataBinder binder in Binders)
                {
                    binder.GetComponent <Animator>().SetTrigger(m_animationTriggerIn);

                    if (m_delay != 0)
                    {
                        yield return(new WaitForSeconds(m_delay));
                    }
                }
            }
        }
    }
    /// <summary>
    /// Registers new data to the list of databinders. Does not clear list unless the response is not the same size as the current List.
    /// </summary>
    /// <param name="json"></param>
    public void UpdateListInstant(JSONNode json, DataBinder prefabOverride = null)
    {
        JSONNode jsonStructure;

        JsonNodeBuilder.GetCompleteNodeStructure(json, m_nodeStructure, out jsonStructure, 0);
        int numberToGenerate = jsonStructure.Count;

        if (numberToGenerate > 0)
        {
            //find what is lowest, the size fo the requested list or the current list
            if (m_isCapped)
            {
                numberToGenerate = m_maxCount > jsonStructure.Count ? jsonStructure.Count : m_maxCount;
            }

            if (numberToGenerate != Binders.Count)
            {
                ClearListInstant();
                GenerateListInstant(json, prefabOverride);
            }
            else
            {
                //register the new data to all available binders
                for (int i = 0; i < numberToGenerate; i++)
                {
                    //if manually sorted, ignore the order from the data and get the data from the element with the corresponding ID
                    if (m_isManuallySorted && m_sortField.HasValue() && Binders[i].SortID.HasValue())
                    {
                        foreach (JSONNode jsonElement in jsonStructure)
                        {
                            if (jsonElement[m_sortField] == Binders[i].SortID)
                            {
                                Binders[i].RegisterData(jsonElement);
                                Binders[i].BindData();
                                Debug.Log("sort");
                            }
                        }
                    }

                    //otherwise, get the data from the same element in the array
                    else
                    {
                        Binders[i].RegisterData(jsonStructure[i]);
                        Binders[i].BindData();
                    }
                }
            }
        }
    }
    /// <summary>
    /// Registers new data to the list of databinders without clearing the whole list
    /// </summary>
    /// <param name="json"></param>
    public void UpdateList(JSONNode json, bool createNodeStructure = true)
    {
        JSONNode jsonStructure;

        if (createNodeStructure)
        {
            JsonNodeBuilder.GetCompleteNodeStructure(json, m_nodeStructure, out jsonStructure, 0);
        }
        else
        {
            jsonStructure = json;
        }

        if (jsonStructure.Count > 0)
        {
            if (m_isAnimatedList && m_databinderPrefabToGenerate.GetComponent <Animator>() == null)
            {
                Debug.LogError($"The prefab {m_databinderPrefabToGenerate.name} is missing an animator and you are trying to animate it");
            }

            if (m_isAnimatedList && m_animationTriggerUpdate.HasValue() && m_databinderPrefabToGenerate.GetComponent <Animator>() != null)
            {
                StartCoroutine(UpdateWithAnimation(jsonStructure));
                return;
            }
            else
            {
                UpdateListInstant(json);
            }
        }
        else
        {
            if (m_isAnimatedList && m_animationTriggerOut.HasValue() && m_databinderPrefabToGenerate.GetComponent <Animator>() != null)
            {
                StartCoroutine(ClearListRoutine());
                return;
            }
            else
            {
                ClearListInstant();
                Debug.LogWarning("Trying to update from empty list in Json. Update was skipped");
            }
        }
    }
    /// <summary>
    /// Instantly generates a list of databinders based on the supplied json file. Clears current list if already exists
    /// </summary>
    /// <param name="json"></param>
    public void GenerateListInstant(JSONNode json, DataBinder prefabOverride = null)
    {
        if (HasContent)
        {
            ClearListInstant();
        }

        JSONNode jsonStructure;

        JsonNodeBuilder.GetCompleteNodeStructure(json, m_nodeStructure, out jsonStructure, 0);
        int numberToGenerate = 0;

        if (m_isCapped)
        {
            numberToGenerate = m_maxCount > jsonStructure.Count ? jsonStructure.Count : m_maxCount;
        }

        if (prefabOverride != null)
        {
            m_databinderPrefabToGenerate = prefabOverride;
        }

        Binders = Generator.GenerateListFromData(jsonStructure, m_databinderPrefabToGenerate, m_holder, numberToGenerate);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Registers all of the data requested by the keys in all of the connected DataBinders at the node structure for each data registry attached, and adds them to the data dictionary.
    /// </summary>
    /// <param name="json">The full unparsed Json</param>
    /// <param name="index">Represents the index representing the dataBinder's position in a list. Only necessary for use when generating from a list.</param>
    public void RegisterData(JSONNode json, int index = 0)
    {
        m_dataDictionary.Clear();

        //create a new jsonNode for each data registrar using its node structure
        foreach (DataBinderLink binderLink in m_allBinderLinks)
        {
            JSONNode data;
            JsonNodeBuilder.GetCompleteNodeStructure(json, binderLink.NodeStructureSteps, out data, index);

            if (data != null)
            {
                //go into each component binder attached to the data registrar
                foreach (BinderComponent binder in binderLink.ConnectedBinderComponents)
                {
                    //go into each IDataBindable attahced to the component binder
                    foreach (IDataBindable Idata in binder.GetAllBinders())
                    {
                        //if the key is not empty then register it and its data to the data dictionary
                        if (Idata.Key != null && Idata.Key.Length > 0)
                        {
                            if (data[Idata.Key].ToString().ToLower() != "null")
                            {
                                if (!m_dataDictionary.ContainsKey(Idata.Key))
                                {
                                    m_dataDictionary.Add(Idata.Key, data[Idata.Key]);
                                }
                            }
                            else
                            {
                                Debug.LogError($"PAYLOAD ERROR: The key {Idata.Key} returned a null value from the json file. Likely the field {Idata.Key} does not exist at the node structure in the json payload or it has no value.");
                            }
                        }

                        //if the keys array exists and has elements, then go into each key within
                        if (Idata.Keys != null && Idata.Keys.Length > 0)
                        {
                            foreach (string key in Idata.Keys)
                            {
                                //if the key is not empty then register it and its data to the data dictionary
                                if (key != null && key.Length > 0)
                                {
                                    if (data[key].ToString().ToLower() != "null")
                                    {
                                        if (!m_dataDictionary.ContainsKey(key))
                                        {
                                            m_dataDictionary.Add(key, data[key]);
                                        }
                                    }
                                    else
                                    {
                                        Debug.LogError($"PAYLOAD ERROR: The key {key} returned a null value from the json file. Likely the field {key} does not exist at the node structure in the json payload or it has no value.");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }