Skip to content

Commit

Permalink
Different implementation per platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Misieq01 committed Sep 24, 2024
1 parent 911fb51 commit e391159
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,11 @@ async fn get_seed_words(
}

#[tauri::command]
async fn trigger_block_win_notification(
async fn trigger_notification(
summary: &str,
body: &str,
) -> Result<(), String> {
NotificationManager::current().trigger_notification("Test", "test");
NotificationManager::current().trigger_notification(summary, body);
Ok(())
}

Expand Down Expand Up @@ -1480,7 +1482,7 @@ fn main() {
restart_application,
resolve_application_language,
set_application_language,
trigger_block_win_notification,
trigger_notification,
get_miner_metrics,
get_app_config,
get_p2pool_stats,
Expand Down
60 changes: 51 additions & 9 deletions src-tauri/src/notification_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use log::info;
const LOG_TARGET: &str = "tari::universe::notification_manager";
static INSTANCE: LazyLock<NotificationManager> = LazyLock::new(NotificationManager::new);

pub enum CurrentOperatingSystem {
Windows,
Linux,
MacOS,
}

pub struct NotificationManager {}

impl NotificationManager {
Expand All @@ -15,16 +21,52 @@ impl NotificationManager {

pub fn trigger_notification(&self, summary: &str, body: &str) {
info!(target: LOG_TARGET, "Triggering notification with summary: {} and body: {}", summary, body);
Notification::new()
.summary("Firefox News")
.body("This will almost look like a real firefox notification.")
.icon("firefox")
.timeout(Timeout::Milliseconds(6000)) //milliseconds
.show().unwrap().on_close(
|| {
info!(target: LOG_TARGET,"Notification closed");
let notification = self.build_notification(summary, body);

match Self::detect_current_os() {
CurrentOperatingSystem::Linux => {
notification.show().unwrap().on_close(
|notification| {
info!(target: LOG_TARGET, "Notification closed: {:?}", notification);
},
);
}
CurrentOperatingSystem::MacOS => {
notification.show().unwrap();
}
CurrentOperatingSystem::Windows => {
notification.show().unwrap();
}
}
}

fn build_notification(&self, summary: &str, body: &str) -> Notification {
let mut notification = Notification::new()
.summary(summary)
.body(body).finalize();

match Self::detect_current_os() {
CurrentOperatingSystem::Linux => {
return notification.auto_icon().appname("Tari Universe").finalize();
}
CurrentOperatingSystem::MacOS => {
return notification.finalize();
}
CurrentOperatingSystem::Windows => {
return notification.finalize();
}
}
}
fn detect_current_os() -> CurrentOperatingSystem {
if cfg!(target_os = "windows") {
CurrentOperatingSystem::Windows
} else if cfg!(target_os = "linux") {
CurrentOperatingSystem::Linux
} else if cfg!(target_os = "macos") {
CurrentOperatingSystem::MacOS
} else {
panic!("Unsupported OS");
}
);
}

pub fn current() -> &'static NotificationManager {
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/mining/useBalanceChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { useMiningStore } from '@app/store/useMiningStore.ts';
import { useVisualisation } from '@app/hooks/mining/useVisualisation.ts';
import { useWalletStore } from '@app/store/useWalletStore';
import { useNotifcations } from '../useNotifications';
import formatBalance from '@app/utils/formatBalance';

function logBalanceChanges({ balance, prevBalance, balanceDiff }) {
console.groupCollapsed('Balance changes:');
Expand All @@ -13,6 +15,7 @@ function logBalanceChanges({ balance, prevBalance, balanceDiff }) {

const TIMER_VALUE = 15 * 1000; // 15s
export function useBalanceChanges() {
const { winNotification } = useNotifcations();
const { handleWin, handleFail } = useVisualisation();
const [balanceChangeBlock, setBalanceChangeBlock] = useState<number | null>(null);
const [blockHeightChanged, setBlockHeightChanged] = useState(false);
Expand Down Expand Up @@ -44,6 +47,7 @@ export function useBalanceChanges() {
const hasEarnings = Boolean(balance > 0 && diff > 0 && diff !== balance);
if (hasEarnings) {
handleWin();
winNotification(formatBalance(diff).toString());
setEarnings(diff);
}
balanceRef.current = balance;
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { invoke } from '@tauri-apps/api/tauri';
import { useCallback } from 'react';

export const useNotifcations = () => {
const winNotification = useCallback(async (winAmount: string) => {
await invoke('trigger_notification', {
summary: 'You won!',
body: `You won ${winAmount}!`,
});
}, []);

const testNotification = useCallback(async () => {
await invoke('trigger_notification', {
summary: 'Test Notification',
body: 'This is a test notification.',
});
}, []);

return { winNotification, testNotification };
};
2 changes: 1 addition & 1 deletion src/types/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module '@tauri-apps/api/tauri' {
): Promise<void>;
function invoke(param: 'set_application_language', payload: { applicationLanguage: Language }): Promise<void>;
function invoke(param: 'resolve_application_language'): Promise<Language>;
function invoke(param: 'trigger_block_win_notification'): Promise<void>;
function invoke(param: 'trigger_notification', payload: { summary: string; body: string }): Promise<void>;
function invoke(param: 'setup_application'): Promise<boolean>;
function invoke(param: 'open_log_dir'): Promise<void>;
function invoke(param: 'start_mining'): Promise<void>;
Expand Down

0 comments on commit e391159

Please sign in to comment.