Created
November 29, 2011 00:00
-
-
Save Clancey/1402690 to your computer and use it in GitHub Desktop.
NotificationBar
This file contains hidden or 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
using System; | |
using ClanceysLib; | |
using MonoTouch.UIKit; | |
using MonoTouch.Foundation; | |
using System.Threading; | |
using MonoTouch.ObjCRuntime; | |
using System.Drawing; | |
using MonoTouch.CoreGraphics; | |
using System.Linq; | |
namespace Collaborate | |
{ | |
public static class NotificationBar | |
{ | |
private static UIToolbar theView; | |
private static NSObject notificationObserver; | |
private static bool isVisible; | |
private static bool isShown; | |
private static float height = 50; | |
private static UIButton closeButton; | |
private static UIButton messageLabel; | |
private static myDelegate Delegate = new myDelegate(); | |
private class myDelegate : NSObject | |
{ | |
[Export("fadeInDidFinish")] | |
public void FadeInDidFinish () | |
{ | |
isShown = true; | |
Thread thread = new Thread(NotificationBar.startCountdown); | |
thread.Start(); | |
} | |
[Export("fadeOutDidFinish")] | |
public void FadeOutDidFinish () | |
{ | |
NotificationBar.EnsureInvokedOnMainThread(delegate { | |
NotificationBar.theView.RemoveFromSuperview(); | |
}); | |
NotificationBar.isShown = false; | |
NotificationBar.isVisible = false; | |
} | |
} | |
static NotificationBar() | |
{ | |
var frame = UIScreen.MainScreen.Bounds; | |
startLocation = new PointF(0,frame.Height); | |
theView = new UIToolbar(new RectangleF(startLocation,new SizeF(frame.Width,height))); | |
theView.TintColor = Theme.NavigationTint; | |
messageLabel = new UIButton(new RectangleF(5,0,theView.Frame.Width - 10,theView.Frame.Height)); | |
messageLabel.SetTitleColor(UIColor.White,UIControlState.Normal); | |
messageLabel.BackgroundColor = UIColor.Clear; | |
theView.AddSubview(messageLabel); | |
closeButton = new UIButton(new RectangleF(new PointF(5,-Images.closeBoxImage.Size.Height / 2),Images.closeBoxImage.Size)); | |
closeButton.SetImage(Images.closeBoxImage,UIControlState.Normal); | |
closeButton.TouchDown += delegate { | |
hide(); | |
}; | |
theView.AddSubview(closeButton); | |
//theView.Alpha = 0.9f; | |
notificationObserver = NSNotificationCenter.DefaultCenter | |
.AddObserver("UIDeviceOrientationDidChangeNotification", DeviceRotated ); | |
} | |
private static UIDeviceOrientation lastOrientation = UIDeviceOrientation.Unknown; | |
private static void DeviceRotated(NSNotification notification){ | |
if(lastOrientation == UIDevice.CurrentDevice.Orientation) | |
return; | |
if(UIDevice.CurrentDevice.Orientation == UIDeviceOrientation.FaceDown || | |
UIDevice.CurrentDevice.Orientation == UIDeviceOrientation.FaceUp || | |
UIDevice.CurrentDevice.Orientation == UIDeviceOrientation.Unknown) | |
lastOrientation = lastOrientation == UIDeviceOrientation.Unknown ? UIDeviceOrientation.Portrait : lastOrientation; | |
else | |
lastOrientation = UIDevice.CurrentDevice.Orientation; | |
Console.WriteLine("rotated! " + lastOrientation); | |
EnsureInvokedOnMainThread(delegate{ | |
var bounds = UIApplication.SharedApplication.KeyWindow.Bounds; | |
switch (lastOrientation){ | |
case UIDeviceOrientation.Portrait: | |
case UIDeviceOrientation.PortraitUpsideDown: | |
setFrame(bounds.Width,bounds.Height,false); | |
break; | |
case UIDeviceOrientation.LandscapeLeft: | |
case UIDeviceOrientation.LandscapeRight: | |
setFrame(bounds.Height,bounds.Width,true); | |
break; | |
} | |
}); | |
} | |
private static PointF startLocation; | |
private static void setFrame(float width, float theHeight, bool transform) | |
{ | |
SizeF size = new SizeF(width,height); | |
startLocation = PointF.Empty; | |
startLocation = new PointF(0, theHeight); | |
theView.Frame = theView.Frame.SetSize(size); | |
messageLabel.Frame = messageLabel.Frame.SetSize(theView.Frame.Size.Subtract(new SizeF(10,0))); | |
theView.RemoveFromSuperview(); | |
if(isVisible) | |
{ | |
Thread thread = new Thread(reshow); | |
thread.Start(); | |
} | |
isVisible = false; | |
isShown = false; | |
} | |
static void reshow() | |
{ | |
Thread.Sleep(500); | |
theView.BeginInvokeOnMainThread(delegate { | |
startShow(); | |
}); | |
} | |
static float DegreesToRadians (int deg) | |
{ | |
return (float)(deg * Math.PI / 180.0); | |
} | |
public static void Show(string message) | |
{ | |
if(isVisible) | |
return; | |
messageLabel.SetTitle(message,UIControlState.Normal); | |
//Console.WriteLine("showing message: " + message); | |
isVisible = true; | |
startedDate = DateTime.Now; | |
EnsureInvokedOnMainThread( delegate {startShow();}); | |
} | |
private static void startShow() | |
{ | |
theView.Frame = theView.Frame.SetLocation(startLocation); | |
Console.WriteLine("notification frame: " + theView.Frame); | |
Util.MainWindow.Subviews.Last().AddSubview(theView); | |
UIView.BeginAnimations ("showNotification"); | |
UIView.SetAnimationDuration (.3f); | |
//UIView.SetAnimationCurve(UIViewAnimationCurve.EaseOut); | |
UIView.SetAnimationDelegate (Delegate); | |
theView.Frame = theView.Frame.SubtractLocation(0,height); | |
UIView.SetAnimationDidStopSelector (new Selector ("fadeInDidFinish")); | |
UIView.CommitAnimations (); | |
} | |
private static DateTime startedDate; | |
private static void startCountdown() | |
{ | |
while(startedDate > DateTime.Now.AddSeconds(-4)) | |
{ | |
} | |
//Console.WriteLine("closing message"); | |
theView.BeginInvokeOnMainThread(delegate { | |
if(isShown) | |
hide(); | |
}); | |
} | |
private static void hide() | |
{ | |
isShown = false; | |
//UIApplication.SharedApplication.KeyWindow.Add(theView); | |
UIView.BeginAnimations ("hideNotification"); | |
UIView.SetAnimationDuration (.3f); | |
//UIView.SetAnimationCurve(UIViewAnimationCurve.EaseOut); | |
UIView.SetAnimationDelegate (Delegate); | |
theView.Frame = theView.Frame.AddLocation(0,height + 20); | |
UIView.SetAnimationDidStopSelector (new Selector ("fadeOutDidFinish")); | |
UIView.CommitAnimations (); | |
} | |
private static bool IsMainThread() { | |
return Messaging.bool_objc_msgSend(GetClassHandle("NSThread"), new Selector("isMainThread").Handle); | |
} | |
private static IntPtr GetClassHandle (string clsName) | |
{ | |
return (new Class(clsName)).Handle; | |
} | |
private static void EnsureInvokedOnMainThread (Action action) | |
{ | |
if (IsMainThread ()) | |
{ | |
action (); | |
return; | |
} | |
theView.BeginInvokeOnMainThread (() => action()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment