private void ShowRandomRule(int?seed = null, bool addToHistory = true) { if (rules == null) { return; } if (seed == null) { seed = DateTime.Now.Millisecond; } Random random = new Random(seed.Value); List <Rule> li = new List <Rule>(); Rule ru = rules[random.Next(rules.Count)]; li.Add(ru); while (ru.SubRules.Count > 0) { ru = ru.SubRules[random.Next(ru.SubRules.Count)]; li.Add(ru); } if (addToHistory) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; ScrollViewer scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(list, 0), 0) as ScrollViewer; if (scrollViewer != null) { actualSearch.VerticalOffset = scrollViewer.VerticalOffset; } history.Add(actualSearch); actualSearch = new HistoryItem(HistoryType.Random, seed); } list.ItemsSource = li; CustomEventlogger.Log("RandomRule"); homeButton.IsEnabled = true; }
private void OnBackButtonRequested(object sender, BackRequestedEventArgs args) { if (history.Count >= 1) { HistoryItem item = history.Last(); switch (item.Type) { case HistoryType.Search: Search((string)item.Value, false); break; case HistoryType.Number: ShowByNumber((int)item.Value, false); break; case HistoryType.Text: ShowByText((string)item.Value, false); break; case HistoryType.Random: ShowRandomRule((int?)item.Value, false); break; } list.UpdateLayout(); ScrollViewer scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(list, 0), 0) as ScrollViewer; scrollViewer?.ChangeView(null, item.VerticalOffset, null, true); history.RemoveAt(history.Count() - 1); actualSearch = item; args.Handled = true; if (history.Count == 0) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed; } } }
private void ShowByNumber(int number = 0, bool addToHistory = true) { if (rules == null) { return; } List <Rule> source = new List <Rule>(); if (number == 0) { source = rules; } else if (number >= 1 && number <= 9) { foreach (var rule in rules) { if (rule.Title.StartsWith(number + ".")) { source.Add(rule); foreach (var rule2 in rule.SubRules) { source.Add(rule2); } break; } } } else if (number >= 100 && number <= 999) { int high = number / 100; foreach (var rule in rules) { if (rule.Title.StartsWith(high + ".")) { source.Add(rule); foreach (var rule2 in rule.SubRules) { if (rule2.Title.StartsWith(number + ".")) { source.Add(rule2); source.AddRange(rule2.SubRules); break; } } break; } } } else if (number == 10) { source.Add(rules.Last()); source.AddRange(rules.Last().SubRules); } if (source.Count > 0) { if (addToHistory) { SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; ScrollViewer scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(list, 0), 0) as ScrollViewer; if (scrollViewer != null) { actualSearch.VerticalOffset = scrollViewer.VerticalOffset; } history.Add(actualSearch); actualSearch = new HistoryItem(HistoryType.Number, number); } list.ItemsSource = source; homeButton.IsEnabled = number != 0; } }