// Add the selected field in the "group by" list to the "order by" list private void AddSortFieldClicked(object sender, EventArgs e) { // Verify that there is a selected field in the "group by" list if (GroupFieldsListBox.SelectedItem == null) { return; } // Get the name of the selected field and ensure that it's in the list of selected group fields (checked on in the list, e.g.) string selectedFieldName = GroupFieldsListBox.SelectedItem.ToString(); if (!_groupByFields.Contains(selectedFieldName)) { DisplayAlert("Only fields used for grouping can be used to order results.", "Query", "OK"); return; } // Verify that the field isn't already in the "order by" list OrderFieldOption existingOrderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == selectedFieldName); if (existingOrderBy == null) { // Create a new OrderBy for this field and add it to the collection (default to ascending sort order) OrderBy newOrderBy = new OrderBy(selectedFieldName, SortOrder.Ascending); OrderFieldOption orderField = new OrderFieldOption(false, newOrderBy); _orderByFields.Add(orderField); } }
// Toggle the sort order (ascending/descending) for the field selected in the sort fields list private void ChangeFieldSortOrder(object sender, EventArgs e) { // Verify that there is a selected sort field in the list OrderFieldOption selectedSortField = OrderByFieldsListBox.SelectedItem as OrderFieldOption; if (selectedSortField == null) { return; } // Create a new order field info to define the sort for the selected field OrderBy newOrderBy = new OrderBy(selectedSortField.OrderInfo.FieldName, selectedSortField.OrderInfo.SortOrder); OrderFieldOption newSortDefinition = new OrderFieldOption(true, newOrderBy); // Toggle the sort order from the current value if (newSortDefinition.OrderInfo.SortOrder == SortOrder.Ascending) { newSortDefinition.OrderInfo.SortOrder = SortOrder.Descending; } else { newSortDefinition.OrderInfo.SortOrder = SortOrder.Ascending; } // Add the new OrderBy at the same location in the collection and remove the old one _orderByFields.Insert(_orderByFields.IndexOf(selectedSortField), newSortDefinition); _orderByFields.Remove(selectedSortField); }
// Handle the dismiss event on the dialog to raise a custom event that passes the updated order fields back public override void OnDismiss(IDialogInterface dialog) { base.OnDismiss(dialog); // Verify the event has listeners if (OrderFieldDialogClosed != null) { // Get an array of checked list item positions (indices) SparseBooleanArray checkedItemsArray = _orderFieldsListView.CheckedItemPositions; // Loop through all the available order fields for (int i = 0; i < _potentialOrderByFields.Count; i++) { // Initially set each order option to false OrderFieldOption orderOption = _potentialOrderByFields[i]; orderOption.OrderWith = false; // If the item was checked in the list view, set the order option to true if (checkedItemsArray.KeyAt(i) == i && checkedItemsArray.ValueAt(i)) { orderOption.OrderWith = true; } } // Raise the event and pass back the updated list of order field options OrderFieldDialogClosed(this, _potentialOrderByFields); } }
private void OrderBySwitched(object sender, EventArgs e) { // Use the control's tag to get the row that was changed nint index = (sender as UISwitch).Tag; // Get the corresponding field and update it's choice as a sort field OrderFieldOption orderByOption = _potentialOrderByFields.ElementAt((int)index); orderByOption.OrderWith = (sender as UISwitch).On; }
// Remove the selected field from the list of "order by" fields private void RemoveSortFieldClicked(object sender, EventArgs e) { // Verify that there is a selected item in the "order by" list if (OrderByFieldsListBox.SelectedItem == null) { return; } // Get the selected OrderFieldOption object and remove it from the collection OrderFieldOption selectedOrderBy = OrderByFieldsListBox.SelectedItem as OrderFieldOption; _orderByFields.Remove(selectedOrderBy); }
private void ShowGroupFields(object sender, EventArgs e) { // Create a dialog for choosing fields to group results with GroupFieldsDialog groupFieldsDialog = new GroupFieldsDialog(_groupByFields); // Handle the dialog closing event to read the selected fields groupFieldsDialog.GroupFieldDialogClosed += (s, args) => { // Update the dictionary of group fields from the dialog _groupByFields = args; // Get the current list of group fields and create/update the sort field choices // (only fields selected for grouping can be used to order results) IEnumerable <KeyValuePair <string, bool> > currentGroupFields = _groupByFields.Where(field => field.Value == true); // Loop through the group fields foreach (KeyValuePair <string, bool> groupField in currentGroupFields) { // Check if this field is missing from the current sort field options OrderFieldOption existingOption = _orderByFields.Find((opt) => opt.OrderInfo.FieldName == groupField.Key); if (existingOption == null) { // If the field is missing, create a new OrderFieldOption and add it to the list existingOption = new OrderFieldOption(false, new OrderBy(groupField.Key, SortOrder.Ascending)); _orderByFields.Add(existingOption); } } // Also make sure to remove any 'order by' fields that were removed from the 'group by' list for (int i = _orderByFields.Count - 1; i >= 0; i--) { // If the order by field is not also one of the group fields, remove it from the list OrderFieldOption opt = _orderByFields.ElementAt(i); KeyValuePair <string, bool> existingGroupField = currentGroupFields.FirstOrDefault(field => field.Key == opt.OrderInfo.FieldName); if (existingGroupField.Key == null) { _orderByFields.RemoveAt(i); } } }; // Begin a transaction to show a UI fragment (group fields dialog) FragmentTransaction trans = FragmentManager.BeginTransaction(); groupFieldsDialog.Show(trans, "group_flds"); }
// Show fields the user can choose to sort results with (must be one of the group by fields) private void ShowOrderByFields(object sender, EventArgs e) { // Create a new table UITableViewController sortFieldsTable = new UITableViewController(UITableViewStyle.Plain); // Get the current list of group fields and create/update the sort field choices IEnumerable <KeyValuePair <string, bool> > sortFieldChoices = _groupByFields.Where(field => field.Value == true); foreach (KeyValuePair <string, bool> sortChoice in sortFieldChoices) { // If this group field is not in the list of available order fields, add it to the list OrderFieldOption existingOption = _orderByFields.Find(opt => opt.OrderInfo.FieldName == sortChoice.Key); if (existingOption == null) { existingOption = new OrderFieldOption(false, new OrderBy(sortChoice.Key, SortOrder.Ascending)); _orderByFields.Add(existingOption); } } // Also make sure to remove any order by fields that were removed as 'group by' fields for (int i = _orderByFields.Count - 1; i >= 0; i--) { // If this field is not in the grouped field list, remove it from the order fields list OrderFieldOption opt = _orderByFields.ElementAt(i); KeyValuePair <string, bool> existingGroupField = sortFieldChoices.FirstOrDefault(field => field.Key == opt.OrderInfo.FieldName); if (existingGroupField.Key == null) { _orderByFields.RemoveAt(i); } } // Create an instance of a custom data source to show the order fields OrderByFieldsDataSource sortFieldsDataSource = new OrderByFieldsDataSource(_orderByFields); // Set the data source on the table sortFieldsTable.TableView.Source = sortFieldsDataSource; // Show the table view this.NavigationController.PushViewController(sortFieldsTable, true); }
// Handle when the switch for a "group by" field is toggled on or off by adding or removing the field from the collection private void GroupFieldCheckChanged(object sender, EventArgs e) { // Get the check box that raised the event (group field) Switch groupFieldCheckBox = (sender as Switch); // Get the field name string fieldName = groupFieldCheckBox.BindingContext.ToString(); // See if the field is being added or removed from the "group by" list bool fieldAdded = groupFieldCheckBox.IsToggled; // See if the field already exists in the "group by" list bool fieldIsInList = _groupByFields.Contains(fieldName); // If the field is being added, and is NOT in the list, add it ... if (fieldAdded && !fieldIsInList) { _groupByFields.Add(fieldName); // Also add it to the "order by" list OrderBy orderBy = new OrderBy(fieldName, SortOrder.Ascending); OrderFieldOption orderOption = new OrderFieldOption(false, orderBy); _orderByFields.Add(orderOption); } // If the field is being removed and it IS in the list, remove it ... else if (!fieldAdded && fieldIsInList) { _groupByFields.Remove(fieldName); // Also check for this field in the "order by" list and remove if necessary (only group fields can be used to order results) OrderFieldOption orderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == fieldName); if (orderBy != null) { // Remove the field from the "order by" list _orderByFields.Remove(orderBy); } } }