diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1a1a5e0..5fd304f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,15 +4,11 @@ )] use crate::data::{AppPaths, ArcData, Data}; -use crate::settings::yt_email_notifier; use crate::settings::VersionedSettings; use data::UndoHistory; -use rfd::MessageDialogResult; #[cfg(target_os = "macos")] use tauri::AboutMetadata; -use tauri::{ - command, Manager, WebviewUrl, WebviewWindowBuilder -}; +use tauri::{command, Manager, WebviewUrl, WebviewWindowBuilder}; use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; mod api; @@ -52,44 +48,16 @@ fn error_popup(app_handle: tauri::AppHandle, msg: String) { .show(|_| {}); } -/// Note title and message to show asynchronously when/after the app starts -type ImportedNote = Option<(String, String)>; - /// This can display dialogs, which needs to happen before tauri runs to not panic -fn load_data(paths: &AppPaths) -> Result<(VersionedSettings, ImportedNote), String> { +fn load_data(paths: &AppPaths) -> Result { if paths.settings_file.exists() { return match settings::VersionedSettings::load(paths) { - Ok(settings) => Ok((settings, None)), + Ok(settings) => Ok(settings), Err(e) => Err(e), }; } - let will_import = match yt_email_notifier::can_import() { - true => { - let result = rfd::MessageDialog::new() - .set_title("Import") - .set_description("Do you want to import your data from YouTube Email Notifier?") - .set_buttons(rfd::MessageButtons::YesNo) - .set_level(rfd::MessageLevel::Info) - .show(); - - match result { - MessageDialogResult::Yes => true, - _ => false, - } - }, - false => false, - }; - if will_import { - let imported_stuff = yt_email_notifier::import()?; - let versioned_settings = imported_stuff.settings.wrap(); - versioned_settings.save(paths)?; - - let import_note = Some(("Import note".to_string(), imported_stuff.update_note)); - return Ok((versioned_settings, import_note)); - } - - Ok((VersionedSettings::default(), None)) + Ok(VersionedSettings::default()) } #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -123,8 +91,7 @@ async fn run() { let app = tauri::Builder::default() .plugin(tauri_plugin_opener::init()) - .plugin(tauri_plugin_notification::init()) - + .plugin(tauri_plugin_notification::init()) .invoke_handler(specta_builder.invoke_handler()) .setup(move |app| { let win = WebviewWindowBuilder::new(app, "main", WebviewUrl::default()) diff --git a/src-tauri/src/settings.rs b/src-tauri/src/settings.rs index 6ead635..5a7ee27 100644 --- a/src-tauri/src/settings.rs +++ b/src-tauri/src/settings.rs @@ -107,116 +107,3 @@ impl Settings { self.api_key = key; } } - -pub mod v1 { - pub use super::{Channel, Settings}; -} - -#[allow(non_snake_case)] -pub mod yt_email_notifier { - use crate::settings::v1; - use crate::throw; - use chrono::Utc; - use serde::{Deserialize, Serialize}; - use std::fs::File; - use std::io::Read; - use std::path::PathBuf; - - #[derive(Serialize, Deserialize, Debug)] - pub struct Channel { - pub id: String, - pub name: String, - pub icon: String, - pub uploadsPlaylistId: String, - pub fromTime: i64, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct Instance { - pub email: String, - pub minutesBetweenRefreshes: String, - pub channels: Vec, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct Settings { - pub apiKey: String, - pub fromEmail: String, - pub unreadErrors: bool, - pub maxConcurrentRequests: u32, - pub instances: Vec, - } - fn load_settings(file_path: PathBuf) -> Result { - let mut file = match File::open(file_path) { - Ok(file) => file, - Err(e) => throw!("Error opening file: {}", e.to_string()), - }; - let mut json_str = String::new(); - match file.read_to_string(&mut json_str) { - Ok(_) => {} - Err(err) => throw!("Error reading file: {}", err), - }; - let settings: Settings = match serde_json::from_str(&json_str) { - Ok(v) => v, - Err(err) => { - throw!("Error parsing file: {}", err.to_string()); - } - }; - Ok(settings) - } - fn convert(settings: Settings) -> v1::Settings { - v1::Settings { - api_key: settings.apiKey, - max_concurrent_requests: settings.maxConcurrentRequests, - channels: { - let mut channels = Vec::new(); - for v1_instance in settings.instances.iter() { - for v1_channel in v1_instance.channels.iter() { - let refresh_rate_mins = - v1_instance.minutesBetweenRefreshes.parse().unwrap_or(60.0); - channels.push(v1::Channel { - id: v1_channel.id.clone(), - name: v1_channel.name.clone(), - icon: v1_channel.icon.clone(), - uploads_playlist_id: v1_channel.uploadsPlaylistId.clone(), - from_time: Utc::now().timestamp_millis(), - refresh_rate_ms: refresh_rate_mins as u64 * 60 * 1000, - tags: vec![v1_instance.email.clone()], - }); - } - } - channels - }, - check_in_background: true, - } - } - fn app_dir() -> PathBuf { - let data_dir = tauri::api::path::data_dir().expect("No data dir"); - data_dir.join("YouTube Email Notifier") - } - pub fn can_import() -> bool { - if cfg!(target_os = "macos") { - app_dir().exists() - } else { - false - } - } - pub struct ImportedStuff { - pub settings: v1::Settings, - pub update_note: String, - } - pub fn import() -> Result { - let app_dir = app_dir(); - let settings = match load_settings(app_dir.join("settings.json")) { - Ok(settings) => settings, - Err(err) => throw!("Error migrating v1 settings: {}", err), - }; - let imported_user_data = convert(settings); - Ok(ImportedStuff{ - settings: imported_user_data, - update_note: "Your old settings and data has been imported.\n\ - \n\ - To enable \"Launch on Startup\", open System Preferences, go to Users & Groups > Login Items and add YouTube Email Notifier.".to_string(), - }) - } -}