Created
August 13, 2015 08:50
-
-
Save jamie94bc/850bfdb8214a220b82e5 to your computer and use it in GitHub Desktop.
Prettier presentation hint handling in MvvmCross
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class YourPlatformSpecificPresenter : PlatformSpecificMvxPresenter, IMvxViewPresenterWithDelegate { | |
public MvxViewPresenterDelegate Delegate { get; } = new MvxViewPresenterDelegate(); | |
public YourPlatformSpecificPresenter() { | |
this.AddPresentationHintHandler<MyPresentationHint>(hint => { | |
// Do something | |
}); | |
} | |
public override void ChangePresentation(MvxPresentationHint hint) { | |
if (!Delegate.HandleChangePresentation(hint)) { | |
base.ChangePresentation(hint); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IMvxViewPresenterWithDelegate : IMvxViewPresenter { | |
MvxViewPresenterDelegate Delegate { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvxViewPresenterDelegate { | |
private readonly Dictionary<Type, Action<MvxPresentationHint>> _presentationHintHandlers = new Dictionary<Type, Action<MvxPresentationHint>>(); | |
public void AddPresentationHintHandler<THint>(Action<THint> action) where THint : MvxPresentationHint { | |
_presentationHintHandlers[typeof(THint)] = hint => action((THint)hint); | |
} | |
public bool HandleChangePresentation(MvxPresentationHint hint) { | |
Action<MvxPresentationHint> handler; | |
if (_presentationHintHandlers.TryGetValue(hint.GetType(), out handler)) { | |
handler(hint); | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MvxViewPresenterWithDelegateExtensions { | |
public static void AddPresentationHintHandler<THint>(this IMvxViewPresenterWithDelegate viewPresenter, Action<THint> action) where THint : MvxPresentationHint { | |
viewPresenter.Delegate.AddPresentationHintHandler(action); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment