Skip to content

Commit

Permalink
Got it working with iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
davefxy committed May 12, 2023
1 parent 6e6eb8b commit df70514
Show file tree
Hide file tree
Showing 66 changed files with 238 additions and 1,650 deletions.
4 changes: 2 additions & 2 deletions AutoSuggestBox/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Maui.AutoSuggestBox;

/// <summary>
/// This class contains CustomSwitch <see cref="MauiAppBuilder"/> extensions.
/// This class contains AutoSuggestBox <see cref="MauiAppBuilder"/> extensions.
/// </summary>
public static class AppBuilderExtensions
{
/// <summary>
/// Initializes the Switch control
/// Initializes the AutoSuggestBox control
/// </summary>
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/>.</param>
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="AutoSuggestBox"/>.</returns>
Expand Down
29 changes: 10 additions & 19 deletions AutoSuggestBox/Controls/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AutoSuggestBox : View, IAutoSuggestBox
/// </summary>
public AutoSuggestBox()
{
Unloaded += OnAutoSuggestBoxUnloaded;
//Unloaded += OnAutoSuggestBoxUnloaded;
}

/// <summary>
Expand Down Expand Up @@ -51,7 +51,6 @@ private static void OnTextPropertyChanged(BindableObject bindable, object oldVal
{
var box = (AutoSuggestBox)bindable;
if (!box.suppressTextChangedEvent) //Ensure this property changed didn't get call because we were updating it from the native text property
// box.TextChanged?.Invoke(box, new AutoSuggestBoxTextChangedEventArgs(AutoSuggestBoxTextChangeReason.ProgrammaticChange));
box.textChangedEventManager.HandleEvent(box, new AutoSuggestBoxTextChangedEventArgs("", AutoSuggestBoxTextChangeReason.ProgrammaticChange), nameof(TextChanged));
}

Expand Down Expand Up @@ -205,14 +204,11 @@ public System.Collections.IList ItemsSource
public void RaiseSuggestionChosen(AutoSuggestBoxSuggestionChosenEventArgs args)
{
suggestionChosenEventManager.HandleEvent(this, args, nameof(SuggestionChosen));
//SuggestionChosen?.Invoke(this, new AutoSuggestBoxSuggestionChosenEventArgs(selectedItem));
}

/// <summary>
/// Raised before the text content of the editable control component is updated.
/// </summary>
//public event EventHandler<AutoSuggestBoxSuggestionChosenEventArgs> SuggestionChosen;

public event EventHandler<AutoSuggestBoxTextChangedEventArgs> TextChanged
{
add => textChangedEventManager.AddEventHandler(value);
Expand All @@ -225,41 +221,36 @@ public void NativeControlTextChanged(AutoSuggestBoxTextChangedEventArgs args)
suppressTextChangedEvent = true; //prevent loop of events raising, as setting this property will make it back into the native control
Text = args.Text;
suppressTextChangedEvent = false;
//TextChanged?.Invoke(this, new AutoSuggestBoxTextChangedEventArgs(reason));
textChangedEventManager.HandleEvent(this, args, nameof(TextChanged));

if (TextChangedCommand?.CanExecute(args.Text) ?? false)
{
TextChangedCommand.Execute(args.Text);
}
//if (TextChangedCommand?.CanExecute(args.Text) ?? false)
//{
// TextChangedCommand.Execute(args.Text);
//}
}

/// <summary>
/// Raised after the text content of the editable control component is updated.
/// </summary>
//public event EventHandler<AutoSuggestBoxTextChangedEventArgs> TextChanged;
public void RaiseQuerySubmitted(AutoSuggestBoxQuerySubmittedEventArgs args)
{
//QuerySubmitted?.Invoke(this, new AutoSuggestBoxQuerySubmittedEventArgs(queryText, chosenSuggestion));
querySubmittedEventManager.HandleEvent(this, args, nameof(QuerySubmitted));
}

/// <summary>
/// Executes QuerySubmitted event
/// Occurs when the user submits a search query.
/// </summary>
//public event EventHandler<AutoSuggestBoxQuerySubmittedEventArgs> QuerySubmitted;

public event EventHandler<AutoSuggestBoxQuerySubmittedEventArgs> QuerySubmitted
{
add => querySubmittedEventManager.AddEventHandler(value);
remove => querySubmittedEventManager.RemoveEventHandler(value);
}

void OnAutoSuggestBoxUnloaded(object? sender, EventArgs e)
{
Unloaded -= OnAutoSuggestBoxUnloaded;
Handler?.DisconnectHandler();
}
//void OnAutoSuggestBoxUnloaded(object? sender, EventArgs e)
//{
// Unloaded -= OnAutoSuggestBoxUnloaded;
// Handler?.DisconnectHandler();
//}
}
}
15 changes: 6 additions & 9 deletions AutoSuggestBox/Handlers/AutoSuggestBoxHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public partial class AutoSuggestBoxHandler : ViewHandler<IAutoSuggestBox, AutoSu
{
/// <inheritdoc />
//protected override AutoSuggestBoxView CreatePlatformView() => new AutoSuggestBoxView(Context);

protected override AutoSuggestBoxView CreatePlatformView() => new(Context);
protected override void ConnectHandler(AutoSuggestBoxView platformView)
{
Expand Down Expand Up @@ -53,12 +52,10 @@ public static void MapTextColor(AutoSuggestBoxHandler handler, IAutoSuggestBox v
}
public static void MapPlaceholderText(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
//handler.PlatformView.Hint = view.PlaceholderText;
handler.PlatformView.PlaceholderText = view.PlaceholderText;
}
public static void MapPlaceholderTextColor(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
//handler.PlatformView?.SetHintTextColor(view.PlaceholderTextColor.ToPlatform());
handler.PlatformView?.SetPlaceholderTextColor(view.PlaceholderTextColor);
}
public static void MapTextMemberPath(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
Expand Down Expand Up @@ -91,16 +88,16 @@ private void UpdateTextColor(AutoSuggestBoxView platformView)
var color = VirtualView?.TextColor;
platformView.SetTextColor(color.ToPlatform());
}
private void UpdateDisplayMemberPath(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
handler.PlatformView.SetItems(view?.ItemsSource?.OfType<object>(), (o) => FormatType(o, view?.DisplayMemberPath), (o) => FormatType(o, view?.TextMemberPath));
}
private void UpdatePlaceholderTextColor(AutoSuggestBoxView platformView)
{
var placeholderColor = VirtualView?.PlaceholderTextColor;
platformView.SetPlaceholderTextColor(placeholderColor);
}

private void UpdateDisplayMemberPath(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
handler.PlatformView.SetItems(view ?.ItemsSource?.OfType<object>(), (o) => FormatType(o, view?.DisplayMemberPath), (o) => FormatType(o, view?.TextMemberPath));
}
private void UpdatePlaceholderText(AutoSuggestBoxView platformView) => platformView.PlaceholderText = VirtualView?.PlaceholderText;

private void UpdateIsEnabled(AutoSuggestBoxView platformView)
{
Expand All @@ -119,4 +116,4 @@ private static string FormatType(object instance, string memberPath)
else
return instance?.ToString() ?? "";
}
}
}
91 changes: 55 additions & 36 deletions AutoSuggestBox/Handlers/AutoSuggestBoxHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ protected override void ConnectHandler(AutoSuggestBoxView platformView)
{
base.ConnectHandler(platformView);

//UpdateTextColor(platformView);
//UpdatePlaceholderText(platformView);
//UpdatePlaceholderTextColor(platformView);
//UpdateIsEnabled(platformView);
PlatformView.Text = VirtualView.Text ?? string.Empty;
PlatformView.Frame = new RectangleF(0, 20, 320, 50);

UpdateTextColor(platformView);
UpdatePlaceholderText(platformView);
UpdatePlaceholderTextColor(platformView);
UpdateDisplayMemberPath(platformView);
UpdateIsEnabled(platformView);
platformView.UpdateTextOnSelect = VirtualView.UpdateTextOnSelect;
platformView.IsSuggestionListOpen = VirtualView.IsSuggestionListOpen;

UpdateItemsSource(platformView);

platformView.SuggestionChosen += OnPlatformViewSuggestionChosen;
platformView.TextChanged += OnPlatformViewTextChanged;
platformView.QuerySubmitted += OnPlatformViewQuerySubmitted;
//platformView.UpdateTextOnSelect = e.NewElement.UpdateTextOnSelect;
//platformView.IsSuggestionListOpen = e.NewElement.IsSuggestionListOpen;
//Control.EditingDidBegin += Control_EditingDidBegin;
//Control.EditingDidEnd += Control_EditingDidEnd;
//Frame = new RectangleF(0, 20, 320, 40);

PlatformView.EditingDidBegin += Control_EditingDidBegin;
PlatformView.EditingDidEnd += Control_EditingDidEnd;
}
protected override void DisconnectHandler(AutoSuggestBoxView platformView)
{
platformView.SuggestionChosen -= OnPlatformViewSuggestionChosen;
platformView.TextChanged -= OnPlatformViewTextChanged;
platformView.QuerySubmitted -= OnPlatformViewQuerySubmitted;
PlatformView.EditingDidBegin -= Control_EditingDidBegin;
PlatformView.EditingDidEnd -= Control_EditingDidEnd;

platformView.Dispose();
base.DisconnectHandler(platformView);
Expand All @@ -53,27 +61,37 @@ private void OnPlatformViewQuerySubmitted(object? sender, AutoSuggestBoxQuerySub
VirtualView?.RaiseQuerySubmitted(e);
}

static readonly int baseHeight = 10;
static readonly int baseHeight = 20;
/// <inheritdoc />
//public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
//{
// var baseResult = base.GetDesiredSize(widthConstraint, heightConstraint);
// var testString = new Foundation.NSString("Tj");
// var testSize = testString.GetSizeUsingAttributes(new UIStringAttributes { Font = Control.Font });
// double height = baseHeight + testSize.Height;
// height = Math.Round(height);

// return new SizeRequest(new global::Xamarin.Forms.Size(baseResult.Request.Width, height));
//}

//private void Control_EditingDidBegin(object sender, EventArgs e)
//{
// Element?.SetValue(VisualElement.IsFocusedPropertyKey, true);
//}
//private void Control_EditingDidEnd(object sender, EventArgs e)
//{
// Element?.SetValue(VisualElement.IsFocusedPropertyKey, false);
//}
public override Microsoft.Maui.Graphics.Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
var baseResult = base.GetDesiredSize(widthConstraint, heightConstraint);
var testString = new Foundation.NSString("Tj");
var testSize = testString.GetSizeUsingAttributes(new UIStringAttributes { Font = PlatformView.Font });
double height = baseHeight + testSize.Height;
height = Math.Round(height);
if (double.IsInfinity(widthConstraint) || double.IsInfinity(heightConstraint))
{
// If we drop an infinite value into base.GetDesiredSize for the Editor, we'll
// get an exception; it doesn't know what do to with it. So instead we'll size
// it to fit its current contents and use those values to replace infinite constraints

PlatformView.SizeToFit();
var sz = new Microsoft.Maui.Graphics.Size(PlatformView.Frame.Width, PlatformView.Frame.Height);
return sz;
}

return base.GetDesiredSize(baseResult.Width, height);
}

void Control_EditingDidBegin(object sender, EventArgs e)
{
VirtualView.IsFocused = true;
}
void Control_EditingDidEnd(object sender, EventArgs e)
{
VirtualView.IsFocused = false;
}

public static void MapText(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
Expand All @@ -87,7 +105,6 @@ public static void MapTextColor(AutoSuggestBoxHandler handler, IAutoSuggestBox v
}
public static void MapPlaceholderText(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
//handler.PlatformView.Hint = view.PlaceholderText;
handler.PlatformView.PlaceholderText = view.PlaceholderText;
}
public static void MapPlaceholderTextColor(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
Expand All @@ -100,7 +117,7 @@ public static void MapTextMemberPath(AutoSuggestBoxHandler handler, IAutoSuggest
}
public static void MapDisplayMemberPath(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
handler.PlatformView.SetItems(view?.ItemsSource?.OfType<object>(), (o) => FormatType(o, view?.DisplayMemberPath), (o) => FormatType(o, view?.TextMemberPath));
handler.PlatformView.SetItems(view?.ItemsSource?.OfType<object>(), (o) => FormatType(o, view.DisplayMemberPath), (o) => FormatType(o, view.TextMemberPath));
}
public static void MapIsSuggestionListOpen(AutoSuggestBoxHandler handler, IAutoSuggestBox view)
{
Expand All @@ -121,19 +138,21 @@ public static void MapItemsSource(AutoSuggestBoxHandler handler, IAutoSuggestBox

private void UpdateTextColor(AutoSuggestBoxView platformView)
{
var color = VirtualView?.TextColor;
platformView.SetTextColor(color);
platformView.SetTextColor(VirtualView?.TextColor);
}
private void UpdateDisplayMemberPath(AutoSuggestBoxView platformView)
{
platformView.SetItems(VirtualView.ItemsSource?.OfType<object>(), (o) => FormatType(o, VirtualView.DisplayMemberPath), (o) => FormatType(o, VirtualView.TextMemberPath));
}
private void UpdatePlaceholderTextColor(AutoSuggestBoxView platformView)
{
var placeholderColor = VirtualView?.PlaceholderTextColor;
platformView.SetPlaceholderTextColor(placeholderColor);
platformView.SetPlaceholderTextColor(VirtualView?.PlaceholderTextColor);
}
private void UpdatePlaceholderText(AutoSuggestBoxView platformView) => platformView.PlaceholderText = VirtualView?.PlaceholderText;

private void UpdateIsEnabled(AutoSuggestBoxView platformView)
{
platformView.UserInteractionEnabled = (bool)(VirtualView?.IsEnabled);
platformView.UserInteractionEnabled = (bool)(VirtualView.IsEnabled);
}

private void UpdateItemsSource(AutoSuggestBoxView platformView)
Expand Down
2 changes: 0 additions & 2 deletions AutoSuggestBox/Handlers/NativeAutoSuggestBox.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ protected override void ReplaceText(ICharSequence text)
//Override to avoid updating textbox on itemclick. We'll do this later using TextMemberPath and raise the proper TextChanged event then
}



/// <summary>
/// Raised after the text content of the editable control component is updated.
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions AutoSuggestBox/Handlers/NativeAutoSuggestBox.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private void SuggestionTableSource_TableRowSelected(object sender, TableRowSelec

private void InputText_EditingChanged(object sender, EventArgs e)
{
TextChanged?.Invoke(this, new AutoSuggestBoxTextChangedEventArgs("", AutoSuggestBoxTextChangeReason.UserInput));
TextChanged?.Invoke(this, new AutoSuggestBoxTextChangedEventArgs(this.Text, AutoSuggestBoxTextChangeReason.UserInput));
IsSuggestionListOpen = true;
}

Expand Down Expand Up @@ -299,7 +299,7 @@ public virtual void SetTextColor(Color color)
}

/// <summary>
/// Raised after the text content of the editable control component is updated.
/// Raised after the text config of the editable control component is updated.
/// </summary>
public event EventHandler<AutoSuggestBoxTextChangedEventArgs> TextChanged;

Expand All @@ -309,7 +309,7 @@ public virtual void SetTextColor(Color color)
public event EventHandler<AutoSuggestBoxQuerySubmittedEventArgs> QuerySubmitted;

/// <summary>
/// Raised before the text content of the editable control component is updated.
/// Raised before the text config of the editable control component is updated.
/// </summary>
public event EventHandler<AutoSuggestBoxSuggestionChosenEventArgs> SuggestionChosen;

Expand All @@ -334,11 +334,11 @@ public override UITableViewCell GetCell(UITableView tableView, NSIndexPath index

var item = _items.ElementAt(indexPath.Row);

//cell.TextLabel.Text = _labelFunc(item);
cell.TextLabel.Text = _labelFunc(item);
#pragma warning disable CA1416
var content = cell.DefaultContentConfiguration;
content.Text = _labelFunc(item);
cell.ContentConfiguration = content;
//var config = cell.DefaultContentConfiguration;
//config.Text = _labelFunc(item);
//cell.ContentConfiguration = config;
#pragma warning restore CA1416
return cell;
}
Expand Down
9 changes: 3 additions & 6 deletions AutoSuggestBox/Maui.AutoSuggestBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<NeutralLanguage>en</NeutralLanguage>
<LangVersion>latest</LangVersion>
<GitInfoReportImportance>high</GitInfoReportImportance>
<PackageId>AutoSuggestBox</PackageId>
<PackageId>MauiAutoSuggestBox</PackageId>
<Summary>A control that allows you to design any type of switch/ toggle for .NET MAUI. It's also 100% accessible.</Summary>
<Authors>Morten Nielsen</Authors>
<Copyright>Morten Nielsen</Copyright>
Expand All @@ -45,11 +45,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-android|AnyCPU'">
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
<PropertyGroup Condition="'$(TargetFramework)'=='net7.0-ios'">
<ProvisioningType>manual</ProvisioningType>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-android')) != true">
Expand Down
25 changes: 25 additions & 0 deletions AutoSuggestBox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# XamarinFormsControls

I took the source for this project from the original DotMorten project at (https://github.com/dotMorten/XamarinFormsControls)
I wanted a Maui version. So I modified the source to and migrated it to .Net Maui. It only supports Android and iOS, 2 types of hardware I posses.
This was and exercise in how to migrate a Xamarin.Forms control to a .Net Maui control using the Maui Handler recipe. It took a number of days
to get this sorted out. I know that it works for my needs. I did what I needed to get it to work. I am sure there is some unused code that can be removed.
Someone familiar with Maui handlers and.or iOS code or Android code can make improvements.
I tired of trying to find a comparable .Net Maui version of this AutoSuggestion box that I had used in Xamarin.Forms. I wanted to finish migrating my apps
from Xamarin.Forms to .Net Maui. After migrating this control and the Microcharts control, I was able to complete the redesign.
Recently I replaced all of the <Frame> with <Border> in my apps Xaml and understand how to specify shadows and colors so I am very happy. I do like
.Net Maui now more than Xamarin.Forms. Yes, you need to make desigh changes but in fact it isnt that much of a stretch.

So many thanks to dotMorten for creating this control so long ago.

## Sponsoring

If you like this library and use it a lot, consider sponsoring me. Anything helps and encourages me to keep going.

See here for details: https://github.com/sponsors/dotMorten

### Controls

- [AutoSuggestBox](AutoSuggestBox/) : Represents a text control that makes suggestions to users as they type. The app is notified when text has been changed by the user and is responsible for providing relevant suggestions for this control to display.
![autosuggestbox](https://user-images.githubusercontent.com/1378165/51137780-42b30b80-17f4-11e9-8ac1-7b129fc3d9ee.gif)

Loading

0 comments on commit df70514

Please sign in to comment.