PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/BaconographyWP8Core/Converters/ReifiedSubredditTemplateCollectionConverter.cs

https://github.com/hippiehunter/Baconography
C# | 90 lines | 80 code | 10 blank | 0 comment | 9 complexity | df93923edeb7a583503f63c8ef4be46d MD5 | raw file
  1. using BaconographyPortable.ViewModel;
  2. using BaconographyWP8.View;
  3. using BaconographyWP8.ViewModel;
  4. using GalaSoft.MvvmLight;
  5. using Microsoft.Phone.Controls;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. namespace BaconographyWP8.Converters
  15. {
  16. public class ReifiedSubredditTemplateCollectionConverter : IValueConverter
  17. {
  18. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  19. {
  20. ObservableCollection<PivotItem> boundControls = new ObservableCollection<PivotItem>();
  21. var redditViewModelCollection = value as ObservableCollection<ViewModelBase>;
  22. redditViewModelCollection.CollectionChanged += (sender, arg) => redditViewModelCollection_CollectionChanged(sender, arg, boundControls);
  23. foreach (var viewModel in redditViewModelCollection)
  24. {
  25. boundControls.Add(MapViewModel(viewModel));
  26. }
  27. if (boundControls.Count > 0 && boundControls[0].Content == null)
  28. {
  29. boundControls[0].Content = new RedditView { DataContext = boundControls[0].DataContext };
  30. }
  31. return boundControls;
  32. }
  33. PivotItem MapViewModel(ViewModelBase viewModel)
  34. {
  35. var rvm = viewModel as RedditViewModel;
  36. var plainHeader = rvm.Heading == "The front page of this device" ? "front page" : rvm.Heading.ToLower();
  37. return new PivotItem { DataContext = viewModel, Header = rvm.IsTemporary ? "*" + plainHeader : plainHeader };
  38. }
  39. void redditViewModelCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, ObservableCollection<PivotItem> adaptedTarget)
  40. {
  41. switch (e.Action)
  42. {
  43. case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
  44. if (e.NewStartingIndex == adaptedTarget.Count)
  45. {
  46. if (adaptedTarget.Count == 0)
  47. {
  48. var firstResult = MapViewModel(e.NewItems[0] as ViewModelBase);
  49. firstResult.Content = new RedditView { DataContext = e.NewItems[0] };
  50. adaptedTarget.Add(firstResult);
  51. }
  52. else
  53. adaptedTarget.Add(MapViewModel(e.NewItems[0] as ViewModelBase));
  54. }
  55. else
  56. {
  57. adaptedTarget.Insert(e.NewStartingIndex, MapViewModel(e.NewItems[0] as ViewModelBase));
  58. }
  59. break;
  60. case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
  61. break;
  62. case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
  63. adaptedTarget.RemoveAt(e.OldStartingIndex);
  64. break;
  65. case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
  66. adaptedTarget[e.OldStartingIndex] = MapViewModel(e.NewItems[0] as ViewModelBase);
  67. break;
  68. case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
  69. adaptedTarget.Clear();
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. }
  80. }