-
Notifications
You must be signed in to change notification settings - Fork 5
/
appirater.lua
333 lines (259 loc) · 11.1 KB
/
appirater.lua
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
------------------------------------------------------------------------------
------------------------------------------------------------------------------
--[[
This file is part of Appirater for Corona SDK.
Copyright (c) 2012, RedBytes Software
All rights reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
* appirater.lua
* appirater
*
* Created on 12th April 2012 by:
Vimal Venugopolan - [email protected] Twitter: @vvvimal22 Facebook: www.facebook.com/vvvimal
Aliasgar Poonawala - [email protected] Twitter: @aliasgar84 Facebook: www.facebook.com/aliasgar.poonawala
* http://www.redbytes.in
* Copyright 2012 RedBytes Software. All rights reserved.
]]--
------------------------------------------------------------------------------
------------------------------------------------------------------------------
module(..., package.seeall)
require("sqlite3")
local appirater_App_ID = "301377083"
local appirater_App_Name = "RocketMouse"
local appirater_Message_Part1 = "If you enjoy using "
local appirater_Message_Part2 = ", would you mind taking a moment to rate it? It won't take more than a minute. Thanks for your support!"
local appirater_Message_Title = "Rate"
local appirater_Cancel_Button = "No, Thanks"
local appirater_Rate_Button = "Rate"
local appirater_Rate_Later = "Remind me later"
local appirater_Days_Until_Prompt = 0.01
local appirater_Uses_Until_Prompt = 3
local appirater_Sig_Events_Until_Prompt = 5
local appirater_Time_Before_Reminding = 0.01
local appirater_Debug = false
local canPromptForRating = false
local ratingAlert = nil
local userDefaults = {}
local templateReviewURL = "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=";
------------------------ Reachability Listener ------------------------------
function networkReachabilityListener(event)
print( "address", event.address )
print( "isReachable", event.isReachable )
print("isConnectionRequired", event.isConnectionRequired)
print("isConnectionOnDemand", event.isConnectionOnDemand)
print("IsInteractionRequired", event.isInteractionRequired)
print("IsReachableViaCellular", event.isReachableViaCellular)
print("IsReachableViaWiFi", event.isReachableViaWiFi)
if event.isReachable == "isReachable" then
return true
elseif event.isReachable ~= "isReachable" then
return false
end
end
------------------------- Check if connected to internet ----------------------
function connectedToNetwork()
if network.canDetectNetworkStatusChanges then
network.setStatusListener( "www.apple.com", MyNetworkReachabilityListener )
return true
else
print("network reachability not supported on this platform")
return false
end
end
-------------------------- rating conditions met --------------------------------
function ratingConditionsHaveBeenMet()
if (appirater_Debug == true) then
return true
end
local timeSinceFirstLaunch = os.difftime( os.time(), userDefaults[1].kAppiraterFirstUseDate)
local timeUntilRate = 60 * 60 * 24 * appirater_Days_Until_Prompt
if( timeSinceFirstLaunch < timeUntilRate) then
return false
end
-- check if the app has been used enough
local useCount = userDefaults[1].kAppiraterUseCount
if (useCount <= appirater_Uses_Until_Prompt) then
return false
end
-- check if the user has done enough significant events
local sigEventCount = userDefaults[1].kAppiraterSignificantEventCount
if (sigEventCount <= appirater_Sig_Events_Until_Prompt) then
return false
end
-- has the user previously declined to rate this version of the app?
if (userDefaults[1].kAppiraterDeclinedToRate == 1) then
return false
end
-- has the user already rated the app?
if (userDefaults[1].kAppiraterRatedCurrentVersion == 1) then
return false
end
-- if the user wanted to be reminded later, has enough time passed?
local timeSinceReminderRequest = os.difftime( os.time(), userDefaults[1].kAppiraterReminderRequestDate)
local timeUntilReminder = 60 * 60 * 24 * appirater_Time_Before_Reminding
if( timeSinceReminderRequest < timeUntilReminder) then
return false
end
return true
end
-------------------------- Increment Use Count -----------------------------------
function incrementUseCount()
-- check if the first use date has been set. if not, set it.
if(userDefaults[1].kAppiraterFirstUseDate == 0) then
userDefaults[1].kAppiraterFirstUseDate = os.time()
end
-- increment the use count
local useCount = userDefaults[1].kAppiraterUseCount
useCount = useCount + 1
userDefaults[1].kAppiraterUseCount = useCount
print("APPIRATER Use count: "..useCount)
if(appirater_Debug == true) then
print("APPIRATER Use count: "..useCount)
end
end
-----------------------------Increment Significant Event Count ---------------------
function incrementSignificantEventCount()
-- check if the first use date has been set. if not, set it.
if(userDefaults[1].kAppiraterFirstUseDate == 0) then
userDefaults[1].kAppiraterFirstUseDate = os.time()
end
-- increment the significant event count
local sigEventCount = userDefaults[1].kAppiraterSignificantEventCount
sigEventCount = sigEventCount + 1
userDefaults[1].kAppiraterSignificantEventCount = sigEventCount
if(appirater_Debug == true) then
print("APPIRATER Significant event count: "..sigEventCount)
end
end
----------------------------- Increment and rate ----------------------------------
function incrementAndRate(canPromptForRating)
incrementUseCount()
if(canPromptForRating and ratingConditionsHaveBeenMet() and connectedToNetwork()) then
showRatingAlert()
end
end
----------------------------- Increment Significant Event and rate ----------------------------------
function incrementSignificantEventAndRate(canPromptForRating)
incrementSignificantEventCount()
if(canPromptForRating and ratingConditionsHaveBeenMet() and connectedToNetwork()) then
showRatingAlert()
end
end
-------------------------- Application Launched ---------------------------------
function appLaunched(canPromptForRating)
connectedToNetwork()
createSQLiteDB()
userDefaults = selectSQLiteDB()
if #userDefaults == 0 then
local tempArray = {}
tempArray.kAppiraterFirstUseDate = os.time()
tempArray.kAppiraterUseCount = 0
tempArray.kAppiraterSignificantEventCount = 0
tempArray.kAppiraterCurrentVersion = ""
tempArray.kAppiraterRatedCurrentVersion = 0
tempArray.kAppiraterDeclinedToRate = 0
tempArray.kAppiraterReminderRequestDate = 0
table.insert(userDefaults, 1, tempArray)
end
end
-------------------------- hiding rating alert ---------------------------
function hideRatingAlert()
if(ratingAlert) then
--if(ratingAlert.isVisible == true) then
if(appirater_Debug == true) then
print("Appirater hiding Alert")
end
native.cancelAlert( ratingAlert )
--end
end
end
-------------------------- Application Will Resign Active ---------------------------
function appWillResignActive()
if(appirater_Debug == true) then
print("Appirater appWillResignActive")
end
insertSQLiteDB()
hideRatingAlert()
end
------------------------- Application entered into foreground -------------------------
function appEnteredForeground(canPromptForRating)
userDefaults = selectSQLiteDB()
incrementAndRate(canPromptForRating)
end
--------------------------- user Did Significant Event thread---------------------------------
function userDidSignificantEvent(canPromptForRating)
incrementSignificantEventAndRate(canPromptForRating)
end
------------------------- alert asking for rating the app ---------------------------
function showRatingAlert()
ratingAlert = native.showAlert( appirater_Message_Title, appirater_Message_Part1..appirater_App_Name..appirater_Message_Part2, { appirater_Cancel_Button, appirater_Rate_Button, appirater_Rate_Later}, onAlertComplete )
end
------------------------- alert button click event handler ----------------------------
function onAlertComplete( event )
if "clicked" == event.action then
local i = event.index
if 1 == i then
userDefaults[1].kAppiraterDeclinedToRate = 1
elseif 2 == i then
system.openURL( templateReviewURL..appirater_App_ID )
elseif 3 == i then
userDefaults[1].kAppiraterReminderRequestDate = 1
end
end
end
----------------------------------SQLite DB functions -------------------------------
function createSQLiteDB()
local path = system.pathForFile("dataBase.db", system.DocumentsDirectory)
db = sqlite3.open( path )
--Setup the table if it doesn't exist
local tablesetup = [[CREATE TABLE IF NOT EXISTS NSUserDefaults (id INTEGER PRIMARY KEY ON CONFLICT REPLACE, kAppiraterFirstUseDate INTEGER, kAppiraterUseCount INTEGER, kAppiraterSignificantEventCount INTEGER, kAppiraterCurrentVersion, kAppiraterRatedCurrentVersion INTEGER, kAppiraterDeclinedToRate INTEGER, kAppiraterReminderRequestDate INTEGER); ]]
print(tablesetup)
db:exec( tablesetup )
db:close()
end
function insertSQLiteDB()
print("insertSQLiteDB")
--Open data.db. If the file doesn't exist it will be created
local path = system.pathForFile("dataBase.db", system.DocumentsDirectory)
db = sqlite3.open( path )
--Insert the data in table
local myValue = ""..userDefaults[1].kAppiraterFirstUseDate..", "..userDefaults[1].kAppiraterUseCount..", "..userDefaults[1].kAppiraterSignificantEventCount..", '"..userDefaults[1].kAppiraterCurrentVersion.."', "..userDefaults[1].kAppiraterRatedCurrentVersion..", "..userDefaults[1].kAppiraterDeclinedToRate..", "..userDefaults[1].kAppiraterReminderRequestDate..""
local tablefill = [[INSERT OR REPLACE INTO NSUserDefaults VALUES ( 1, ]]..myValue..[[ ); ]]
print(tablefill)
db:exec( tablefill )
myValue = ""
db:close()
end
function selectSQLiteDB()
print("selectSQLiteDB")
--Open data.db. If the file doesn't exist it will be created
local path = system.pathForFile("dataBase.db", system.DocumentsDirectory)
db = sqlite3.open( path )
local resultsArray = {}
local query = "SELECT * FROM NSUserDefaults"
print(query)
for row in db:nrows(query) do
table.insert(resultsArray,row)
end
db:close()
print("resultsArray count = "..#resultsArray)
return resultsArray
end
-----------------------------------------------------------------------------------------