Пример #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);
            }
        }
Пример #2
0
        private void UpdateBindedCollection()
        {
            if (ShowCategory == CategoryEnum.All)
            {
                if (Device.RuntimePlatform == Device.Android)
                {
                    FilteredCollection.Clear();
                }
                else
                {
                    FilteredCollection = new ObservableCollection <JokeItem>();
                }

                foreach (var item in JokeList)
                {
                    FilteredCollection.Add(item);
                }
            }
            else
            {
                if (Device.RuntimePlatform == Device.Android)
                {
                    FilteredCollection.Clear();
                }
                else
                {
                    FilteredCollection = new ObservableCollection <JokeItem>();
                }

                foreach (var item in JokeList.Where(joke => joke.Category == ShowCategory))
                {
                    FilteredCollection.Add(item);
                }
            }
        }
Пример #3
0
        public async void DeleteJoke(JokeItem joke)
        {
            Scroll = ScrollEnum.NoScroll;

            JokeList.Remove(joke);
            UpdateBindedCollection();

            if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.UWP)
            {
                await _databaseService.DeleteJoke(joke);
            }
        }
        public async Task <JokeList> SearchJoke([FromQuery] string query)
        {
            try
            {
                JokeList joke = await REST.RestAPI.GetAsync <JokeList>(REST.RestAPI.BaseURL + $"search?query={query}");

                return(joke);
            }
            catch (System.Exception)
            {
                throw;
            }
        }
Пример #5
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); }
        }