Пример #1
0
        public WordViewModel(Word word)
            : base(word)
        {
            m_model = word;

            Definitions = m_model.Definitions.Select(d => new DefinitionViewModel(d)).ToList();
        }
        private List<Element> ExtractElementsFromData(JObject data)
        {
            List<Element> elements = new List<Element>();

            JArray jaElements = (JArray)data["elements"];

            foreach (var e in jaElements.Children())
            {
                Element newElement = null;

                switch (e["type"].ToString())
                {
                    case "word":

                        List<Definition> definitions = new List<Definition>();
                        foreach (var def in (JArray)e["definitions"])
                        {
                            definitions.Add(new Definition()
                            {
                                Details = def["details"].ToString(),
                                Content = def["content"].ToString()
                            });
                        }

                        newElement = new Word()
                        {
                            Id = Convert.ToInt32(e["id"].ToString()),
                            Date = Convert.ToDateTime(e["date"].ToString()),
                            Title = e["title"].ToString(),
                            IsFavorite = false,
                            VoteCount = Convert.ToInt32(e["voteCount"].ToString()),
                            Author = e["author"].ToString(),
                            AuthorInfo = e["authorInfo"].ToString(),
                            Definitions = definitions
                        };

                        break;

                    case "contrepeterie":
                        newElement = new Contrepeterie()
                        {
                            Id = Convert.ToInt32(e["id"].ToString()),
                            Date = Convert.ToDateTime(e["date"].ToString()),
                            Title = e["title"].ToString(),
                            IsFavorite = false,
                            VoteCount = Convert.ToInt32(e["voteCount"].ToString()),
                            Author = e["author"].ToString(),
                            AuthorInfo = e["authorInfo"].ToString(),
                            Content = e["content"].ToString(),
                            Solution = e["solution"].ToString()
                        };
                        break;

                    default:
                        throw new ArgumentException("Unknow type: " + e["type"]);
                }

                elements.Add(newElement);
            }

            // Sort by date
            elements.Sort(delegate(Element e1, Element e2) { return e2.Date.CompareTo(e1.Date); });

            return elements;
        }
        private void launchFakeUpdater()
        {
            m_launchTime = DateTime.Now;

            var worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler((o, a) =>
            {
                while (true)
                {
                    Thread.Sleep(15000);

                    IsAvailable = false;

                    m_fakeCount++;

                    var rand = new Random(DateTime.Now.Millisecond);
                    int n = rand.Next(2);

                    Element randelement = null;

                    // Create a random word
                    if (n % 2 == 0)
                    {
                        randelement = new Word()
                        {
                            Id = m_fakeCount,
                            Definitions = new List<Definition>()
                            {
                                new Definition(){
                                    Content = "Definition 1 blabablla",
                                    Details = "bla blabla blabla blabla blabla blabla blabla blabla blabla bla bla bla bla bla"
                                },
                                new Definition(){
                                    Content = "Definition 1 blabablla",
                                    Details = "bla blabla blabla blabla blabla blabla blabla blabla blabla bla bla bla bla bla"
                                },
                            },
                            Title = "Generated Word no " + m_fakeCount,
                            Date = DateTime.Now,
                            VoteCount = 42,
                            Author = "Dam",
                            AuthorInfo = "Tocard"
                        };

                        ((Word)randelement).Definitions.ForEach(d =>
                        {
                            d.Word = randelement as Word;
                        });
                    }
                    // Or create a random contrepeterie
                    else
                    {
                        randelement = new Contrepeterie()
                        {
                            Id = m_fakeCount,
                            Title = "Generated CTP no " + m_fakeCount,
                            Content = "Bla bla bllllaaa dsfsdfsdfsd",
                            Solution = "!!!!!!!!!!!!!!!!!!!!!!!!!",
                            Date = DateTime.Now,
                            VoteCount = 42,
                            Author = "Dam",
                            AuthorInfo = "Tocard"
                        };
                    }

                    m_elements.Insert(0, randelement);

                    IsAvailable = true;
                    Debug.WriteLine("Generated: " + randelement.Title);
                }

            });
            worker.RunWorkerAsync();
        }
Пример #4
0
        /// <summary>
        /// Parse the XML response of the webservice
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private List<Element> parseResponse(string xml)
        {
            List<Element> elements = new List<Element>();

            XDocument xdoc = XDocument.Parse(xml);

            foreach (var element in xdoc.Descendants("element"))
            {
                try
                {
                    int id = Convert.ToInt32(element.Element("id").Value);
                    string type = element.Element("type").Value;
                    DateTime date = Convert.ToDateTime(element.Element("date").Value);
                    string title = element.Element("title").Value.Replace(@"\", "");
                    int voteCount = Convert.ToInt32(element.Element("voteCount").Value);
                    string author = element.Element("author").Value;
                    string authorInfo = element.Element("authorInfo").Value;

                    Element newElement = null;

                    switch (type)
                    {
                        case "mot":

                            List<Definition> definitions = new List<Definition>();
                            foreach (XElement defXml in element.Element("definitions").Elements("definition"))
                            {
                                Definition newDef = new Definition()
                                {
                                    Details = defXml.Element("details").Value.Replace(@"\", ""),
                                    Content = defXml.Element("content").Value.Replace(@"\", ""),
                                };

                                definitions.Add(newDef);
                            }

                            Word w = new Word()
                            {
                                Id = id,
                                Date = date,
                                Title = title,
                                IsFavorite = false,
                                IsRead = false,
                                VoteCount = voteCount,
                                Author = author,
                                AuthorInfo = authorInfo,
                                Definitions = definitions
                            };

                            newElement = w;

                            break;

                        case "contrepétrie":

                            Contrepeterie c = new Contrepeterie()
                            {
                                Id = id,
                                Date = date,
                                Title = title,
                                IsFavorite = false,
                                IsRead = false,
                                VoteCount = voteCount,
                                Author = author,
                                AuthorInfo = authorInfo,
                                Content = element.Element("content").Value.Replace(@"\", ""),
                                Solution = element.Element("solution").Value.Replace(@"\", ""),
                            };

                            newElement = c;

                            break;

                        default:
            #if DEBUG
                            Debug.WriteLine("WARNING: Ignored element " + id);
            #endif
                            continue;
                    }

                    elements.Add(newElement);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("ERROR: (parsing) " + ex);
                    continue;
                }
            }

            // Link definitions
            elements.Where(e => e is Word).ToList().ForEach(w => ((Word)w).Definitions.ForEach(d =>
               {
               d.Word = w as Word;
               }));

            Debug.WriteLine("--- WS: Parse complete (" + elements.Count + ")");

            return elements;
        }