Skip to content

Commit

Permalink
NR: simplify code by using new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Dec 3, 2023
1 parent 312c7b7 commit 85df5c6
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public AppController(INetworkInfoService networkInfoService, DataController data
this.feedsController = feedsController;
this.settingsController = settingsController;
this.shellViewModel = shellViewModel;
addFeedNavigationItem = new NavigationItem(Resources.AddFeed, "\uf412") { Command = feedsController.AddFeedCommand };
settingsNavigationItem = new NavigationItem(Resources.Settings, "\uf493")
addFeedNavigationItem = new(Resources.AddFeed, "\uf412") { Command = feedsController.AddFeedCommand };
settingsNavigationItem = new(Resources.Settings, "\uf493")
{
Command = new DelegateCommand(() => shellViewModel.Navigate(this.settingsController.SettingsViewModel))
};
shellViewModel.ShowFeedViewCommand = feedsController.ShowFeedViewCommand;
shellViewModel.EditFeedCommand = feedsController.EditFeedCommand;
shellViewModel.RemoveFeedCommand = feedsController.RemoveFeedCommand;
shellViewModel.FooterMenu = new[] { addFeedNavigationItem, settingsNavigationItem };
shellViewModel.FooterMenu = [ addFeedNavigationItem, settingsNavigationItem ];
shellViewModel.PropertyChanged += ShellViewModelPropertyChanged;
shellViewModel.Initialize();
MainView = shellViewModel.View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class DataController
private readonly IMessageService messageService;
private readonly AsyncDelegateCommand signInCommand;
private readonly AsyncDelegateCommand signOutCommand;
private readonly TaskCompletionSource<FeedManager> loadCompletion;
private readonly TaskCompletionSource<FeedManager> loadCompletion = new();
private bool isInitialized;
private bool isInSync;

Expand All @@ -29,9 +29,8 @@ public DataController(ISettingsService settingsService, IDataService dataService
this.networkInfoService = networkInfoService;
this.webStorageService = webStorageService;
this.messageService = messageService;
signInCommand = new AsyncDelegateCommand(SignIn, () => isInitialized);
signOutCommand = new AsyncDelegateCommand(SignOutAsync);
loadCompletion = new TaskCompletionSource<FeedManager>();
signInCommand = new(SignIn, () => isInitialized);
signOutCommand = new(SignOutAsync);
webStorageService.PropertyChanged += WebStorageServicePropertyChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public FeedsController(IMessageService messageService, INavigationService naviga
this.networkInfoService = networkInfoService;
this.launcherService = launcherService;
this.shellViewModel = shellViewModel;
this.addEditFeedViewModel = new Lazy<AddEditFeedViewModel>(() => InitializeViewModel(addEditFeedViewModel.Value));
this.feedViewModel = new Lazy<FeedViewModel>(() => InitializeViewModel(feedViewModel.Value));
this.feedItemViewModel = new Lazy<FeedItemViewModel>(() => InitializeViewModel(feedItemViewModel.Value));
addFeedCommand = new DelegateCommand(AddFeed);
addEditLoadFeedCommand = new AsyncDelegateCommand(AddEditLoadFeed);
addUpdateFeedCommand = new AsyncDelegateCommand(AddUpdateFeed, CanAddUpdateFeed);
showFeedViewCommand = new AsyncDelegateCommand(ShowFeedView);
editFeedCommand = new AsyncDelegateCommand(EditFeed);
removeFeedCommand = new AsyncDelegateCommand(RemoveFeed);
refreshFeedCommand = new AsyncDelegateCommand(Update);
readUnreadCommand = new DelegateCommand(MarkAsReadUnread);
showFeedItemViewCommand = new AsyncDelegateCommand(ShowFeedItemView);
launchBrowserCommand = new AsyncDelegateCommand(LaunchBrowser, CanLaunchBrowser);
this.addEditFeedViewModel = new(() => InitializeViewModel(addEditFeedViewModel.Value));
this.feedViewModel = new(() => InitializeViewModel(feedViewModel.Value));
this.feedItemViewModel = new(() => InitializeViewModel(feedItemViewModel.Value));
addFeedCommand = new(AddFeed);
addEditLoadFeedCommand = new(AddEditLoadFeed);
addUpdateFeedCommand = new(AddUpdateFeed, CanAddUpdateFeed);
showFeedViewCommand = new(ShowFeedView);
editFeedCommand = new(EditFeed);
removeFeedCommand = new(RemoveFeed);
refreshFeedCommand = new(Update);
readUnreadCommand = new(MarkAsReadUnread);
showFeedItemViewCommand = new(ShowFeedItemView);
launchBrowserCommand = new(LaunchBrowser, CanLaunchBrowser);
}

public FeedManager FeedManager { get; set; } = null!;
Expand All @@ -69,22 +69,28 @@ public FeedsController(IMessageService messageService, INavigationService naviga

public async void Run()
{
FeedManager.Feeds.CollectionChanging += FeedsCollectionChanging;
FeedManager.Feeds.CollectionChanged += FeedsCollectionChanged;
await ShowFeedView(FeedManager.Feeds.FirstOrDefault());
await Update();
}

public Task Update() => Task.WhenAll(FeedManager.Feeds.Select(x => LoadFeed(x)));

private void FeedsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
private void FeedsCollectionChanging(object? sender, NotifyCollectionChangedEventArgs e)
{
foreach (Feed x in e.NewItems?.Cast<Feed>() ?? Array.Empty<Feed>()) LoadFeed(x).NoWait();
if ((e.OldItems?.Cast<Feed>() ?? Array.Empty<Feed>()).Any(x => x == feedViewModel.Value.Feed))
var oldItems = e.Action == NotifyCollectionChangedAction.Reset ? FeedManager.Feeds : e.OldItems?.Cast<Feed>();
if (oldItems?.Any(x => x == feedViewModel.Value.Feed) == true)
{
shellViewModel.SelectedFeed = feedViewModel.Value.Feed = null;
}
}

private void FeedsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
foreach (Feed x in e.NewItems?.Cast<Feed>() ?? Array.Empty<Feed>()) LoadFeed(x).NoWait();
}

private async Task LoadFeed(Feed feed, bool ignoreInternetAccessStatus = false)
{
if (!ignoreInternetAccessStatus && !networkInfoService.InternetAccess) return;
Expand Down Expand Up @@ -213,29 +219,29 @@ private void MarkAsReadUnread(object? parameter)

private async Task LaunchBrowser() => await launcherService.LaunchBrowser(feedItemViewModel.Value.FeedItem!.Uri);

private AddEditFeedViewModel InitializeViewModel(AddEditFeedViewModel viewModel)
private AddEditFeedViewModel InitializeViewModel(AddEditFeedViewModel vm)
{
viewModel.LoadFeedCommand = addEditLoadFeedCommand;
viewModel.AddUpdateCommand = addUpdateFeedCommand;
viewModel.PropertyChanged += AddEditFeedViewModelPropertyChanged;
viewModel.FeedChanged += AddEditFeedViewModelFeedChanged;
return viewModel;
vm.LoadFeedCommand = addEditLoadFeedCommand;
vm.AddUpdateCommand = addUpdateFeedCommand;
vm.PropertyChanged += AddEditFeedViewModelPropertyChanged;
vm.FeedChanged += AddEditFeedViewModelFeedChanged;
return vm;
}

private FeedViewModel InitializeViewModel(FeedViewModel viewModel)
private FeedViewModel InitializeViewModel(FeedViewModel vm)
{
viewModel.RefreshCommand = refreshFeedCommand;
viewModel.ReadUnreadCommand = readUnreadCommand;
viewModel.ShowFeedItemViewCommand = showFeedItemViewCommand;
viewModel.PropertyChanged += FeedViewModelPropertyChanged;
return viewModel;
vm.RefreshCommand = refreshFeedCommand;
vm.ReadUnreadCommand = readUnreadCommand;
vm.ShowFeedItemViewCommand = showFeedItemViewCommand;
vm.PropertyChanged += FeedViewModelPropertyChanged;
return vm;
}

private FeedItemViewModel InitializeViewModel(FeedItemViewModel viewModel)
private FeedItemViewModel InitializeViewModel(FeedItemViewModel vm)
{
viewModel.LaunchBrowserCommand = launchBrowserCommand;
viewModel.PropertyChanged += FeedItemViewModelPropertyChanged;
return viewModel;
vm.LaunchBrowserCommand = launchBrowserCommand;
vm.PropertyChanged += FeedItemViewModelPropertyChanged;
return vm;
}

private void AddEditFeedViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public SettingsController(ISettingsService settingsService, DataController dataC
{
appSettings = settingsService.Get<AppSettings>();
this.dataController = dataController;
this.settingsViewModel = new Lazy<SettingsViewModel>(() => InitializeViewModel(settingsViewModel.Value));
enableDeveloperSettingsCommand = new DelegateCommand(() =>
this.settingsViewModel = new(() => InitializeViewModel(settingsViewModel.Value));
enableDeveloperSettingsCommand = new(() =>
{
enableDeveloperSettingsCallCount++;
if (enableDeveloperSettingsCallCount > 2) settingsViewModel.Value.DeveloperSettingsEnabled = true;
Expand All @@ -30,17 +30,17 @@ public SettingsController(ISettingsService settingsService, DataController dataC

public IViewModelCore SettingsViewModel => settingsViewModel.Value;

private SettingsViewModel InitializeViewModel(SettingsViewModel viewModel)
private SettingsViewModel InitializeViewModel(SettingsViewModel vm)
{
viewModel.FeedManager = FeedManager;
viewModel.EnableDeveloperSettingsCommand = enableDeveloperSettingsCommand;
viewModel.SignInCommand = dataController.SignInCommand;
viewModel.SignOutCommand = dataController.SignOutCommand;
viewModel.Languages = new[] { "Auto", "en-US", "de-DE" };
viewModel.SelectedLanguage = appSettings.Language ?? "Auto";
viewModel.PropertyChanged += DeveloperSettingsViewModelPropertyChanged;
viewModel.Initialize();
return viewModel;
vm.FeedManager = FeedManager;
vm.EnableDeveloperSettingsCommand = enableDeveloperSettingsCommand;
vm.SignInCommand = dataController.SignInCommand;
vm.SignOutCommand = dataController.SignOutCommand;
vm.Languages = [ "Auto", "en-US", "de-DE" ];
vm.SelectedLanguage = appSettings.Language ?? "Auto";
vm.PropertyChanged += DeveloperSettingsViewModelPropertyChanged;
vm.Initialize();
return vm;
}

private void DeveloperSettingsViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@

namespace Waf.NewsReader.Applications.DataModels;

public class NavigationItem : Model
public class NavigationItem(string title, string iconGlyph) : Model
{
private string title;
private ICommand? command;
private bool isCommandEnabled;
private string iconGlyph;

public NavigationItem(string title, string iconGlyph)
{
this.title = title;
this.iconGlyph = iconGlyph;
}
public string Title { get => title; set => SetProperty(ref title, value); }

public string Title
{
get => title;
set => SetProperty(ref title, value);
}

public string IconGlyph
{
get => iconGlyph;
set => SetProperty(ref iconGlyph, value);
}
public string IconGlyph { get => iconGlyph; set => SetProperty(ref iconGlyph, value); }

public ICommand? Command
{
Expand All @@ -42,11 +26,7 @@ public ICommand? Command
}
}

public bool IsCommandEnabled
{
get => isCommandEnabled;
set => SetProperty(ref isCommandEnabled, value);
}
public bool IsCommandEnabled { get => isCommandEnabled; set => SetProperty(ref isCommandEnabled, value); }

private void CommandCanExecuteChanged(object? sender, EventArgs e) => IsCommandEnabled = command?.CanExecute(null) ?? false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AddEditFeedViewModel : ViewModelCore<IAddEditFeedView>

public AddEditFeedViewModel(IAddEditFeedView view) : base(view, false)
{
useTitleAsNameCommand = new DelegateCommand(() => Feed!.Name = Feed.Title, () => !string.IsNullOrEmpty(Feed?.Title) && Feed!.Name != Feed.Title);
useTitleAsNameCommand = new(() => Feed!.Name = Feed.Title, () => !string.IsNullOrEmpty(Feed?.Title) && Feed!.Name != Feed.Title);
}

public ICommand LoadFeedCommand { get; internal set; } = null!;
Expand All @@ -27,50 +27,34 @@ public AddEditFeedViewModel(IAddEditFeedView view) : base(view, false)

public event PropertyChangedEventHandler? FeedChanged;

public bool IsEditMode
{
get => isEditMode;
set => SetProperty(ref isEditMode, value);
}
public bool IsEditMode { get => isEditMode; set => SetProperty(ref isEditMode, value); }

public string? FeedUrl
{
get => feedUrl;
set
{
if (SetProperty(ref feedUrl, value))
{
Feed = null;
LoadErrorMessage = null;
}
if (!SetProperty(ref feedUrl, value)) return;
Feed = null;
LoadErrorMessage = null;
}
}

public Feed? OldFeed
{
get => oldFeed;
set => SetProperty(ref oldFeed, value);
}
public Feed? OldFeed { get => oldFeed; set => SetProperty(ref oldFeed, value); }

public Feed? Feed
{
get => feed;
set
{
var oldFeed = feed;
if (SetProperty(ref feed, value))
{
if (oldFeed != null) oldFeed.PropertyChanged -= FeedPropertyChanged;
if (feed != null) feed.PropertyChanged += FeedPropertyChanged;
}
if (!SetProperty(ref feed, value)) return;
if (oldFeed != null) oldFeed.PropertyChanged -= FeedPropertyChanged;
if (feed != null) feed.PropertyChanged += FeedPropertyChanged;
}
}

public string? LoadErrorMessage
{
get => loadErrorMessage;
set => SetProperty(ref loadErrorMessage, value);
}
public string? LoadErrorMessage { get => loadErrorMessage; set => SetProperty(ref loadErrorMessage, value); }

protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@

namespace Waf.NewsReader.Applications.ViewModels;

public class FeedItemViewModel : ViewModelCore<IFeedItemView>
public class FeedItemViewModel(IFeedItemView view) : ViewModelCore<IFeedItemView>(view, false)
{
private FeedItem? feedItem;

public FeedItemViewModel(IFeedItemView view) : base(view, false)
{
}

public ICommand LaunchBrowserCommand { get; set; } = null!;

public FeedItem? FeedItem
{
get => feedItem;
set => SetProperty(ref feedItem, value);
}
public FeedItem? FeedItem { get => feedItem; set => SetProperty(ref feedItem, value); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private bool FilterFeedItems(FeedItem item) => string.IsNullOrEmpty(SearchText)

private void UpdateItemsListView()
{
ItemsListView = new ObservableGroupedListView<DateTime, FeedItem>(Feed?.Items ?? (IReadOnlyList<FeedItem>)Array.Empty<FeedItem>(),
x => x.GroupBy(y => y.Date.LocalDateTime.Date)) { Filter = FilterFeedItems };
ItemsListView = new(Feed?.Items ?? (IReadOnlyList<FeedItem>)[], x => x.GroupBy(y => y.Date.LocalDateTime.Date)) { Filter = FilterFeedItems };
RaisePropertyChanged(nameof(ItemsListView));
}
}
Loading

0 comments on commit 85df5c6

Please sign in to comment.