Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial sample #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
Expand Down Expand Up @@ -52,7 +51,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Forms" Version="4.0.0.394984-pre10" />
<PackageReference Include="Xamarin.Android.Support.ViewPager" Version="28.0.0.1" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.1" />
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="28.0.0.1" />
Expand Down Expand Up @@ -102,7 +101,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CollectionViewChallenge\CollectionViewChallenge.csproj">
<Project>{BA39825E-3CF8-4A31-B120-782B2A651542}</Project>
<Project>{9A9DDEE5-8C48-4274-82D7-22EB79C97206}</Project>
<Name>CollectionViewChallenge</Name>
</ProjectReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.CollectionViewChallenge">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<application android:label="CollectionViewChallenge.Android"></application>
</manifest>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<application android:label="CollectionViewChallenge.Android"></application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Forms" Version="4.0.0.394984-pre10" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\CollectionViewChallenge\CollectionViewChallenge.csproj">
<Project>{BA39825E-3CF8-4A31-B120-782B2A651542}</Project>
<Project>{9A9DDEE5-8C48-4274-82D7-22EB79C97206}</Project>
<Name>CollectionViewChallenge</Name>
</ProjectReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Forms" Version="4.0.0.394984-pre10" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
<PackageReference Include="NLipsum" Version="1.1.0" />
<PackageReference Include="DynamicData" Version="6.9.1.2588" />
<PackageReference Include="Fody" Version="5.0.5" />
<PackageReference Include="PropertyChanged.Fody" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Linq;
using NLipsum.Core;

namespace CollectionViewChallenge
{
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public String Column1 { get; set; } = String.Empty;
public String Column2 { get; set; } = String.Empty;

LipsumGenerator lipsum = new LipsumGenerator();
Random random = new Random(DateTime.Now.Millisecond);
bool collapsed = false;

public ViewModel()
{
CreateRandomText();
}

public void Toggle()
{
collapsed = !collapsed;
CreateRandomText();
}

void CreateRandomText()
{
if (collapsed)
{
Column1 = "Tap to create random Text!";
Column2 = String.Empty;
}
else
{
var words = lipsum.GenerateWords(random.Next(5, 50));
var text = String.Join(" ", words.ToArray());

Column1 = text;

words = lipsum.GenerateWords(random.Next(2, 20));
text = String.Join(" ", words.ToArray());

Column2 = text;
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
<?xml version="1.0" encoding="utf-8" ?>
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="CollectionViewChallenge.Views.CollectionViewChallengePage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Clear" Clicked="OnClearClicked"/>
<ToolbarItem Text="" x:Name="TBUpdating"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<!-- Use your own layout and functionality here! -->
<CollectionView>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>This is a CollectionView!</x:String>
<x:String>Your feedback on the experience of converting a ListView to a CollectionView is incredibly appreciated.</x:String>
<x:String>Here are three general questions:</x:String>
<x:String>1. How was the experience of converting your existing ListView to a CollectionView?</x:String>
<x:String>2. How is the performance compared to the ListView?</x:String>
<x:String>3. Is there a specific piece of functionality that you'd like to see?</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="10"
ItemSizingStrategy="MeasureAllItems"
BackgroundColor="White"
ItemsSource="{Binding Data}"
SelectionMode="Single"
SelectionChanged="OnCollectionViewSelectionChanged">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="5">
<Label Text="{Binding .}" d:Text="Design Time Data" FontSize="10"/>
</StackLayout>
<Grid
ColumnSpacing="10"
RowSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*" />
<ColumnDefinition Width="65*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0"
Text="{Binding Column1}"
FontAttributes="Bold" />
<Label Grid.Column="1"
Text="{Binding Column2}"
FontAttributes="Italic" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.EmptyView>
<StackLayout>
<Label Text="No entries!"
Margin="10,25,10,10"
FontAttributes="Bold"
FontSize="18"
HorizontalOptions="Fill"
HorizontalTextAlignment="Center" />
</StackLayout>
</CollectionView.EmptyView>
</CollectionView>
</StackLayout>
</ContentPage.Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DynamicData;
using DynamicData.Binding;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
Expand All @@ -12,9 +14,50 @@ namespace CollectionViewChallenge.Views
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CollectionViewChallengePage : ContentPage
{
public CollectionViewChallengePage()
public IObservableCollection<ViewModel> Data { get; } = new ObservableCollectionExtended<ViewModel>();
SourceList<ViewModel> List = new SourceList<ViewModel>();

public CollectionViewChallengePage()
{
InitializeComponent();
}
}

List.AddRange(Enumerable.Range(0, 1000).Select(x => new ViewModel()));
List.Connect().Bind(Data).DisposeMany().Subscribe();

BindingContext = this;
}

void OnClearClicked(object sender, EventArgs e)
{
// verify that PropertyChanged interface works
Data.PropertyChanged += (sn, ev) =>
{
Console.WriteLine("Data changed");
};

List.Clear();
}

void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count == 0)
return;

var item = e.CurrentSelection.FirstOrDefault() as ViewModel;
item.Toggle();
var colView = sender as CollectionView;
UpdateSelection(colView, item);
}

async void UpdateSelection(CollectionView colView, ViewModel item)
{
TBUpdating.Text = "Updating Selection ...";

await Task.Delay(1000); // make scroll action visible
colView.ScrollTo(item, position: ScrollToPosition.Center);
colView.SelectedItem = null;

TBUpdating.Text = String.Empty;
}
}
}