Skip to content

Commit

Permalink
Implement reset functions for saved objects and viewable elements, an…
Browse files Browse the repository at this point in the history
…d update app version handling
  • Loading branch information
waveyboym committed Dec 7, 2024
1 parent b3376cf commit c03b61a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 11 deletions.
31 changes: 31 additions & 0 deletions muzik-offline/src/database/saved_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,35 @@ export const emptySavedObject: SavedObject = {
PlayBackSpeed: "1",
AudioTransition: "No",
OutputDevice: ""
}

export function resetObject(obj: SavedObject){
obj.LaunchTab = "All tracks";
obj.AppActivityDiscord = "No";
obj.BGColour = "blue_purple_gradient";
obj.ThemeColour = "blueberry";
obj.PlayerBar = true;
obj.Volume = 0;
obj.VolumeStepAmount = "5";
obj.SongSeeker = 0;
obj.Animations = false;
obj.AppThemeBlur = true;
obj.WallpaperOpacityAmount = "8";
obj.OStype = OSTYPEenum.Windows;
obj.CompressImage = "No";
obj.UpcomingHistoryLimit = "10";
obj.SeekStepAmount = "10";
obj.SongLengthORremaining = "song length";
obj.AudioLabPreset = "flat";
obj.SavedPresets = ["flat", "hip-hop"];
obj.AlwaysRoundedCornersWindows = "No";
obj.AutoStartApp = "No";
obj.DirectoryScanningDepth = 1;
obj.player = "rodio";
obj.AudioQuality = "High(320kbps)";
obj.PlayBackSpeed = "1";
obj.AudioTransition = "No";
obj.OutputDevice = "";

return obj;
}
10 changes: 10 additions & 0 deletions muzik-offline/src/database/side_elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ export const viewableSideElements: viewableSideEl = {
All_albums: true,
All_genres: true,
All_playlists: true,
}

export function resetViewableElements(obj: viewableSideEl){
obj.All_tracks = true;
obj.All_artists = true;
obj.All_albums = true;
obj.All_genres = true;
obj.All_playlists = true;

return obj;
}
19 changes: 11 additions & 8 deletions muzik-offline/src/interface/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { isPermissionGranted, requestPermission, sendNotification } from '@tauri
import { MiniPlayer } from "@App/index";
import { listen } from "@tauri-apps/api/event";
import { processOSMediaControlsEvent } from "@utils/OSeventControl";
import { fetch_library, getWallpaperURL } from "@utils/index";
import { fetch_library, getWallpaperURL, shouldClearZustandStores } from "@utils/index";
import { local_songs_db } from "@database/database";
import { startPlayingNewSong } from "@utils/playerControl";

Expand Down Expand Up @@ -142,13 +142,16 @@ const App = () => {
}

useEffect(() => {
request_song();
checkOSType();
checkAndRequestNotificationPermission();
connect_to_discord();
get_server_port();
check_paths_for_new_music();
check_if_paths_are_still_valid();
shouldClearZustandStores().then(() => {
request_song();
checkOSType();
checkAndRequestNotificationPermission();
connect_to_discord();
get_server_port();
check_paths_for_new_music();
check_if_paths_are_still_valid();
});

const listenForOSeventsfunc = listenForOSevents();

return () => { listenForOSeventsfunc.then((unlisten) => unlisten()); }
Expand Down
14 changes: 13 additions & 1 deletion muzik-offline/src/interface/layouts/AboutSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ import { App_logo } from "@logos/index";
import "@styles/layouts/AboutSettings.scss";
import { open } from '@tauri-apps/plugin-shell';
import { motion } from "framer-motion";
import { getVersion } from '@tauri-apps/api/app';
import { useState, useEffect } from "react";

const AboutSettings = () => {
const [version, setVersion] = useState<string>('');

useEffect(() => {
async function fetchVersion() {
const version = await getVersion();
setVersion(version);
}
fetchVersion();
}, []);

return (
<div className="AboutSettings">
<h2>About Settings</h2>
<div className="logo">
<App_logo />
</div>
<h3>Copyright 2024 muzik-apps. All rights reserved.</h3>
<h3>Version "0.7.0"</h3>
<h3>Version {version}</h3>
<h3>
<motion.span whileTap={{scale: 0.98}} onClick={() => open("https://github.com/muzik-apps/muzik-offline")}>
muzik-offline
Expand Down
21 changes: 20 additions & 1 deletion muzik-offline/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { firstRunState, FSState, MaximisedState, PlayerInterface, PlayingPositionInterface, portState, QueueInterface, SavedDirectoriesInterface, SavedObjectInterface, searchInterface, toastInterface, viewableSideElInterface, wallpaperInterface, SavedPresetsValues } from './storeTypes';
import { firstRunState, FSState, MaximisedState, PlayerInterface, PlayingPositionInterface, portState, QueueInterface, SavedDirectoriesInterface, SavedObjectInterface, searchInterface, toastInterface, viewableSideElInterface, wallpaperInterface, SavedPresetsValues, VersionInterface } from './storeTypes';
import { emptyDirectories } from '@database/directories';
import { emptyPlayer } from '@database/player';
import { emptySavedObject } from '@database/saved_object';
Expand Down Expand Up @@ -37,6 +37,7 @@ export const useFisrstRunStore = create<firstRunState>()(
(set) => ({
firstRun: true,
setFirstRun: (nFR) => set((_state) => ({ firstRun: nFR })),
reset: () => set((_state) => ({ firstRun: true })),
}),
{name: 'firstRun',}
)
Expand Down Expand Up @@ -86,6 +87,7 @@ export const useWallpaperStore = create<wallpaperInterface>()(
wallpaperUUID: "",
setWallpaper: (nW) => set((_state) => ({ wallpaperUUID: nW })),
unsetWallpaper: () => set((_state) => ({ wallpaperUUID: null})),
reset: () => set((_state) => ({ wallpaperUUID: "" })),
}),
{name: 'SavedWallpaperUUID-offline',}
)
Expand All @@ -98,6 +100,7 @@ export const useViewableSideElStore = create<viewableSideElInterface>()(
(set) => ({
viewableEl: viewableSideElements,
setviewableEl: (setTo) => set((_state) => ({ viewableEl: setTo })),
reset: () => set((_state) => ({ viewableEl: viewableSideElements })),
}),
{name: 'viewableEl',}
)
Expand All @@ -110,6 +113,7 @@ export const useDirStore = create<SavedDirectoriesInterface>()(
(set) => ({
dir: emptyDirectories,
setDir: (setTo) => set((_state) => ({ dir: setTo })),
reset: () => set((_state) => ({ dir: emptyDirectories })),
}),
{
name: 'directories', // Key for localStorage
Expand Down Expand Up @@ -151,6 +155,7 @@ export const useSavedObjectStore = create<SavedObjectInterface>()(
(set) => ({
local_store: emptySavedObject,
setStore: (setTo) => set((_state) => ({ local_store: setTo })),
reset: () => set((_state) => ({ local_store: emptySavedObject })),
}),
{name: 'SavedObject-offline',}
)
Expand Down Expand Up @@ -206,5 +211,19 @@ export const useSavedPresetsValues = create<SavedPresetsValues>()(
(set) => ({
map: premade_audio_labs,
addValue: (key: string, value: AudioLabPreset) => set((state) => ({ map: state.map.set(key, value) })),
set: (setTo) => set((_state) => ({ map: setTo })),
}),
)

export const useVersionStore = create<VersionInterface>()(
devtools(
persist(
(set) => ({
version: "",
setVersion: (nV) => set((_state) => ({ version: nV })),
reset: () => set((_state) => ({ version: "" })),
}),
{name: 'version',}
)
)
)
11 changes: 11 additions & 0 deletions muzik-offline/src/store/storeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface MaximisedState {
export interface firstRunState{
firstRun: boolean;
setFirstRun: (nFR: boolean) => void;
reset: () => void;
}

export interface portState{
Expand Down Expand Up @@ -39,21 +40,25 @@ export interface wallpaperInterface{
wallpaperUUID: string | null;
setWallpaper: (nW: string) => void;
unsetWallpaper: () => void;
reset: () => void;
}

export interface viewableSideElInterface{
viewableEl: viewableSideEl;
setviewableEl: (setTo: viewableSideEl) => void;
reset: () => void;
}

export interface SavedDirectoriesInterface{
dir: SavedDirectories;
setDir: (setTo: SavedDirectories) => void;
reset: () => void;
}

export interface SavedObjectInterface{
local_store: SavedObject;
setStore: (setTo: SavedObject) => void;
reset: () => void;
}

export interface PlayerInterface{
Expand Down Expand Up @@ -84,4 +89,10 @@ export interface QueueInterface{
export interface SavedPresetsValues{
map: Map<string, AudioLabPreset>;
addValue: (key: string, value: AudioLabPreset) => void;
set: (setTo: Map<string, AudioLabPreset>) => void;
}

export interface VersionInterface{
version: string;
setVersion: (setTo: string) => void;
}
26 changes: 25 additions & 1 deletion muzik-offline/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NullCoverOne, NullCoverTwo, NullCoverThree, NullCoverFour } from "@assets/index";
import { local_albums_db, local_artists_db, local_genres_db, local_playlists_db, local_songs_db } from "@database/database";
import { Song, album, artist, genre, playlist } from "@muziktypes/index";
import { useDirStore, useHistorySongs, usePortStore, useSavedObjectStore, useToastStore, useUpcomingSongs } from "@store/index";
import { useDirStore, useFisrstRunStore, useHistorySongs, usePortStore, useSavedObjectStore, useToastStore, useUpcomingSongs, useVersionStore, useViewableSideElStore, useWallpaperStore } from "@store/index";
import { invoke } from "@tauri-apps/api/core";
import { toastType } from '../types/index';
import { getVersion } from "@tauri-apps/api/app";
import { resetObject } from "@database/saved_object";
import { resetViewableElements } from "@database/side_elements";

export const fetch_library = async(fresh_library: boolean): Promise<{status: string, message: string}> => {
const res_songs = await fetch_songs_metadata(fresh_library);
Expand Down Expand Up @@ -317,4 +320,25 @@ export async function reloadLibrary(paths: string[]){
console.log(_error);
useToastStore.getState().setToast({title: "Loading songs...", message: "No new songs detected in given folders or you dropped a file", type: toastType.error, timeout: 5000});
});
}

export const shouldClearZustandStores = async() => {
const savedAppVersion = useVersionStore.getState().version;
const currentVersion = await getVersion();

if(savedAppVersion === currentVersion)return;

useVersionStore.getState().setVersion(currentVersion);
useFisrstRunStore.getState().reset();
useWallpaperStore.getState().reset();
useViewableSideElStore.getState().setviewableEl(resetViewableElements(useViewableSideElStore.getState().viewableEl));
useDirStore.getState().setDir({Dir: new Set<string>()});
useSavedObjectStore.getState().setStore(resetObject(useSavedObjectStore.getState().local_store));

await local_songs_db.songs.clear();
await local_albums_db.albums.clear();
await local_artists_db.artists.clear();
await local_genres_db.genres.clear();

useToastStore.getState().setToast({title: "App updated", message: "We have updated the app to a new version, please reload the app to see the changes", type: toastType.info, timeout: 5000});
}

0 comments on commit c03b61a

Please sign in to comment.