예제 #1
0
        public ChannelsViewModel(
            ICategoriesRepository categoriesRepository,
            ITranslationsService translationsService,
            INavigationService navigationService,
            IFactoryService factoryService,
            IDialogService dialogService)
        {
            (IsEmpty, IsLoading) = (false, true);
            Items       = new ObservableCollection <ChannelCategoryViewModel>();
            OpenSearch  = new ObservableCommand(navigationService.Navigate <SearchViewModel>);
            AddCategory = new ObservableCommand(async() =>
            {
                var name = await dialogService.ShowDialogForResults(
                    translationsService.Resolve("EnterNameOfNewCategory"),
                    translationsService.Resolve("EnterNameOfNewCategoryTitle"));
                if (string.IsNullOrWhiteSpace(name))
                {
                    return;
                }
                var category = new Category {
                    Title = name
                };
                await categoriesRepository.InsertAsync(category);
                Items.Add(factoryService.CreateInstance <
                              ChannelCategoryViewModel>(category, this));
            });
            Load = new ObservableCommand(async() =>
            {
                IsLoading.Value = true;
                var categories  = await categoriesRepository.GetAllAsync();
                Items.Clear();
                foreach (var category in categories)
                {
                    Items.Add(factoryService.CreateInstance <
                                  ChannelCategoryViewModel>(category, this));
                }
                IsEmpty.Value   = Items.Count == 0;
                IsLoading.Value = false;

                // Subscribe on collection changed to perform items rearranging.
                Items.CollectionChanged += async(s, a) =>
                {
                    IsEmpty.Value = Items.Count == 0;
                    var items     = Items.Select(i => i.Category.Value);
                    await categoriesRepository.RearrangeAsync(items);
                };
            });
        }
예제 #2
0
 public FeedViewModel(
     ICategoriesRepository categoriesRepository,
     INavigationService navigationService,
     IFactoryService factoryService)
 {
     (IsEmpty, IsLoading) = (false, true);
     Selected             = new ObservableProperty <FeedCategoryViewModel>();
     OpenSources          = new ObservableCommand(navigationService.Navigate <ChannelsViewModel>);
     Items = new ObservableCollection <FeedCategoryViewModel>();
     Load  = new ObservableCommand(async() =>
     {
         IsEmpty.Value   = false;
         IsLoading.Value = true;
         Items.Clear();
         var categories = await categoriesRepository.GetAllAsync();
         foreach (var category in categories)
         {
             Items.Add(factoryService.CreateInstance <
                           FeedCategoryViewModel>(category));
         }
         Selected.Value  = Items.FirstOrDefault();
         IsEmpty.Value   = Items.Count == 0;
         IsLoading.Value = false;
     });
 }
예제 #3
0
 public FeedCategoryViewModel(
     INavigationService navigationService,
     IFeedStoreService feedStoreService,
     IFactoryService factoryService,
     Category category)
 {
     Title = category.Title;
     (IsLoading, IsEmpty) = (true, false);
     Items       = new ObservableCollection <ArticleViewModel>();
     OpenSources = new ObservableCommand(navigationService.Navigate <ChannelsViewModel>);
     Fetch       = new ObservableCommand(async() =>
     {
         IsLoading.Value = true;
         var sources     = category.Channels;
         var response    = await feedStoreService.LoadAsync(sources);
         Items.Clear();
         foreach (var article in response.Item2)
         {
             Items.Add(factoryService.CreateInstance <
                           ArticleViewModel>(article));
         }
         IsEmpty.Value   = Items.Count == 0;
         IsLoading.Value = false;
     });
 }
예제 #4
0
        public async Task Navigate <T>(object arg) where T : class
        {
            switch (typeof(T).Name)
            {
            case nameof(FeedViewModel):
            case nameof(FaveViewModel):
            case nameof(SearchViewModel):
            case nameof(ChannelsViewModel):
            case nameof(SettingsViewModel):
                NavigateFrame(GetChild <Frame>(Window.Current.Content, 0));
                break;

            case nameof(MenuViewModel):
                NavigateFrame((Frame)Window.Current.Content);
                break;

            case nameof(ArticleViewModel) when arg is Guid guid:
                var article = await _categoriesRepository.GetArticleByIdAsync(guid);

                if (article == null)
                {
                    return;
                }
                var viewModel = _factoryService.CreateInstance <ArticleViewModel>(article);
                if (GetChild <Frame>(Window.Current.Content, 1) == null)
                {
                    await Navigate <FeedViewModel>();
                }
                await Task.Delay(150);
                await Navigate <ArticleViewModel>(viewModel);

                break;

            case nameof(ArticleViewModel):
                NavigateFrame(GetChild <Frame>(Window.Current.Content, 1));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            void NavigateFrame(Frame frame)
            {
                frame.Navigate(Pages[typeof(T)], arg);
                ((Page)frame.Content).DataContext = arg;
                RaiseNavigated(arg);
            }
        }
예제 #5
0
 public SearchViewModel(
     IFactoryService factoryService,
     ISearchService searchService)
 {
     SearchQuery = string.Empty;
     (IsGreeting, IsEmpty, IsLoading) = (true, false, false);
     Items = new ObservableCollection <SearchItemViewModel>();
     Fetch = new ObservableCommand(async() =>
     {
         IsLoading.Value   = true;
         var query         = SearchQuery.Value;
         var searchResults = await searchService.SearchAsync(query);
         IsGreeting.Value  = false;
         Items.Clear();
         foreach (var feedlyItem in searchResults.Results)
         {
             Items.Add(factoryService.CreateInstance <
                           SearchItemViewModel>(feedlyItem));
         }
         IsEmpty.Value   = Items.Count == 0;
         IsLoading.Value = false;
     });
 }
예제 #6
0
        public FaveViewModel(
            IFavoritesRepository favoritesReposirory,
            IFactoryService factoryService)
        {
            (IsEmpty, IsLoading) = (false, true);
            var longDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;

            Items = new ObservableCollection <ObservableGrouping <string, ArticleViewModel> >();
            Load  = new ObservableCommand(async() =>
            {
                IsLoading.Value = true;
                var articles    = await favoritesReposirory.GetAllAsync();
                Items.Clear();
                var groupings = articles
                                .Select(i => factoryService.CreateInstance <ArticleViewModel>(i))
                                .OrderByDescending(i => i.PublishedDate.Value)
                                .GroupBy(i => i.PublishedDate.Value.ToString(longDatePattern))
                                .Select(i => new ObservableGrouping <string, ArticleViewModel>(i))
                                .ToList();

                groupings.ForEach(Items.Add);
                foreach (var grouping in groupings)
                {
                    foreach (var viewModel in grouping)
                    {
                        viewModel.IsFavorite.PropertyChanged += (o, args) => RemoveOrRestore(viewModel);
                    }
                }

                IsEmpty.Value   = Items.Count == 0;
                IsLoading.Value = false;
            });
            OrderByDate = new ObservableCommand(() =>
            {
                IsLoading.Value = true;
                var groupings   = Items
                                  .SelectMany(i => i)
                                  .OrderByDescending(i => i.PublishedDate.Value)
                                  .GroupBy(i => i.PublishedDate.Value.ToString(longDatePattern))
                                  .Select(i => new ObservableGrouping <string, ArticleViewModel>(i))
                                  .ToList();

                Items.Clear();
                groupings.ForEach(Items.Add);
                IsLoading.Value = false;
            });
            OrderByFeed = new ObservableCommand(() =>
            {
                IsLoading.Value = true;
                var groupings   = Items
                                  .SelectMany(i => i)
                                  .OrderBy(i => i.Feed.Value)
                                  .GroupBy(i => i.Feed.Value.ToString())
                                  .Select(i => new ObservableGrouping <string, ArticleViewModel>(i))
                                  .ToList();

                Items.Clear();
                groupings.ForEach(Items.Add);
                IsLoading.Value = false;
            });
            void RemoveOrRestore(ArticleViewModel viewModel)
            {
                if (!viewModel.IsFavorite.Value)
                {
                    var related = Items.First(i => i.Contains(viewModel));
                    related.Remove(viewModel);
                    if (related.Count == 0)
                    {
                        Items.Remove(related);
                    }
                }
                else
                {
                    const string restored = "*Restored";
                    var          existing = Items.FirstOrDefault(i => i.Key == restored);
                    if (existing == null)
                    {
                        Items.Add(new ObservableGrouping <
                                      string, ArticleViewModel>(restored, new[] { viewModel }));
                    }
                    else
                    {
                        existing.Add(viewModel);
                    }
                }
            }
        }
예제 #7
0
        public ChannelCategoryViewModel(
            ICategoriesRepository categoriesRepository,
            ITranslationsService translationsService,
            IFactoryService factoryService,
            IDialogService dialogService,
            ChannelsViewModel channelsViewModel,
            Category category)
        {
            Category  = category;
            Title     = category.Title;
            SourceUri = string.Empty;

            Items          = new ObservableCollection <ChannelViewModel>();
            RenameCategory = new ObservableCommand(async() =>
            {
                var name = await dialogService.ShowDialogForResults(
                    translationsService.Resolve("EnterNameOfNewCategory"),
                    translationsService.Resolve("EnterNameOfNewCategoryTitle"));
                if (string.IsNullOrWhiteSpace(name))
                {
                    return;
                }
                category.Title = name;
                await categoriesRepository.UpdateAsync(category);
                category.Title = Title.Value = name;
            });
            RemoveCategory = new ObservableCommand(async() =>
            {
                var shouldDelete = await dialogService.ShowDialogForConfirmation(
                    translationsService.Resolve("DeleteCategory"),
                    translationsService.Resolve("DeleteElement"));
                if (!shouldDelete)
                {
                    return;
                }
                await categoriesRepository.RemoveAsync(category);
                channelsViewModel.Items.Remove(this);
            });
            AddSource = new ObservableCommand(async() =>
            {
                var sourceUri = SourceUri.Value;
                if (string.IsNullOrWhiteSpace(sourceUri) ||
                    !Uri.IsWellFormedUriString(sourceUri, UriKind.Absolute))
                {
                    return;
                }
                SourceUri.Value = string.Empty;
                var model       = new Channel {
                    Uri = sourceUri, Notify = true
                };
                await categoriesRepository.InsertChannelAsync(category, model);
                Items.Add(factoryService.CreateInstance <
                              ChannelViewModel>(model, this));
            });
            Load = new ObservableCommand(() =>
            {
                Items.Clear();
                foreach (var channel in category.Channels)
                {
                    Items.Add(factoryService.CreateInstance <
                                  ChannelViewModel>(channel, this));
                }
            });
        }