handle enable and disable map deep links

This commit is contained in:
MathieuG-P
2022-12-17 01:24:37 +01:00
parent ca5c86dbf1
commit c626b850c9
6 changed files with 115 additions and 21 deletions
+45
View File
@@ -82,4 +82,49 @@ ipcMain.on("download-map", async (event, request: IpcRequest<{map: BsvMapDetail,
console.log(err);
utils.ipcSend(request.responceChannel, {success: false, error: err});
});
});
ipcMain.on("register-maps-deep-link", async (event, request: IpcRequest<void>) => {
const maps = LocalMapsManagerService.getInstance();
const utils = UtilsService.getInstance();
try{
const res = maps.enableDeepLinks();
utils.ipcSend(request.responceChannel, {success: true, data: res});
}
catch(e){
utils.ipcSend(request.responceChannel, {success: false});
}
});
ipcMain.on("unregister-maps-deep-link", async (event, request: IpcRequest<void>) => {
const maps = LocalMapsManagerService.getInstance();
const utils = UtilsService.getInstance();
try{
const res = maps.disableDeepLinks();
utils.ipcSend(request.responceChannel, {success: true, data: res});
}
catch(e){
utils.ipcSend(request.responceChannel, {success: false});
}
});
ipcMain.on("is-map-deep-links-enabled", async (event, request: IpcRequest<void>) => {
const maps = LocalMapsManagerService.getInstance();
const utils = UtilsService.getInstance();
try{
const res = maps.isDeepLinksEnabled();
utils.ipcSend(request.responceChannel, {success: true, data: res});
}
catch(e){
utils.ipcSend(request.responceChannel, {success: false});
}
});
+14 -12
View File
@@ -61,23 +61,25 @@ app.on('window-all-closed', () => {
}
});
app.on('open-url', (event, url) => {
DeepLinkService.getInstance().dispatchLinkOpened(url);
});
const gotTheLock = app.requestSingleInstanceLock();
if(!gotTheLock){
app.quit();
}
else{
app.whenReady().then(() => {
createWindow();
}).catch(log.error);
app.whenReady().then(() => {
createWindow();
app.whenReady().then(() => {
protocol.registerFileProtocol('file', (request, callback) => {
const pathname = decodeURI(request.url.replace('file:///', ''));
callback(pathname);
protocol.registerFileProtocol('file', (request, callback) => {
const pathname = decodeURI(request.url.replace('file:///', ''));
callback(pathname);
});
}).catch(log.error);
app.on('open-url', (event, url) => {
DeepLinkService.getInstance().dispatchLinkOpened(url);
});
});
}
+4
View File
@@ -1,6 +1,7 @@
import { app } from "electron";
import path from "path";
import { URL } from "url";
import log from "electron-log"
export class DeepLinkService {
@@ -65,6 +66,9 @@ export class DeepLinkService {
}
public dispatchLinkOpened(link: string){
log.info("DEISPATCH", link);
const url = new URL(link);
const protocolListeners = this.listeners.get(url.protocol.replace(":", "")) ?? [];
@@ -12,6 +12,8 @@ import StreamZip from "node-stream-zip";
import { RequestService } from "../request.service";
import sanitize from "sanitize-filename";
import archiver from "archiver";
import { DeepLinkService } from "../deep-link.service";
import log from 'electron-log';
export class LocalMapsManagerService {
@@ -25,16 +27,31 @@ export class LocalMapsManagerService {
private readonly LEVELS_ROOT_FOLDER = "Beat Saber_Data";
private readonly CUSTOM_LEVELS_FOLDER = "CustomLevels";
private readonly DEEP_LINKS = {
BeatSaver: "beatsaver",
ScoreSaber: "web+bsmap"
};
private readonly localVersion: BSLocalVersionService;
private readonly installLocation: InstallationLocationService;
private readonly utils: UtilsService;
private readonly reqService: RequestService
private readonly reqService: RequestService;
private readonly deepLink: DeepLinkService;
private constructor(){
this.localVersion = BSLocalVersionService.getInstance();
this.installLocation = InstallationLocationService.getInstance();
this.utils = UtilsService.getInstance();
this.reqService = RequestService.getInstance();
this.deepLink = DeepLinkService.getInstance();
this.deepLink.addLinkOpenedListener(this.DEEP_LINKS.BeatSaver, (link) => {
log.info("RECEIVED FROM", this.DEEP_LINKS.BeatSaver, link);
});
this.deepLink.addLinkOpenedListener(this.DEEP_LINKS.ScoreSaber, (link) => {
log.info("RECEIVED FROM", this.DEEP_LINKS.ScoreSaber, link);
});
}
private async getMapsFolderPath(version?: BSVersion): Promise<string>{
@@ -212,7 +229,7 @@ export class LocalMapsManagerService {
archive.pipe(output);
archive.on("error", (e) => {throw e});
if(!maps || maps.length === 0){
const mapsFolder = await this.getMapsFolderPath(version);
@@ -233,8 +250,16 @@ export class LocalMapsManagerService {
}
public async enableDeepLink(){
// TODO
public enableDeepLinks(): boolean{
return Array.from(Object.values(this.DEEP_LINKS)).every(link => this.deepLink.registerDeepLink(link));
}
public disableDeepLinks(): boolean{
return Array.from(Object.values(this.DEEP_LINKS)).every(link => this.deepLink.unRegisterDeepLink(link));
}
public isDeepLinksEnabled(): boolean{
return Array.from(Object.values(this.DEEP_LINKS)).every(link => this.deepLink.isDeepLinkRegistred(link));
}
@@ -60,12 +60,14 @@ export function SettingsPage() {
const[languageSelected, setLanguageSelected]= useState(languagesItems.find(e => e.value === i18nService.currentLanguage).id);
const [installationFolder, setInstallationFolder] = useState(null);
const [showSupporters, setShowSupporters] = useState(false);
const [mapDeepLinksEnabled, setMapDeepLinksEnabled] = useState(false);
const [appVersion, setAppVersion] = useState("");
const nav = useNavigate();
useEffect(() => {
loadInstallationFolder();
ipcService.send<string>("current-version").then(res => setAppVersion(res.data));
mapsManager.isDeepLinksEnabled().then(enabled => setMapDeepLinksEnabled(() => enabled));
}, []);
const resetColors = () => {
@@ -138,8 +140,8 @@ export function SettingsPage() {
const openLogs = () => ipcService.sendLazy("open-logs");
const enableMapsDeepLink = () => {
mapsManager.enableDeepLink();
const toogleMapDeepLinks = () => {
mapsManager.toogleDeepLinks().finally(() => mapsManager.isDeepLinksEnabled().then(enabled => setMapDeepLinksEnabled(() => enabled)));
}
return (
@@ -183,7 +185,7 @@ export function SettingsPage() {
<ul className="w-full flex flex-col gap-1.5">
<li className="bg-main-color-1 rounded-md group-one flex justify-between items-center basis-0 py-2 px-3">
<div className="flex items-center gap-2">
<BsmCheckbox className="relative z-[1] h-5 w-5" onChange={enableMapsDeepLink}/>
<BsmCheckbox className="relative z-[1] h-5 w-5" onChange={toogleMapDeepLinks} checked={mapDeepLinksEnabled}/>
<span className="font-extrabold">Maps</span>
</div>
<div className="flex h-full gap-2">
+18 -2
View File
@@ -159,8 +159,24 @@ export class MapsManagerService {
this.progressBar.hide(true);
}
public async enableDeepLink(): Promise<boolean>{
return true; // TODO
public isDeepLinksEnabled(): Promise<boolean>{
return this.ipcService.send<boolean>("is-map-deep-links-enabled").then(res => (
res.success ? res.data : false
));
}
public async toogleDeepLinks(): Promise<boolean>{
const isEnabled = await this.isDeepLinksEnabled();
const channel = isEnabled ? "unregister-maps-deep-link" : "register-maps-deep-link";
const res = await this.ipcService.send<boolean>(channel);
return res.success ? res.data : false;
}
public get versionLinked$(): Observable<BSVersion>{