forked from bradmartin/nativescript-snackbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnackbar.ios.ts
118 lines (106 loc) · 2.87 KB
/
snackbar.ios.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
declare var NSBundle, SSSnackbar: any;
import { device } from "platform";
export class SnackBar {
private _snackbar = null;
private _isDismissedManual: boolean = false;
public simple(snackText: string) {
return new Promise((resolve, reject) => {
let timeout = 3;
try {
this._snackbar = SSSnackbar.snackbarWithMessageActionTextDurationActionBlockDismissalBlock(
snackText,
this._getActionText(),
timeout,
args => {
//Action, Do Nothing, just close it
this._snackbar.dismiss(); //Force close
resolve({
command: "Dismiss",
reason: "Manual",
event: args
});
},
args => {
//Dismissal, Do Nothing
resolve({
command: "Dismiss",
reason: "Timeout",
event: args
});
}
);
this._snackbar.show();
} catch (ex) {
reject(ex);
}
});
}
public action(options) {
return new Promise((resolve, reject) => {
try {
if (!options.hideDelay) options.hideDelay = 3000;
this._snackbar = SSSnackbar.snackbarWithMessageActionTextDurationActionBlockDismissalBlock(
options.snackText,
options.actionText,
options.hideDelay / 1000,
args => {
resolve({
command: "Action",
event: args
});
},
args => {
let reason = this._isDismissedManual ? "Manual" : "Timeout";
this._isDismissedManual = false; //reset
resolve({
command: "Dismiss",
reason: reason,
event: args
});
}
);
this._snackbar.show();
} catch (ex) {
reject(ex);
}
});
}
public dismiss(options) {
return new Promise((resolve, reject) => {
if (this._snackbar !== null && this._snackbar != "undefined") {
try {
this._isDismissedManual = true;
this._snackbar.dismiss();
//Return AFTER the item is dismissed, 200ms delay
setTimeout(() => {
resolve({
action: "Dismiss",
reason: "Manual"
});
}, 200);
} catch (ex) {
reject(ex);
}
} else {
resolve({
action: "None",
message: "No actionbar to dismiss"
});
}
});
}
public getSnackbar() {
return this._snackbar;
}
private _getActionText() {
let bundle = typeof NSBundle.mainBundle === "function"
? NSBundle.mainBundle()
: NSBundle.mainBundle;
var actionText = bundle.objectForInfoDictionaryKey("NSSnackBarActionText");
if (actionText != "" && actionText != null) {
return actionText;
} else {
return "Close";
}
}
}