Skip to content

Commit

Permalink
Added BandCollection to wrap NSMutableArray
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Apr 22, 2015
1 parent a2433d8 commit 04e5d11
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 6 deletions.
12 changes: 7 additions & 5 deletions Microsoft.Band/Microsoft.Band.iOS/ApiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Microsoft.Band;
using Microsoft.Band.Notifications;
using Microsoft.Band.Pages;
using Microsoft.Band.Personalization;
using Microsoft.Band.Sensors;
using Microsoft.Band.Tiles;
Expand Down Expand Up @@ -127,15 +128,15 @@ interface BandPageElement

// @property (nonatomic, strong) MSBRect * rect;
[Export ("rect", ArgumentSemantic.Retain)]
BandRect Rect { get; set; }
BandRect Rect { get; [NullAllowed] set; }

// @property (nonatomic, strong) MSBMargins * margins;
[Export ("margins", ArgumentSemantic.Retain)]
BandMargins Margins { get; set; }
BandMargins Margins { get; [NullAllowed] set; }

// @property (nonatomic, strong) MSBColor * color;
[Export ("color", ArgumentSemantic.Retain)]
BandColor Color { get; set; }
BandColor Color { get; [NullAllowed] set; }

// @property (assign, nonatomic) MSBPageElementHorizontalAlignment horizontalAlignment;
[Export ("horizontalAlignment", ArgumentSemantic.UnsafeUnretained)]
Expand Down Expand Up @@ -256,8 +257,9 @@ interface BandPageBarcodeCode39Data
interface BandPagePanel
{
// @property (nonatomic, strong) NSMutableArray * children;
[Internal]
[Export ("children", ArgumentSemantic.Retain)]
NSMutableArray Children { get; set; }
NSMutableArray ChildrenInternal { get; set; }
}

// @interface MSBTextElement : MSBPageElement
Expand Down Expand Up @@ -842,7 +844,7 @@ interface BandTileManager
// @required -(void)setPages:(NSArray *)pageData tileId:(NSUUID *)tileId completionHandler:(void (^)(NSError *))completionHandler;
[Abstract]
[Export ("setPages:tileId:completionHandler:")]
void SetPagesAsync (NSObject[] pageData, NSUuid tileId, Action<NSError> completionHandler);
void SetPagesAsync (BandPageData[] pageData, NSUuid tileId, Action<NSError> completionHandler);

// @required -(void)removePagesInTile:(NSUUID *)tileId completionHandler:(void (^)(NSError *))completionHandler;
[Abstract]
Expand Down
171 changes: 171 additions & 0 deletions Microsoft.Band/Microsoft.Band.iOS/BandCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Collections;
using System.Collections.Generic;

using Foundation;

namespace Microsoft.Band
{
internal class BandCollection<T> : IList<T>
where T : NSObject
{
private readonly NSMutableArray children;
private int version;

public BandCollection (NSMutableArray childrenInternal)
{
children = childrenInternal;
}

public int IndexOf (T item)
{
nuint count = children.Count;
for (nuint idx = 0; idx < count; idx++) {
if (children.GetItem<T> (idx) == item) {
return (int)idx;
}
}
return -1;
}

public void Insert (int index, T item)
{
children.Insert (item, (nint)index);
version++;
}

public void RemoveAt (int index)
{
children.RemoveObject ((nint)index);
version++;
}

public T this [int index] {
get {
return children.GetItem<T> ((nuint)index);
}
set {
children.ReplaceObject (index, value);
version++;
}
}

public void Add (T item)
{
children.Add (item);
version++;
}

public void Clear ()
{
children.RemoveAllObjects ();
version++;
}

public bool Contains (T item)
{
return IndexOf (item) != -1;
}

public void CopyTo (T[] array, int arrayIndex)
{
foreach (var item in this) {
array [arrayIndex++] = item;
}
}

public bool Remove (T item)
{
int index = IndexOf (item);
if (index == -1) {
return false;
}
children.RemoveObject ((nint)index);
version++;
return true;
}

public int Count {
get {
return (int)children.Count;
}
}

public bool IsReadOnly {
get {
return false;
}
}

public IEnumerator<T> GetEnumerator ()
{
return new BandCollection<T>.Enumerator (this);
}

IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}

public struct Enumerator : IEnumerator<T>
{
private readonly BandCollection<T> collection;
private int next;
private readonly int ver;
private T current;

internal Enumerator (BandCollection<T> bandCollection)
{
this = default(BandCollection<T>.Enumerator);

collection = bandCollection;
ver = bandCollection.version;
}

public T Current {
get {
return current;
}
}

object IEnumerator.Current {
get {
if (ver != collection.version) {
throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
}
if (next <= 0) {
throw new InvalidOperationException ();
}
return current;
}
}

public void Dispose ()
{
}

public bool MoveNext ()
{
BandCollection<T> list = collection;
if (next < list.Count && ver == list.version) {
current = list [next++];
return true;
}
if (ver != collection.version) {
throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
}
next = -1;
return false;
}

void IEnumerator.Reset ()
{
if (ver != collection.version) {
throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
}
next = 0;
current = default(T);
}
}
}
}
3 changes: 3 additions & 0 deletions Microsoft.Band/Microsoft.Band.iOS/Microsoft.Band.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<ItemGroup>
<Folder Include="Resources\" />
<Folder Include="Notifications\" />
<Folder Include="Pages\" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -59,6 +60,8 @@
<Compile Include="Sensors\UVSensor.cs" />
<Compile Include="AsyncHelpers.cs" />
<Compile Include="Tiles\BandTileManagerExtensions.cs" />
<Compile Include="Pages\BandPagePanel.cs" />
<Compile Include="BandCollection.cs" />
</ItemGroup>
<ItemGroup>
<ObjcBindingApiDefinition Include="ApiDefinition.cs" />
Expand Down
27 changes: 27 additions & 0 deletions Microsoft.Band/Microsoft.Band.iOS/Pages/BandPagePanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;

namespace Microsoft.Band.Pages
{
public partial class BandPagePanel
{
private object childrenLock = new object();
private BandCollection<BandPageElement> children;

public IList<BandPageElement> Children {
get {
// check outside to avoid unnecessary locking
if (children == null) {
// we lock for thread safety
lock (childrenLock) {
// do a check before init
if (children == null) {
children = new BandCollection<BandPageElement> (ChildrenInternal);
}
}
}

return children;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Foundation;

using Microsoft.Band.Pages;
using Microsoft.Band.Tiles;

namespace Microsoft.Band.Tiles
Expand Down Expand Up @@ -45,7 +46,7 @@ public static Task<nuint> GetRemainingTileCapacityTaskAsync (this IBandTileManag
return tcs.Task;
}

public static Task SetPagesTaskAsync (this IBandTileManager manager, NSObject[] pageData, NSUuid tileId)
public static Task SetPagesTaskAsync (this IBandTileManager manager, BandPageData[] pageData, NSUuid tileId)
{
var tcs = new TaskCompletionSource<object> ();
manager.SetPagesAsync (pageData, tileId, tcs.AttachCompletionHandler ());
Expand Down

0 comments on commit 04e5d11

Please sign in to comment.