Added self updater
This commit is contained in:
1 parent
2607314da3
commit
32c976beb8
12 files changed
+752
-6
No files matched your search
@@ -10,6 +10,9 @@ use crate::library::models::{
|
||||
/// the playback engine, imports). Tasks never touch AppState directly.
|
||||
#[derive(Debug)]
|
||||
pub enum AppEvent {
|
||||
UpdateChecked(Result<Option<crate::updater::Update>, String>),
|
||||
UpdateProgress(String),
|
||||
UpdateInstalled(Result<(), String>),
|
||||
StatusMessage(String),
|
||||
/// A page of the artists list arrived (or failed).
|
||||
ArtistsLoaded(Result<ArtistsPage, String>),
|
||||
|
||||
@@ -431,6 +431,13 @@ pub async fn run(
|
||||
}
|
||||
|
||||
if state.should_quit {
|
||||
// A replacement must finish before the runtime/process is torn down.
|
||||
if state.updater.busy {
|
||||
state.should_quit = false;
|
||||
state.status_message =
|
||||
Some("Please wait for the update operation to finish".into());
|
||||
continue;
|
||||
}
|
||||
if runtime.plain_text_mode {
|
||||
leave_plain_text_mode()?;
|
||||
runtime.plain_text_mode = false;
|
||||
@@ -1415,6 +1422,31 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
|
||||
return;
|
||||
}
|
||||
match effect {
|
||||
Effect::CheckUpdate => {
|
||||
let tx = runtime.event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let result = tokio::task::spawn_blocking(crate::updater::check)
|
||||
.await
|
||||
.map_err(|err| format!("update worker failed: {err}"))
|
||||
.and_then(|result| result.map_err(|err| format!("{err:#}")));
|
||||
let _ = tx.send(AppEvent::UpdateChecked(result));
|
||||
});
|
||||
}
|
||||
Effect::InstallUpdate(update) => {
|
||||
let tx = runtime.event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let progress_tx = tx.clone();
|
||||
let result = tokio::task::spawn_blocking(move || {
|
||||
crate::updater::install(&update, |message| {
|
||||
let _ = progress_tx.send(AppEvent::UpdateProgress(message));
|
||||
})
|
||||
})
|
||||
.await
|
||||
.map_err(|err| format!("update worker failed: {err}"))
|
||||
.and_then(|result| result.map_err(|err| format!("{err:#}")));
|
||||
let _ = tx.send(AppEvent::UpdateInstalled(result));
|
||||
});
|
||||
}
|
||||
Effect::PlayCurrent => {
|
||||
play_current(state, runtime);
|
||||
push_media_metadata(state, runtime);
|
||||
@@ -3098,6 +3130,37 @@ fn handle_playback_command(
|
||||
|
||||
fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent) {
|
||||
match event {
|
||||
AppEvent::UpdateChecked(result) => {
|
||||
state.updater.busy = false;
|
||||
state.updater.message = match result {
|
||||
Ok(Some(update)) => {
|
||||
let message = format!("v{} available", update.version);
|
||||
state.updater.available = Some(update);
|
||||
message
|
||||
}
|
||||
Ok(None) => "No newer stable release".into(),
|
||||
Err(error) => format!("Check failed: {error}"),
|
||||
};
|
||||
state.status_message = Some(state.updater.message.clone());
|
||||
}
|
||||
AppEvent::UpdateProgress(message) => state.updater.message = message,
|
||||
AppEvent::UpdateInstalled(result) => {
|
||||
state.updater.busy = false;
|
||||
state.updater.message = match result {
|
||||
Ok(()) => {
|
||||
state.updater.installed = true;
|
||||
let version = state
|
||||
.updater
|
||||
.available
|
||||
.as_ref()
|
||||
.map(|u| u.version.as_str())
|
||||
.unwrap_or("new version");
|
||||
format!("Installed {version}; restart furumi")
|
||||
}
|
||||
Err(error) => format!("Update failed: {error}"),
|
||||
};
|
||||
state.status_message = Some(state.updater.message.clone());
|
||||
}
|
||||
AppEvent::StatusMessage(message) => state.status_message = Some(message),
|
||||
AppEvent::ListenHistoryLoaded(result) => {
|
||||
state.listen_history = Some(match result {
|
||||
|
||||
+8
-1
@@ -926,6 +926,8 @@ impl FedRow {
|
||||
/// the config directory.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SettingsRow {
|
||||
CheckUpdate,
|
||||
InstallUpdate,
|
||||
MusicDirectory,
|
||||
Similarity(SimilarityRow),
|
||||
Federation(FedRow),
|
||||
@@ -1065,7 +1067,11 @@ pub fn device_status_order(state: &AppState) -> Vec<usize> {
|
||||
}
|
||||
|
||||
pub fn settings_rows(state: &AppState) -> Vec<SettingsRow> {
|
||||
let mut rows = vec![SettingsRow::MusicDirectory];
|
||||
let mut rows = vec![
|
||||
SettingsRow::MusicDirectory,
|
||||
SettingsRow::CheckUpdate,
|
||||
SettingsRow::InstallUpdate,
|
||||
];
|
||||
rows.extend(SimilarityRow::ALL.into_iter().map(SettingsRow::Similarity));
|
||||
rows.extend(FedRow::ALL.into_iter().map(SettingsRow::Federation));
|
||||
rows.push(SettingsRow::DeviceName);
|
||||
@@ -1571,6 +1577,7 @@ impl DevicePlaybackState {
|
||||
/// event handlers in the main loop; views render from `&AppState`.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct AppState {
|
||||
pub updater: crate::updater::State,
|
||||
pub active_tab: Tab,
|
||||
pub should_quit: bool,
|
||||
pub shutting_down: bool,
|
||||
|
||||
@@ -18,6 +18,8 @@ pub const QUIT_CONFIRM_HINT: &str = "press quit again to exit";
|
||||
/// owns the Runtime (audio controller, API client). Keeps update() pure.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Effect {
|
||||
CheckUpdate,
|
||||
InstallUpdate(crate::updater::Update),
|
||||
/// (Re)start playback of `queue[queue_pos]`.
|
||||
PlayCurrent,
|
||||
TogglePause,
|
||||
@@ -2815,6 +2817,24 @@ fn fed_card_featured_artist_names(track: &crate::federation::FedCardTrack) -> Ve
|
||||
fn federation_select(state: &mut AppState) -> Option<Effect> {
|
||||
use super::state::{FedInputField, FedRow, Popup, SettingsRow, SimilarityRow};
|
||||
match settings_rows(state).get(state.settings_cursor).copied()? {
|
||||
SettingsRow::CheckUpdate => {
|
||||
if state.updater.busy || state.updater.installed {
|
||||
return None;
|
||||
}
|
||||
state.updater.busy = true;
|
||||
state.updater.available = None;
|
||||
state.updater.message = "Checking GitHub Releases...".into();
|
||||
Some(Effect::CheckUpdate)
|
||||
}
|
||||
SettingsRow::InstallUpdate => {
|
||||
if state.updater.busy || state.updater.installed {
|
||||
return None;
|
||||
}
|
||||
let update = state.updater.available.clone()?;
|
||||
state.updater.busy = true;
|
||||
state.updater.message = "Downloading update...".into();
|
||||
Some(Effect::InstallUpdate(update))
|
||||
}
|
||||
SettingsRow::MusicDirectory => {
|
||||
if state.music_dir_changing {
|
||||
state.status_message = Some("music directory change is already running".into());
|
||||
|
||||
@@ -1,6 +1,27 @@
|
||||
use super::*;
|
||||
use crate::library::models::{ArtistCard, ArtistDetail, TrackItem};
|
||||
|
||||
#[test]
|
||||
fn manual_update_check_is_single_flight_and_disabled_after_install() {
|
||||
let mut state = AppState::default();
|
||||
state.settings_cursor = settings_rows(&state)
|
||||
.iter()
|
||||
.position(|row| *row == crate::app::state::SettingsRow::CheckUpdate)
|
||||
.unwrap();
|
||||
assert_eq!(federation_select(&mut state), Some(Effect::CheckUpdate));
|
||||
assert!(state.updater.busy);
|
||||
assert_eq!(federation_select(&mut state), None);
|
||||
state.updater.busy = false;
|
||||
state.updater.installed = true;
|
||||
assert_eq!(federation_select(&mut state), None);
|
||||
state.updater.installed = false;
|
||||
state.settings_cursor = settings_rows(&state)
|
||||
.iter()
|
||||
.position(|row| *row == crate::app::state::SettingsRow::InstallUpdate)
|
||||
.unwrap();
|
||||
assert_eq!(federation_select(&mut state), None);
|
||||
}
|
||||
|
||||
fn with_artists(n: usize) -> AppState {
|
||||
let mut state = AppState::default();
|
||||
state.global.artists = (0..n)
|
||||
|
||||
Reference in new issue
Block a user