This repository has been archived by the owner on Dec 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
FBAlertViewCommands.m
95 lines (81 loc) · 2.89 KB
/
FBAlertViewCommands.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBAlertViewCommands.h"
#import "FBAlert.h"
#import "FBApplication.h"
#import "FBRouteRequest.h"
#import "FBSession.h"
@implementation FBAlertViewCommands
#pragma mark - <FBCommandHandler>
+ (NSArray *)routes
{
return
@[
[[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertTextCommand:)],
[[FBRoute POST:@"/alert/accept"] respondWithTarget:self action:@selector(handleAlertAcceptCommand:)],
[[FBRoute POST:@"/alert/dismiss"] respondWithTarget:self action:@selector(handleAlertDismissCommand:)],
[[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)],
];
}
#pragma mark - Commands
+ (id<FBResponsePayload>)handleAlertTextCommand:(FBRouteRequest *)request
{
FBSession *session = request.session;
NSString *alertText = [FBAlert alertWithApplication:session.application].text;
if (!alertText) {
return FBResponseWithStatus(FBCommandStatusNoAlertPresent, nil);
}
return FBResponseWithStatus(FBCommandStatusNoError, alertText);
}
+ (id<FBResponsePayload>)handleAlertAcceptCommand:(FBRouteRequest *)request
{
FBSession *session = request.session;
NSString *name = request.arguments[@"name"];
FBAlert *alert = [FBAlert alertWithApplication:session.application];
NSError *error;
if (!alert.isPresent) {
return FBResponseWithStatus(FBCommandStatusNoAlertPresent, nil);
}
if (name) {
if (![alert clickAlertButton:name error:&error]) {
return FBResponseWithError(error);
}
} else if (![alert acceptWithError:&error]) {
return FBResponseWithError(error);
}
return FBResponseWithOK();
}
+ (id<FBResponsePayload>)handleAlertDismissCommand:(FBRouteRequest *)request
{
FBSession *session = request.session;
NSString *name = request.arguments[@"name"];
FBAlert *alert = [FBAlert alertWithApplication:session.application];
NSError *error;
if (!alert.isPresent) {
return FBResponseWithStatus(FBCommandStatusNoAlertPresent, nil);
}
if (name) {
if (![alert clickAlertButton:name error:&error]) {
return FBResponseWithError(error);
}
} else if (![alert dismissWithError:&error]) {
return FBResponseWithError(error);
}
return FBResponseWithOK();
}
+ (id<FBResponsePayload>)handleGetAlertButtonsCommand:(FBRouteRequest *)request {
FBSession *session = request.session;
FBAlert *alert = [FBAlert alertWithApplication:session.application];
if (!alert.isPresent) {
return FBResponseWithStatus(FBCommandStatusNoAlertPresent, nil);
}
NSArray *labels = alert.buttonLabels;
return FBResponseWithStatus(FBCommandStatusNoError, labels);
}
@end