Skip to content

Commit 77cacfc

Browse files
Attempt to fix tauri plugin init
1 parent 916ebf5 commit 77cacfc

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

frontend/src-tauri/capabilities/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
}
2929
]
3030
},
31-
"sign-in-with-apple:default"
31+
"sign-in-with-apple:default",
32+
"store:allow-get-region"
3233
]
3334
}

frontend/src-tauri/capabilities/mobile-ios.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030
]
3131
},
32-
"sign-in-with-apple:default"
32+
"sign-in-with-apple:default",
33+
"store:allow-get-region"
3334
]
3435
}

frontend/src/utils/region-gate.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,58 @@ const US_CODES = ["USA", "ASM", "GUM", "PRI", "VIR", "MNP", "UMI"]; // US and te
88
* Note: The Swift code returns "UNKNOWN" if it can't determine the region.
99
*/
1010
export async function getStoreRegion(): Promise<string> {
11-
console.log("[Region Gate] Attempting to invoke plugin:store|get_region...");
11+
// First, check if we're running on iOS
12+
let isIOSPlatform = false;
1213
try {
14+
console.log("[Region Gate] Checking if running on iOS...");
15+
const { isTauri } = await import("@tauri-apps/api/core");
16+
const isTauriEnv = await isTauri();
17+
18+
if (isTauriEnv) {
19+
const { type } = await import("@tauri-apps/plugin-os");
20+
const platform = await type();
21+
isIOSPlatform = platform === "ios";
22+
console.log("[Region Gate] Platform check:", platform, "iOS:", isIOSPlatform);
23+
} else {
24+
console.log("[Region Gate] Not in Tauri environment");
25+
return "NOT_TAURI";
26+
}
27+
} catch (e) {
28+
console.error("[Region Gate] Error checking platform:", e);
29+
return "PLATFORM_ERROR";
30+
}
31+
32+
// If not on iOS, no need to call the plugin
33+
if (!isIOSPlatform) {
34+
console.log("[Region Gate] Not on iOS, skipping region check");
35+
return "NOT_IOS";
36+
}
37+
38+
// We're on iOS, try to get the App Store region
39+
console.log("[Region Gate] On iOS, invoking store plugin...");
40+
try {
41+
// Using the same pattern as sign-in-with-apple
1342
const code = await invoke<string>("plugin:store|get_region");
14-
console.log("[Region Gate] Success! Store region code:", code);
43+
console.log("[Region Gate] Successfully got region code:", code);
1544
return code;
1645
} catch (error) {
17-
console.error("[Region Gate] Error invoking plugin:", error);
18-
console.error("[Region Gate] Stack trace:", new Error().stack);
46+
// Format error consistently with other plugin patterns
47+
console.error("[Region Gate] Error getting region:", error);
1948

20-
// Return the error message for diagnosis
21-
let errorMsg = "Error: ";
2249
if (error instanceof Error) {
23-
errorMsg += error.message;
50+
console.error("[Region Gate] Error name:", error.name);
51+
console.error("[Region Gate] Error message:", error.message);
52+
return `ERROR:${error.name}:${error.message.substring(0, 50)}`;
2453
} else if (typeof error === "string") {
25-
errorMsg += error;
54+
return `ERROR:${error.substring(0, 50)}`;
2655
} else {
27-
errorMsg += JSON.stringify(error);
56+
try {
57+
const errorStr = JSON.stringify(error).substring(0, 50);
58+
return `ERROR:JSON:${errorStr}`;
59+
} catch {
60+
return "ERROR:UNKNOWN";
61+
}
2862
}
29-
30-
// Truncate if the error message is too long
31-
if (errorMsg.length > 30) {
32-
errorMsg = errorMsg.substring(0, 27) + "...";
33-
}
34-
35-
return errorMsg;
3663
}
3764
}
3865

plugins/store/src/mobile.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
1212
_app: &AppHandle<R>,
1313
api: PluginApi<R, C>,
1414
) -> crate::Result<Store<R>> {
15+
// For iOS, use the actual plugin
1516
#[cfg(target_os = "ios")]
16-
let handle = api.register_ios_plugin(init_plugin_store)?;
17+
let handle = {
18+
println!("[Store Plugin] Registering iOS plugin");
19+
let handle = api.register_ios_plugin(init_plugin_store)?;
20+
println!("[Store Plugin] Successfully registered iOS plugin");
21+
handle
22+
};
1723

24+
// For other platforms, use a dummy handle
1825
#[cfg(not(target_os = "ios"))]
1926
let handle = {
20-
// Dummy handle for non-iOS platforms
27+
println!("[Store Plugin] Creating dummy handle for non-iOS platform");
2128
let handle: PluginHandle<R> = unsafe { std::mem::zeroed() };
2229
handle
2330
};
@@ -30,9 +37,23 @@ pub struct Store<R: Runtime>(PluginHandle<R>);
3037

3138
impl<R: Runtime> Store<R> {
3239
pub fn get_region(&self) -> crate::Result<String> {
33-
self.0
34-
.run_mobile_plugin("getRegion", ())
35-
.map_err(Into::into)
40+
println!("[Store Plugin] Calling getRegion");
41+
#[cfg(target_os = "ios")]
42+
let result = self.0.run_mobile_plugin("getRegion", ());
43+
44+
#[cfg(not(target_os = "ios"))]
45+
let result = {
46+
println!("[Store Plugin] Non-iOS platform, returning UNKNOWN");
47+
Ok(String::from("UNKNOWN"))
48+
};
49+
50+
// Log the result and convert any errors
51+
match &result {
52+
Ok(region) => println!("[Store Plugin] getRegion success: {}", region),
53+
Err(e) => println!("[Store Plugin] getRegion error: {:?}", e),
54+
}
55+
56+
result.map_err(Into::into)
3657
}
3758
}
3859

0 commit comments

Comments
 (0)