|
| 1 | +import { EventData, Label, StackLayout } from '@nativescript/core'; |
| 2 | +import { addCallback, removeCallback, start, stop } from '@nativescript/core/fps-meter'; |
| 3 | + |
| 4 | +let callbackId; |
| 5 | +let fpsLabel: any; |
| 6 | +export function startFPSMeter() { |
| 7 | + callbackId = addCallback((fps: number, minFps: number) => { |
| 8 | + // console.log(`Frames per seconds: ${fps.toFixed(2)}`); |
| 9 | + // console.log(minFps.toFixed(2)); |
| 10 | + if (fpsLabel) { |
| 11 | + fpsLabel.text = `${fps}`; |
| 12 | + } |
| 13 | + }); |
| 14 | + start(); |
| 15 | +} |
| 16 | + |
| 17 | +export function stopFPSMeter() { |
| 18 | + removeCallback(callbackId); |
| 19 | + stop(); |
| 20 | +} |
| 21 | + |
| 22 | +let timeouts = []; |
| 23 | +let intervals = []; |
| 24 | + |
| 25 | +let reusableItem; |
| 26 | +let vcToggle; |
| 27 | +let loaded = false; |
| 28 | +let isIn1 = false; |
| 29 | + |
| 30 | +function updateVcToggleText() { |
| 31 | + vcToggle.text = `Container is${reusableItem.reusable ? ' ' : ' NOT '}Reusable` |
| 32 | +} |
| 33 | + |
| 34 | +export function pageLoaded(args) { |
| 35 | + startFPSMeter(); |
| 36 | + if (loaded) { |
| 37 | + fpsLabel = null; |
| 38 | + // stopFPSMeter(); |
| 39 | + timeouts.forEach((v) => clearTimeout(v)); |
| 40 | + intervals.forEach((v) => clearInterval(v)); |
| 41 | + reusableItem._tearDownUI(true); |
| 42 | + } |
| 43 | + loaded = true; |
| 44 | + reusableItem = args.object.getViewById('reusableItem'); |
| 45 | + vcToggle = args.object.getViewById('vcToggle'); |
| 46 | + updateVcToggleText(); |
| 47 | + fpsLabel = args.object.getViewById('fpslabel'); |
| 48 | + const stack1: StackLayout = args.object.getViewById('stack1'); |
| 49 | + const stack2: StackLayout = args.object.getViewById('stack2'); |
| 50 | + setTimeout(() => { |
| 51 | + // label.android.setTextColor(new Color("red").android); |
| 52 | + // label.android.setBackgroundColor(new Color("red").android); |
| 53 | + startFPSMeter(); |
| 54 | + console.log('setRed'); |
| 55 | + }, 1000); |
| 56 | + // console.log(label._context); |
| 57 | + // isIn1 = false; |
| 58 | + // timeouts.push(setTimeout(() => { |
| 59 | + // intervals.push(setInterval(() => { |
| 60 | + // label.parent.removeChild(label); |
| 61 | + // // console.log(label.nativeView); |
| 62 | + // if(isIn1) { |
| 63 | + // isIn1 = false; |
| 64 | + // stack2.addChild(label); |
| 65 | + // } else { |
| 66 | + // isIn1 = true; |
| 67 | + // stack1.addChild(label); |
| 68 | + // } |
| 69 | + // }, 10)); |
| 70 | + // }, 1001)); |
| 71 | +} |
| 72 | + |
| 73 | +export function pageUnloaded(args) { |
| 74 | + // |
| 75 | +} |
| 76 | + |
| 77 | +export function makeReusable(args: EventData) { |
| 78 | + console.log('loaded:', args.object); |
| 79 | + // console.log("making reusable"); |
| 80 | + if ((args.object as any).___reusableRan) { |
| 81 | + return; |
| 82 | + } |
| 83 | + (args.object as any).___reusableRan = true; |
| 84 | + (args.object as any).reusable = true; |
| 85 | + if(args.object === reusableItem) { |
| 86 | + updateVcToggleText(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export function onReusableUnloaded(args: EventData) { |
| 91 | + console.log('unloaded:', args.object); |
| 92 | +} |
| 93 | +var testLabel: Label; |
| 94 | + |
| 95 | +export function test(args: any) { |
| 96 | + const page = args.object.page; |
| 97 | + reusableItem = page.getViewById('reusableItem'); |
| 98 | + const stack1: StackLayout = page.getViewById('stack1'); |
| 99 | + const stack2: StackLayout = page.getViewById('stack2'); |
| 100 | + if (!testLabel) { |
| 101 | + testLabel = new Label(); |
| 102 | + testLabel.text = 'This label is not reusable and is dynamic'; |
| 103 | + testLabel.on('loaded', () => { |
| 104 | + console.log('LODADED testLabel'); |
| 105 | + }); |
| 106 | + testLabel.on('unloaded', () => { |
| 107 | + console.log('UNLODADED testLabel'); |
| 108 | + }); |
| 109 | + } |
| 110 | + reusableItem.parent.removeChild(reusableItem); |
| 111 | + if (!reusableItem._suspendNativeUpdatesCount) { |
| 112 | + console.log('reusableItem SHOULD BE UNLOADED'); |
| 113 | + } |
| 114 | + if (!testLabel._suspendNativeUpdatesCount) { |
| 115 | + console.log('testLabel SHOULD BE UNLOADED'); |
| 116 | + } |
| 117 | + if (!testLabel.parent) { |
| 118 | + reusableItem.addChild(testLabel); |
| 119 | + } |
| 120 | + if (!testLabel.nativeView) { |
| 121 | + console.log('testLabel NATIVE VIEW SHOULD BE CREATED'); |
| 122 | + } |
| 123 | + if (!testLabel._suspendNativeUpdatesCount) { |
| 124 | + console.log('testLabel SHOULD BE UNLOADED'); |
| 125 | + } |
| 126 | + if (isIn1) { |
| 127 | + isIn1 = false; |
| 128 | + stack2.addChild(reusableItem); |
| 129 | + } else { |
| 130 | + isIn1 = true; |
| 131 | + stack1.addChild(reusableItem); |
| 132 | + } |
| 133 | + if (reusableItem._suspendNativeUpdatesCount) { |
| 134 | + console.log('reusableItem SHOULD BE LOADED AND RECEIVING UPDATES'); |
| 135 | + } |
| 136 | + if (testLabel._suspendNativeUpdatesCount) { |
| 137 | + console.log('testLabel SHOULD BE LOADED AND RECEIVING UPDATES'); |
| 138 | + } |
| 139 | + // console.log("onTap"); |
| 140 | + // alert("onTap"); |
| 141 | +} |
| 142 | +let ignoreInput = false; |
| 143 | + |
| 144 | +export function toggleReusable(args: EventData) { |
| 145 | + if (ignoreInput) { |
| 146 | + return; |
| 147 | + } |
| 148 | + ignoreInput = true; |
| 149 | + setTimeout(() => (ignoreInput = false), 0); // hack to avoid gesture collision |
| 150 | + const target: any = args.object; |
| 151 | + target.reusable = !target.reusable; |
| 152 | + console.log(`${target} is now ${target.reusable ? '' : 'NOT '}reusable`); |
| 153 | +} |
| 154 | + |
| 155 | +export function toggleVCReusable() { |
| 156 | + reusableItem.reusable = !reusableItem.reusable; |
| 157 | + updateVcToggleText(); |
| 158 | +} |
0 commit comments