Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 45 additions & 48 deletions tests/common/MonoTouch.Dialog/DialogViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace MonoTouch.Dialog {
/// </summary>
public class DialogViewController : UITableViewController {
public UITableViewStyle Style = UITableViewStyle.Grouped;
public event Action<NSIndexPath> OnSelection;
public event Action<NSIndexPath>? OnSelection;
#if !__TVOS__
UISearchBar searchBar;
UISearchBar? searchBar;
#endif
UITableView tableView;
UITableView? tableView;
RootElement root;
bool pushing;
bool dirty;
Expand All @@ -50,7 +50,7 @@ public RootElement Root {
}
}

EventHandler refreshRequested;
EventHandler? refreshRequested;
/// <summary>
/// If you assign a handler to this event before the view is shown, the
/// DialogViewController will have support for pull-to-refresh UI.
Expand Down Expand Up @@ -87,7 +87,7 @@ public bool EnableSearch {
// the user manually pulls it down.
public bool AutoHideSearch { get; set; }

public string SearchPlaceholder { get; set; }
public string? SearchPlaceholder { get; set; }

/// <summary>
/// Invoke this method to trigger a data refresh.
Expand Down Expand Up @@ -126,7 +126,7 @@ public void ReloadComplete ()
reloading = false;

#if !__TVOS__
RefreshControl.EndRefreshing ();
RefreshControl?.EndRefreshing ();
#endif
}

Expand All @@ -150,8 +150,8 @@ public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
}
#endif

Section [] originalSections;
Element [] [] originalElements;
Section []? originalSections;
Element [] []? originalElements;

/// <summary>
/// Allows caller to programatically activate the search bar and start the search process
Expand All @@ -162,7 +162,7 @@ public void StartSearch ()
return;

#if !__TVOS__
searchBar.BecomeFirstResponder ();
searchBar?.BecomeFirstResponder ();
#endif
originalSections = Root.Sections.ToArray ();
originalElements = new Element [originalSections.Length] [];
Expand All @@ -182,33 +182,24 @@ public virtual void FinishSearch ()
originalSections = null;
originalElements = null;
#if !__TVOS__
searchBar.ResignFirstResponder ();
searchBar?.ResignFirstResponder ();
#endif
ReloadData ();
}

public delegate void SearchTextEventHandler (object sender, SearchChangedEventArgs args);
public event SearchTextEventHandler SearchTextChanged;

public virtual void OnSearchTextChanged (string text)
{
if (SearchTextChanged is not null)
SearchTextChanged (this, new SearchChangedEventArgs (text));
}

public void PerformFilter (string text)
{
if (originalSections is null)
var sections = originalSections;
var elementsBySection = originalElements;
if (sections is null || elementsBySection is null)
return;

OnSearchTextChanged (text);

var newSections = new List<Section> ();

for (int sidx = 0; sidx < originalSections.Length; sidx++) {
Section newSection = null;
var section = originalSections [sidx];
Element [] elements = originalElements [sidx];
for (int sidx = 0; sidx < sections.Length; sidx++) {
Section? newSection = null;
var section = sections [sidx];
Element [] elements = elementsBySection [sidx];

for (int eidx = 0; eidx < elements.Length; eidx++) {
if (elements [eidx].Matches (text)) {
Expand All @@ -229,10 +220,6 @@ public void PerformFilter (string text)
ReloadData ();
}

public virtual void SearchButtonClicked (string text)
{
}

class SearchDelegate : UISearchBarDelegate {
DialogViewController container;

Expand Down Expand Up @@ -266,22 +253,22 @@ public override void TextChanged (UISearchBar searchBar, string searchText)
public override void CancelButtonClicked (UISearchBar searchBar)
{
searchBar.ShowsCancelButton = false;
container.searchBar.Text = "";
if (container.searchBar is not null)
container.searchBar.Text = "";
container.FinishSearch ();
searchBar.ResignFirstResponder ();
}
#endif

public override void SearchButtonClicked (UISearchBar searchBar)
{
container.SearchButtonClicked (searchBar.Text);
}
}

public class Source : UITableViewSource {
const float yboundary = 65;
WeakReference<DialogViewController> container;
protected DialogViewController Container => container.TryGetTarget (out var result) ? result : null;
protected DialogViewController? Container => container.TryGetTarget (out var result) ? result : null;
protected RootElement Root;

public Source (DialogViewController container)
Expand Down Expand Up @@ -311,12 +298,12 @@ public override nint NumberOfSections (UITableView tableView)
return Root.Sections.Count;
}

public override string TitleForHeader (UITableView tableView, nint section)
public override string? TitleForHeader (UITableView tableView, nint section)
{
return Root.Sections [(int) section].Caption;
}

public override string TitleForFooter (UITableView tableView, nint section)
public override string? TitleForFooter (UITableView tableView, nint section)
{
return Root.Sections [(int) section].Footer;
}
Expand All @@ -342,21 +329,26 @@ public override void WillDisplay (UITableView tableView, UITableViewCell cell, N

public override void RowDeselected (UITableView tableView, NSIndexPath indexPath)
{
Container.Deselected (indexPath);
var container = Container;
if (container is not null)
container.Deselected (indexPath);
}

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
var onSelection = Container.OnSelection;
var container = Container;
if (container is null)
return;
var onSelection = container.OnSelection;
if (onSelection is not null)
onSelection (indexPath);
Container.Selected (indexPath);
container.Selected (indexPath);
}

public override UIView GetViewForHeader (UITableView tableView, nint sectionIdx)
{
var section = Root.Sections [(int) sectionIdx];
return section.HeaderView;
return section.HeaderView!;
}
Comment thread
rolfbjarne marked this conversation as resolved.

public override nfloat GetHeightForHeader (UITableView tableView, nint sectionIdx)
Expand All @@ -370,7 +362,7 @@ public override nfloat GetHeightForHeader (UITableView tableView, nint sectionId
public override UIView GetViewForFooter (UITableView tableView, nint sectionIdx)
{
var section = Root.Sections [(int) sectionIdx];
return section.FooterView;
return section.FooterView!;
}
Comment thread
rolfbjarne marked this conversation as resolved.

public override nfloat GetHeightForFooter (UITableView tableView, nint sectionIdx)
Expand Down Expand Up @@ -466,13 +458,14 @@ void SetupSearch ()
// Can't create a UISearchBar in tvOS, you can only use one from a UISearchController,
// which require bigger changes, so just skip this for now.
#else
if (enableSearch) {
searchBar = new UISearchBar (new CGRect (0, 0, tableView.Bounds.Width, 44)) {
var tv = tableView;
if (enableSearch && tv is not null) {
searchBar = new UISearchBar (new CGRect (0, 0, tv.Bounds.Width, 44)) {
Delegate = new SearchDelegate (this)
};
if (SearchPlaceholder is not null)
searchBar.Placeholder = this.SearchPlaceholder;
tableView.TableHeaderView = searchBar;
tv.TableHeaderView = searchBar;
} else {
// Does not work with current Monotouch, will work with 3.0
// tableView.TableHeaderView = null;
Expand All @@ -482,6 +475,8 @@ void SetupSearch ()

public virtual void Deselected (NSIndexPath indexPath)
{
if (tableView is null)
return;
var section = root.Sections [(int) indexPath.Section];
var element = section.Elements [(int) indexPath.Row];

Expand All @@ -490,6 +485,8 @@ public virtual void Deselected (NSIndexPath indexPath)

public virtual void Selected (NSIndexPath indexPath)
{
if (tableView is null)
return;
var section = root.Sections [(int) indexPath.Section];
var element = section.Elements [(int) indexPath.Row];

Expand Down Expand Up @@ -531,7 +528,7 @@ void ConfigureTableView ()
#endif
}

public event EventHandler ViewAppearing;
public event EventHandler? ViewAppearing;

public override void ViewWillAppear (bool animated)
{
Expand All @@ -553,7 +550,7 @@ public override void ViewWillAppear (bool animated)
if (root.Caption is not null)
NavigationItem.Title = root.Caption;
if (dirty) {
tableView.ReloadData ();
tableView?.ReloadData ();
dirty = false;
}

Expand All @@ -579,11 +576,11 @@ public virtual Source CreateSizingSource (bool unevenRows)
return unevenRows ? new SizingSource (this) : new Source (this);
}

Source TableSource;
Source? TableSource;

void UpdateSource ()
{
if (root is null)
if (root is null || tableView is null)
return;

TableSource = CreateSizingSource (root.UnevenRows);
Expand All @@ -606,7 +603,7 @@ public void ReloadData ()
dirty = false;
}

public event EventHandler ViewDisappearing;
public event EventHandler? ViewDisappearing;

[Obsolete ("Use the ViewDisappearing event instead")]
public event EventHandler ViewDissapearing {
Expand Down
Loading
Loading