Exemple #1
0
        public ChuckJokesPageViewModel(IJokeService jokeService,
                                       IDatabaseService databaseService,
                                       ISettingsService settingsService,
                                       IToastMessage toastMessage)
        {
            _jokeService     = jokeService;
            _databaseService = databaseService;
            _settingsService = settingsService;
            _toastMessage    = toastMessage;

            OnAddJokeCommand    = new DelegateCommand(AddJokeAsync, CanAddJokeAsync);
            OnCellTappedCommand = new DelegateCommand <string>(ReadJokeAsync, CanReadJokeAsync);
            OnDeleteJokeCommand = new DelegateCommand <JokeItem>(DeleteJoke);

            LoadUrl();

            var dbJokes = _databaseService.GetJokes();

            if (dbJokes != null)
            {
                foreach (var item in dbJokes)
                {
                    JokeList.Add(item);
                }
            }

            UpdateBindedCollection();

            if (!ExtensionMethods.IsConnected())
            {
                _toastMessage.ShowToast(notConnectedMessage);
            }
        }
Exemple #2
0
        public async void AddJokeAsync()
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    Scroll = ScrollEnum.Scroll;
                    var actualCategory = (Category == 0) ? (CategoryEnum)random.Next(1, 16) : Category;

                    var joke = await _jokeService.GetJokeAsync(url, actualCategory.ToString().ToLower()); //if category is "All", select random one

                    if (joke != null)
                    {
                        int id = 1;

                        if (JokeList.Count > 0)
                        {
                            id = JokeList.Last().Id + 1;
                        }

                        var jokeItem = new JokeItem
                        {
                            Id       = id,
                            Icon     = iconUrl,
                            Joke     = joke.value,
                            Url      = new Uri(joke.url),
                            RestId   = joke.icon_url,
                            Category = actualCategory
                        };

                        JokeList.Add(jokeItem);
                        UpdateBindedCollection();

                        if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.UWP)
                        {
                            await _databaseService.AddJoke(jokeItem);
                        }
                    }
                    else
                    {
                        _toastMessage.ShowToast(commonErrorMessage);
                    }
                }
                else
                {
                    _toastMessage.ShowToast(apiErrorMessage);
                }
            }
            catch { _toastMessage.ShowToast(commonErrorMessage); }
        }