-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfeedback.android.ts
More file actions
165 lines (140 loc) · 5.49 KB
/
feedback.android.ts
File metadata and controls
165 lines (140 loc) · 5.49 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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, android: any;
export { FeedbackType };
export { FeedbackPosition };
export class Feedback extends FeedbackCommon {
private lastAlert: any = null;
show(options: FeedbackShowOptions): Promise<void> {
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);
}
}
if (options.android && options.android.iconPulseEnabled !== undefined) {
alerter.enableIconPulse(options.android.iconPulseEnabled);
}
if (options.titleFont) {
const assetManger = utils.ad.getApplicationContext().getAssets();
const fontPath = `app/fonts/${options.titleFont}`;
const typeface = android.graphics.Typeface.createFromAsset(assetManger, fontPath);
alerter.setTitleTypeface(typeface);
}
if (options.messageFont) {
const assetManger = utils.ad.getApplicationContext().getAssets();
const fontPath = `app/fonts/${options.messageFont}`;
const typeface = android.graphics.Typeface.createFromAsset(assetManger, fontPath);
alerter.setTextTypeface(typeface);
}
alerter.setOnClickListener(
new android.view.View.OnClickListener({
onClick: (view => {
this.lastAlert.hide();
if (options.onTap) {
options.onTap();
}
})
})
);
if (options.onShow) {
alerter.setOnShowListener(
new com.tapadoo.alerter.OnShowAlertListener({
onShow: () => options.onShow(),
})
);
}
if (options.onHide) {
alerter.setOnHideListener(
new com.tapadoo.alerter.OnHideAlertListener({
onHide: () => options.onHide(),
})
);
}
this.lastAlert = alerter.show();
if (options.android && options.android.addToView) {
const parent = this.lastAlert.getParent();
parent.removeView(this.lastAlert);
options.android.addToView.addView(this.lastAlert);
}
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);
}
const titleSize = options.titleSize || 16;
const messageSize = options.messageSize || 13;
this.lastAlert.getTitle().setTextSize(titleSize);
this.lastAlert.getText().setTextSize(messageSize);
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<void> {
return new Promise((resolve) => {
if (this.lastAlert !== null) {
this.lastAlert.hide();
this.lastAlert = null;
}
resolve();
});
}
}