Skip to content

Commit

Permalink
feat: save recents menu state (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
goweiwen committed Jul 20, 2023
1 parent 49b9d5c commit 7fd61f3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 54 deletions.
53 changes: 14 additions & 39 deletions allium-launcher/src/view/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use async_trait::async_trait;
use common::battery::Battery;
use common::command::Command;
use common::constants::{ALLIUM_APPS_DIR, ALLIUM_GAMES_DIR, ALLIUM_LAUNCHER_STATE};
use common::constants::ALLIUM_LAUNCHER_STATE;
use common::display::Display;
use common::geom::{Alignment, Point, Rect};
use common::locale::Locale;
Expand All @@ -17,17 +17,17 @@ use log::{trace, warn};
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::Sender;

use crate::entry::directory::Directory;
use crate::view::apps::{AppsSort, AppsState};
use crate::view::entry_list::EntryList;
use crate::view::games::{GamesSort, GamesState};
use crate::view::apps::AppsState;
use crate::view::games::GamesState;
use crate::view::recents::RecentsState;
use crate::view::settings::SettingsState;
use crate::view::Recents;
use crate::view::{Apps, Games, Settings};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct AppState {
selected: usize,
recents: RecentsState,
games: GamesState,
apps: AppsState,
settings: SettingsState,
Expand Down Expand Up @@ -114,21 +114,11 @@ where
let file = File::open(ALLIUM_LAUNCHER_STATE.as_path())?;
if let Ok(state) = serde_json::from_reader::<_, AppState>(file) {
let views = (
Recents::new(tab_rect, res.clone())?,
Games::load(tab_rect, res.clone(), state.games).unwrap_or_else(|_| {
Games::new(
tab_rect,
res.clone(),
EntryList::new(
tab_rect,
res.clone(),
GamesSort::Alphabetical(Directory::new(ALLIUM_GAMES_DIR.clone())),
)
.unwrap(),
)
.unwrap()
}),
Apps::load(tab_rect, res.clone(), state.apps)?,
Recents::load_or_new(tab_rect, res.clone(), Some(state.recents))?,
Games::load_or_new(tab_rect, res.clone(), Some(state.games)).unwrap_or_else(
|_| Games::load_or_new(tab_rect, res.clone(), None).unwrap(),
),
Apps::load_or_new(tab_rect, res.clone(), Some(state.apps))?,
Settings::new(tab_rect, res.clone(), state.settings)?,
);
return Self::new(rect, res, views, state.selected, battery);
Expand All @@ -138,25 +128,9 @@ where
}

let views = (
Recents::new(tab_rect, res.clone())?,
Games::new(
tab_rect,
res.clone(),
EntryList::new(
tab_rect,
res.clone(),
GamesSort::Alphabetical(Directory::new(ALLIUM_GAMES_DIR.clone())),
)?,
)?,
Apps::new(
tab_rect,
res.clone(),
EntryList::new(
tab_rect,
res.clone(),
AppsSort::Alphabetical(Directory::new(ALLIUM_APPS_DIR.clone())),
)?,
)?,
Recents::load_or_new(tab_rect, res.clone(), None)?,
Games::load_or_new(tab_rect, res.clone(), None)?,
Apps::load_or_new(tab_rect, res.clone(), None)?,
Settings::new(tab_rect, res.clone(), Default::default())?,
);
let selected = 1;
Expand All @@ -167,6 +141,7 @@ where
let file = File::create(ALLIUM_LAUNCHER_STATE.as_path())?;
let state = AppState {
selected: self.selected,
recents: self.views.0.save(),
games: self.views.1.save(),
apps: self.views.2.save(),
settings: self.views.3.save(),
Expand Down
19 changes: 14 additions & 5 deletions allium-launcher/src/view/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use async_trait::async_trait;

use common::command::Command;
use common::constants::ALLIUM_APPS_DIR;
use common::database::Database;
use common::geom::{Point, Rect};
use common::locale::Locale;
Expand Down Expand Up @@ -32,11 +33,19 @@ impl Apps {
Ok(Self { rect, list })
}

pub fn load(rect: Rect, res: Resources, state: AppsState) -> Result<Self> {
let selected = state.selected;

let mut list = EntryList::load(rect, res.clone(), state)?;
list.select(selected);
pub fn load_or_new(rect: Rect, res: Resources, state: Option<AppsState>) -> Result<Self> {
let list = if let Some(state) = state {
let selected = state.selected;
let mut list = EntryList::load(rect, res.clone(), state)?;
list.select(selected);
list
} else {
EntryList::new(
rect,
res.clone(),
AppsSort::Alphabetical(Directory::new(ALLIUM_APPS_DIR.clone())),
)?
};

Self::new(rect, res, list)
}
Expand Down
19 changes: 14 additions & 5 deletions allium-launcher/src/view/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use async_trait::async_trait;
use chrono::Duration;
use common::command::Command;
use common::constants::ALLIUM_GAMES_DIR;
use common::database::Database;
use common::geom::{Alignment, Point, Rect};
use common::locale::Locale;
Expand Down Expand Up @@ -60,11 +61,19 @@ impl Games {
})
}

pub fn load(rect: Rect, res: Resources, state: GamesState) -> Result<Self> {
let selected = state.selected;

let mut list = EntryList::load(rect, res.clone(), state)?;
list.select(selected);
pub fn load_or_new(rect: Rect, res: Resources, state: Option<GamesState>) -> Result<Self> {
let list = if let Some(state) = state {
let selected = state.selected;
let mut list = EntryList::load(rect, res.clone(), state)?;
list.select(selected);
list
} else {
EntryList::new(
rect,
res.clone(),
GamesSort::Alphabetical(Directory::new(ALLIUM_GAMES_DIR.clone())),
)?
};

Self::new(rect, res, list)
}
Expand Down
27 changes: 22 additions & 5 deletions allium-launcher/src/view/recents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::entry::directory::Directory;
use crate::entry::game::Game;
use crate::entry::lazy_image::LazyImage;
use crate::entry::{Entry, Sort};
use crate::view::entry_list::EntryList;
use crate::view::entry_list::{EntryList, EntryListState};

pub type RecentsState = EntryListState<RecentsSort>;

#[derive(Debug)]
pub struct Recents {
Expand All @@ -32,13 +34,11 @@ pub struct Recents {
}

impl Recents {
pub fn new(rect: Rect, res: Resources) -> Result<Self> {
let Rect { x, y, w, h } = rect;
pub fn new(rect: Rect, res: Resources, list: EntryList<RecentsSort>) -> Result<Self> {
let Rect { x, y, w: _w, h } = rect;

let styles = res.get::<Stylesheet>();

let list = EntryList::new(Rect::new(x, y, w, h), res.clone(), RecentsSort::LastPlayed)?;

let button_hints = Row::new(
Point::new(
x + 12,
Expand Down Expand Up @@ -68,6 +68,23 @@ impl Recents {
})
}

pub fn load_or_new(rect: Rect, res: Resources, state: Option<RecentsState>) -> Result<Self> {
let list = if let Some(state) = state {
let selected = state.selected;
let mut list = EntryList::load(rect, res.clone(), state)?;
list.select(selected);
list
} else {
EntryList::new(rect, res.clone(), RecentsSort::LastPlayed)?
};

Self::new(rect, res, list)
}

pub fn save(&self) -> RecentsState {
self.list.save()
}

pub fn start_search(&mut self) {
self.keyboard = Some(Keyboard::new(self.res.clone(), String::new(), false));
}
Expand Down

0 comments on commit 7fd61f3

Please sign in to comment.