PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/FlashCard/ExternalModel.cs

https://github.com/willkurt/Flash-Card-for-the-Microsoft-Surface
C# | 221 lines | 170 code | 22 blank | 29 comment | 10 complexity | 9de0c538b4131eb18e27c437edadb83f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Windows.Threading;
  14. using Microsoft.Surface;
  15. using Microsoft.Surface.Presentation;
  16. using Microsoft.Surface.Presentation.Controls;
  17. using System.IO;
  18. using System.Windows.Markup;
  19. using System.Xml;
  20. using System.Text.RegularExpressions;
  21. namespace FlashCard
  22. {
  23. class ExternalModel
  24. {
  25. public int tag;
  26. public string title;
  27. public string[] notePaths;
  28. public string[] imageUris;
  29. public List<string> imageUrisLabeled;
  30. public List<string> imageUrisUnlabeled;
  31. public ExternalModel(int tag, string title, string[] imageUris, string[] notePaths)
  32. {
  33. this.tag = tag;
  34. this.title = title;
  35. this.notePaths = notePaths;
  36. this.imageUris = imageUris;
  37. this.imageUrisLabeled = new List<string>{ };
  38. this.imageUrisUnlabeled = new List<string> { };
  39. foreach (string address in imageUris)
  40. {
  41. if(LabeledCondition(address))
  42. {
  43. imageUrisLabeled.Add(address);
  44. }
  45. else if (UnlabeledCondition(address))
  46. {
  47. imageUrisUnlabeled.Add(address);
  48. }
  49. //not 100% sure what I want to be the final else conditions
  50. //this would be a good place to catch things that don't
  51. //clearly fall in the labeled/unlabeled category
  52. }
  53. }
  54. /*
  55. * Overall I'm not too happy with the level of abstraction here
  56. * the heuristic to determine which image is labeled and which is not
  57. * doesn't seem quite obvious, so I've done my best put this logic
  58. * in as modular of an area as I can in order to be able to understand
  59. * this in the future
  60. *
  61. *
  62. * the current model is
  63. * filename-U.jpg -> Unlabeled version (so we're looking for '-U.' in the filename
  64. * filename-L.jpg -> Labeled version (so we're looking for '-L.' in the filename
  65. *
  66. */
  67. //this method check against the property which determines whether or not an
  68. //image is labeld or not
  69. private static bool LabeledCondition(string uri)
  70. {
  71. string filename = uri.Split('\\').Last();
  72. //we may need to tweak this but probably not
  73. if (filename.Contains("-L."))
  74. {
  75. return true;
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81. }
  82. private static bool UnlabeledCondition(string uri)
  83. {
  84. string filename = uri.Split('\\').Last();
  85. //we may need to tweak this but probably not
  86. if (filename.Contains("-U."))
  87. {
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. //I also want to flip logic in the class, so that if the logic
  96. //of determining labeled v. unlabled changes, we can just change it here
  97. public static string FlipImageUri(string originalUri)
  98. {
  99. string flippedUri;
  100. if (LabeledCondition(originalUri))
  101. {
  102. flippedUri = originalUri.Replace("-L.", "-U.");
  103. }
  104. else if (UnlabeledCondition(originalUri))
  105. {
  106. flippedUri = originalUri.Replace("-U.", "-L.");
  107. }
  108. else
  109. {
  110. flippedUri = originalUri;
  111. }
  112. return flippedUri;
  113. }
  114. public MediaElement[] GetImagesNodes()
  115. {
  116. string[] labeledUris = this.imageUrisLabeled.ToArray();
  117. MediaElement[] images = new MediaElement[labeledUris.Length];
  118. for (int i = 0; i < images.Length; i++)
  119. {
  120. MediaElement imageNode = new MediaElement();
  121. imageNode.Source = new Uri(labeledUris[i]);
  122. images[i] = imageNode;
  123. }
  124. return images;
  125. }
  126. //this should hopefully build the scatter view items
  127. //minus their appropriate even handlers
  128. public ScatterViewItem[] GetSVIs()
  129. {
  130. string[] labeledUris = this.imageUrisLabeled.ToArray();
  131. XmlReader[] generatedXaml = new XmlReader[labeledUris.Length];
  132. ScatterViewItem[] generatedSVIs = new ScatterViewItem[labeledUris.Length];
  133. for (int i = 0; i < generatedXaml.Length; i++)
  134. {
  135. //relying heavily on naming conventions here
  136. //but I think this will be the most helpful solutions
  137. //every piece will have a -X. part that will explain what
  138. //it does
  139. string labeledSource = labeledUris[i];
  140. string unlabeledSource = labeledSource.Replace("-L.","-U.");
  141. //needs to start with a letter so we'll add 'i'
  142. string imageRoot = "i"+Regex.Split((Regex.Split(labeledSource, "images").Last()), "-L")[0].Replace("\\","").Replace("-","");
  143. string labeledName = imageRoot + "_L";
  144. string unlabeledName = imageRoot + "_U";
  145. string sviName = imageRoot + "_SVI";
  146. string flipButtonName = imageRoot+"_FLIPBUTTON";
  147. string xamlDoc = @" <s:ScatterViewItem
  148. xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  149. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  150. xmlns:s='http://schemas.microsoft.com/surface/2008'
  151. MinWidth='100' MaxWidth='1200' Name='"+sviName+@"'>
  152. <Grid>
  153. <!-- Row and column definitions to position content and buttons -->
  154. <Grid.RowDefinitions>
  155. <RowDefinition Height='30'/>
  156. <RowDefinition />
  157. <RowDefinition Height='30'/>
  158. </Grid.RowDefinitions>
  159. <Grid.ColumnDefinitions>
  160. <ColumnDefinition Width='30'/>
  161. <ColumnDefinition/>
  162. <ColumnDefinition Width='30'/>
  163. <ColumnDefinition Width='30'/>
  164. <ColumnDefinition Width='30'/>
  165. <ColumnDefinition/>
  166. <ColumnDefinition Width='30'/>
  167. </Grid.ColumnDefinitions>"+@"
  168. <!-- The Ink Canvas -->
  169. <Grid Grid.Column='0' Grid.ColumnSpan='7' Grid.Row='0' Grid.RowSpan='5'>
  170. <Viewbox Name='"+labeledName+@"' StretchDirection='Both' Stretch='Uniform' Visibility='Hidden' >
  171. <Image Source='"+labeledSource+@"' />
  172. </Viewbox>
  173. <Viewbox Name='"+unlabeledName+@"' Visibility='Visible' StretchDirection='Both' Stretch='Uniform' >
  174. <Image Source='"+unlabeledSource+@"' />
  175. </Viewbox>
  176. </Grid>
  177. <!-- Clear -->
  178. <s:SurfaceButton Grid.Row='2' Grid.Column='2' Padding='5' Name='"+flipButtonName+ @"'>
  179. <Image Source='Resources\Flip.png' />
  180. </s:SurfaceButton>
  181. <!-- I don't need this button now, but I might want it later
  182. <s:SurfaceButton Name='SpaceHolder' Grid.Row='2' Grid.Column='4' Padding='5'>
  183. <Image Source='Resources\Move.png' />
  184. </s:SurfaceButton>-->
  185. </Grid>
  186. </s:ScatterViewItem>";
  187. StringReader sr = new StringReader(xamlDoc);
  188. XmlReader xr = XmlReader.Create(sr);
  189. ScatterViewItem svi = (ScatterViewItem)XamlReader.Load(xr);
  190. generatedSVIs[i] = svi;
  191. }
  192. return generatedSVIs;
  193. }
  194. }
  195. }