Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animate cell deselection only when navigating back to that table view #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 43 additions & 11 deletions MonoTouch.Dialog/DialogViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class DialogViewController : UITableViewController
bool pushing;
bool dirty;
bool reloading;
Element nestedNavigationTriggerElement;

/// <summary>
/// The root element displayed by the DialogViewController, the value can be changed during runtime to update the contents.
Expand Down Expand Up @@ -461,17 +462,32 @@ public override float GetHeightForRow (UITableView tableView, MonoTouch.Foundati
/// </summary>
public void ActivateController (UIViewController controller)
{
dirty = true;

var parent = ParentViewController;
var nav = parent as UINavigationController;

// We can not push a nav controller into a nav controller
if (nav != null && !(controller is UINavigationController))
nav.PushViewController (controller, true);
else
PresentModalViewController (controller, true);
}
ActivateController(controller, null);
}

/// <summary>
/// Activates a nested view controller from the DialogViewController.
/// If the view controller is hosted in a UINavigationController it
/// will push the result. Otherwise it will show it as a modal
/// dialog.
/// Records the element that triggered the navigation, and will
/// animate the deselection of that element when returning to this
/// DialogViewController.
/// </summary>
public void ActivateController (UIViewController controller, Element triggerElement)
{
dirty = true;
nestedNavigationTriggerElement = triggerElement;

var parent = ParentViewController;
var nav = parent as UINavigationController;

// We can not push a nav controller into a nav controller
if (nav != null && !(controller is UINavigationController))
nav.PushViewController (controller, true);
else
PresentModalViewController (controller, true);
}

/// <summary>
/// Dismisses the view controller. It either pops or dismisses
Expand Down Expand Up @@ -582,6 +598,17 @@ public override void ViewWillAppear (bool animated)
tableView.ReloadData ();
dirty = false;
}

if (nestedNavigationTriggerElement != null)
{
// TableView.ReloadData clears selection.
// Therefor reselect without animation now, then deslect animated.
// This allow the deselection animation to still play when navigating back.
var path = nestedNavigationTriggerElement.IndexPath;
tableView.SelectRow (path, false, UITableViewScrollPosition.None);
tableView.DeselectRow (path, true);
nestedNavigationTriggerElement = null;
}
}

public bool Pushing {
Expand Down Expand Up @@ -649,12 +676,14 @@ public override void ViewWillDisappear (bool animated)
public DialogViewController (RootElement root) : base (UITableViewStyle.Grouped)
{
this.root = root;
ClearsSelectionOnViewWillAppear = false;
}

public DialogViewController (UITableViewStyle style, RootElement root) : base (style)
{
Style = style;
this.root = root;
ClearsSelectionOnViewWillAppear = false;
}

/// <summary>
Expand All @@ -672,17 +701,20 @@ public DialogViewController (RootElement root, bool pushing) : base (UITableView
{
this.pushing = pushing;
this.root = root;
ClearsSelectionOnViewWillAppear = false;
}

public DialogViewController (UITableViewStyle style, RootElement root, bool pushing) : base (style)
{
Style = style;
this.pushing = pushing;
this.root = root;
ClearsSelectionOnViewWillAppear = false;
}
public DialogViewController (IntPtr handle) : base(handle)
{
this.root = new RootElement ("");
ClearsSelectionOnViewWillAppear = false;
}
}
}
9 changes: 4 additions & 5 deletions MonoTouch.Dialog/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public override void Selected (DialogViewController dvc, UITableView tableView,
vc.View.AutosizesSubviews = true;
vc.View.AddSubview (web);

dvc.ActivateController (vc);
dvc.ActivateController (vc, this);
web.LoadRequest (NSUrlRequest.FromUrl (nsUrl));
}
}
Expand Down Expand Up @@ -1296,7 +1296,7 @@ public override void Selected (DialogViewController dvc, UITableView tableView,

default:
case UIUserInterfaceIdiom.Phone:
dvc.ActivateController (picker);
dvc.ActivateController (picker, this);
break;
}
currentController = dvc;
Expand Down Expand Up @@ -1828,7 +1828,7 @@ public override void Selected (DialogViewController dvc, UITableView tableView,

vc.View.BackgroundColor = BackgroundColor;
vc.View.AddSubview (datePicker);
dvc.ActivateController (vc);
dvc.ActivateController (vc, this);

datePicker.Frame = PickerFrameWithSize (datePicker.SizeThatFits (SizeF.Empty));
}
Expand Down Expand Up @@ -2816,10 +2816,9 @@ protected virtual UIViewController MakeViewController ()

public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
tableView.DeselectRow (path, false);
var newDvc = MakeViewController ();
PrepareDialogViewController (newDvc);
dvc.ActivateController (newDvc);
dvc.ActivateController (newDvc, this);
}

public void Reload (Section section, UITableViewRowAnimation animation)
Expand Down
2 changes: 1 addition & 1 deletion MonoTouch.Dialog/Elements/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override void Selected (DialogViewController dvc, UITableView tableView,
Autorotate = true
};
PrepareDialogViewController (newDvc);
dvc.ActivateController (newDvc);
dvc.ActivateController (newDvc, this);
return;
}
} catch (Exception ee){
Expand Down
3 changes: 2 additions & 1 deletion Sample/DemoDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ void msgSelected (DialogViewController dvc, UITableView tv, NSIndexPath path)
"This is very simple!")
}
}, true);
dvc.ActivateController (np);
var element = dvc.Root[path.Section][path.Row];
dvc.ActivateController (np,element);
}
}

Expand Down