Skip to content

Commit

Permalink
#8 : Add settings window
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver254 committed Jun 12, 2022
1 parent 3913787 commit af80af5
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 145 deletions.
18 changes: 18 additions & 0 deletions src/Monbsoft.Feeader.Avalonia/Models/FeedContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Monbsoft.Feeader.Avalonia.Models
{
public class FeedContext
{
public FeedContext(IEnumerable<Feed> feeds)
{
Feeds = feeds.ToList();
}

public List<Feed> Feeds { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
<PackageReference Include="WebViewControl-Avalonia" Version="2.91.25" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
</ItemGroup>
<ItemGroup>
<Compile Update="Views\ChangeFeedView.axaml.cs">
<DependentUpon>ChangeFeedView.axaml</DependentUpon>
</Compile>
</ItemGroup>
</Project>
61 changes: 0 additions & 61 deletions src/Monbsoft.Feeader.Avalonia/ViewModels/AddFeedViewModel.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Monbsoft.Feeader.Avalonia/ViewModels/ArticleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ namespace Monbsoft.Feeader.Avalonia.ViewModels
{
public class ArticleViewModel : ViewModelBase
{
private Uri? _pictureUri;
private Bitmap? _picture;
private Uri? _pictureUri;
private Feed? _selectedFeed;

public ArticleViewModel(Article article)
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public async Task LoadPictureAsync(CancellationToken cancellationToken)
if (_pictureUri != null)
{
Picture = await PictureService.LoadPictureBitmapAsync(_pictureUri, cancellationToken);
}
}
}
}
}
110 changes: 110 additions & 0 deletions src/Monbsoft.Feeader.Avalonia/ViewModels/ChangeFeedViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Monbsoft.Feeader.Avalonia.Models;
using Monbsoft.Feeader.Avalonia.Services;
using NLog.Fluent;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;

namespace Monbsoft.Feeader.Avalonia.ViewModels
{
public class ChangeFeedViewModel : ViewModelBase
{
private FeedContext _context;
private string _name;
private string _url;
private Feed? _selectedFeed;
private FeedState _state;

public ChangeFeedViewModel(FeedContext context)
{
this.WhenAnyValue(x => x.Url)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Throttle(TimeSpan.FromMilliseconds(400))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(DoSearch);

this.WhenAnyValue(x => x.SelectedFeed)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(f =>
{
Name = f?.Name;
Url = f?.Link;
_state = f != null ? FeedState.Change : FeedState.Add;
});

Feeds = new ObservableCollection<Feed>(context.Feeds);
_state = FeedState.Add;
_context = context;

AddCommand = ReactiveCommand.Create(() =>
{
if(_state == FeedState.Change && _selectedFeed != null)
{
_context.Feeds.Remove(_selectedFeed);
}
_context.Feeds.Add(new Feed(Name!, Url!));
return _context;
});

}

/// <summary>
/// A
/// </summary>
public ReactiveCommand<Unit, FeedContext> AddCommand { get; }
/// <summary>
/// Gets the feeds
/// </summary>
public ObservableCollection<Feed> Feeds { get; }
/// <summary>
/// Gets the name of the feed
/// </summary>
public string? Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
/// <summary>
/// Gets or sets the selected feed
/// </summary>
public Feed? SelectedFeed
{
get => _selectedFeed;
set => this.RaiseAndSetIfChanged(ref _selectedFeed, value);
}
/// <summary>
/// Gets the url of the feed
/// </summary>
public string? Url
{
get => _url;
set => this.RaiseAndSetIfChanged(ref _url, value);
}

private async void DoSearch(string? url)
{
try
{
if (!string.IsNullOrEmpty(url))
{
var feed = await FeedService.GetFeedDataAsync(url);
Name = feed.Name;
}
}
catch(Exception ex)
{
Log.Error(ex.Message);
}
}

enum FeedState
{
Add = 0,
Change = 1
}

}
}
16 changes: 7 additions & 9 deletions src/Monbsoft.Feeader.Avalonia/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
using Monbsoft.Feeader.Avalonia.Services;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Monbsoft.Feeader.Avalonia.ViewModels;
Expand All @@ -23,13 +21,12 @@ public class MainWindowViewModel : ViewModelBase
public MainWindowViewModel()
{

ShowDialog = new Interaction<AddFeedViewModel, Feed?>();
ShowDialog = new Interaction<SettingsWindowViewModel, MainWindowViewModel>();

AddFeedCommand = ReactiveCommand.CreateFromTask(async () =>
ShowSettingsCommand = ReactiveCommand.CreateFromTask(async () =>
{
var store = new AddFeedViewModel();
var store = new SettingsWindowViewModel(new FeedContext(Feeds));
var feed = await ShowDialog.Handle(store);
AddFeed(feed);
});

this.WhenAnyValue(x => x.SelectedFeed)
Expand All @@ -39,7 +36,7 @@ public MainWindowViewModel()
RxApp.MainThreadScheduler.Schedule(LoadFeedsAsync);
}

public ICommand AddFeedCommand { get; }
public ICommand ShowSettingsCommand { get; }
public ObservableCollection<ArticleViewModel> Articles { get; } = new ();
public ObservableCollection<Feed> Feeds { get; } = new();
public Feed? SelectedFeed
Expand All @@ -52,8 +49,8 @@ public ArticleViewModel? SelectedArticle
get => _selectedArticle;
set => this.RaiseAndSetIfChanged(ref _selectedArticle, value);
}
public Interaction<AddFeedViewModel, Feed?> ShowDialog { get; }

public Interaction<SettingsWindowViewModel, MainWindowViewModel> ShowDialog { get; }
public async void LoadFeedsAsync()
{
FeedService.InitializeCache();
Expand All @@ -72,6 +69,7 @@ private async void AddFeed(Feed? feed)
return;
Feeds.Add(feed);
await FeedService.SaveAsync(Feeds.ToList());
Debug.WriteLine($"Feed {feed?.Name} added");
}
private async void LoadArticles(Feed feed)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Monbsoft.Feeader.Avalonia.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Monbsoft.Feeader.Avalonia.ViewModels
{
public class SettingsWindowViewModel : ViewModelBase
{
private readonly FeedContext _feedContext;

public SettingsWindowViewModel(FeedContext context)
{
ChangeFeedViewModel = new ChangeFeedViewModel(context);
}

public ChangeFeedViewModel ChangeFeedViewModel { get; }
}
}
27 changes: 0 additions & 27 deletions src/Monbsoft.Feeader.Avalonia/Views/AddFeedWindow.axaml

This file was deleted.

37 changes: 15 additions & 22 deletions src/Monbsoft.Feeader.Avalonia/Views/ArticleView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Monbsoft.Feeader.Avalonia.Views.ArticleView">
<UserControl.Resources>
<SolidColorBrush x:Key="ImageBackground" Color="Black" Opacity="0.8"/>
</UserControl.Resources>
<Grid>

<StackPanel Background="{DynamicResource ImageBackground}"
VerticalAlignment="Bottom">
<Image Source="{Binding Picture}"
Stretch="Fill"
VerticalAlignment="Center" />
<StackPanel Background="{DynamicResource ImageBackground}"
VerticalAlignment="Bottom">
<TextBlock Classes="h1"
Foreground="White"
Text="{Binding Title}"
TextAlignment="Center"
TextWrapping="Wrap" />
<TextBlock Foreground="White"
Margin="10,0"
MaxLines="2"
Text="{Binding Summary}"
TextTrimming="CharacterEllipsis"
TextWrapping="Wrap" />
</StackPanel>
</Grid>
HorizontalAlignment="Stretch"
MaxHeight="300"/>
<TextBlock Classes="h1"
Margin="0,0,0,10"
Text="{Binding Title}"
TextAlignment="Center"
TextWrapping="Wrap" />
<TextBlock Margin="10,0"
MaxLines="2"
Text="{Binding Summary}"
TextTrimming="CharacterEllipsis"
TextWrapping="Wrap" />
</StackPanel>
</UserControl>
Loading

0 comments on commit af80af5

Please sign in to comment.