mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
feat: enhance local storage and webview APIs with improved error handling and resource management
This commit is contained in:
parent
f4306ad1c3
commit
bb0afa5a0c
@ -13,7 +13,7 @@ class PluginLocalStorageApi {
|
|||||||
required this.pluginName,
|
required this.pluginName,
|
||||||
}) {
|
}) {
|
||||||
runtime.onMessage("LocalStorage.getItem", (args) {
|
runtime.onMessage("LocalStorage.getItem", (args) {
|
||||||
final key = args[0];
|
final key = args[0]["key"];
|
||||||
final value = getItem(key);
|
final value = getItem(key);
|
||||||
runtime.evaluate(
|
runtime.evaluate(
|
||||||
"""
|
"""
|
||||||
|
@ -70,4 +70,11 @@ class PluginSetIntervalApi {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dispose() {
|
||||||
|
for (var timer in _timers.values) {
|
||||||
|
timer.cancel();
|
||||||
|
}
|
||||||
|
_timers.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ class PluginTotpGenerator {
|
|||||||
|
|
||||||
runtime.evaluate(
|
runtime.evaluate(
|
||||||
"""
|
"""
|
||||||
eventEmitter.emit('Totp.generate', '$otp');
|
eventEmitter.emit('TotpGenerator.generate', '$otp');
|
||||||
""",
|
""",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -49,13 +49,17 @@ class PluginWebViewApi {
|
|||||||
}
|
}
|
||||||
showWebView(
|
showWebView(
|
||||||
url: args[0]["url"] as String,
|
url: args[0]["url"] as String,
|
||||||
initialSettings: WebviewInitialSettings.fromJson(
|
initialSettings: args[0]["initialSettings"] != null
|
||||||
|
? WebviewInitialSettings.fromJson(
|
||||||
args[0]["initialSettings"],
|
args[0]["initialSettings"],
|
||||||
),
|
)
|
||||||
|
: null,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Webview? webviewWindow;
|
||||||
|
|
||||||
Future showWebView({
|
Future showWebView({
|
||||||
required String url,
|
required String url,
|
||||||
WebviewInitialSettings? initialSettings,
|
WebviewInitialSettings? initialSettings,
|
||||||
@ -117,6 +121,8 @@ class PluginWebViewApi {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
webviewWindow = webview;
|
||||||
|
|
||||||
runtime.onMessage("WebView.close", (args) {
|
runtime.onMessage("WebView.close", (args) {
|
||||||
webview.close();
|
webview.close();
|
||||||
});
|
});
|
||||||
@ -134,7 +140,7 @@ class PluginWebViewApi {
|
|||||||
"domain": e.domain,
|
"domain": e.domain,
|
||||||
"path": e.path,
|
"path": e.path,
|
||||||
};
|
};
|
||||||
});
|
}).toList();
|
||||||
|
|
||||||
runtime.evaluate(
|
runtime.evaluate(
|
||||||
"""
|
"""
|
||||||
@ -157,4 +163,9 @@ class PluginWebViewApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dispose() {
|
||||||
|
webviewWindow?.close();
|
||||||
|
webviewWindow = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,19 +61,6 @@ class MetadataApiSignature {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
final res = runtime.evaluate(
|
|
||||||
"""
|
|
||||||
;$libraryCode;
|
|
||||||
const metadataApi = new MetadataApi();
|
|
||||||
""",
|
|
||||||
);
|
|
||||||
|
|
||||||
if (res.isError) {
|
|
||||||
AppLogger.reportError(
|
|
||||||
"Error evaluating code: $libraryCode\n${res.rawResult}",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create all the PluginAPIs after library code is evaluated
|
// Create all the PluginAPIs after library code is evaluated
|
||||||
final localStorageApi = PluginLocalStorageApi(
|
final localStorageApi = PluginLocalStorageApi(
|
||||||
runtime: runtime,
|
runtime: runtime,
|
||||||
@ -93,12 +80,26 @@ class MetadataApiSignature {
|
|||||||
setIntervalApi,
|
setIntervalApi,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final res = runtime.evaluate(
|
||||||
|
"""
|
||||||
|
;$libraryCode;
|
||||||
|
const metadataApi = new MetadataApi();
|
||||||
|
""",
|
||||||
|
);
|
||||||
metadataApi._signatureFlags = await metadataApi._getSignatureFlags();
|
metadataApi._signatureFlags = await metadataApi._getSignatureFlags();
|
||||||
|
|
||||||
|
if (res.isError) {
|
||||||
|
AppLogger.reportError(
|
||||||
|
"Error evaluating code: $libraryCode\n${res.rawResult}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return metadataApi;
|
return metadataApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
setIntervalApi.dispose();
|
||||||
|
webViewApi.dispose();
|
||||||
runtime.dispose();
|
runtime.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +120,7 @@ class MetadataApiSignature {
|
|||||||
$method(...${args != null ? jsonEncode(args) : "[]"})
|
$method(...${args != null ? jsonEncode(args) : "[]"})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
sendMessage("$method", JSON.stringify(res));
|
sendMessage("$method", res ? JSON.stringify(res) : "[]");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to send message in $method.then: ", `\${e.toString()}\n\${e.stack.toString()}`);
|
console.error("Failed to send message in $method.then: ", `\${e.toString()}\n\${e.stack.toString()}`);
|
||||||
}
|
}
|
||||||
@ -497,6 +498,12 @@ class MetadataApiSignature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----- User ------
|
// ----- User ------
|
||||||
|
Future<SpotubeUserObject> getMe() async {
|
||||||
|
final res = await invoke("metadataApi.getMe");
|
||||||
|
|
||||||
|
return SpotubeUserObject.fromJson(res);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> followArtist(String userId, String artistId) async {
|
Future<void> followArtist(String userId, String artistId) async {
|
||||||
await invoke("metadataApi.followArtist", [userId, artistId]);
|
await invoke("metadataApi.followArtist", [userId, artistId]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user