Skip to content

Commit

Permalink
Merge pull request #8247 from hansemannn/TIMOB-23797
Browse files Browse the repository at this point in the history
[TIMOB-23797] iOS10: Support Ti.UI.Clipboard.setItems including options
  • Loading branch information
AngelkPetkov committed Aug 30, 2016
2 parents 0a7e92e + c0c1fe7 commit 0daab04
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 3 deletions.
85 changes: 85 additions & 0 deletions apidoc/Titanium/UI/Clipboard/Clipboard.yml
Expand Up @@ -127,6 +127,29 @@ methods:
summary: New item of data.
type: String

- name: setItems
summary: Adds an array of items to a clipboard, and sets privacy options for all included items.
description: |
You can pass multiple options to the options object. Note that this API is mime-type based.
The key must be a valid mime-type and the value must be the value that matches the given
mime-type. If no valid mime-type is provided a a key, receiving items with
<Titanium.UI.Clipboard.getItems> will crash the app. See the below example for more information
on the usage.
parameters:
- name: items
type: ClipboardItemsType
since: "5.5.0"
osver: {ios: {min: "10.0"}}
platforms: [iphone, ipad]

- name: getItems
summary: Gets the items that have been specified earlier using <Titanium.UI.Clipboard.setItems>.
returns:
- type: Array<Dictionary>
since: "5.5.0"
osver: {ios: {min: "10.0"}}
platforms: [iphone, ipad]

examples:
- title: Copy Text to the Clipboard
example: |
Expand All @@ -142,3 +165,65 @@ examples:
Ti.UI.Clipboard.setText('hello');
Ti.API.info('Clipboard.hasText(), should be true: ' + Ti.UI.Clipboard.hasText()); // returns true on Android and 1 on iOS
Ti.API.info('Clipboard.getText(), should be hello: ' + Ti.UI.Clipboard.getText());
- title: Set multiple items including privacy options (iOS 10 and later)
example: |
The items are represented as an array that holds different objects of key-value items. Optionally,
you can set privacy options described in `Ti.UI.CLIPBOARD_OPTION_*`. Note that you can not have the
same two keys in one object and the key must match a valid mime-type. If no valid mime-type is specified
var win = Ti.UI.createWindow({
backgroundColor : "#fff"
});
var btn1 = Ti.UI.createButton({
title : "Set clipboard items",
top: 40
});
var btn2 = Ti.UI.createButton({
title : "Get clipboard items",
top: 80
});
btn1.addEventListener("click", function() {
var localOnly = Ti.UI.CLIPBOARD_OPTION_LOCAL_ONLY;
var expirationDate = Ti.UI.CLIPBOARD_OPTION_EXPIRATION_DATE;
Ti.UI.Clipboard.setItems({
items: [{
"text/plain": "John",
},{
"text/plain": "Doe"
}],
options: {
localOnly: true,
expirationDate: new Date(2020, 04, 20)
}
});
});
btn2.addEventListener("click", function() {
alert(Ti.UI.Clipboard.getItems());
});
win.add(btn1);
win.add(btn2);
win.open();
---
name: ClipboardItemsType
summary: Dictionary describing the items for <Titanium.UI.Clipboard.setItems>.
properties:
- name: items
summary: |
An array of key-value items to add to the clipboard. The key must a valid mime-type
matching the mime-type of the value.
type: Array<Dictionary>

- name: options
summary: |
The privacy options to apply to all the items on the clipboard. The available options are
described in `Ti.UI.CLIPBOARD_OPTION_*`. Depending on the key, the value can be a Date or
Boolean.
type: Dictionary
16 changes: 16 additions & 0 deletions apidoc/Titanium/UI/UI.yml
Expand Up @@ -1909,6 +1909,22 @@ properties:
permission: read-only
platforms: [android]

- name: CLIPBOARD_OPTION_LOCAL_ONLY
summary: Specifies that the clipboard items should not be available to other devices through Handoff.
type: String
permission: read-only
platforms: [iphone, ipad]
osver: {ios: {min: "10.0"}}
since: "5.5.0"

- name: CLIPBOARD_OPTION_EXPIRATION_DATE
summary: Specifies the time and date that you want the system to remove the clipboard items from the clipboard.
type: String
permission: read-only
platforms: [iphone, ipad]
osver: {ios: {min: "10.0"}}
since: "5.5.0"

- name: PICKER_TYPE_COUNT_DOWN_TIMER
summary: Use a picker with a countdown timer appearance, showing hours and minutes.
description: |
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/UI/Window.yml
Expand Up @@ -1413,7 +1413,7 @@ description: |
transform : Ti.UI.create2DMatrix().scale(1.1),
duration : 2000,
});
a.addEventListener('complete', function(){
a.addEventListener('complete', function() {
$.index.animate({
transform: Ti.UI.create2DMatrix(),
duration: 200
Expand Down
3 changes: 2 additions & 1 deletion iphone/Classes/TiUIClipboardProxy.h
Expand Up @@ -5,8 +5,8 @@
* Please see the LICENSE included with this distribution for details.
*/

#ifdef USE_TI_UICLIPBOARD
#import "TiProxy.h"

@interface TiUIClipboardProxy : TiProxy {
@private
}
Expand All @@ -24,3 +24,4 @@
-(void)setText:(id)args;

@end
#endif
77 changes: 77 additions & 0 deletions iphone/Classes/TiUIClipboardProxy.m
Expand Up @@ -5,6 +5,7 @@
* Please see the LICENSE included with this distribution for details.
*/

#ifdef USE_TI_UICLIPBOARD
#import "TiUIClipboardProxy.h"
#import "TiUtils.h"
#import "TiApp.h"
Expand All @@ -19,6 +20,7 @@
CLIPBOARD_TEXT,
CLIPBOARD_URI_LIST,
CLIPBOARD_IMAGE,
CLIPBOARD_COLOR,
CLIPBOARD_UNKNOWN
} ClipboardType;

Expand All @@ -40,6 +42,10 @@ static ClipboardType mimeTypeToDataType(NSString *mimeType)
{
return CLIPBOARD_IMAGE;
}
else if ([mimeType isEqualToString: @"color"])
{
return CLIPBOARD_COLOR;
}
else
{
// Something else, work from the MIME type.
Expand Down Expand Up @@ -90,6 +96,11 @@ -(void)clearData:(id)arg
board.images = nil;
break;
}
case CLIPBOARD_COLOR:
{
board.colors = nil;
break;
}
case CLIPBOARD_UNKNOWN:
default:
{
Expand Down Expand Up @@ -146,6 +157,10 @@ -(id)getData_:(NSString *)mimeType
{
return [board.URL absoluteString];
}
case CLIPBOARD_COLOR:
{
return [TiUtils hexColorValue:[board color]];
}
case CLIPBOARD_IMAGE:
{
UIImage *image = board.image;
Expand Down Expand Up @@ -212,6 +227,11 @@ -(id)hasData:(id)args
result=[board containsPasteboardTypes: UIPasteboardTypeListImage];
break;
}
case CLIPBOARD_COLOR:
{
result=[board containsPasteboardTypes: UIPasteboardTypeListColor];
break;
}
case CLIPBOARD_UNKNOWN:
default:
{
Expand Down Expand Up @@ -272,6 +292,57 @@ -(id)hasURLs:(id)unused
return NUMBOOL(NO);
}

-(void)setItems:(id)args
{
#if IS_XCODE_8
if ([TiUtils isIOS10OrGreater]) {
NSArray *items = [args objectForKey:@"items"];
NSDictionary *options = [args objectForKey:@"options"];

__block NSMutableArray *result = [[[NSMutableArray alloc] init] retain];

// The key of the items must be a string (mime-type)
for (id item in items) {
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
for (id key in item) {
ENSURE_TYPE(key, NSString);
[newDict setValue:[item valueForKey:key] forKey:mimeTypeToUTType(key)];
}
if (newDict != nil) {
[result addObject:newDict];
}
RELEASE_TO_NIL(newDict);
}

TiThreadPerformOnMainThread(^{
if (options == nil) {
[[UIPasteboard generalPasteboard] setItems:result];
} else {
[[UIPasteboard generalPasteboard] setItems:result options:options];
}
RELEASE_TO_NIL(result);
}, YES);
}
#endif
}

-(id)getItems:(id)unused
{
#if IS_XCODE_8
if ([TiUtils isIOS10OrGreater]) {
__block id result;

TiThreadPerformOnMainThread(^{
result = [[[UIPasteboard generalPasteboard] items] retain];
}, YES);

return [result autorelease];
}
#endif

return @[];
}

-(void)setData:(id)args
{
ENSURE_ARG_COUNT(args,2);
Expand Down Expand Up @@ -303,6 +374,11 @@ -(void)setData:(id)args
board.image = [TiUtils toImage: data proxy: self];
break;
}
case CLIPBOARD_COLOR:
{
board.color = [[TiUtils colorValue:data] color];
break;
}
case CLIPBOARD_UNKNOWN:
default:
{
Expand Down Expand Up @@ -333,3 +409,4 @@ -(void)setText:(id)arg
}

@end
#endif
8 changes: 8 additions & 0 deletions iphone/Classes/TiUtils.h
Expand Up @@ -330,6 +330,14 @@ typedef enum
*/
+(TiColor*)colorValue:(id)value;


/**
Converts a native color value into the string-color.
@param value The input value of a UIColor type.
@return The string-representation of the value.
*/
+(NSString*)hexColorValue:(UIColor *)color;

/**
Converts input value into the dimention type.
@param value The input value that could be converted to a color.
Expand Down
10 changes: 10 additions & 0 deletions iphone/Classes/TiUtils.m
Expand Up @@ -606,6 +606,16 @@ +(TiColor*)colorValue:(id)value
return nil;
}

+ (NSString *)hexColorValue:(UIColor *)color
{
const CGFloat *components = CGColorGetComponents(color.CGColor);

return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(components[0] * 255),
lroundf(components[1] * 255),
lroundf(components[2] * 255)];
}

+(TiDimension)dimensionValue:(id)value
{
return TiDimensionFromObject(value);
Expand Down
25 changes: 24 additions & 1 deletion iphone/Classes/UIModule.m
Expand Up @@ -624,9 +624,32 @@ -(NSNumber*)ATTRIBUTE_LINE_BREAK_BY_TRUNCATING_MIDDLE
}
#endif

#ifdef USE_TI_UICLIPBOARD
-(NSString*)CLIPBOARD_OPTION_LOCAL_ONLY
{
if ([TiUtils isIOS10OrGreater]) {
#if IS_XCODE_8
return UIPasteboardOptionLocalOnly;
#endif
} else {
return @"";
}
}
-(NSString*)CLIPBOARD_OPTION_EXPIRATION_DATE
{
if ([TiUtils isIOS10OrGreater]) {
#if IS_XCODE_8
return UIPasteboardOptionExpirationDate;
#endif
} else {
return @"";
}
}
#endif

MAKE_SYSTEM_PROP(TABLE_VIEW_SEPARATOR_STYLE_NONE,UITableViewCellSeparatorStyleNone);
MAKE_SYSTEM_PROP(TABLE_VIEW_SEPARATOR_STYLE_SINGLE_LINE,UITableViewCellSeparatorStyleSingleLine);

@end

#endif
#endif

0 comments on commit 0daab04

Please sign in to comment.