Skip to content

Commit

Permalink
Merge pull request #38 from muak/PickerCellSurvey
Browse files Browse the repository at this point in the history
fix PickerCell #16
  • Loading branch information
muak authored Sep 2, 2018
2 parents d0c3d2b + 4608401 commit fd19ac0
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 14 deletions.
107 changes: 107 additions & 0 deletions Sample/Sample/ViewModels/PickerSurveyViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using System;

namespace Sample.ViewModels
{
class PickerSurveyViewModel
{
public ObservableCollection<Person> MasterItemsSource { get; } = new ObservableCollection<Person>();
public ObservableCollection<Person> MasterItemsSourceSelectedItems { get; } = new ObservableCollection<Person>();

public ObservableCollection<Person> SlaveItemsSource { get; } = new ObservableCollection<Person>();
public ObservableCollection<Person> SlaveItemsSourceSelectedItems { get; } = new ObservableCollection<Person>();

string[] type = { "letters", "number" };

string[] listLetters = { "a", "b", "c", "d", "e" };
string[] listNumbers = { "1", "2", "3", "4", "5" };

public PickerSurveyViewModel()
{
foreach (var item in type)
{
MasterItemsSource.Add(new Person()
{
Name = item,
Age = 1
});
}

}

ICommand selectMasterCommand;
public ICommand SelectMasterCommand =>
selectMasterCommand ?? (selectMasterCommand = new Command(async () => await ExecuteSelectMasterCommand()));

async Task ExecuteSelectMasterCommand()
{
try
{

if (MasterItemsSourceSelectedItems.Count == 0)
return;

SlaveItemsSource.Clear();
SlaveItemsSourceSelectedItems.Clear();


await Task.Delay(100);

switch (MasterItemsSourceSelectedItems[0].Name)
{
case "letters":

foreach (var item in listLetters)
{
SlaveItemsSource.Add(new Person()
{
Name = item,
Age = 1
});
}


break;
case "number":
foreach (var item in listNumbers)
{
SlaveItemsSource.Add(new Person()
{
Name = item,
Age = 1
});
}


break;

default:
break;
}


SlaveItemsSourceSelectedItems.Add(SlaveItemsSource[0]);

}
catch (Exception ex)
{

}
finally
{

}
}
}

//public class Person
//{
// public string Name { get; set; }
// public int Age { get; set; }
//}


}
2 changes: 1 addition & 1 deletion Sample/Sample/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<Button Text="ButtonCellTest" Command="{Binding GoToTest}" CommandParameter="ButtonCellTest" />
<Button Text="FormsCellTest" Command="{Binding GoToTest}" CommandParameter="FormsCellTest" />
<Button Text="OnTableViewTest" Command="{Binding GoToTest}" CommandParameter="OnTableViewTest" />
<Button Text="Fix" Command="{Binding GoToTest}" CommandParameter="FixProp" />
<Button Text="PickerSurvey" Command="{Binding GoToTest}" CommandParameter="PickerSurvey" />
</StackLayout>
</ScrollView>
</ContentPage>
75 changes: 75 additions & 0 deletions Sample/Sample/Views/PickerSurvey.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestAiForms"
xmlns:sv="clr-namespace:AiForms.Renderers;assembly=SettingsView"
x:Class="Sample.Views.PickerSurvey">

<ContentPage.Resources>
<ResourceDictionary>
<!-- アプリ全体の背景色 -->
<Color x:Key="AppBackground">#ffffff</Color>
<!-- アクセントカラー -->
<Color x:Key="AccentColor">#FFBF00</Color>
<!-- 非アクティブカラー -->
<Color x:Key="DisabledColor">#E6DAB9</Color>
<!-- タイトルテキストカラー -->
<Color x:Key="TitleTextColor">#CC9900</Color>
<!-- 薄い背景色1 -->
<Color x:Key="PaleBackColorPrimary">#F2EFE6</Color>
<!-- 薄い背景色2 -->
<Color x:Key="PaleBackColorSecondary">#F2EDDA</Color>
<!-- 濃いめの文字色 -->
<Color x:Key="DeepTextColor">#555555</Color>
<!-- 通常文字色 -->
<Color x:Key="NormalTextColor">#666666</Color>
<!-- 薄い文字色 -->
<Color x:Key="PaleTextColor">#999999</Color>
<!-- 強調文字色 -->
<Color x:Key="EmphasisTextColor">#FF0000</Color>
<!-- 通常フォントサイズ -->
<x:Double x:Key="BaseFontSize">12</x:Double>
<!-- ちょい大きいフォントサイズ -->
<x:Double x:Key="BaseFontSize+">14</x:Double>
<!-- 大きいフォントサイズ -->
<x:Double x:Key="BaseFontSize++">17</x:Double>
<!-- ちょい小さいフォントサイズ -->
<x:Double x:Key="BaseFontSize-">11</x:Double>

<Style TargetType="sv:SettingsView">
<Setter Property="SeparatorColor" Value="{StaticResource DisabledColor}" />
<Setter Property="BackgroundColor" Value="{StaticResource PaleBackColorPrimary}" />
<Setter Property="HeaderBackgroundColor" Value="{StaticResource PaleBackColorPrimary}" />
<Setter Property="CellBackgroundColor" Value="{StaticResource AppBackground}" />
<Setter Property="CellTitleColor" Value="{StaticResource DeepTextColor}" />
<Setter Property="CellValueTextColor" Value="{StaticResource NormalTextColor}" />
<Setter Property="CellTitleFontSize" Value="{StaticResource BaseFontSize++}" />
<Setter Property="CellValueTextFontSize" Value="{StaticResource BaseFontSize}" />
<Setter Property="CellDescriptionColor" Value="{StaticResource NormalTextColor}" />
<Setter Property="CellDescriptionFontSize" Value="{StaticResource BaseFontSize-}" />
<Setter Property="CellAccentColor" Value="{StaticResource AccentColor}" />
<Setter Property="SelectedColor" Value="#50FFBF00" />
<Setter Property="HeaderTextColor" Value="{StaticResource TitleTextColor}" />
<Setter Property="FooterFontSize" Value="{StaticResource BaseFontSize-}" />
<Setter Property="FooterTextColor" Value="{StaticResource PaleTextColor}" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>

<sv:SettingsView HasUnevenRows="true" HeaderHeight="36" HeaderPadding="14,0,0,6" HeaderTextVerticalAlign="End" FooterPadding="14,4,4,6">

<sv:Section x:Name="svv" Title="Select a letter and then select a number..." FooterText="...the old letter value is still visible, even if you clear the selecteditems in viewmode">

<sv:PickerCell Title="number o letter?" ItemsSource="{Binding MasterItemsSource}" DisplayMember="Name" MaxSelectedNumber="1"
SelectedItems="{Binding MasterItemsSourceSelectedItems}" SelectedCommand="{Binding SelectMasterCommand}" ValueText=""
KeepSelectedUntilBack="true" PageTitle="Select" />
</sv:Section>

<sv:Section Title="Select detail">
<sv:PickerCell Title="Detail" ItemsSource="{Binding SlaveItemsSource}" DisplayMember="Name" MaxSelectedNumber="1"
SelectedItems="{Binding SlaveItemsSourceSelectedItems}" KeepSelectedUntilBack="true" PageTitle="Select" ValueText="" />
</sv:Section>

</sv:SettingsView>

</ContentPage>
15 changes: 15 additions & 0 deletions Sample/Sample/Views/PickerSurvey.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Sample.Views
{
public partial class PickerSurvey : ContentPage
{
public PickerSurvey()
{
InitializeComponent();
}
}
}
31 changes: 25 additions & 6 deletions SettingsView.Droid/Cells/PickerCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class PickerCellView : LabelCellView, IDialogInterfaceOnShowListener, IDi
PickerAdapter _adapter;
Context _context;
string _valueTextCache;
INotifyCollectionChanged _notifyCollection;
INotifyCollectionChanged _notifyCollection;
INotifyCollectionChanged _selectedCollection;

/// <summary>
/// Initializes a new instance of the <see cref="T:AiForms.Renderers.Droid.PickerCellView"/> class.
Expand All @@ -47,8 +48,9 @@ public PickerCellView(Context context, Cell cell) : base(context, cell)
/// <param name="e">E.</param>
public override void CellPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.CellPropertyChanged(sender, e);
if (e.PropertyName == PickerCell.SelectedItemsProperty.PropertyName ||
base.CellPropertyChanged(sender, e);

if (e.PropertyName == PickerCell.SelectedItemsProperty.PropertyName ||
e.PropertyName == PickerCell.DisplayMemberProperty.PropertyName ||
e.PropertyName == PickerCell.UseNaturalSortProperty.PropertyName ||
e.PropertyName == PickerCell.SelectedItemsOrderKeyProperty.PropertyName) {
Expand Down Expand Up @@ -88,7 +90,14 @@ public void UpdateSelectedItems(bool force = false)
}


if (force || string.IsNullOrEmpty(_valueTextCache)) {
if (force || string.IsNullOrEmpty(_valueTextCache)) {
if(_selectedCollection != null) {
_selectedCollection.CollectionChanged -= SelectedItems_CollectionChanged;
}
_selectedCollection = _PickerCell.SelectedItems as INotifyCollectionChanged;
if(_selectedCollection != null) {
_selectedCollection.CollectionChanged += SelectedItems_CollectionChanged;
}
_valueTextCache = _PickerCell.GetSelectedItemsText();
}

Expand All @@ -113,6 +122,10 @@ protected override void Dispose(bool disposing)
if (_notifyCollection != null) {
_notifyCollection.CollectionChanged -= ItemsSourceCollectionChanged;
_notifyCollection = null;
}
if(_selectedCollection != null) {
_selectedCollection.CollectionChanged -= SelectedItems_CollectionChanged;
_selectedCollection = null;
}
}
base.Dispose(disposing);
Expand All @@ -128,7 +141,7 @@ void UpdateCollectionChanged()

if (_notifyCollection != null) {
_notifyCollection.CollectionChanged += ItemsSourceCollectionChanged;
ItemsSourceCollectionChanged(this, EventArgs.Empty);
ItemsSourceCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}

Expand All @@ -143,7 +156,7 @@ protected override void UpdateIsEnabled()
base.UpdateIsEnabled();
}

void ItemsSourceCollectionChanged(object sender, EventArgs e)
void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (!CellBase.IsEnabled) {
return;
Expand All @@ -152,6 +165,12 @@ void ItemsSourceCollectionChanged(object sender, EventArgs e)
SetEnabledAppearance(_PickerCell.ItemsSource.Count > 0);
}

void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateSelectedItems(true);
}


internal void ShowDialog()
{
CreateDialog();
Expand Down
5 changes: 2 additions & 3 deletions SettingsView.Droid/SettingsView.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
<OutputType>Library</OutputType>
<RootNamespace>AiForms.Renderers.Droid</RootNamespace>
<AssemblyName>SettingsView.Droid</AssemblyName>
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
<ReleaseVersion>1.1.2</ReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -148,7 +147,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SettingsView\SettingsView.csproj">
<Project>{8FFB1EF3-FAF3-478C-B9F1-4D02E599C3C6}</Project>
<Project>{FBB3476A-AC66-4284-B0C1-C3982F5872BB}</Project>
<Name>SettingsView</Name>
</ProjectReference>
</ItemGroup>
Expand Down
17 changes: 15 additions & 2 deletions SettingsView.iOS/Cells/PickerCellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class PickerCellView : LabelCellView
PickerCell _PickerCell => Cell as PickerCell;
string _valueTextCache;
INotifyCollectionChanged _notifyCollection;
INotifyCollectionChanged _selectedCollection;

/// <summary>
/// Initializes a new instance of the <see cref="T:AiForms.Renderers.iOS.PickerCellView"/> class.
Expand Down Expand Up @@ -82,6 +83,13 @@ public void UpdateSelectedItems(bool force = false)
}

if (force || string.IsNullOrEmpty(_valueTextCache)) {
if (_selectedCollection != null) {
_selectedCollection.CollectionChanged -= SelectedItems_CollectionChanged;
}
_selectedCollection = _PickerCell.SelectedItems as INotifyCollectionChanged;
if (_selectedCollection != null) {
_selectedCollection.CollectionChanged += SelectedItems_CollectionChanged;
}
_valueTextCache = _PickerCell.GetSelectedItemsText();
}

Expand All @@ -99,7 +107,7 @@ void UpdateCollectionChanged()
if (_notifyCollection != null)
{
_notifyCollection.CollectionChanged += ItemsSourceCollectionChanged;
ItemsSourceCollectionChanged(this, EventArgs.Empty);
ItemsSourceCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}

Expand All @@ -114,7 +122,7 @@ protected override void UpdateIsEnabled()
base.UpdateIsEnabled();
}

void ItemsSourceCollectionChanged(object sender, EventArgs e)
void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (!CellBase.IsEnabled){
return;
Expand All @@ -123,6 +131,11 @@ void ItemsSourceCollectionChanged(object sender, EventArgs e)
SetEnabledAppearance(_PickerCell.ItemsSource.Count > 0);
}

void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateSelectedItems(true);
}

/// <summary>
/// Dispose the specified disposing.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions nuget/SettingsView_mac.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>AiForms.SettingsView</id>
<version>0.2.6</version>
<version>0.2.9</version>
<title>SettingsView for Xamarin.Forms</title>
<authors>kamu</authors>
<owners>kamu</owners>
Expand All @@ -16,7 +16,7 @@ There are various cells such as (LabelCell,ButtonCell,CommandCell,SwitchCell,Che
<releaseNotes>
## Bug fixes

* When doing with toggling section visibility and changing cell properties simultaneously, null exception occurs. (Android)
* [PickerCell] When the SelectedItems is changed programmatically, the new value text is not shown. (#16)
</releaseNotes>
<tags>Xamarin.Forms TableView Cell Setting Configuration Option ListView UITableView RecyclerView ReOrder DragDrop</tags>
<language>en-US</language>
Expand Down

0 comments on commit fd19ac0

Please sign in to comment.