forked from EddyVerbruggen/nativescript-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback.android.ts
119 lines (102 loc) · 3.97 KB
/
feedback.android.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
119
import { FeedbackCommon, FeedbackShowOptions, FeedbackHideOptions, FeedbackType, FeedbackPosition } from "./feedback.common";
import * as application from "tns-core-modules/application";
import * as utils from "tns-core-modules/utils/utils";
import { Color } from "tns-core-modules/color";
declare let com: any;
// Export the enums for devs not using TS
exports.FeedbackPosition = FeedbackPosition;
exports.FeedbackType = FeedbackType;
export class Feedback extends FeedbackCommon {
private lastAlert: any = null;
show(options: FeedbackShowOptions): Promise<any> {
return new Promise((resolve, reject) => {
this.lastAlert = null;
let alerter = com.tapadoo.alerter.Alerter.create(application.android.foregroundActivity)
.setTitle(options.title)
.setText(options.message)
.setDuration(options.duration ? options.duration : 4000);
if (options.icon) {
let resourceId: number = Feedback.getIconResourceId(options.icon);
if (resourceId === 0) {
console.log(`icon '${options.icon}' resource not found`);
} else {
alerter.setIcon(resourceId);
}
} else {
let resourcename = Feedback.getIconName(options.type);
if (resourcename !== null) {
alerter.setIcon(Feedback.getIconResourceId(resourcename));
} else {
alerter.showIcon(false);
}
}
alerter.setOnClickListener(
new android.view.View.OnClickListener({
onClick: (view => {
this.lastAlert.hide();
if (options.onTap) {
options.onTap();
}
})
})
);
this.lastAlert = alerter.show();
if (options.backgroundColor) {
this.lastAlert.setAlertBackgroundColor(options.backgroundColor.android);
} else {
this.lastAlert.setAlertBackgroundColor(Feedback.getBackgroundColor(options.type).android);
}
if (options.titleColor) {
let titleView = this.lastAlert.getTitle(); // android.widget.TextView
titleView.setTextColor(options.titleColor.android);
}
if (options.messageColor) {
let messageView = this.lastAlert.getText(); // android.widget.TextView
messageView.setTextColor(options.messageColor.android);
}
if (options.android && options.android.iconColor) {
let iconView = this.lastAlert.getIcon(); // android.widget.ImageView
iconView.setColorFilter(options.android.iconColor.android);
}
resolve();
});
}
private static getBackgroundColor(type?: FeedbackType): Color {
if (type === undefined || type === null || type as FeedbackType === FeedbackType.Custom) {
return new Color("#73b7e8");
} else if (type as FeedbackType === FeedbackType.Warning) {
return new Color("#f18b34");
} else if (type as FeedbackType === FeedbackType.Error) {
return new Color("#ee664c");
} else if (type as FeedbackType === FeedbackType.Info) {
return new Color("#516a78");
} else {
return new Color("#51ae8c");
}
}
private static getIconResourceId(resourcename: string): number {
let res = utils.ad.getApplicationContext().getResources();
return res.getIdentifier(resourcename, "drawable", utils.ad.getApplication().getPackageName());
}
private static getIconName(type?: FeedbackType): string {
if (type === undefined || type === null || type as FeedbackType === FeedbackType.Custom) {
return null;
} else if (type as FeedbackType === FeedbackType.Warning) {
return "warningicon";
} else if (type as FeedbackType === FeedbackType.Error) {
return "erroricon";
} else if (type as FeedbackType === FeedbackType.Info) {
return "infoicon";
} else {
return "successicon";
}
}
hide(options?: FeedbackHideOptions): Promise<any> {
return new Promise((resolve, reject) => {
if (this.lastAlert !== null) {
this.lastAlert.hide();
this.lastAlert = null;
}
});
}
}