Instantly share code, notes, and snippets.

Embed
What would you like to do?
Files needed to do inline ads/reviews/whatever
namespace ScottIsAFool.Windows.Core.ViewModel
{
public interface IListType
{
ListType ListType { get; }
}
}
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using ScottIsAFool.Windows.Core.ViewModel;
namespace ScottIsAFool.Windows.Data
{
public class ItemTypeDataTemplateSelector : DataTemplateSelector
{
public DataTemplate NormalTemplate { get; set; }
public DataTemplate AdTemplate { get; set; }
public DataTemplate ReviewTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item)
{
var listType = item as IListType;
if (listType == null)
{
return base.SelectTemplateCore(item);
}
switch (listType.ListType)
{
case ListType.Normal:
return NormalTemplate;
case ListType.Ad:
return AdTemplate;
case ListType.Review:
return ReviewTemplate;
default:
return base.SelectTemplateCore(item);
}
}
}
}
namespace ScottIsAFool.Windows.Core.ViewModel
{
public enum ListType
{
Normal,
Review,
Ad
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using ScottIsAFool.Windows.Core.ViewModel;
namespace ScottIsAFool.Windows.Core.Extensions
{
public static class ListTypeExtensions
{
public static List<IListType> AddEveryOften(this IEnumerable<IListType> collectionList, int everyOften, int maxNumberToShow, IListType itemToAdd, int startAt = 0, bool addAsFirstItem = false)
{
var list = collectionList.ToList();
if (list == null || !list.Any())
{
return new List<IListType>();
}
var itemsAdded = 0;
var numberOfItemsToAdd = Math.Floor((decimal)list.Count / everyOften);
for (var i = 0; i < numberOfItemsToAdd; i++)
{
var multiplyValue = addAsFirstItem && startAt > 0 ? i : i + 1;
var addAt = addAsFirstItem && i == 0 ? startAt : (multiplyValue * everyOften) + startAt;
if (addAt > list.Count)
{
break;
}
list.Insert(addAt, itemToAdd);
itemsAdded++;
if (itemsAdded == maxNumberToShow)
{
break;
}
}
return list;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment