2026-08-31 11:46:35 +03:00
|
|
|
use std::sync::Arc;
|
2026-08-31 12:12:14 +03:00
|
|
|
use crate::data_provider::decks::{DeckRepository};
|
2026-08-25 12:04:07 +03:00
|
|
|
use crate::data_provider::settings::get_setting;
|
2026-08-31 11:46:35 +03:00
|
|
|
use crate::data_provider::sqlite::{SqliteConnection};
|
2026-08-31 12:12:14 +03:00
|
|
|
use crate::data_provider::words::{WordRepository};
|
2026-08-25 12:04:07 +03:00
|
|
|
use crate::navigation::RootMessage::Keyboard;
|
|
|
|
|
use crate::navigation::{AppSettings, RootMessage, ScreenState};
|
|
|
|
|
use iced::{Subscription, keyboard, window};
|
2026-08-23 21:49:01 +03:00
|
|
|
use iced_core::window::Position;
|
2026-08-24 01:37:00 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
2026-08-23 21:49:01 +03:00
|
|
|
use iced_core::window::settings::PlatformSpecific;
|
2026-08-25 12:04:07 +03:00
|
|
|
use iced_core::{Font, Size, Theme};
|
2026-08-23 22:48:00 +03:00
|
|
|
use log::info;
|
2026-08-31 11:46:35 +03:00
|
|
|
use parking_lot::RwLock;
|
2026-08-23 21:49:01 +03:00
|
|
|
use rusqlite::Connection;
|
2026-08-31 12:12:14 +03:00
|
|
|
use crate::data::repositories::Repository;
|
2026-08-31 14:35:11 +03:00
|
|
|
use crate::data_provider::card_states::CardStateRepository;
|
2026-09-04 11:18:23 +03:00
|
|
|
use crate::data_provider::history::HistoryRepository;
|
2026-08-31 12:12:14 +03:00
|
|
|
use crate::data_provider::word_groups::WordGroupRepository;
|
|
|
|
|
|
2026-08-23 21:49:01 +03:00
|
|
|
pub const USER_FONT: Font = Font::with_name("Noto Sans JP");
|
|
|
|
|
|
|
|
|
|
pub fn start() -> iced::Result {
|
2026-08-23 22:48:00 +03:00
|
|
|
info!("Welcome to JapLearn");
|
2026-08-23 21:49:01 +03:00
|
|
|
iced::application(ScreenState::boot, ScreenState::update, ScreenState::view)
|
|
|
|
|
.window(window_settings())
|
|
|
|
|
.subscription(subscription)
|
|
|
|
|
.title("JapLearn")
|
|
|
|
|
.settings(iced::Settings {
|
|
|
|
|
default_text_size: iced::Pixels(18.0),
|
|
|
|
|
..iced::Settings::default()
|
|
|
|
|
})
|
|
|
|
|
.font(include_bytes!("../noto.ttf"))
|
|
|
|
|
.default_font(USER_FONT)
|
|
|
|
|
.theme(Theme::GruvboxDark)
|
|
|
|
|
.run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
|
fn window_settings() -> window::Settings {
|
|
|
|
|
let mut settings = window::Settings {
|
|
|
|
|
position: Position::Centered,
|
|
|
|
|
min_size: Some(Size::new(700.0_f32, 700.0_f32)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
settings.platform_specific = PlatformSpecific {
|
|
|
|
|
application_id: "JapLearn".to_string(),
|
|
|
|
|
override_redirect: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(_state: &ScreenState) -> Subscription<RootMessage> {
|
|
|
|
|
keyboard::listen().map(Keyboard)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 11:46:35 +03:00
|
|
|
#[derive(Clone)]
|
2026-08-23 21:49:01 +03:00
|
|
|
pub struct AppState {
|
2026-08-31 12:12:14 +03:00
|
|
|
pub dictionary: WordRepository,
|
|
|
|
|
pub decks: DeckRepository,
|
|
|
|
|
pub word_groups: WordGroupRepository,
|
2026-08-31 14:35:11 +03:00
|
|
|
pub card_states: CardStateRepository,
|
2026-08-31 11:46:35 +03:00
|
|
|
pub connection: Arc<RwLock<SqliteConnection>>,
|
|
|
|
|
pub sync_data: Arc<RwLock<AppSettings>>,
|
2026-09-04 11:18:23 +03:00
|
|
|
pub history: HistoryRepository,
|
2026-08-23 21:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for AppState {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
|
|
|
|
pub fn new() -> Self {
|
2026-08-31 12:12:14 +03:00
|
|
|
let connection = Arc::new(RwLock::new(SqliteConnection::default()));
|
2026-08-23 21:49:01 +03:00
|
|
|
Self {
|
2026-08-31 12:12:14 +03:00
|
|
|
dictionary: WordRepository::new(connection.clone()),
|
|
|
|
|
decks: DeckRepository::new(connection.clone()),
|
|
|
|
|
word_groups: WordGroupRepository::new(connection.clone()),
|
2026-08-31 14:35:11 +03:00
|
|
|
card_states: CardStateRepository::new(connection.clone()),
|
2026-09-04 11:18:23 +03:00
|
|
|
history: HistoryRepository::new(connection.clone()),
|
2026-08-31 11:46:35 +03:00
|
|
|
sync_data: Arc::new(RwLock::new(AppSettings {
|
2026-08-23 21:49:01 +03:00
|
|
|
key: None,
|
|
|
|
|
auto_web_fetch: false,
|
2026-08-31 11:46:35 +03:00
|
|
|
})),
|
2026-08-31 12:12:14 +03:00
|
|
|
connection,
|
2026-08-23 21:49:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn fill_state(state: &mut AppState) {
|
2026-08-31 11:46:35 +03:00
|
|
|
let connection = state.connection.read();
|
2026-08-31 12:12:14 +03:00
|
|
|
|
2026-08-31 11:46:35 +03:00
|
|
|
let setting = load_settings(&connection);
|
2026-08-23 21:49:01 +03:00
|
|
|
|
2026-08-31 12:12:14 +03:00
|
|
|
state.word_groups.write().start().unwrap();
|
|
|
|
|
state.dictionary.write().start().unwrap();
|
|
|
|
|
state.decks.write().start().unwrap();
|
|
|
|
|
|
|
|
|
|
|
2026-08-31 11:46:35 +03:00
|
|
|
state.sync_data = Arc::new(RwLock::new(setting))
|
2026-08-23 21:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn load_settings(connection: &Connection) -> AppSettings {
|
|
|
|
|
let key = get_setting("SYNC_KEY".to_string(), connection);
|
|
|
|
|
let fetch = get_setting("AUTO_WEB_FETCH".to_string(), connection)
|
|
|
|
|
.unwrap_or("false".to_string())
|
|
|
|
|
.parse::<bool>()
|
|
|
|
|
.unwrap();
|
|
|
|
|
AppSettings {
|
|
|
|
|
key,
|
|
|
|
|
auto_web_fetch: fetch,
|
|
|
|
|
}
|
|
|
|
|
}
|