public async Task <Encouragement> GetRandomEncouragement(Mood mood)
        {
            var encouragements = await _database.GetEncouragementsForMoodAsync(mood.Id);

            if (encouragements.Count == 0)
            {
                return(new Encouragement
                {
                    Message = "You're great, but the developer of this app made a mistake."
                });
            }
            var randomIndex = _random.Next(0, encouragements.Count);

            return(encouragements[randomIndex]);
        }
Beispiel #2
0
        async void InitializePropertiesAsync()
        {
            var encouragements = await _encouragementDatabase.GetEncouragementsForMoodAsync(Mood.Id);

            foreach (var encouragement in encouragements)
            {
                Encouragements.Add(new EncouragementEditorViewModel(encouragement));
            }
            // We add the event handler AFTER we load the initial set of encouragements.
            Encouragements.CollectionChanged += async(s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (var removed in e.OldItems.Cast <EncouragementEditorViewModel>().Select(r => r.Encouragement))
                    {
                        await _encouragementDatabase.DeleteEncouragmentAsync(removed);
                    }
                }
            };
            EncouragementCountLabel = $"This mood has {Encouragements.Count} encouragements";
            NotifyPropertyChange(nameof(EncouragementCountLabel));
        }