Skip to content

Commit

Permalink
Merge pull request #164 from muak/development
Browse files Browse the repository at this point in the history
Fix Cell IsVisible property
  • Loading branch information
muak authored Apr 30, 2021
2 parents dde17cb + 0c470a7 commit b187945
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ public class Option
* IconRadius
* アイコンの角丸半径。
* IsEnabled
* セルを有効にするかどうか。無効にした場合はセル全体の色が薄くなり操作を受け付けなくなります。
* セルを有効にするかどうか。無効にした場合はセル全体の色が薄くなり操作を受け付けなくなります。
* IsVisible
* セルの表示・非表示

### メソッド

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ public class Option
* Icon corners radius.
* IsEnabled
* Whether a cell is enabled. If set to false, the entire cell color will turn translucent and the cell won't accept any operations.
* IsVisible
* Whether a cell is visible or not.

### Methods

Expand Down
12 changes: 11 additions & 1 deletion Sample/Sample/ViewModels/RowManipulationTemplateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Reactive.Bindings;
using Xamarin.Forms;
using System.Linq;
using Prism.Mvvm;

namespace Sample.ViewModels
{
Expand Down Expand Up @@ -44,6 +45,10 @@ public RowManipulationTemplateViewModel()
case "Replace1":
Settings[0][0] = CreateItem();
break;
case "ShowHide":
var item = Settings[0][1];
item.IsVisible = !item.IsVisible;
break;
case "AddSecFirst":
Settings.Insert(0, CreateSection());
break;
Expand Down Expand Up @@ -97,8 +102,13 @@ public class SettingsGroup:ObservableCollection<SettingsSectionItem>
public SettingsGroup(IList<SettingsSectionItem> list) : base(list) { }
}

public class SettingsSectionItem
public class SettingsSectionItem:BindableBase
{
public string Text { get; set; }
private bool _IsVisible = true;
public bool IsVisible{
get => _IsVisible;
set => SetProperty(ref _IsVisible, value);
}
}
}
4 changes: 3 additions & 1 deletion Sample/Sample/Views/RowManipulation.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
<StackLayout Grid.Column="0" Spacing="1">
<Button Text="AddFirst" Clicked="AddFirstClicked" />
<Button Text="AddLast" Clicked="AddLastClicked"/>
<Button Text="Add2nd" Clicked="Add2ndClicked"/>
<Button Text="Add2nd" Clicked="Add2ndClicked"/>
<Button Text="DelFirst" Clicked="DelFirstClicked"/>
<Button Text="DelLast" Clicked="DelLastClicked"/>
<Button Text="Del2nd" Clicked="Del2ndClicked" />
<Button Text="Replace1" Clicked="Replace1Clicked" />
<Button Text="ShowHide2nd" Clicked="ShowHide2ndCell" />

<Button Text="AddSecFirst" Clicked="AddSecFirstClicked" />
<Button Text="AddSecLast" Clicked="AddSecLastClicked"/>
Expand All @@ -43,6 +44,7 @@
<Button Text="ReplaceSec1" Clicked="ReplaceSec1Clicked" />

<Button Text="ShowHide1st" Clicked="ShowHide1stClicked" />


</StackLayout>

Expand Down
8 changes: 8 additions & 0 deletions Sample/Sample/Views/RowManipulation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void Replace1Clicked(object sender, System.EventArgs e)
settings.Root[0][0] = CreateCell();
}

void ShowHide2ndCell(System.Object sender, System.EventArgs e)
{
var cell = settings.Root[0][1] as CellBase;
cell.IsVisible = !cell.IsVisible;
}

void AddSecFirstClicked(object sender, System.EventArgs e)
{
settings.Root.Insert(0, CreateSection());
Expand Down Expand Up @@ -115,5 +121,7 @@ Section CreateSection()
);
return sec;
}


}
}
3 changes: 2 additions & 1 deletion Sample/Sample/Views/RowManipulationTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Button Text="DelLast" Command="{Binding ManipulateCommand}" CommandParameter="DelLast"/>
<Button Text="Del2nd" Command="{Binding ManipulateCommand}" CommandParameter="Del2nd" />
<Button Text="Replace1" Command="{Binding ManipulateCommand}" CommandParameter="Replace1" />
<Button Text="ShowHide2nd" Command="{Binding ManipulateCommand}" CommandParameter="ShowHide" />

<Button Text="AddSecFirst" Command="{Binding ManipulateCommand}" CommandParameter="AddSecFirst" />
<Button Text="AddSecLast" Command="{Binding ManipulateCommand}" CommandParameter="AddSecLast"/>
Expand Down Expand Up @@ -60,7 +61,7 @@
</sv:Section.FooterView>
<sv:Section.ItemTemplate>
<DataTemplate>
<sv:LabelCell Title="{Binding Text}" />
<sv:LabelCell Title="{Binding Text}" IsVisible="{Binding IsVisible}" />
</DataTemplate>
</sv:Section.ItemTemplate>
</sv:Section>
Expand Down
15 changes: 15 additions & 0 deletions SettingsView/CellPropertyChangedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.ComponentModel;

namespace AiForms.Renderers
{
public class CellPropertyChangedEventArgs : PropertyChangedEventArgs
{
public Section Section { get; }

public CellPropertyChangedEventArgs(string propertyName, Section section) : base(propertyName)
{
Section = section;
}
}
}
22 changes: 21 additions & 1 deletion SettingsView/Platforms/Android/SettingsViewRecyclerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public SettingsViewRecyclerAdapter(Context context, SettingsView settingsView,Re

_settingsView.ModelChanged += _settingsView_ModelChanged;
_settingsView.SectionPropertyChanged += OnSectionPropertyChanged;
}
_settingsView.CellPropertyChanged += OnCellPropertyChanged;
}

void _settingsView_ModelChanged(object sender, EventArgs e)
{
Expand Down Expand Up @@ -78,12 +79,30 @@ void OnSectionPropertyChanged(object sender, System.ComponentModel.PropertyChang
}
}

void OnCellPropertyChanged(object sender, CellPropertyChangedEventArgs e)
{
if(e.PropertyName == CellBase.IsVisibleProperty.PropertyName)
{
UpdateCellVisible(e.Section, (CellBase)sender);
}
}

void UpdateSectionVisible(Section section)
{
var indexes = _proxy.Select((x, idx) => new { idx, x.Section }).Where(x => x.Section == section).Select(x => x.idx).ToList();
NotifyItemRangeChanged(indexes[0], indexes.Count);
}

void UpdateCellVisible(Section section, CellBase cell)
{
var result = _proxy.Select((x, idx) => new { idx, x.Section, x.Cell }).FirstOrDefault(x => x.Section == section && x.Cell == cell);
if(result == null)
{
return;
}
NotifyItemChanged(result.idx);
}

void UpdateSectionHeader(Section section)
{
var index = _proxy.FindIndex(x => x.Section == section);
Expand Down Expand Up @@ -299,6 +318,7 @@ protected override void Dispose(bool disposing)
if(disposing){
_settingsView.ModelChanged -= _settingsView_ModelChanged;
_settingsView.SectionPropertyChanged -= OnSectionPropertyChanged;
_settingsView.CellPropertyChanged -= OnCellPropertyChanged;
_proxy?.Dispose();
_proxy = null;
_settingsView = null;
Expand Down
17 changes: 17 additions & 0 deletions SettingsView/Platforms/ios/Cells/CellBaseView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public virtual void CellPropertyChanged(object sender, PropertyChangedEventArgs
{
UpdateIsEnabled();
}
else if (e.PropertyName == CellBase.IsVisibleProperty.PropertyName)
{
UpdateIsVisible();
}
}

/// <summary>
Expand Down Expand Up @@ -396,6 +400,18 @@ protected virtual void UpdateIsEnabled()
SetEnabledAppearance(CellBase.IsEnabled);
}

/// <summary>
/// Updates the IsVisible
/// </summary>
protected virtual void UpdateIsVisible()
{
// If AccessoryView is set, hide the view because it overflows outside when IsVisible is false.
if (AccessoryView != null)
{
AccessoryView.Hidden = !CellBase.IsVisible;
}
}

/// <summary>
/// Sets the enabled appearance.
/// </summary>
Expand Down Expand Up @@ -581,6 +597,7 @@ public virtual void UpdateCell(UITableView tableView = null)
UpdateIconRadius();

UpdateIsEnabled();
UpdateIsVisible();

SetNeedsLayout();
}
Expand Down
22 changes: 21 additions & 1 deletion SettingsView/Platforms/ios/SettingsViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<SettingsView> e
e.OldElement.CollectionChanged -= OnCollectionChanged;
e.OldElement.SectionCollectionChanged -= OnSectionCollectionChanged;
e.OldElement.SectionPropertyChanged -= OnSectionPropertyChanged;
e.OldElement.CellPropertyChanged -= OnCellPropertyChanged;
}

if (e.NewElement != null)
Expand Down Expand Up @@ -91,6 +92,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<SettingsView> e
e.NewElement.CollectionChanged += OnCollectionChanged;
e.NewElement.SectionCollectionChanged += OnSectionCollectionChanged;
e.NewElement.SectionPropertyChanged += OnSectionPropertyChanged;
e.NewElement.CellPropertyChanged += OnCellPropertyChanged;

UpdateBackgroundColor();
UpdateSeparator();
Expand All @@ -117,7 +119,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<SettingsView> e
_contentSizeObserver = _tableview.AddObserver("contentSize", NSKeyValueObservingOptions.New, OnContentSizeChanged);

}
}
}

void OnContentSizeChanged(NSObservedChange change)
{
Expand Down Expand Up @@ -155,6 +157,14 @@ void OnSectionPropertyChanged(object sender, System.ComponentModel.PropertyChang
}
}

void OnCellPropertyChanged(object sender, CellPropertyChangedEventArgs e)
{
if(e.PropertyName == CellBase.IsVisibleProperty.PropertyName)
{
UpdateCellVisible(e.Section,(CellBase)sender);
}
}

void UpdateSectionVisible(Section section)
{
var secIndex = Element.Model.GetSectionIndex(section);
Expand All @@ -163,6 +173,15 @@ void UpdateSectionVisible(Section section)
Control.EndUpdates();
}

void UpdateCellVisible(Section section, CellBase cell)
{
var secIndex = Element.Model.GetSectionIndex(section);
var rowIndex = section.IndexOf(cell);
Control.BeginUpdates();
Control.ReloadRows(GetPaths(secIndex,rowIndex,1), UITableViewRowAnimation.Automatic);
Control.EndUpdates();
}

void UpdateSectionNoAnimation(Section section)
{
var secIndex = Element.Model.GetSectionIndex(section);
Expand Down Expand Up @@ -449,6 +468,7 @@ protected override void Dispose(bool disposing)
Element.CollectionChanged -= OnCollectionChanged;
Element.SectionCollectionChanged -= OnSectionCollectionChanged;
Element.SectionPropertyChanged -= OnSectionPropertyChanged;
Element.CellPropertyChanged -= OnCellPropertyChanged;
_insetTracker?.Dispose();
_insetTracker = null;
foreach (UIView subview in Subviews)
Expand Down
14 changes: 7 additions & 7 deletions SettingsView/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,8 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
}

void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(CellBase.IsVisible))
{
// if visibility of a section item changed, we treat it the same as if the
// whole section visibility changed
SectionPropertyChanged?.Invoke(this, e);
}
{
CellPropertyChanged?.Invoke(sender, new CellPropertyChangedEventArgs(e.PropertyName, this));
}

/// <summary>
Expand All @@ -189,6 +184,11 @@ void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
/// </summary>
public event PropertyChangedEventHandler SectionPropertyChanged;

/// <summary>
/// Occurs when cell property changed.
/// </summary>
public event EventHandler<CellPropertyChangedEventArgs> CellPropertyChanged;

public SettingsView Parent { get; set; }

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions SettingsView/SettingsRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public SettingsRoot()
/// Occurs when section property changed.
/// </summary>
public event PropertyChangedEventHandler SectionPropertyChanged;
/// <summary>
/// Occurs when cell property changed.
/// </summary>
public event EventHandler<CellPropertyChangedEventArgs> CellPropertyChanged;

void ChildCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
Expand All @@ -37,6 +41,11 @@ void ChildPropertyChanged(object sender, PropertyChangedEventArgs e)
SectionPropertyChanged?.Invoke(sender, e);
}

void OnCellPropertyChanged(object sender, CellPropertyChangedEventArgs e)
{
CellPropertyChanged?.Invoke(sender, e);
}

void SetupEvents()
{
CollectionChanged += (sender, args) =>
Expand All @@ -45,13 +54,15 @@ void SetupEvents()
foreach (Section section in args.NewItems) {
section.SectionCollectionChanged += ChildCollectionChanged;
section.SectionPropertyChanged += ChildPropertyChanged;
section.CellPropertyChanged += OnCellPropertyChanged;
}
}
if (args.OldItems != null) {
foreach (Section section in args.OldItems) {
section.SectionCollectionChanged -= ChildCollectionChanged;
section.SectionPropertyChanged -= ChildPropertyChanged;
section.CellPropertyChanged -= OnCellPropertyChanged;
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions SettingsView/SettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static void ClearCache()
/// Occurs when section property changed.
/// </summary>
public event PropertyChangedEventHandler SectionPropertyChanged;
/// <summary>
/// Occurs when cell property changed.
/// </summary>
public event EventHandler<CellPropertyChangedEventArgs> CellPropertyChanged;

/// <summary>
/// Initializes a new instance of the <see cref="T:AiForms.Renderers.SettingsView"/> class.
Expand All @@ -68,6 +72,7 @@ public SettingsView()
_root.SectionPropertyChanged -= OnSectionPropertyChanged;
_root.CollectionChanged -= OnCollectionChanged;
_root.SectionCollectionChanged -= OnSectionCollectionChanged;
_root.CellPropertyChanged -= OnCellPropertyChanged;
}

_root = value;
Expand All @@ -78,6 +83,7 @@ public SettingsView()
_root.SectionPropertyChanged += OnSectionPropertyChanged;
_root.CollectionChanged += OnCollectionChanged;
_root.SectionCollectionChanged += OnSectionCollectionChanged;
_root.CellPropertyChanged += OnCellPropertyChanged;
OnModelChanged();
}
}
Expand All @@ -97,6 +103,11 @@ void OnSectionPropertyChanged(object sender, System.ComponentModel.PropertyChang
SectionPropertyChanged?.Invoke(sender, e);
}

void OnCellPropertyChanged(object sender, CellPropertyChangedEventArgs e)
{
CellPropertyChanged?.Invoke(sender, e);
}

/// <summary>
/// Ons the property changed.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions SettingsView/SettingsView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@
<ItemGroup Condition=" $(TargetFramework.StartsWith('xamarin.ios')) ">
<Compile Include="Platforms\ios\**\*.cs" />
</ItemGroup>
<ItemGroup>
<Compile Update="ChildPropertyChangedEventHandler.cs">
<SubType></SubType>
</Compile>
</ItemGroup>
</Project>
Loading

0 comments on commit b187945

Please sign in to comment.