@cond/simasdk 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CondSimasdk.podspec +18 -0
- package/Package.swift +31 -0
- package/README.md +508 -2
- package/android/build.gradle +63 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/simasdk/SimaFirebasePlugin.java +139 -0
- package/android/src/main/java/com/simasdk/SimaGamesPlugin.java +235 -0
- package/android/src/main/java/com/simasdk/SimaHapticsPlugin.java +101 -0
- package/android/src/main/java/com/simasdk/SimaReviewPlugin.java +48 -0
- package/android/src/main/java/com/simasdk/SimaStoragePlugin.java +79 -0
- package/dist/ads.cjs +214 -0
- package/dist/ads.cjs.map +1 -0
- package/dist/ads.d.cts +98 -0
- package/dist/ads.d.ts +98 -0
- package/dist/ads.js +211 -0
- package/dist/ads.js.map +1 -0
- package/dist/att.cjs +37 -0
- package/dist/att.cjs.map +1 -0
- package/dist/att.d.cts +31 -0
- package/dist/att.d.ts +31 -0
- package/dist/att.js +33 -0
- package/dist/att.js.map +1 -0
- package/dist/back-button.cjs +95 -0
- package/dist/back-button.cjs.map +1 -0
- package/dist/back-button.d.cts +45 -0
- package/dist/back-button.d.ts +45 -0
- package/dist/back-button.js +88 -0
- package/dist/back-button.js.map +1 -0
- package/dist/cli.js +723 -0
- package/dist/firebase.cjs +102 -0
- package/dist/firebase.cjs.map +1 -0
- package/dist/firebase.d.cts +40 -0
- package/dist/firebase.d.ts +40 -0
- package/dist/firebase.js +99 -0
- package/dist/firebase.js.map +1 -0
- package/dist/games.cjs +98 -0
- package/dist/games.cjs.map +1 -0
- package/dist/games.d.cts +73 -0
- package/dist/games.d.ts +73 -0
- package/dist/games.js +95 -0
- package/dist/games.js.map +1 -0
- package/dist/haptics.cjs +81 -0
- package/dist/haptics.cjs.map +1 -0
- package/dist/haptics.d.cts +51 -0
- package/dist/haptics.d.ts +51 -0
- package/dist/haptics.js +78 -0
- package/dist/haptics.js.map +1 -0
- package/dist/index.cjs +233 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +99 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.js +226 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +118 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +51 -0
- package/dist/react.d.ts +51 -0
- package/dist/react.js +113 -0
- package/dist/react.js.map +1 -0
- package/dist/review.cjs +195 -0
- package/dist/review.cjs.map +1 -0
- package/dist/review.d.cts +76 -0
- package/dist/review.d.ts +76 -0
- package/dist/review.js +192 -0
- package/dist/review.js.map +1 -0
- package/dist/shell.cjs +97 -0
- package/dist/shell.cjs.map +1 -0
- package/dist/shell.d.cts +36 -0
- package/dist/shell.d.ts +36 -0
- package/dist/shell.js +91 -0
- package/dist/shell.js.map +1 -0
- package/dist/storage.cjs +140 -0
- package/dist/storage.cjs.map +1 -0
- package/dist/storage.d.cts +48 -0
- package/dist/storage.d.ts +48 -0
- package/dist/storage.js +136 -0
- package/dist/storage.js.map +1 -0
- package/ios/Sources/SimasdkPlugin/SimaAttPlugin.swift +41 -0
- package/ios/Sources/SimasdkPlugin/SimaFirebasePlugin.swift +188 -0
- package/ios/Sources/SimasdkPlugin/SimaGamesPlugin.swift +145 -0
- package/ios/Sources/SimasdkPlugin/SimaHapticsPlugin.swift +61 -0
- package/ios/Sources/SimasdkPlugin/SimaReviewPlugin.swift +40 -0
- package/ios/Sources/SimasdkPlugin/SimaStoragePlugin.swift +58 -0
- package/package.json +128 -4
- package/skills/simasdk/SKILL.md +189 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storage.ts"],"names":["registerPlugin","Capacitor"],"mappings":";;;;;AASO,IAAM,WAAA,GAAcA,oBAAwC,aAAa;AAoChF,IAAM,cAAA,uBAAqB,GAAA,EAAoB;AAE/C,SAAS,mBAAA,GAA+B;AACtC,EAAA,OACE,OAAO,WAAW,WAAA,IAClBC,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capacitor, registerPlugin } from '@capacitor/core';\n\nexport interface SimaStorageNativePlugin {\n get(options: { key: string }): Promise<{ value: string | null }>;\n set(options: { key: string; value: string }): Promise<void>;\n remove(options: { key: string }): Promise<void>;\n clearPrefix(options: { prefix: string }): Promise<void>;\n}\n\nexport const SimaStorage = registerPlugin<SimaStorageNativePlugin>('SimaStorage');\n\nexport interface StorageBackend {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n removeItem(key: string): void | Promise<void>;\n clearPrefix?(prefix: string): void | Promise<void>;\n}\n\nexport interface StorageConfig {\n /** Namespace prefix isolating keys for this store (e.g. `\"settings\"` or `\"game\"`). */\n namespace?: string;\n /** Optional schema version for stored envelopes (defaults to `1`). */\n version?: number;\n /**\n * Optional migration function called when a stored value has an older schema version.\n * Return the migrated value for the current version.\n */\n migrate?: (key: string, oldValue: unknown, fromVersion: number) => unknown;\n /** Optional custom storage backend (primarily for testing). */\n backend?: StorageBackend;\n}\n\nexport interface SimaStore {\n get<T>(key: string, fallback: T): Promise<T>;\n getOptional<T>(key: string): Promise<T | null>;\n set<T>(key: string, value: T): Promise<void>;\n remove(key: string): Promise<void>;\n clear(): Promise<void>;\n}\n\ninterface StoredEnvelope<T> {\n v: number;\n d: T;\n}\n\nconst memoryFallback = new Map<string, string>();\n\nfunction canUseNativeStorage(): boolean {\n return (\n typeof window !== 'undefined' &&\n Capacitor.isNativePlatform() &&\n Capacitor.isPluginAvailable('SimaStorage')\n );\n}\n\nexport const defaultStorageBackend: StorageBackend = {\n async getItem(key: string): Promise<string | null> {\n if (canUseNativeStorage()) {\n const { value } = await SimaStorage.get({ key });\n return value ?? null;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n return window.localStorage.getItem(key);\n } catch {\n return memoryFallback.get(key) ?? null;\n }\n }\n return memoryFallback.get(key) ?? null;\n },\n\n async setItem(key: string, value: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.set({ key, value });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.setItem(key, value);\n return;\n } catch {\n memoryFallback.set(key, value);\n return;\n }\n }\n memoryFallback.set(key, value);\n },\n\n async removeItem(key: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.remove({ key });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.removeItem(key);\n return;\n } catch {\n memoryFallback.delete(key);\n return;\n }\n }\n memoryFallback.delete(key);\n },\n\n async clearPrefix(prefix: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.clearPrefix({ prefix });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n const toDelete: string[] = [];\n for (let i = 0; i < window.localStorage.length; i += 1) {\n const k = window.localStorage.key(i);\n if (k && k.startsWith(prefix)) {\n toDelete.push(k);\n }\n }\n for (const k of toDelete) {\n window.localStorage.removeItem(k);\n }\n return;\n } catch {\n // Fall through to memoryFallback cleanup.\n }\n }\n for (const k of Array.from(memoryFallback.keys())) {\n if (k.startsWith(prefix)) {\n memoryFallback.delete(k);\n }\n }\n },\n};\n\nexport function createStorage(config: StorageConfig = {}): SimaStore {\n const namespace = config.namespace ?? 'default';\n const version = config.version ?? 1;\n const backend = config.backend ?? defaultStorageBackend;\n const prefix = `simasdk:store:${namespace}:`;\n\n function fullKey(key: string): string {\n return `${prefix}${key}`;\n }\n\n async function readOptional<T>(key: string): Promise<T | null> {\n const raw = await backend.getItem(fullKey(key));\n if (raw === null) {\n return null;\n }\n try {\n const parsed = JSON.parse(raw) as Partial<StoredEnvelope<T>>;\n if (typeof parsed !== 'object' || parsed === null || !('d' in parsed)) {\n return null;\n }\n const storedVersion = typeof parsed.v === 'number' ? parsed.v : 1;\n if (storedVersion !== version && config.migrate) {\n const migrated = config.migrate(key, parsed.d, storedVersion) as T;\n await backend.setItem(fullKey(key), JSON.stringify({ v: version, d: migrated }));\n return migrated;\n }\n return parsed.d as T;\n } catch {\n return null;\n }\n }\n\n return {\n async get<T>(key: string, fallback: T): Promise<T> {\n const value = await readOptional<T>(key);\n return value === null ? fallback : value;\n },\n\n getOptional: readOptional,\n\n async set<T>(key: string, value: T): Promise<void> {\n const envelope: StoredEnvelope<T> = { v: version, d: value };\n await backend.setItem(fullKey(key), JSON.stringify(envelope));\n },\n\n async remove(key: string): Promise<void> {\n await backend.removeItem(fullKey(key));\n },\n\n async clear(): Promise<void> {\n if (backend.clearPrefix) {\n await backend.clearPrefix(prefix);\n }\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
interface SimaStorageNativePlugin {
|
|
2
|
+
get(options: {
|
|
3
|
+
key: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
value: string | null;
|
|
6
|
+
}>;
|
|
7
|
+
set(options: {
|
|
8
|
+
key: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
remove(options: {
|
|
12
|
+
key: string;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
clearPrefix(options: {
|
|
15
|
+
prefix: string;
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
declare const SimaStorage: SimaStorageNativePlugin;
|
|
19
|
+
interface StorageBackend {
|
|
20
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
21
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
22
|
+
removeItem(key: string): void | Promise<void>;
|
|
23
|
+
clearPrefix?(prefix: string): void | Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
interface StorageConfig {
|
|
26
|
+
/** Namespace prefix isolating keys for this store (e.g. `"settings"` or `"game"`). */
|
|
27
|
+
namespace?: string;
|
|
28
|
+
/** Optional schema version for stored envelopes (defaults to `1`). */
|
|
29
|
+
version?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Optional migration function called when a stored value has an older schema version.
|
|
32
|
+
* Return the migrated value for the current version.
|
|
33
|
+
*/
|
|
34
|
+
migrate?: (key: string, oldValue: unknown, fromVersion: number) => unknown;
|
|
35
|
+
/** Optional custom storage backend (primarily for testing). */
|
|
36
|
+
backend?: StorageBackend;
|
|
37
|
+
}
|
|
38
|
+
interface SimaStore {
|
|
39
|
+
get<T>(key: string, fallback: T): Promise<T>;
|
|
40
|
+
getOptional<T>(key: string): Promise<T | null>;
|
|
41
|
+
set<T>(key: string, value: T): Promise<void>;
|
|
42
|
+
remove(key: string): Promise<void>;
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
declare const defaultStorageBackend: StorageBackend;
|
|
46
|
+
declare function createStorage(config?: StorageConfig): SimaStore;
|
|
47
|
+
|
|
48
|
+
export { SimaStorage, type SimaStorageNativePlugin, type SimaStore, type StorageBackend, type StorageConfig, createStorage, defaultStorageBackend };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
interface SimaStorageNativePlugin {
|
|
2
|
+
get(options: {
|
|
3
|
+
key: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
value: string | null;
|
|
6
|
+
}>;
|
|
7
|
+
set(options: {
|
|
8
|
+
key: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
remove(options: {
|
|
12
|
+
key: string;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
clearPrefix(options: {
|
|
15
|
+
prefix: string;
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
declare const SimaStorage: SimaStorageNativePlugin;
|
|
19
|
+
interface StorageBackend {
|
|
20
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
21
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
22
|
+
removeItem(key: string): void | Promise<void>;
|
|
23
|
+
clearPrefix?(prefix: string): void | Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
interface StorageConfig {
|
|
26
|
+
/** Namespace prefix isolating keys for this store (e.g. `"settings"` or `"game"`). */
|
|
27
|
+
namespace?: string;
|
|
28
|
+
/** Optional schema version for stored envelopes (defaults to `1`). */
|
|
29
|
+
version?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Optional migration function called when a stored value has an older schema version.
|
|
32
|
+
* Return the migrated value for the current version.
|
|
33
|
+
*/
|
|
34
|
+
migrate?: (key: string, oldValue: unknown, fromVersion: number) => unknown;
|
|
35
|
+
/** Optional custom storage backend (primarily for testing). */
|
|
36
|
+
backend?: StorageBackend;
|
|
37
|
+
}
|
|
38
|
+
interface SimaStore {
|
|
39
|
+
get<T>(key: string, fallback: T): Promise<T>;
|
|
40
|
+
getOptional<T>(key: string): Promise<T | null>;
|
|
41
|
+
set<T>(key: string, value: T): Promise<void>;
|
|
42
|
+
remove(key: string): Promise<void>;
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
declare const defaultStorageBackend: StorageBackend;
|
|
46
|
+
declare function createStorage(config?: StorageConfig): SimaStore;
|
|
47
|
+
|
|
48
|
+
export { SimaStorage, type SimaStorageNativePlugin, type SimaStore, type StorageBackend, type StorageConfig, createStorage, defaultStorageBackend };
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { registerPlugin, Capacitor } from '@capacitor/core';
|
|
2
|
+
|
|
3
|
+
// src/storage.ts
|
|
4
|
+
var SimaStorage = registerPlugin("SimaStorage");
|
|
5
|
+
var memoryFallback = /* @__PURE__ */ new Map();
|
|
6
|
+
function canUseNativeStorage() {
|
|
7
|
+
return typeof window !== "undefined" && Capacitor.isNativePlatform() && Capacitor.isPluginAvailable("SimaStorage");
|
|
8
|
+
}
|
|
9
|
+
var defaultStorageBackend = {
|
|
10
|
+
async getItem(key) {
|
|
11
|
+
if (canUseNativeStorage()) {
|
|
12
|
+
const { value } = await SimaStorage.get({ key });
|
|
13
|
+
return value ?? null;
|
|
14
|
+
}
|
|
15
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
16
|
+
try {
|
|
17
|
+
return window.localStorage.getItem(key);
|
|
18
|
+
} catch {
|
|
19
|
+
return memoryFallback.get(key) ?? null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return memoryFallback.get(key) ?? null;
|
|
23
|
+
},
|
|
24
|
+
async setItem(key, value) {
|
|
25
|
+
if (canUseNativeStorage()) {
|
|
26
|
+
await SimaStorage.set({ key, value });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
30
|
+
try {
|
|
31
|
+
window.localStorage.setItem(key, value);
|
|
32
|
+
return;
|
|
33
|
+
} catch {
|
|
34
|
+
memoryFallback.set(key, value);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
memoryFallback.set(key, value);
|
|
39
|
+
},
|
|
40
|
+
async removeItem(key) {
|
|
41
|
+
if (canUseNativeStorage()) {
|
|
42
|
+
await SimaStorage.remove({ key });
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
46
|
+
try {
|
|
47
|
+
window.localStorage.removeItem(key);
|
|
48
|
+
return;
|
|
49
|
+
} catch {
|
|
50
|
+
memoryFallback.delete(key);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
memoryFallback.delete(key);
|
|
55
|
+
},
|
|
56
|
+
async clearPrefix(prefix) {
|
|
57
|
+
if (canUseNativeStorage()) {
|
|
58
|
+
await SimaStorage.clearPrefix({ prefix });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
62
|
+
try {
|
|
63
|
+
const toDelete = [];
|
|
64
|
+
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
65
|
+
const k = window.localStorage.key(i);
|
|
66
|
+
if (k && k.startsWith(prefix)) {
|
|
67
|
+
toDelete.push(k);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const k of toDelete) {
|
|
71
|
+
window.localStorage.removeItem(k);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
} catch {
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const k of Array.from(memoryFallback.keys())) {
|
|
78
|
+
if (k.startsWith(prefix)) {
|
|
79
|
+
memoryFallback.delete(k);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function createStorage(config = {}) {
|
|
85
|
+
const namespace = config.namespace ?? "default";
|
|
86
|
+
const version = config.version ?? 1;
|
|
87
|
+
const backend = config.backend ?? defaultStorageBackend;
|
|
88
|
+
const prefix = `simasdk:store:${namespace}:`;
|
|
89
|
+
function fullKey(key) {
|
|
90
|
+
return `${prefix}${key}`;
|
|
91
|
+
}
|
|
92
|
+
async function readOptional(key) {
|
|
93
|
+
const raw = await backend.getItem(fullKey(key));
|
|
94
|
+
if (raw === null) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const parsed = JSON.parse(raw);
|
|
99
|
+
if (typeof parsed !== "object" || parsed === null || !("d" in parsed)) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const storedVersion = typeof parsed.v === "number" ? parsed.v : 1;
|
|
103
|
+
if (storedVersion !== version && config.migrate) {
|
|
104
|
+
const migrated = config.migrate(key, parsed.d, storedVersion);
|
|
105
|
+
await backend.setItem(fullKey(key), JSON.stringify({ v: version, d: migrated }));
|
|
106
|
+
return migrated;
|
|
107
|
+
}
|
|
108
|
+
return parsed.d;
|
|
109
|
+
} catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
async get(key, fallback) {
|
|
115
|
+
const value = await readOptional(key);
|
|
116
|
+
return value === null ? fallback : value;
|
|
117
|
+
},
|
|
118
|
+
getOptional: readOptional,
|
|
119
|
+
async set(key, value) {
|
|
120
|
+
const envelope = { v: version, d: value };
|
|
121
|
+
await backend.setItem(fullKey(key), JSON.stringify(envelope));
|
|
122
|
+
},
|
|
123
|
+
async remove(key) {
|
|
124
|
+
await backend.removeItem(fullKey(key));
|
|
125
|
+
},
|
|
126
|
+
async clear() {
|
|
127
|
+
if (backend.clearPrefix) {
|
|
128
|
+
await backend.clearPrefix(prefix);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { SimaStorage, createStorage, defaultStorageBackend };
|
|
135
|
+
//# sourceMappingURL=storage.js.map
|
|
136
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/storage.ts"],"names":[],"mappings":";;;AASO,IAAM,WAAA,GAAc,eAAwC,aAAa;AAoChF,IAAM,cAAA,uBAAqB,GAAA,EAAoB;AAE/C,SAAS,mBAAA,GAA+B;AACtC,EAAA,OACE,OAAO,WAAW,WAAA,IAClB,SAAA,CAAU,kBAAiB,IAC3B,SAAA,CAAU,kBAAkB,aAAa,CAAA;AAE7C;AAEO,IAAM,qBAAA,GAAwC;AAAA,EACnD,MAAM,QAAQ,GAAA,EAAqC;AACjD,IAAA,IAAI,qBAAoB,EAAG;AACzB,MAAA,MAAM,EAAE,OAAM,GAAI,MAAM,YAAY,GAAA,CAAI,EAAE,KAAK,CAAA;AAC/C,MAAA,OAAO,KAAA,IAAS,IAAA;AAAA,IAClB;AACA,IAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,OAAO,MAAA,CAAO,iBAAiB,WAAA,EAAa;AAC/E,MAAA,IAAI;AACF,QAAA,OAAO,MAAA,CAAO,YAAA,CAAa,OAAA,CAAQ,GAAG,CAAA;AAAA,MACxC,CAAA,CAAA,MAAQ;AACN,QAAA,OAAO,cAAA,CAAe,GAAA,CAAI,GAAG,CAAA,IAAK,IAAA;AAAA,MACpC;A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AAA,KAAU,OAAO,QAAA,GAAW,KAAA;AAAA,IACrC,CAAA;AAAA,IAEA,WAAA,EAAa,YAAA;AAAA,IAEb,MAAM,GAAA,CAAO,GAAA,EAAa,KAAA,EAAyB;AACjD,MAAA,MAAM,QAAA,GAA8B,EAAE,CAAA,EAAG,OAAA,EAAS,GAAG,KAAA,EAAM;AAC3D,MAAA,MAAM,OAAA,CAAQ,QAAQ,OAAA,CAAQ,GAAG,GAAG,IAAA,CAAK,SAAA,CAAU,QAAQ,CAAC,CAAA;AAAA,IAC9D,CAAA;AAAA,IAEA,MAAM,OAAO,GAAA,EAA4B;AACvC,MAAA,MAAM,OAAA,CAAQ,UAAA,CAAW,OAAA,CAAQ,GAAG,CAAC,CAAA;AAAA,IACvC,CAAA;AAAA,IAEA,MAAM,KAAA,GAAuB;AAC3B,MAAA,IAAI,QAAQ,WAAA,EAAa;AACvB,QAAA,MAAM,OAAA,CAAQ,YAAY,MAAM,CAAA;AAAA,MAClC;AAAA,IACF;AAAA,GACF;AACF","file":"storage.js","sourcesContent":["import { Capacitor, registerPlugin } from '@capacitor/core';\n\nexport interface SimaStorageNativePlugin {\n get(options: { key: string }): Promise<{ value: string | null }>;\n set(options: { key: string; value: string }): Promise<void>;\n remove(options: { key: string }): Promise<void>;\n clearPrefix(options: { prefix: string }): Promise<void>;\n}\n\nexport const SimaStorage = registerPlugin<SimaStorageNativePlugin>('SimaStorage');\n\nexport interface StorageBackend {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n removeItem(key: string): void | Promise<void>;\n clearPrefix?(prefix: string): void | Promise<void>;\n}\n\nexport interface StorageConfig {\n /** Namespace prefix isolating keys for this store (e.g. `\"settings\"` or `\"game\"`). */\n namespace?: string;\n /** Optional schema version for stored envelopes (defaults to `1`). */\n version?: number;\n /**\n * Optional migration function called when a stored value has an older schema version.\n * Return the migrated value for the current version.\n */\n migrate?: (key: string, oldValue: unknown, fromVersion: number) => unknown;\n /** Optional custom storage backend (primarily for testing). */\n backend?: StorageBackend;\n}\n\nexport interface SimaStore {\n get<T>(key: string, fallback: T): Promise<T>;\n getOptional<T>(key: string): Promise<T | null>;\n set<T>(key: string, value: T): Promise<void>;\n remove(key: string): Promise<void>;\n clear(): Promise<void>;\n}\n\ninterface StoredEnvelope<T> {\n v: number;\n d: T;\n}\n\nconst memoryFallback = new Map<string, string>();\n\nfunction canUseNativeStorage(): boolean {\n return (\n typeof window !== 'undefined' &&\n Capacitor.isNativePlatform() &&\n Capacitor.isPluginAvailable('SimaStorage')\n );\n}\n\nexport const defaultStorageBackend: StorageBackend = {\n async getItem(key: string): Promise<string | null> {\n if (canUseNativeStorage()) {\n const { value } = await SimaStorage.get({ key });\n return value ?? null;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n return window.localStorage.getItem(key);\n } catch {\n return memoryFallback.get(key) ?? null;\n }\n }\n return memoryFallback.get(key) ?? null;\n },\n\n async setItem(key: string, value: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.set({ key, value });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.setItem(key, value);\n return;\n } catch {\n memoryFallback.set(key, value);\n return;\n }\n }\n memoryFallback.set(key, value);\n },\n\n async removeItem(key: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.remove({ key });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.removeItem(key);\n return;\n } catch {\n memoryFallback.delete(key);\n return;\n }\n }\n memoryFallback.delete(key);\n },\n\n async clearPrefix(prefix: string): Promise<void> {\n if (canUseNativeStorage()) {\n await SimaStorage.clearPrefix({ prefix });\n return;\n }\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n const toDelete: string[] = [];\n for (let i = 0; i < window.localStorage.length; i += 1) {\n const k = window.localStorage.key(i);\n if (k && k.startsWith(prefix)) {\n toDelete.push(k);\n }\n }\n for (const k of toDelete) {\n window.localStorage.removeItem(k);\n }\n return;\n } catch {\n // Fall through to memoryFallback cleanup.\n }\n }\n for (const k of Array.from(memoryFallback.keys())) {\n if (k.startsWith(prefix)) {\n memoryFallback.delete(k);\n }\n }\n },\n};\n\nexport function createStorage(config: StorageConfig = {}): SimaStore {\n const namespace = config.namespace ?? 'default';\n const version = config.version ?? 1;\n const backend = config.backend ?? defaultStorageBackend;\n const prefix = `simasdk:store:${namespace}:`;\n\n function fullKey(key: string): string {\n return `${prefix}${key}`;\n }\n\n async function readOptional<T>(key: string): Promise<T | null> {\n const raw = await backend.getItem(fullKey(key));\n if (raw === null) {\n return null;\n }\n try {\n const parsed = JSON.parse(raw) as Partial<StoredEnvelope<T>>;\n if (typeof parsed !== 'object' || parsed === null || !('d' in parsed)) {\n return null;\n }\n const storedVersion = typeof parsed.v === 'number' ? parsed.v : 1;\n if (storedVersion !== version && config.migrate) {\n const migrated = config.migrate(key, parsed.d, storedVersion) as T;\n await backend.setItem(fullKey(key), JSON.stringify({ v: version, d: migrated }));\n return migrated;\n }\n return parsed.d as T;\n } catch {\n return null;\n }\n }\n\n return {\n async get<T>(key: string, fallback: T): Promise<T> {\n const value = await readOptional<T>(key);\n return value === null ? fallback : value;\n },\n\n getOptional: readOptional,\n\n async set<T>(key: string, value: T): Promise<void> {\n const envelope: StoredEnvelope<T> = { v: version, d: value };\n await backend.setItem(fullKey(key), JSON.stringify(envelope));\n },\n\n async remove(key: string): Promise<void> {\n await backend.removeItem(fullKey(key));\n },\n\n async clear(): Promise<void> {\n if (backend.clearPrefix) {\n await backend.clearPrefix(prefix);\n }\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import AppTrackingTransparency
|
|
4
|
+
|
|
5
|
+
@objc(SimaAttPlugin)
|
|
6
|
+
public class SimaAttPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
|
+
public let identifier = "SimaAttPlugin"
|
|
8
|
+
public let jsName = "SimaAtt"
|
|
9
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
10
|
+
CAPPluginMethod(name: "getStatus", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "requestAuthorization", returnType: CAPPluginReturnPromise)
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
private func mapStatus(_ status: ATTrackingManager.AuthorizationStatus) -> String {
|
|
15
|
+
switch status {
|
|
16
|
+
case .authorized:
|
|
17
|
+
return "authorized"
|
|
18
|
+
case .denied:
|
|
19
|
+
return "denied"
|
|
20
|
+
case .restricted:
|
|
21
|
+
return "restricted"
|
|
22
|
+
case .notDetermined:
|
|
23
|
+
return "not_determined"
|
|
24
|
+
@unknown default:
|
|
25
|
+
return "not_determined"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@objc func getStatus(_ call: CAPPluginCall) {
|
|
30
|
+
let status = mapStatus(ATTrackingManager.trackingAuthorizationStatus)
|
|
31
|
+
call.resolve(["status": status])
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@objc func requestAuthorization(_ call: CAPPluginCall) {
|
|
35
|
+
DispatchQueue.main.async {
|
|
36
|
+
ATTrackingManager.requestTrackingAuthorization { status in
|
|
37
|
+
call.resolve(["status": self.mapStatus(status)])
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import ObjectiveC.runtime
|
|
3
|
+
import Capacitor
|
|
4
|
+
|
|
5
|
+
/// Runtime bridge to `FIRApp` and `FIRAnalytics` in the host app binary.
|
|
6
|
+
///
|
|
7
|
+
/// Using Objective-C runtime dispatch keeps `simasdk` completely free of compile-time
|
|
8
|
+
/// Firebase / Google measurement dependencies. Apps that do not opt into Firebase
|
|
9
|
+
/// link zero Firebase binaries, while apps that add `FirebaseAnalytics` to their iOS
|
|
10
|
+
/// target get full native Firebase Analytics bridging.
|
|
11
|
+
private enum FirebaseRuntimeBridge {
|
|
12
|
+
private typealias DefaultAppFn = @convention(c) (AnyObject, Selector) -> AnyObject?
|
|
13
|
+
private typealias ConfigureFn = @convention(c) (AnyObject, Selector) -> Void
|
|
14
|
+
private typealias LogEventFn = @convention(c) (AnyObject, Selector, NSString, NSDictionary?) -> Void
|
|
15
|
+
private typealias SetCollectionEnabledFn = @convention(c) (AnyObject, Selector, ObjCBool) -> Void
|
|
16
|
+
|
|
17
|
+
static var isFirebaseLinked: Bool {
|
|
18
|
+
NSClassFromString("FIRApp") != nil && NSClassFromString("FIRAnalytics") != nil
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static var isDefaultAppConfigured: Bool {
|
|
22
|
+
guard let appClass = NSClassFromString("FIRApp") else {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
let selector = Selector(("defaultApp"))
|
|
26
|
+
guard let method = class_getClassMethod(appClass, selector) else {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
let fn = unsafeBitCast(method_getImplementation(method), to: DefaultAppFn.self)
|
|
30
|
+
return fn(appClass, selector) != nil
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static func configureDefaultAppIfPossible() {
|
|
34
|
+
guard isFirebaseLinked, !isDefaultAppConfigured else {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
guard Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") != nil else {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
guard let appClass = NSClassFromString("FIRApp") else {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
let selector = Selector(("configure"))
|
|
44
|
+
guard let method = class_getClassMethod(appClass, selector) else {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
let fn = unsafeBitCast(method_getImplementation(method), to: ConfigureFn.self)
|
|
48
|
+
fn(appClass, selector)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static func logEvent(name: String, parameters: [String: Any]?) -> Bool {
|
|
52
|
+
guard let analyticsClass = NSClassFromString("FIRAnalytics") else {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
let selector = Selector(("logEventWithName:parameters:"))
|
|
56
|
+
guard let method = class_getClassMethod(analyticsClass, selector) else {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
let fn = unsafeBitCast(method_getImplementation(method), to: LogEventFn.self)
|
|
60
|
+
fn(analyticsClass, selector, name as NSString, parameters as NSDictionary?)
|
|
61
|
+
return true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static func setAnalyticsCollectionEnabled(_ enabled: Bool) -> Bool {
|
|
65
|
+
guard let analyticsClass = NSClassFromString("FIRAnalytics") else {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
let selector = Selector(("setAnalyticsCollectionEnabled:"))
|
|
69
|
+
guard let method = class_getClassMethod(analyticsClass, selector) else {
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
let fn = unsafeBitCast(method_getImplementation(method), to: SetCollectionEnabledFn.self)
|
|
73
|
+
fn(analyticsClass, selector, ObjCBool(enabled))
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@objc(SimaFirebasePlugin)
|
|
79
|
+
public class SimaFirebasePlugin: CAPPlugin, CAPBridgedPlugin {
|
|
80
|
+
public let identifier = "SimaFirebasePlugin"
|
|
81
|
+
public let jsName = "SimaFirebaseAnalytics"
|
|
82
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
83
|
+
CAPPluginMethod(name: "logEvent", returnType: CAPPluginReturnPromise),
|
|
84
|
+
CAPPluginMethod(name: "setAnalyticsCollectionEnabled", returnType: CAPPluginReturnPromise)
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
/// Runs when Capacitor initializes registered plugins at app launch (before any JS call).
|
|
88
|
+
/// If the host app linked `FirebaseAnalytics` and bundled `GoogleService-Info.plist` but
|
|
89
|
+
/// did not call `FirebaseApp.configure()` in `AppDelegate`, this safely configures the
|
|
90
|
+
/// default Firebase app once. If `AppDelegate` already configured Firebase, this is a no-op.
|
|
91
|
+
override public func load() {
|
|
92
|
+
super.load()
|
|
93
|
+
FirebaseRuntimeBridge.configureDefaultAppIfPossible()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@objc func logEvent(_ call: CAPPluginCall) {
|
|
97
|
+
guard FirebaseRuntimeBridge.isFirebaseLinked else {
|
|
98
|
+
call.reject(
|
|
99
|
+
"FirebaseAnalytics native SDK is not linked in this iOS app. Add FirebaseAnalytics (firebase-ios-sdk) to your app target.",
|
|
100
|
+
"NOT_CONFIGURED"
|
|
101
|
+
)
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
guard FirebaseRuntimeBridge.isDefaultAppConfigured else {
|
|
106
|
+
call.reject(
|
|
107
|
+
"Firebase is not configured. Ensure GoogleService-Info.plist is included in the app target and FirebaseApp.configure() is called.",
|
|
108
|
+
"NOT_CONFIGURED"
|
|
109
|
+
)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
guard let name = call.getString("name"), !name.isEmpty else {
|
|
114
|
+
call.reject("Missing required event name.", "INVALID_EVENT_NAME")
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var convertedParams: [String: Any]? = nil
|
|
119
|
+
if let rawParams = call.getObject("params") {
|
|
120
|
+
var dict: [String: Any] = [:]
|
|
121
|
+
for (key, value) in rawParams {
|
|
122
|
+
if let strVal = value as? String {
|
|
123
|
+
dict[key] = strVal
|
|
124
|
+
} else if let numVal = value as? NSNumber {
|
|
125
|
+
if CFGetTypeID(numVal) == CFBooleanGetTypeID() {
|
|
126
|
+
call.reject(
|
|
127
|
+
"Parameter \"\(key)\" has unsupported boolean value. Only string and finite number values are supported.",
|
|
128
|
+
"INVALID_PARAM_VALUE"
|
|
129
|
+
)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
if !numVal.doubleValue.isFinite {
|
|
133
|
+
call.reject(
|
|
134
|
+
"Parameter \"\(key)\" must be a finite number.",
|
|
135
|
+
"INVALID_PARAM_VALUE"
|
|
136
|
+
)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
dict[key] = numVal
|
|
140
|
+
} else {
|
|
141
|
+
call.reject(
|
|
142
|
+
"Parameter \"\(key)\" has an unsupported value type. Only string and finite number values are supported.",
|
|
143
|
+
"INVALID_PARAM_VALUE"
|
|
144
|
+
)
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
convertedParams = dict
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
guard FirebaseRuntimeBridge.logEvent(name: name, parameters: convertedParams) else {
|
|
152
|
+
call.reject("Unable to invoke FIRAnalytics.logEventWithName:parameters:.", "NOT_CONFIGURED")
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
call.resolve()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@objc func setAnalyticsCollectionEnabled(_ call: CAPPluginCall) {
|
|
160
|
+
guard FirebaseRuntimeBridge.isFirebaseLinked else {
|
|
161
|
+
call.reject(
|
|
162
|
+
"FirebaseAnalytics native SDK is not linked in this iOS app. Add FirebaseAnalytics (firebase-ios-sdk) to your app target.",
|
|
163
|
+
"NOT_CONFIGURED"
|
|
164
|
+
)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
guard FirebaseRuntimeBridge.isDefaultAppConfigured else {
|
|
169
|
+
call.reject(
|
|
170
|
+
"Firebase is not configured. Ensure GoogleService-Info.plist is included in the app target and FirebaseApp.configure() is called.",
|
|
171
|
+
"NOT_CONFIGURED"
|
|
172
|
+
)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
guard let enabled = call.getBool("enabled") else {
|
|
177
|
+
call.reject("Missing required boolean parameter \"enabled\".", "INVALID_PARAM_VALUE")
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
guard FirebaseRuntimeBridge.setAnalyticsCollectionEnabled(enabled) else {
|
|
182
|
+
call.reject("Unable to invoke FIRAnalytics.setAnalyticsCollectionEnabled:.", "NOT_CONFIGURED")
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
call.resolve()
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import GameKit
|
|
4
|
+
import UIKit
|
|
5
|
+
|
|
6
|
+
@objc(SimaGamesPlugin)
|
|
7
|
+
public class SimaGamesPlugin: CAPPlugin, CAPBridgedPlugin, GKGameCenterControllerDelegate {
|
|
8
|
+
public let identifier = "SimaGamesPlugin"
|
|
9
|
+
public let jsName = "SimaGames"
|
|
10
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
11
|
+
CAPPluginMethod(name: "isAuthenticated", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "signIn", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "submitScore", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "showLeaderboard", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "unlockAchievement", returnType: CAPPluginReturnPromise)
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
private var pendingLeaderboardCall: CAPPluginCall?
|
|
19
|
+
|
|
20
|
+
@objc func isAuthenticated(_ call: CAPPluginCall) {
|
|
21
|
+
call.resolve(["authenticated": GKLocalPlayer.local.isAuthenticated])
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@objc func signIn(_ call: CAPPluginCall) {
|
|
25
|
+
DispatchQueue.main.async {
|
|
26
|
+
let player = GKLocalPlayer.local
|
|
27
|
+
if player.isAuthenticated {
|
|
28
|
+
call.resolve(["authenticated": true])
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
var resolved = false
|
|
33
|
+
player.authenticateHandler = { [weak self] viewController, error in
|
|
34
|
+
if let vc = viewController {
|
|
35
|
+
self?.bridge?.viewController?.present(vc, animated: true)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
if !resolved {
|
|
39
|
+
resolved = true
|
|
40
|
+
if let error = error {
|
|
41
|
+
call.resolve(["authenticated": false, "error": error.localizedDescription])
|
|
42
|
+
} else {
|
|
43
|
+
call.resolve(["authenticated": player.isAuthenticated])
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@objc func submitScore(_ call: CAPPluginCall) {
|
|
51
|
+
guard let leaderboardId = call.getString("leaderboardId")?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
52
|
+
!leaderboardId.isEmpty,
|
|
53
|
+
let scoreDouble = call.getDouble("score"),
|
|
54
|
+
scoreDouble.isFinite else {
|
|
55
|
+
call.reject("leaderboardId and finite numeric score are required.", "INVALID_ARGUMENT")
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
guard GKLocalPlayer.local.isAuthenticated else {
|
|
60
|
+
call.resolve(["submitted": false])
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let scoreInt = Int(scoreDouble.rounded())
|
|
65
|
+
GKLeaderboard.submitScore(
|
|
66
|
+
scoreInt,
|
|
67
|
+
context: 0,
|
|
68
|
+
player: GKLocalPlayer.local,
|
|
69
|
+
leaderboardIDs: [leaderboardId]
|
|
70
|
+
) { error in
|
|
71
|
+
if let error = error {
|
|
72
|
+
call.reject("Failed to submit Game Center score.", "SUBMIT_ERROR", error)
|
|
73
|
+
} else {
|
|
74
|
+
call.resolve(["submitted": true])
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@objc func showLeaderboard(_ call: CAPPluginCall) {
|
|
80
|
+
guard let leaderboardId = call.getString("leaderboardId")?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
81
|
+
!leaderboardId.isEmpty else {
|
|
82
|
+
call.reject("leaderboardId is required.", "INVALID_ARGUMENT")
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let timeSpan = call.getString("timeSpan") ?? "allTime"
|
|
87
|
+
let scope: GKLeaderboard.TimeScope
|
|
88
|
+
switch timeSpan.lowercased() {
|
|
89
|
+
case "daily":
|
|
90
|
+
scope = .today
|
|
91
|
+
case "weekly":
|
|
92
|
+
scope = .week
|
|
93
|
+
default:
|
|
94
|
+
scope = .allTime
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
DispatchQueue.main.async {
|
|
98
|
+
guard let presenter = self.bridge?.viewController else {
|
|
99
|
+
call.reject("Unable to locate root view controller.", "NO_VIEW_CONTROLLER")
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
self.pendingLeaderboardCall = call
|
|
103
|
+
let gcVC = GKGameCenterViewController(
|
|
104
|
+
leaderboardID: leaderboardId,
|
|
105
|
+
playerScope: .global,
|
|
106
|
+
timeScope: scope
|
|
107
|
+
)
|
|
108
|
+
gcVC.gameCenterDelegate = self
|
|
109
|
+
presenter.present(gcVC, animated: true)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@objc func unlockAchievement(_ call: CAPPluginCall) {
|
|
114
|
+
guard let achievementId = call.getString("achievementId")?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
115
|
+
!achievementId.isEmpty else {
|
|
116
|
+
call.reject("achievementId is required.", "INVALID_ARGUMENT")
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
guard GKLocalPlayer.local.isAuthenticated else {
|
|
121
|
+
call.resolve(["unlocked": false])
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let achievement = GKAchievement(identifier: achievementId)
|
|
126
|
+
achievement.percentComplete = 100.0
|
|
127
|
+
achievement.showsCompletionBanner = true
|
|
128
|
+
GKAchievement.report([achievement]) { error in
|
|
129
|
+
if let error = error {
|
|
130
|
+
call.reject("Failed to unlock achievement.", "ACHIEVEMENT_ERROR", error)
|
|
131
|
+
} else {
|
|
132
|
+
call.resolve(["unlocked": true])
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
|
|
138
|
+
gameCenterViewController.dismiss(animated: true) {
|
|
139
|
+
if let call = self.pendingLeaderboardCall {
|
|
140
|
+
self.pendingLeaderboardCall = nil
|
|
141
|
+
call.resolve(["shown": true])
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|