Skip to content

Commit

Permalink
updated cell and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Sep 27, 2018
1 parent 6ed664e commit 5520cef
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 2,608 deletions.
164 changes: 82 additions & 82 deletions ContextMenu/BaseActionViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,86 @@

namespace ContextMenu
{
public abstract class BaseActionViewCell : ViewCell
{
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(SideActionBarCell), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseActionViewCell).SetContentView(newValue as View);
});

public static readonly BindableProperty ContextTemplateProperty = BindableProperty.Create(nameof(ContextTemplate), typeof(DataTemplate), typeof(SideActionBarCell), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseActionViewCell).IsContextChanged = true;
});

public event Action<BaseActionViewCell> ContextMenuOpened;

public event Action<BaseActionViewCell> TouchStarted;

private bool IsContextChanged { get; set; }

public View Content
{
get => GetValue(ContentProperty) as View;
set => SetValue(ContentProperty, value);
}

public DataTemplate ContextTemplate
{
get => GetValue(ContextTemplateProperty) as DataTemplate;
set => SetValue(ContextTemplateProperty, value);
}

protected ContextMenuScrollView Scroll { get; } = new ContextMenuScrollView();

public void ForceClose(bool animated = true)
=> Scroll.ForceCloseContextMenu(Scroll, animated);

protected void SetContentView(View content)
=> Scroll.ContentView = content;

protected abstract void SetContextView(View context);

protected override void OnAppearing()
{
base.OnAppearing();
Scroll.TouchStarted += OnTouchStarted;
Scroll.ActionBarOpened += OnContextMenuOpened;
}

protected override void OnDisappearing()
{
base.OnDisappearing();
Scroll.TouchStarted -= OnTouchStarted;
Scroll.ActionBarOpened -= OnContextMenuOpened;
}

protected override void OnBindingContextChanged()
{
IsContextChanged = true;
ForceClose(false);
base.OnBindingContextChanged();
}

private void OnTouchStarted()
{
TouchStarted?.Invoke(this);
if (IsContextChanged)
{
IsContextChanged = false;

var template = ContextTemplate is DataTemplateSelector selector
? selector.SelectTemplate(BindingContext, this)
: ContextTemplate;

if (template == null)
{
return;
}
SetContextView(template.CreateContent() as View);
}
}

private void OnContextMenuOpened() => ContextMenuOpened?.Invoke(this);
}
public abstract class BaseActionViewCell : ViewCell
{
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(SideActionBarCell), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseActionViewCell).SetContentView(newValue as View);
});

public static readonly BindableProperty ContextTemplateProperty = BindableProperty.Create(nameof(ContextTemplate), typeof(DataTemplate), typeof(SideActionBarCell), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as BaseActionViewCell).IsContextChanged = true;
});

public event Action<BaseActionViewCell> ContextMenuOpened;

public event Action<BaseActionViewCell> TouchStarted;

private bool IsContextChanged { get; set; }

public View Content
{
get => GetValue(ContentProperty) as View;
set => SetValue(ContentProperty, value);
}

public DataTemplate ContextTemplate
{
get => GetValue(ContextTemplateProperty) as DataTemplate;
set => SetValue(ContextTemplateProperty, value);
}

protected ContextMenuScrollView Scroll { get; } = new ContextMenuScrollView();

public void ForceClose(bool animated = true)
=> Scroll.ForceCloseContextMenu(Scroll, animated);

protected void SetContentView(View content)
=> Scroll.ContentView = content;

protected abstract void SetContextView(View context);

protected override void OnAppearing()
{
base.OnAppearing();
Scroll.TouchStarted += OnTouchStarted;
Scroll.ActionBarOpened += OnContextMenuOpened;
}

protected override void OnDisappearing()
{
base.OnDisappearing();
Scroll.TouchStarted -= OnTouchStarted;
Scroll.ActionBarOpened -= OnContextMenuOpened;
}

protected override void OnBindingContextChanged()
{
IsContextChanged = true;
ForceClose(false);
base.OnBindingContextChanged();
}

private void OnTouchStarted()
{
TouchStarted?.Invoke(this);
if (IsContextChanged)
{
IsContextChanged = false;

var template = ContextTemplate is DataTemplateSelector selector
? selector.SelectTemplate(BindingContext, this)
: ContextTemplate;

if (template == null)
{
return;
}
SetContextView(template.CreateContent() as View);
}
}

private void OnContextMenuOpened() => ContextMenuOpened?.Invoke(this);
}
}
Loading

0 comments on commit 5520cef

Please sign in to comment.