Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save slodge/2655928 to your computer and use it in GitHub Desktop.
Save slodge/2655928 to your computer and use it in GitHub Desktop.
Some autocomplete stuff...
using System;
using System.Reflection;
using Cirrious.MvvmCross.Binding.Android.Views;
using Cirrious.MvvmCross.Binding.Bindings.Target;
using Cirrious.MvvmCross.Binding.Interfaces;
using Cirrious.MvvmCross.Interfaces.Platform.Diagnostics;
namespace Cirrious.MvvmCross.Binding.Android.Target
{
public class MvxAutoCompleteTextViewConstraintTargetBinding : MvxPropertyInfoTargetBinding<MvxBindableAutoCompleteTextView>
{
public MvxAutoCompleteTextViewConstraintTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo)
{
var autoComplete = View;
if (autoComplete == null)
{
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - AutoComplete is null in MvxAutoCompleteTextViewConstraintTargetBinding");
}
else
{
autoComplete.ConstraintChanged += AutoCompleteOnConstraintChanged;
}
}
private void AutoCompleteOnConstraintChanged(object sender, EventArgs eventArgs)
{
FireValueChanged(View.Constraint);
}
public override MvxBindingMode DefaultMode
{
get
{
return MvxBindingMode.OneWayToSource;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
var autoComplete = View;
if (autoComplete != null)
{
autoComplete.ConstraintChanged -= AutoCompleteOnConstraintChanged;
}
}
}
}
}
#region Copyright
// <copyright file="MvxBindableAutoCompleteTextView.cs" company="Cirrious">
// (c) Copyright Cirrious. http://www.cirrious.com
// This source is subject to the Microsoft Public License (Ms-PL)
// Please see license.txt on http://opensource.org/licenses/ms-pl.html
// All other rights reserved.
// </copyright>
//
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com
#endregion
using System;
using System.Collections;
using Android.Content;
using Android.Util;
using Android.Widget;
namespace Cirrious.MvvmCross.Binding.Android.Views
{
public class MvxBindableAutoCompleteTextView
: AutoCompleteTextView
{
public MvxBindableAutoCompleteTextView(Context context, IAttributeSet attrs)
: this(context, attrs, new MvxFilteringBindableListAdapter(context))
{
}
public MvxBindableAutoCompleteTextView(Context context, IAttributeSet attrs, MvxFilteringBindableListAdapter adapter)
: base(context, attrs)
{
var itemTemplateId = MvxBindableListViewHelpers.ReadTemplatePath(context, attrs);
adapter.ItemTemplateId = itemTemplateId;
Adapter = adapter;
}
public new MvxFilteringBindableListAdapter Adapter
{
get { return base.Adapter as MvxFilteringBindableListAdapter; }
set
{
var existing = Adapter;
if (existing == value)
return;
if (existing != null)
existing.ConstraintChanged -= AdapterOnConstraintChanged;
if (existing != null && value != null)
{
value.ItemsSource = existing.ItemsSource;
value.ItemTemplateId = existing.ItemTemplateId;
}
if (value != null)
value.ConstraintChanged += AdapterOnConstraintChanged;
base.Adapter = value;
}
}
private void AdapterOnConstraintChanged(object sender, EventArgs eventArgs)
{
FireConstraintChanged();
}
public IList ItemsSource
{
get { return Adapter.ItemsSource; }
set { Adapter.ItemsSource = value; }
}
public int ItemTemplateId
{
get { return Adapter.ItemTemplateId; }
set { Adapter.ItemTemplateId = value; }
}
public string Constraint
{
get { return Adapter.Constraint; }
}
public event EventHandler ConstraintChanged;
private void FireConstraintChanged()
{
var handler = ConstraintChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}
using System;
using Android.App;
using Android.Content;
using Android.Widget;
using Java.Lang;
namespace Cirrious.MvvmCross.Binding.Android.Views
{
public class MvxFilteringBindableListAdapter
: MvxBindableListAdapter, IFilterable
{
private class MyFilter : Filter
{
private readonly MvxFilteringBindableListAdapter _owner;
public MyFilter(MvxFilteringBindableListAdapter owner)
{
_owner = owner;
}
#region Overrides of Filter
protected override FilterResults PerformFiltering(ICharSequence constraint)
{
if (constraint != null)
{
_owner.Constraint = constraint.ToString();
}
return new FilterResults();
}
protected override void PublishResults(ICharSequence constraint, FilterResults results)
{
// ignored
}
#endregion
}
private string _constraint;
public event EventHandler ConstraintChanged;
public string Constraint
{
get { return _constraint; }
private set
{
if (_constraint == value)
return;
_constraint = value;
FireConstraintChanged();
}
}
private void FireConstraintChanged()
{
var activity = Context as Activity;
if (activity == null)
return;
activity.RunOnUiThread(() =>
{
var handler = ConstraintChanged;
if (handler != null)
handler(this, EventArgs.Empty);
});
}
public MvxFilteringBindableListAdapter(Context context) : base(context)
{
Filter = new MyFilter(this);
}
#region Implementation of IFilterable
public Filter Filter { get; set; }
#endregion
}
}
private string _s1;
public string S1
{
get { return _s1; }
private set { _s1 = value; FirePropertyChanged("S1"); }
}
private string _s2;
public string S2
{
get { return _s2; }
private set { _s2 = value; FirePropertyChanged("S2"); BindTarget.Clear();BindTarget.Add(S2); BindTarget.Add(S2 + "_2");BindTarget.Add(S2 + "_3"); }
}
public ObservableCollection<string> BindTarget { get; set; }
<Mvx.MvxBindableAutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="{'Text':{'Path':'S1','Mode':'TwoWay'},'ItemsSource':{'Path':'BindTarget'},'Constraint':{'Path':'S2','Mode':'OneWayToSource'}}"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment