Skip to content

Commit

Permalink
several optimizations 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
hiaaryan committed Dec 31, 2024
1 parent a8b17c6 commit c7c0a88
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 303 deletions.
Binary file modified bun.lockb
Binary file not shown.
65 changes: 44 additions & 21 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Menu, Tray, app, dialog, ipcMain, shell } from "electron";
import serve from "electron-serve";
import { createWindow } from "./helpers";
import { protocol } from "electron";
import { RpcClient, ActivityType } from "dc-rpc";
import {
addSongToPlaylist,
addToFavourites,
Expand All @@ -25,6 +24,7 @@ import {
import { initDatabase } from "./helpers/db/createDB";
import { parseFile } from "music-metadata";
import fs from "fs";
import { Client } from "@xhayper/discord-rpc";

const isProd = process.env.NODE_ENV === "production";

Expand All @@ -38,17 +38,26 @@ let mainWindow: any;
let settings: any;

// @hiaaryan: Initialize Database on Startup
(async () => {
initDatabase();
settings = await getSettings();
const initializeLibrary = async () => {
try {
// Initialize SQLite database
await initDatabase();

if (settings) {
await initializeData(settings.musicFolder);
// Get settings
settings = await getSettings();

if (settings) {
// Initialize the music library
await initializeData(settings.musicFolder);
}
} catch (error) {
console.error('Error initializing library:', error);
}
})();
};

(async () => {
await app.whenReady();
await initializeLibrary();

// @hiaaryan: Using Depreciated API [Seeking Not Supported with Net]
protocol.registerFileProtocol("wora", (request, callback) => {
Expand All @@ -59,7 +68,7 @@ let settings: any;
width: 1500,
height: 900,
titleBarStyle: "hidden",
trafficLightPosition: { x: 20, y: 15 },
trafficLightPosition: { x: 20, y: 20 },
transparent: true,
frame: false,
icon: path.join(__dirname, "resources/icon.icns"),
Expand Down Expand Up @@ -103,27 +112,41 @@ let settings: any;
})();

// @hiaaryan: Initialize Discord RPC
const client = new RpcClient({ transport: 'ipc' });
ipcMain.on("set-rpc-state", (_, { details, state, timestamp }) => {
const client = new Client({
clientId: "1243707416588320800"
});

client.login();

ipcMain.on("set-rpc-state", (_, { details, state, seek, duration, cover }) => {
let startTimestamp, endTimestamp;

if (duration && seek) {
const now = Math.ceil(Date.now());
startTimestamp = now - (seek * 1000);
endTimestamp = now + ((duration - seek) * 1000);
}

const setActivity = {
details,
state,
largeImageKey: "logo",
largeImageText: `v${app.getVersion()}`,
largeImageKey: cover,
instance: false,
type: ActivityType.Listening,
type: 2,
startTimestamp: startTimestamp,
endTimestamp: endTimestamp,
};

if (timestamp) {
(setActivity as any).startTimestamp = Date.now();
}
client.user.setActivity(setActivity);
});

client.setActivity(setActivity);
// @hiaaryan: Called to Rescan Library
ipcMain.handle("rescanLibrary", async () => {
await initializeLibrary();
});
client.login({ clientId: "1243707416588320800" });

// @hiaaryan: Called to Set Music Folder
ipcMain.handle("setMusicFolder", async () => {
ipcMain.handle("scanLibrary", async () => {
const diag = await dialog
.showOpenDialog({
properties: ["openDirectory", "createDirectory"],
Expand Down Expand Up @@ -261,7 +284,7 @@ ipcMain.handle("updateSettings", async (_, data: any) => {
});

ipcMain.handle("uploadProfilePicture", async (_, file) => {
const uploadsDir = path.join(app.getPath("userData"), "uploads/profile");
const uploadsDir = path.join(app.getPath("userData"), "utilities/uploads/profile");
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
Expand All @@ -275,7 +298,7 @@ ipcMain.handle("uploadProfilePicture", async (_, file) => {
});

ipcMain.handle("uploadPlaylistCover", async (_, file) => {
const uploadsDir = path.join(app.getPath("userData"), "uploads/playlists");
const uploadsDir = path.join(app.getPath("userData"), "utilities/uploads/playlists");
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
Expand Down
Loading

0 comments on commit c7c0a88

Please sign in to comment.