Example #1
0
        //GetRenamedItem is called when the text is changed on an entry.
        // if the item is used by multiple entries, split it up into 2,
        // if there is already an item in the cache by the new name, update thatone, and return it
        // otherwise, update the item's text, and return it.
        public static FoodItem GetRenamedItem(FoodItem source, string Text, Entry entry)
        {
            if (source.Text == Text)
            {
                return(source);
            }

            Debug.Assert(source.Text != Text);
            Debug.Assert(source.NotifyEntries.Contains(entry));

            FoodItem existing = null;

            if (itemCache.TryGetValue(Text, out existing))
            {
                existing.CopyDetailsFrom(source);
                source.NotifyEntries.Remove(entry);
                if (source.NotifyEntries.Count == 0)
                {
                    itemCache.Remove(source.Text);
                }
                existing.NotifyEntries.Add(entry);
                return(existing);
            }

            if (source.NotifyEntries.Count > 1)
            {
                FoodItem newitem = GetItem(Text);                 // adds it to the cache
                newitem.CopyDetailsFrom(source);
                source.NotifyEntries.Remove(entry);
                newitem.NotifyEntries.Add(entry);
                return(newitem);
            }

            // rename existing item, in the cache and in the item
            itemCache.Remove(source.Text);
            source.Text = Text;
            itemCache.Add(Text, source);

            return(source);
        }
Example #2
0
        public static void LoadDay(DateTime date, String Xml)
        {
            if (Xml != null)
            {
                Serializer s = Serializer.FromXML(Xml);

                foreach (var p in s.Select("Period"))
                {
                    Period period = p.Key.ParsePeriod();

                    List <Entry> entries  = new List <Entry> ();
                    DateTime     modified = DateTime.MinValue;
                    if (p.Read("LastChanged") != null)
                    {
                        modified = DateTime.Parse(p.Read("LastChanged"));
                    }
                    String note = p.Read("Note");
                    String time = p.Read("Time");

                    foreach (var e in p.Select("Entry"))
                    {
                        var NewEntry = new Entry(date, period, e.Key);

                        NewEntry.AmountSelectedDB = e.Read("Amount");
                        NewEntry.AmountScaleDB    = Floats.ParseStorage(e.Read("AmountScale"));
                        NewEntry.Date             = date;
                        NewEntry.Period           = period;

                        FoodItem item = new FoodItem(e.Key, true);
                        item.NutritionDB    = e.Read("Properties");
                        item.ServingSizesDB = e.Read("Amount") + "=" + e.Read("Grams");
                        Cache.MergeItem(item);
                        entries.Add(NewEntry);
                    }

                    Cache.MergeMeal(date, period, entries, modified, note, time);
                }
            }
        }
Example #3
0
        private static void SaveDay(DateTime date)
        {
            try {
                var d = new Serializer("Date", date.ToStorageStringDate());

                List <Entry>[] PeriodEntries = Cache.GetEntryCache(date);

                foreach (Period period in PeriodList.All)
                {
                    if (Cache.ShouldSavePeriod(date, period))
                    {
                        var p = d.Add("Period", period.ToString());
                        p.Write("LastChanged", DateTime.Now.ToStorageStringFull());
                        p.Write("Note", Cache.GetPeriodNote(date, period));
                        p.Write("Time", Cache.GetPeriodTime(date, period));

                        foreach (Entry entry in PeriodEntries[(int)period])
                        {
                            FoodItem item = entry.Item;

                            p.Add("Entry", entry.Text)
                            .Write("Amount", entry.AmountSelectedDB)
                            .Write("AmountScale", entry.AmountScaleDB.ToStorageString())
                            .Write("Grams", item.ServingSizes.GetEquivalent(entry.AmountSelectedDB).ToStorageString())
                            .Write("Properties", item.NutritionDB);
                        }
                    }
                }

                string Xml = d.GetXML();

                // Store in LocalDB
                string Container  = date.ToStorageStringMonth();
                string DocumentID = date.ToStorageStringDate();
                LocalDB.WriteAsync(Container, "Day", DocumentID, Xml);
            } catch (Exception ex) {
                LittleWatson.ReportException(ex);
            }
        }
Example #4
0
 public static FoodItem GetItemById(FoodItemType type, int ItemId)
 {
     FoodItem result = null; AllItems.TryGetValue(GetCacheID(type, ItemId), out result); return(result);
 }
Example #5
0
        public static void MergeItem(FoodItem item)
        {
            if (itemCache == null)
            {
                itemCache = new Dictionary <string, FoodItem>();
            }

            FoodItem existing = item;

            if (!itemCache.TryGetValue(item.Text, out existing))
            {
                itemCache.Add(item.Text, item);
            }
            else
            if (item != existing)
            {
                if (item.IsShallowCopy)
                {
                                                #if !DEBUG
                    //todo: consider merging the serving size of shallow items; should they always exist in the item
                    //(imagine an old item with that selection, what happens to it when the serving size gets deleted later?)
                                                #endif

                    // for shallow items cache contents take precedence
                    if (existing.Culture == null)
                    {
                        existing.Culture = item.Culture;
                    }
                    if (existing.DescriptionDB == null)
                    {
                        existing.DescriptionDB = item.DescriptionDB;
                    }
                    if (existing.LastAmountDB == null)
                    {
                        existing.LastAmountDB = item.LastAmountDB;
                    }
                    if (existing.NutritionDB == null)
                    {
                        existing.NutritionDB = item.NutritionDB; existing.ResetNutrition();
                    }
                    if (existing.ServingSizesDB == null)
                    {
                        existing.ServingSizesDB = item.ServingSizesDB; existing.ResetServingsizes();
                    }
                    if (existing.SourceID == null)
                    {
                        existing.SourceID = item.SourceID;
                    }
                }
                else
                {
                    // deep items take precedence when merging in
                    if (item.Culture != null)
                    {
                        existing.Culture = item.Culture;
                    }
                    if (item.DescriptionDB != null)
                    {
                        existing.DescriptionDB = item.DescriptionDB;
                    }
                    if (item.LastAmountDB != null)
                    {
                        existing.LastAmountDB = item.LastAmountDB;
                    }
                    if (item.NutritionDB != null)
                    {
                        existing.NutritionDB = item.NutritionDB; existing.ResetNutrition();
                    }
                    if (item.ServingSizesDB != null)
                    {
                        existing.ServingSizesDB = item.ServingSizesDB; existing.ResetServingsizes();
                    }
                    if (item.SourceID != null)
                    {
                        existing.SourceID = item.SourceID;
                    }
                }

                // if we're trying to add an item to the cache that was already there, make sure all entries now point to the existing item
                if (item.NotifyEntries.Count > 0)
                {
                    foreach (Entry entry in item.NotifyEntries.ToArray())
                    {
                        entry.ResetItem();
                    }
                }
            }
        }