Skip to content

Commit

Permalink
#137 Avalonia UI completed, has some bugs on tab change.
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkAlex committed Jun 9, 2019
1 parent 2fe6083 commit bb280da
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
14 changes: 11 additions & 3 deletions MangaReader.Avalonia/View/ProxySetting.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:view="clr-namespace:MangaReader.Avalonia.View;assemlby=MangaReader.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MangaReader.Avalonia.View.ProxySetting">
<StackPanel>
<StackPanel Margin="10">
<Grid>
<Grid.Styles>
<Style Selector="TextBlock">
Expand All @@ -19,6 +20,9 @@
<Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}"/>
</Style>
</Grid.Styles>
<Grid.Resources>
<view:UriConverter x:Key="UriConverter"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
Expand All @@ -40,7 +44,7 @@
<Button Content="+" Command="{Binding Add}"/>
<Button Content="-" Command="{Binding Remove}"/>
</StackPanel>
<Grid Grid.Column="1" Grid.Row="0" DataContext="{Binding SelectedProxySettingModel}">
<Grid Grid.Column="1" Grid.Row="0" Margin="10" DataContext="{Binding SelectedProxySettingModel}">
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
Expand All @@ -57,7 +61,7 @@


<TextBlock Grid.Row="1" Grid.Column="0" Text="Адрес прокси: " />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Address}" IsEnabled="{Binding IsManual}"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Address, Converter={StaticResource UriConverter}}" IsEnabled="{Binding IsManual}"/>

<TextBlock Grid.Row="2" Grid.Column="0" Text="Имя пользователя: " />
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding UserName}" IsEnabled="{Binding IsManual}"/>
Expand All @@ -77,5 +81,9 @@
Content="Проверить"/>
</Grid>
</Grid>
<WrapPanel Margin="0, 10, 0, 10" HorizontalAlignment="Right">
<Button Command="{Binding Save}" Content="Save"/>
<Button Command="{Binding UndoChanged}" Content="Cancel"/>
</WrapPanel>
</StackPanel>
</UserControl>
37 changes: 37 additions & 0 deletions MangaReader.Avalonia/View/TypeUriConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;

namespace MangaReader.Avalonia.View
{
public class UriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(string) && value is Uri uri)
return uri.OriginalString;

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string uriString && targetType == typeof(Uri))
{
if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute))
{
return new Uri(uriString, UriKind.Absolute);
}

if (Uri.IsWellFormedUriString(uriString, UriKind.Relative))
{
return new Uri(uriString, UriKind.Relative);
}

return new Uri(uriString, UriKind.RelativeOrAbsolute);
}

return null;
}
}
}
5 changes: 0 additions & 5 deletions MangaReader.Avalonia/ViewModel/Command/DelegateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,5 @@ public DelegateCommand(Func<Task> execute, Func<bool> canExecute)
this.taskExecute = execute;
this.canExecute = canExecute;
}

public DelegateCommand(Func<Task> execute, Func<bool> canExecute, Action<DelegateCommand> action) : this(execute, canExecute)
{
action(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ public string TestAddress

public ICommand Save { get; }

public ICommand Undo { get; }
public ICommand UndoChanged { get; }

public override Task OnSelected(ExplorerTabViewModel previousModel)
{
ReloadData();
return base.OnSelected(previousModel);
}

private void ReloadData()
{
using (var context = Repository.GetEntityContext())
{
Expand All @@ -56,7 +62,6 @@ public override Task OnSelected(ExplorerTabViewModel previousModel)
.Select(s => new ProxySettingModel(s)));
this.SelectedProxySettingModel = this.ProxySettingModels.FirstOrDefault();
}
return base.OnSelected(previousModel);
}

public ProxySettingSelectorModel()
Expand All @@ -78,9 +83,18 @@ public ProxySettingSelectorModel()
models.Remove(selected);
this.SelectedProxySettingModel = next;
return Task.CompletedTask;
}, () => this.SelectedProxySettingModel?.IsManual == true, SubscribeToCommand(nameof(SelectedProxySettingModel)));
this.Test = new DelegateCommand(TestAddressImpl, () => !string.IsNullOrWhiteSpace(this.TestAddress), SubscribeToCommand(nameof(TestAddress)));
}, () => this.SelectedProxySettingModel?.IsManual == true);
this.Test = new DelegateCommand(TestAddressImpl, () => !string.IsNullOrWhiteSpace(this.TestAddress));
this.Save = new DelegateCommand(SaveImpl, () => true);
this.UndoChanged = new DelegateCommand(ReloadData);

this.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(SelectedProxySettingModel))
this.Remove.OnCanExecuteChanged();
if (args.PropertyName == nameof(TestAddress))
this.Test.OnCanExecuteChanged();
};
this.testAddress = "https://github.com/MonkAlex/MangaReader";
}

Expand Down
15 changes: 1 addition & 14 deletions MangaReader.Avalonia/ViewModel/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using MangaReader.Avalonia.ViewModel.Command;
using System.Runtime.CompilerServices;
using ReactiveUI;

namespace MangaReader.Avalonia.ViewModel
Expand All @@ -17,15 +14,5 @@ public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
IReactiveObjectExtensions.RaisePropertyChanged(this, propertyName);
}

protected System.Action<DelegateCommand> SubscribeToCommand(string propertyName)
{
return command =>
this.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == propertyName)
command.OnCanExecuteChanged();
};
}
}
}

0 comments on commit bb280da

Please sign in to comment.