adding Toggle control (iOS / Android)

This commit is contained in:
Alex Blount 2019-06-28 10:15:12 -07:00
Родитель 7697ee9a15
Коммит ae0004befd
8 изменённых файлов: 176 добавлений и 2 удалений

Просмотреть файл

@ -51,7 +51,11 @@ namespace HotUI.Samples {
{
OnClick = () => { state.Text = $"Click Count: {clickCount.Value++}"; }
},
}
},
new Toggle(() =>state.CanEdit)
{
IsOnChanged = (e) => state.CanEdit = e
}
}
}
};

Просмотреть файл

@ -0,0 +1,17 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotUI.Samples", "HotUI.Samples.csproj", "{379AF22B-C8AB-4AC5-B178-2F1D32C2C975}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{379AF22B-C8AB-4AC5-B178-2F1D32C2C975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{379AF22B-C8AB-4AC5-B178-2F1D32C2C975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{379AF22B-C8AB-4AC5-B178-2F1D32C2C975}.Release|Any CPU.ActiveCfg = Release|Any CPU
{379AF22B-C8AB-4AC5-B178-2F1D32C2C975}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,57 @@
using System;
using AToggle = Android.Widget.ToggleButton;
using AView = Android.Views.View;
namespace HotUI.Android
{
public class ToggleHandler : AToggle, IView
{
public ToggleHandler() : base(AndroidContext.CurrentContext)
{
Click += HandleClick;
}
private void HandleClick(object sender, EventArgs e) => toggle?.IsOnChanged?.Invoke(this.Checked);
public AView View => this;
Toggle toggle;
public void Remove(View view)
{
toggle = null;
}
public void SetView(View view)
{
toggle = view as Toggle;
this.UpdateProperties(toggle);
}
public void UpdateValue(string property, object value)
{
this.UpdateProperty(property, value);
}
}
public static partial class ControlExtensions
{
public static void UpdateProperties(this AToggle view, Toggle hView)
{
view.Checked = hView?.IsOn ?? false;
view.UpdateBaseProperties(hView);
}
public static bool UpdateProperty(this AToggle view, string property, object value)
{
switch (property)
{
case nameof(Toggle.IsOn):
view.Checked = (bool)value;
return true;
}
return view.UpdateBaseProperty(property, value);
}
}
}

Просмотреть файл

@ -63,6 +63,7 @@
<Compile Include="UI.cs" />
<Compile Include="Handlers\ListViewHandler.cs" />
<Compile Include="Handlers\ViewHandler.cs" />
<Compile Include="Handlers\ToggleHandler.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />

Просмотреть файл

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using UIKit;
namespace HotUI.iOS
{
public class ToggleHandler : UISwitch, IUIView
{
private static readonly PropertyMapper<Toggle, UISwitch> Mapper = new PropertyMapper<Toggle, UISwitch>(new Dictionary<string, Func<UISwitch, Toggle, bool>>()
{
[nameof(Toggle.IsOn)] = MapIsOnProperty
});
public ToggleHandler()
{
this.ValueChanged += HandleValueChanged;
}
void HandleValueChanged(object sender, EventArgs e) => toggle?.IsOnChanged?.Invoke(On);
public UIView View => this;
Toggle toggle;
public void Remove(View view)
{
toggle = null;
}
public void SetView(View view)
{
toggle = view as Toggle;
Mapper.UpdateProperties(this, toggle);
}
public void UpdateValue(string property, object value)
{
Mapper.UpdateProperty(this, toggle, property);
}
public static bool MapIsOnProperty(UISwitch nativeView, Toggle virtualView)
{
nativeView.On = virtualView.IsOn;
return true;
}
}
}

Просмотреть файл

@ -64,6 +64,7 @@
<Compile Include="HotUIViewController.cs" />
<Compile Include="Handlers\ViewHandler.cs" />
<Compile Include="Handlers\ContentViewHandler.cs" />
<Compile Include="Handlers\ToggleHandler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HotUI\HotUI.csproj">

Просмотреть файл

@ -14,7 +14,8 @@ namespace HotUI.iOS {
Registrar.Handlers.Register<Button, ButtonHandler> ();
Registrar.Handlers.Register<TextField, TextFieldHandler> ();
Registrar.Handlers.Register<Text, TextHandler> ();
Registrar.Handlers.Register<Stack, StackHandler> ();
Registrar.Handlers.Register<Toggle, ToggleHandler>();
Registrar.Handlers.Register<Stack, StackHandler> ();
Registrar.Handlers.Register<VStack, VStackHandler> ();
Registrar.Handlers.Register<HStack, HStackHandler> ();
//Registrar.Handlers.Register<WebView, WebViewHandler> ();

Просмотреть файл

@ -0,0 +1,47 @@
using System;
namespace HotUI
{
public class Toggle : View
{
public Toggle(bool isOn)
{
IsOn = isOn;
}
public Toggle(Func<bool> builder)
{
IsOnBinding = builder;
}
private bool isOn;
public bool IsOn
{
get => isOn;
set => this.SetValue(State, ref isOn, value, ViewPropertyChanged);
}
public Func<bool> IsOnBinding { get; private set; }
protected override void WillUpdateView()
{
base.WillUpdateView();
if (IsOnBinding != null)
{
State.StartProperty();
var on = IsOnBinding.Invoke();
var props = State.EndProperty();
var propCount = props.Length;
if (propCount > 0)
{
State.BindingState.AddViewProperty(props, (s, o) => IsOn = IsOnBinding.Invoke());
}
IsOn = on;
}
}
public Action<bool> IsOnChanged { get; set; }
}
}