This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtoast.lcb
More file actions
135 lines (106 loc) · 4.46 KB
/
toast.lcb
File metadata and controls
135 lines (106 loc) · 4.46 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
/*
Copyright (C) 2017 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
A library for displaying toasts on mobile
Description:
A toast is a non-modal temporary notification displayed to the user.
*/
library com.livecode.library.toast
use com.livecode.foreign
use com.livecode.java
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Toast Notification Library"
metadata os is "android"
--==============================================================================
-- Foreign handlers
--==============================================================================
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
binds to "java:android.view.View>getContext()Landroid/content/Context;"
__safe foreign handler _JNI_ToastMakeText(in pContext as JObject, in pText as JString, in pDuration as JInt) returns JObject binds to "java:android.widget.Toast>makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;!static?ui"
__safe foreign handler _JNI_ToastShow(in pToast as JObject) returns nothing binds to "java:android.widget.Toast>show()V?ui"
__safe foreign handler _JNI_ToastCancel(in pToast as JObject) returns nothing binds to "java:android.widget.Toast>cancel()V?ui"
--==============================================================================
-- Private state and handlers
--==============================================================================
private variable mToast as optional JObject
private handler ParseDuration(in pDuration as any) returns Integer
variable tNumericDuration as optional Number
if pDuration is a string then
put pDuration parsed as number into tNumericDuration
if tNumericDuration is nothing then
if the lower of pDuration is "short" then
return 0
else if the lower of pDuration is "long" then
return 1
end if
else if tNumericDuration > 0 then
return the maximum of (the floor of (tNumericDuration * 1000)) and 2
end if
else if pDuration is a number and pDuration > 0 then
return the maximum of (the floor of (pDuration * 1000)) and 2
end if
throw "duration must be a positive number or 'short' or 'long'"
end handler
--==============================================================================
-- Public handlers
--==============================================================================
/**
Display a toast notification
Example:
mobileToast "Hello, I am a toast!", "short"
Parameters:
pMessage: The message to display
pDuration:
The duration of the notification. Either a positive integer, the string
"long" or the string "short".
Description:
Use the <mobileToast> handler to display a temporary non-modal
notification with a specified message, for the given duration.
*/
public handler mobileToast(in pMessage as String, in pDuration as any) returns nothing
mobileToastWithOptions(pMessage, pDuration, the empty array)
end handler
/*
Options array does not do anything at the moment.
*/
handler mobileToastWithOptions(in pMessage as String, in pDuration as any, in pOptions as Array) returns nothing
if the operating system is not "android" then
throw "not available on this platform"
end if
variable tDuration as Number
put ParseDuration(pDuration) into tDuration
mobileToastCancel()
put _JNI_ToastMakeText(_JNI_GetEngineContext(_JNI_GetAndroidEngine()), \
StringToJString(pMessage), \
tDuration) into mToast
_JNI_ToastShow(mToast)
end handler
/**
Cancel the current toast
Description:
Use <mobileToastCancel> to cancel the currently displayed toast.
*/
public handler mobileToastCancel()
if the operating system is not "android" then
throw "not available on this platform"
end if
if mToast is nothing then
return
end if
_JNI_ToastCancel(mToast)
put nothing into mToast
end handler
end library