Skip to content

Commit

Permalink
add statusbarnotifier plugin for ios6
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Sep 13, 2012
1 parent d1ca95a commit 4e39187
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 0 deletions.
38 changes: 38 additions & 0 deletions iOS/StatusBarNotifier/FDStatusBarNotifierView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// StatusBarNotifierView.h
// StatusBarNotifier
//
// Created by Francesco Di Lorenzo on 05/09/12.
// Copyright (c) 2012 Francesco Di Lorenzo. All rights reserved.
//

#import <UIKit/UIKit.h>



@interface FDStatusBarNotifierView : UIView

@property (strong, nonatomic) NSString *message;
@property NSTimeInterval timeOnScreen; // seconds, default: 2s
@property id delegate;


- (id)initWithMessage:(NSString *)message;
- (id)initWithMessage:(NSString *)message delegate:(id /*<StatusBarNotifierViewDelegate>*/)delegate;

- (void)showInWindow:(UIWindow *)window;

@end


@protocol StatusBarNotifierViewDelegate <NSObject>
@optional

- (void)willPresentNotifierView:(FDStatusBarNotifierView *)notifierView; // before animation and showing view
- (void)didPresentNotifierView:(FDStatusBarNotifierView *)notifierView; // after animation
- (void)willHideNotifierView:(FDStatusBarNotifierView *)notifierView; // before hiding animation
- (void)didHideNotifierView:(FDStatusBarNotifierView *)notifierView; // after animation

- (void)notifierViewTapped:(FDStatusBarNotifierView *)notifierView; // user tap the status bar message

@end
133 changes: 133 additions & 0 deletions iOS/StatusBarNotifier/FDStatusBarNotifierView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// StatusBarNotifierView.m
// StatusBarNotifier
//
// Created by Francesco Di Lorenzo on 05/09/12.
// Copyright (c) 2012 Francesco Di Lorenzo. All rights reserved.
//

#import "FDStatusBarNotifierView.h"

@interface FDStatusBarNotifierView ()

@property (strong) UILabel *messageLabel;

@end

@implementation FDStatusBarNotifierView

#define kNotifierViewInitialFramePortrait CGRectMake(0, 20, 320, 20)
#define kNotifierViewFinalFramePortrait CGRectMake(0, 0, 320, 20)
//#define kNotifierViewInitialFrameLandscape CGRectMake(0, 20, 480, 20)
//#define kNotifierViewFinalFrameLandscape CGRectMake(0, 0, 480, 20)

#define kMessageLabelInitialFramePortrait CGRectMake(10, 0, 300, 20)
#define kMessageLabelInitialFrameLandscape CGRectMake(10, 0, 460, 20)

- (id)init {
self = [super init];
if (self) {
self.frame = kNotifierViewInitialFramePortrait;

self.messageLabel = [[UILabel alloc] initWithFrame:kMessageLabelInitialFramePortrait];
self.messageLabel.textColor = [UIColor whiteColor];
self.messageLabel.backgroundColor = [UIColor blackColor];
self.messageLabel.textAlignment = UITextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:self.messageLabel];

self.timeOnScreen = 2.0;
}
return self;
}

- (id)initWithMessage:(NSString *)message {
self = [super init];
if (self) {
self.frame = kNotifierViewInitialFramePortrait;
self.message = message;
self.backgroundColor = [UIColor blackColor];

self.messageLabel = [[UILabel alloc] initWithFrame:kMessageLabelInitialFramePortrait];
self.messageLabel.textColor = [UIColor whiteColor];
self.messageLabel.text = message;
self.messageLabel.backgroundColor = [UIColor blackColor];
self.messageLabel.textAlignment = UITextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:self.messageLabel];

self.timeOnScreen = 2.0;
}
return self;

}

- (id)initWithMessage:(NSString *)message delegate:(id /*<StatusBarNotifierViewDelegate>*/)delegate {
self = [super init];
if (self) {
self.frame = kNotifierViewInitialFramePortrait;
self.delegate = delegate;
self.message = message;
self.backgroundColor = [UIColor blackColor];

self.messageLabel = [[UILabel alloc] initWithFrame:kMessageLabelInitialFramePortrait];
self.messageLabel.textColor = [UIColor whiteColor];
self.messageLabel.text = message;
self.messageLabel.backgroundColor = [UIColor blackColor];
self.messageLabel.textAlignment = UITextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:12];
[self addSubview:self.messageLabel];

self.timeOnScreen = 2.0;
}
return self;
}

- (void)showInWindow:(UIWindow *)window {
if (self.delegate && [self.delegate respondsToSelector:@selector(willPresentNotifierView:)])
[self.delegate willPresentNotifierView:self];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[window insertSubview:self atIndex:0];

[UIView animateWithDuration:.4 animations:^{
self.frame = kNotifierViewFinalFramePortrait;
} completion:^(BOOL finished){

if (self.delegate && [self.delegate respondsToSelector:@selector(didPresentNotifierView:)])
[self.delegate didPresentNotifierView:self];

[NSTimer scheduledTimerWithTimeInterval:self.timeOnScreen target:self selector:@selector(hide) userInfo:nil repeats:NO];

}];
}

- (void)hide {

if (self.delegate && [self.delegate respondsToSelector:@selector(willHideNotifierView:)])
[self.delegate willHideNotifierView:self];

[UIView animateWithDuration:.4 animations:^{
self.frame = kNotifierViewInitialFramePortrait;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
} completion:^(BOOL finished){
if (finished) {

if (self.delegate && [self.delegate respondsToSelector:@selector(didHideNotifierView:)])
[self.delegate didHideNotifierView:self];

[self removeFromSuperview];
}
}];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate notifierViewTapped:self];
}

- (void)setMessage:(NSString *)message {
_message = message;
self.messageLabel.text = message;
}

@end
8 changes: 8 additions & 0 deletions iOS/StatusBarNotifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cordova status bar notifier plugin

depends on this: http://github.com/frankdilo/FDStatusBarNotifierView

window.plugins.statusBarNotifier.show('hello yes this is dog')
window.plugins.statusBarNotifier.show('this will display for 5 seconds', 5.0)

ios6+ only (I think)
17 changes: 17 additions & 0 deletions iOS/StatusBarNotifier/StatusBarNotifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "FDStatusBarNotifierView.h"

#import <Cordova/CDVPlugin.h>

@interface StatusBarNotifier: CDVPlugin {
NSMutableDictionary* callbackIds;
NSString* messageField;
}

@property (nonatomic, retain) NSMutableDictionary* callbackIds;
@property (nonatomic, retain) NSString* messageField;

- (void) show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end
14 changes: 14 additions & 0 deletions iOS/StatusBarNotifier/StatusBarNotifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;(function(cordova) {

function StatusBarNotifier() {}

StatusBarNotifier.prototype.show = function(text, timeOnScreen, callback) {
if (typeof timeOnScreen === "function") callback = timeOnScreen
if (!callback) callback = function() {}
cordova.exec(callback, callback, "StatusBarNotifier", "getDeviceDetails", [{text: text, timeOnScreen: timeOnScreen}])
}

if (!window.plugins) window.plugins = {}
window.plugins.deviceDetails = new StatusBarNotifier()

})(window.cordova || window.Cordova || window.PhoneGap);
32 changes: 32 additions & 0 deletions iOS/StatusBarNotifier/StatusBarNotifier.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#import "StatusBarNotifier.h"

@implementation StatusBarNotifier

@synthesize callbackIds = _callbackIds;
@synthesize messageField;

- (NSMutableDictionary*)callbackIds {
if(_callbackIds == nil) {
_callbackIds = [[NSMutableDictionary alloc] init];
}
return _callbackIds;
}

- (void)show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
[self.callbackIds setValue:[arguments pop] forKey:@"show"];
AppDelegate* appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
[self setMessageField:[options objectForKey:@"text"] ?: @""];
float timeOnScreen = [[options objectForKey:@"timeOnScreen"] floatValue] ?: 3.0;
FDStatusBarNotifierView *notifierView = [[FDStatusBarNotifierView alloc] initWithMessage:self.messageField];
notifierView.timeOnScreen = timeOnScreen;
[notifierView showInWindow:appDelegate.window];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:[self.callbackIds valueForKey:@"show"]]];
}

- (void) dealloc {
[_callbackIds dealloc];
[super dealloc];
}

@end

0 comments on commit 4e39187

Please sign in to comment.