|
1 | 1 | import Tracking from 'src/utils/Tracking' |
2 | 2 | import Storage from 'src/utils/Storage' |
| 3 | + |
3 | 4 | import FigPen from 'src/utils/FigPen' |
| 5 | +import MessageBus from 'src/utils/MessageBus' |
| 6 | + |
4 | 7 | import { shouldShowCountdown, getCountdownSeconds, setCachedLicenseStatus } from 'src/payments/gate' |
5 | 8 | import { |
6 | 9 | getNodesGroupedbyPosition, |
@@ -80,12 +83,11 @@ function ensureDirectCommandGate(commandName, executeCommand, preferences, UUID, |
80 | 83 | }) |
81 | 84 | }, 50) |
82 | 85 |
|
83 | | - // Handle countdown completion |
84 | | - FP.onUIMessage((msg) => { |
85 | | - if (msg.type === 'direct-countdown-complete') { |
86 | | - executeCommand() |
87 | | - FP.closePlugin() |
88 | | - } |
| 86 | + |
| 87 | + // Bind direct countdown completion handler |
| 88 | + MessageBus.bind('direct-countdown-complete', (msg) => { |
| 89 | + executeCommand() |
| 90 | + FP.closePlugin() |
89 | 91 | }) |
90 | 92 | } else { |
91 | 93 | // Execute immediately if licensed |
@@ -184,7 +186,86 @@ Storage.getMultiple([ |
184 | 186 | // Make sure initial selection is sent to the UI |
185 | 187 | FP.notifyUI({ type: 'selection', selection: FP.currentSelection() }) |
186 | 188 | }) |
| 189 | + |
| 190 | + // Set up selection change listener using FigPen |
| 191 | + FP.onSelectionChange((selection) => { |
| 192 | + FP.notifyUI({ type: 'selection', selection: selection }) |
| 193 | + }) |
| 194 | + |
| 195 | + // Bind all message handlers using MessageBus |
| 196 | + MessageBus.bind('tidy', (msg) => { |
| 197 | + var RENAMING_ENABLED = msg.options.renaming |
| 198 | + var REORDER_ENABLED = msg.options.reorder |
| 199 | + var TIDY_ENABLED = msg.options.tidy |
| 200 | + var PAGER_ENABLED = msg.options.pager |
| 201 | + |
| 202 | + if (TIDY_ENABLED) cmdTidy(preferences.spacing.x, preferences.spacing.y, preferences.wrap_instances, preferences.layout_paradigm || 'rows') |
| 203 | + if (RENAMING_ENABLED) cmdRename(preferences.rename_strategy, preferences.start_name, preferences.layout_paradigm || 'rows') |
| 204 | + if (REORDER_ENABLED) cmdReorder(preferences.layout_paradigm || 'rows') |
| 205 | + if (PAGER_ENABLED) cmdPager(preferences.pager_variable, preferences.layout_paradigm || 'rows') |
| 206 | + FP.showNotification('Super Tidy') |
| 207 | + setTimeout(() => FP.closePlugin(), 100) |
| 208 | + }) |
| 209 | + |
| 210 | + MessageBus.bind('preferences', (msg) => { |
| 211 | + preferences = msg.preferences |
| 212 | + Storage.set(Storage.getKey('PREFERENCES'), preferences).then((success) => { |
| 213 | + if (success) { |
| 214 | + FP.showNotification('Preferences saved') |
| 215 | + } else { |
| 216 | + FP.showNotification('Failed to save preferences. Please try again.') |
| 217 | + } |
| 218 | + }) |
| 219 | + }) |
| 220 | + |
| 221 | + MessageBus.bind('displayImpression', (msg) => { |
| 222 | + FP.resizeUI(320, 540+124) |
| 223 | + Storage.setMultiple({ |
| 224 | + [Storage.getKey('AD_LAST_SHOWN_DATE')]: Date.now(), |
| 225 | + [Storage.getKey('AD_LAST_SHOWN_IMPRESSION')]: parseInt(AD_LAST_SHOWN_IMPRESSION)+1 |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + MessageBus.bind('resetImpression', (msg) => { |
| 230 | + Storage.set(Storage.getKey('AD_LAST_SHOWN_IMPRESSION'), 0) |
| 231 | + }) |
| 232 | + |
| 233 | + MessageBus.bind('activate-license', (msg) => { |
| 234 | + // Store license data from UI |
| 235 | + const licenseData = { |
| 236 | + licensed: true, |
| 237 | + productId: msg.productId || 'gumroad', |
| 238 | + licenseKeyHash: msg.licenseKey ? hashLicenseKey(msg.licenseKey) : null, |
| 239 | + licenseKey: msg.licenseKey, // Store actual key for display |
| 240 | + deviceId: UUID, |
| 241 | + activatedAt: Date.now(), |
| 242 | + purchase: msg.purchase || {}, |
| 243 | + uses: msg.uses || 1 // Include usage count from activation |
| 244 | + } |
187 | 245 |
|
| 246 | + Storage.set(Storage.getKey('LICENSE_V1'), licenseData) |
| 247 | + .then((success) => { |
| 248 | + if (success) { |
| 249 | + setCachedLicenseStatus(licenseData) // Update cache |
| 250 | + FP.showNotification('You now have Super Tidy Pro') |
| 251 | + } else { |
| 252 | + FP.showNotification('Failed to save license. Please try again.') |
| 253 | + } |
| 254 | + }) |
| 255 | + }) |
| 256 | + |
| 257 | + MessageBus.bind('remove-license', (msg) => { |
| 258 | + // Remove stored license |
| 259 | + Storage.remove(Storage.getKey('LICENSE_V1')) |
| 260 | + .then((success) => { |
| 261 | + if (success) { |
| 262 | + setCachedLicenseStatus(null) // Update cache |
| 263 | + FP.showNotification('License unlinked from this device') |
| 264 | + } else { |
| 265 | + FP.showNotification('Failed to unlink license. Please try again.') |
| 266 | + } |
| 267 | + }) |
| 268 | + }) |
188 | 269 | // Command triggered by user |
189 | 270 | if (cmd == 'rename') { |
190 | 271 | // RUNS WITH COUNTDOWN GATE |
@@ -237,81 +318,5 @@ Storage.getMultiple([ |
237 | 318 | AD_LAST_SHOWN_IMPRESSION: AD_LAST_SHOWN_IMPRESSION |
238 | 319 | }) |
239 | 320 | FP.notifyUI({ type: 'selection', selection: FP.currentSelection() }) |
240 | | - |
241 | | - FP.onSelectionChange((selection) => { |
242 | | - FP.notifyUI({ type: 'selection', selection: selection }) |
243 | | - }) |
244 | | - |
245 | | - FP.onUIMessage((msg) => { |
246 | | - if (msg.type === 'tidy') { |
247 | | - var RENAMING_ENABLED = msg.options.renaming |
248 | | - var REORDER_ENABLED = msg.options.reorder |
249 | | - var TIDY_ENABLED = msg.options.tidy |
250 | | - var PAGER_ENABLED = msg.options.pager |
251 | | - |
252 | | - if (TIDY_ENABLED) cmdTidy(preferences.spacing.x, preferences.spacing.y, preferences.wrap_instances, preferences.layout_paradigm || 'rows') |
253 | | - if (RENAMING_ENABLED) cmdRename(preferences.rename_strategy, preferences.start_name, preferences.layout_paradigm || 'rows') |
254 | | - if (REORDER_ENABLED) cmdReorder(preferences.layout_paradigm || 'rows') |
255 | | - if (PAGER_ENABLED) cmdPager(preferences.pager_variable, preferences.layout_paradigm || 'rows') |
256 | | - FP.showNotification('Super Tidy') |
257 | | - setTimeout(() => FP.closePlugin(), 100) |
258 | | - } else |
259 | | - if (msg.type === 'preferences') { |
260 | | - preferences = msg.preferences |
261 | | - Storage.set(Storage.getKey('PREFERENCES'), preferences).then((success) => { |
262 | | - if (success) { |
263 | | - FP.showNotification('Preferences saved') |
264 | | - } else { |
265 | | - FP.showNotification('Failed to save preferences. Please try again.') |
266 | | - } |
267 | | - }) |
268 | | - } else |
269 | | - if (msg.type === 'displayImpression') { |
270 | | - FP.resizeUI(320, 540+124) |
271 | | - Storage.setMultiple({ |
272 | | - [Storage.getKey('AD_LAST_SHOWN_DATE')]: Date.now(), |
273 | | - [Storage.getKey('AD_LAST_SHOWN_IMPRESSION')]: parseInt(AD_LAST_SHOWN_IMPRESSION)+1 |
274 | | - }) |
275 | | - } |
276 | | - |
277 | | - if (msg.type === 'resetImpression') { |
278 | | - Storage.set(Storage.getKey('AD_LAST_SHOWN_IMPRESSION'), 0) |
279 | | - } else |
280 | | - if (msg.type === 'activate-license') { |
281 | | - // Store license data from UI |
282 | | - const licenseData = { |
283 | | - licensed: true, |
284 | | - productId: msg.productId || 'gumroad', |
285 | | - licenseKeyHash: msg.licenseKey ? hashLicenseKey(msg.licenseKey) : null, |
286 | | - licenseKey: msg.licenseKey, // Store actual key for display |
287 | | - deviceId: UUID, |
288 | | - activatedAt: Date.now(), |
289 | | - purchase: msg.purchase || {}, |
290 | | - uses: msg.uses || 1 // Include usage count from activation |
291 | | - } |
292 | | - |
293 | | - Storage.set(Storage.getKey('LICENSE_V1'), licenseData) |
294 | | - .then((success) => { |
295 | | - if (success) { |
296 | | - setCachedLicenseStatus(licenseData) // Update cache |
297 | | - FP.showNotification('You now have Super Tidy Pro') |
298 | | - } else { |
299 | | - FP.showNotification('Failed to save license. Please try again.') |
300 | | - } |
301 | | - }) |
302 | | - } else |
303 | | - if (msg.type === 'remove-license') { |
304 | | - // Remove stored license |
305 | | - Storage.remove(Storage.getKey('LICENSE_V1')) |
306 | | - .then((success) => { |
307 | | - if (success) { |
308 | | - setCachedLicenseStatus(null) // Update cache |
309 | | - FP.showNotification('License unlinked from this device') |
310 | | - } else { |
311 | | - FP.showNotification('Failed to unlink license. Please try again.') |
312 | | - } |
313 | | - }) |
314 | | - } |
315 | | - }) |
316 | 321 | } |
317 | 322 | }) |
0 commit comments