ENH: adjust user preset handling

1. async fetch user presets from server (in thread)
2. always load default user presets
3. load user presets from cache immediately
4. not remove user presets cache
5. hide some loading ui

Change-Id: I1d29ed18e09606d0b2f69a69eea2eb5042c26223
This commit is contained in:
chunmao.guo 2023-01-06 11:36:55 +08:00 committed by Lane.Wei
parent dbb5ec9115
commit a4cf284c3d
7 changed files with 99 additions and 68 deletions

View file

@ -241,44 +241,17 @@ PresetsConfigSubstitutions PresetBundle::load_presets(AppConfig &config, Forward
//BBS: change system config to json //BBS: change system config to json
std::tie(substitutions, errors_cummulative) = this->load_system_presets_from_json(substitution_rule); std::tie(substitutions, errors_cummulative) = this->load_system_presets_from_json(substitution_rule);
//BBS load preset from user's folder, load system default if // Load default user presets always
//BBS: change directories by design load_user_presets(DEFAULT_USER_FOLDER_NAME, substitution_rule);
std::string dir_user_presets; // BBS load preset from user's folder, load system default if
if (!config.get("preset_folder").empty()) { // BBS: change directories by design
dir_user_presets = data_dir() + "/" + PRESET_USER_DIR + "/" + config.get("preset_folder"); std::string dir_user_presets = config.get("preset_folder");
if (!dir_user_presets.empty()) {
load_user_presets(dir_user_presets, substitution_rule);
} }
else {
dir_user_presets = data_dir() + "/" + PRESET_USER_DIR + "/" + DEFAULT_USER_FOLDER_NAME;
}
fs::path user_folder(data_dir() + "/" + PRESET_USER_DIR);
if (!fs::exists(user_folder))
fs::create_directory(user_folder);
fs::path folder(dir_user_presets);
if (!fs::exists(folder))
fs::create_directory(folder);
// BBS do not load sla_print
//BBS: change directoties by design
try {
this->prints.load_presets(dir_user_presets, PRESET_PRINT_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
try {
this->filaments.load_presets(dir_user_presets, PRESET_FILAMENT_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
try {
this->printers.load_presets(dir_user_presets, PRESET_PRINTER_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
this->update_multi_material_filament_presets(); this->update_multi_material_filament_presets();
this->update_compatible(PresetSelectCompatibleType::Never); this->update_compatible(PresetSelectCompatibleType::Never);
if (! errors_cummulative.empty())
throw Slic3r::RuntimeError(errors_cummulative);
this->load_selections(config, preferred_selection); this->load_selections(config, preferred_selection);
@ -534,7 +507,42 @@ std::string PresetBundle::get_hotend_model_for_printer_model(std::string model_n
return out; return out;
} }
PresetsConfigSubstitutions PresetBundle::load_user_presets(AppConfig &config, std::map<std::string, std::map<std::string, std::string>>& my_presets, ForwardCompatibilitySubstitutionRule substitution_rule) PresetsConfigSubstitutions PresetBundle::load_user_presets(std::string user, ForwardCompatibilitySubstitutionRule substitution_rule)
{
PresetsConfigSubstitutions substitutions;
std::string errors_cummulative;
fs::path user_folder(data_dir() + "/" + PRESET_USER_DIR);
if (!fs::exists(user_folder)) fs::create_directory(user_folder);
std::string dir_user_presets = data_dir() + "/" + PRESET_USER_DIR + "/" + user;
fs::path folder(user_folder / user);
if (!fs::exists(folder)) fs::create_directory(folder);
// BBS do not load sla_print
// BBS: change directoties by design
try {
this->prints.load_presets(dir_user_presets, PRESET_PRINT_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
try {
this->filaments.load_presets(dir_user_presets, PRESET_FILAMENT_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
try {
this->printers.load_presets(dir_user_presets, PRESET_PRINTER_NAME, substitutions, substitution_rule);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
if (!errors_cummulative.empty()) throw Slic3r::RuntimeError(errors_cummulative);
return PresetsConfigSubstitutions();
}
PresetsConfigSubstitutions PresetBundle::load_user_presets(AppConfig & config,
std::map<std::string, std::map<std::string, std::string>> &my_presets,
ForwardCompatibilitySubstitutionRule substitution_rule)
{ {
// First load the vendor specific system presets. // First load the vendor specific system presets.
PresetsConfigSubstitutions substitutions; PresetsConfigSubstitutions substitutions;

View file

@ -47,6 +47,7 @@ public:
void load_selections(AppConfig &config, const PresetPreferences& preferred_selection = PresetPreferences()); void load_selections(AppConfig &config, const PresetPreferences& preferred_selection = PresetPreferences());
// BBS Load user presets // BBS Load user presets
PresetsConfigSubstitutions load_user_presets(std::string user, ForwardCompatibilitySubstitutionRule rule);
PresetsConfigSubstitutions load_user_presets(AppConfig &config, std::map<std::string, std::map<std::string, std::string>>& my_presets, ForwardCompatibilitySubstitutionRule rule); PresetsConfigSubstitutions load_user_presets(AppConfig &config, std::map<std::string, std::map<std::string, std::string>>& my_presets, ForwardCompatibilitySubstitutionRule rule);
PresetsConfigSubstitutions import_presets(std::vector<std::string> &files, std::function<int(std::string const &)> override_confirm, ForwardCompatibilitySubstitutionRule rule); PresetsConfigSubstitutions import_presets(std::vector<std::string> &files, std::function<int(std::string const &)> override_confirm, ForwardCompatibilitySubstitutionRule rule);
void save_user_presets(AppConfig& config, std::vector<std::string>& need_to_delete_list); void save_user_presets(AppConfig& config, std::vector<std::string>& need_to_delete_list);

View file

@ -2354,8 +2354,9 @@ bool GUI_App::on_init_inner()
if (app_config->get("sync_user_preset") == "true") { if (app_config->get("sync_user_preset") == "true") {
//BBS loading user preset //BBS loading user preset
BOOST_LOG_TRIVIAL(info) << "Loading user presets..."; // Always async, not such startup step
scrn->SetText(_L("Loading user presets...")); //BOOST_LOG_TRIVIAL(info) << "Loading user presets...";
//scrn->SetText(_L("Loading user presets..."));
if (m_agent) { if (m_agent) {
start_sync_user_preset(); start_sync_user_preset();
} }
@ -3339,12 +3340,9 @@ void GUI_App::request_user_logout()
m_agent->user_logout(); m_agent->user_logout();
m_agent->set_user_selected_machine(""); m_agent->set_user_selected_machine("");
BOOST_LOG_TRIVIAL(info) << "preset_folder: set to empty, user_logout";
enable_user_preset_folder(false);
/* delete old user settings */ /* delete old user settings */
m_device_manager->clean_user_info(); m_device_manager->clean_user_info();
GUI::wxGetApp().sidebar().load_ams_list({}); GUI::wxGetApp().sidebar().load_ams_list({});
GUI::wxGetApp().remove_user_presets();
GUI::wxGetApp().stop_sync_user_preset(); GUI::wxGetApp().stop_sync_user_preset();
#ifdef __WINDOWS__ #ifdef __WINDOWS__
@ -3894,14 +3892,15 @@ void GUI_App::reload_settings()
} }
} }
//BBS reload when login //BBS reload when logout
void GUI_App::remove_user_presets() void GUI_App::remove_user_presets()
{ {
if (preset_bundle && m_agent) { if (preset_bundle && m_agent) {
preset_bundle->remove_users_preset(*app_config); preset_bundle->remove_users_preset(*app_config);
std::string user_id = m_agent->get_user_id(); // Not remove user preset cache
preset_bundle->remove_user_presets_directory(user_id); //std::string user_id = m_agent->get_user_id();
//preset_bundle->remove_user_presets_directory(user_id);
//update ui //update ui
mainframe->update_side_preset_ui(); mainframe->update_side_preset_ui();
@ -4015,42 +4014,61 @@ void GUI_App::sync_preset(Preset* preset)
} }
} }
void GUI_App::start_sync_user_preset(bool with_progress_dlg) void GUI_App::start_sync_user_preset(bool load_immediately, bool with_progress_dlg)
{ {
if (!m_agent) return; if (!m_agent || !m_agent->is_user_login()) return;
enable_user_preset_folder(m_agent->is_user_login()); enable_user_preset_folder(true);
// has already start sync // has already start sync
if (enable_sync) if (enable_sync)
return; return;
if (m_agent->is_user_login()) { if (load_immediately) {
// get setting list, update setting list preset_bundle->load_user_presets(m_agent->get_user_id(), ForwardCompatibilitySubstitutionRule::Enable);
std::string version = preset_bundle->get_vendor_profile_version(PresetBundle::BBL_BUNDLE).to_string(); mainframe->update_side_preset_ui();
if (with_progress_dlg) { }
ProgressDialog dlg(_L("Loading"), "", 100, this->mainframe, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
dlg.Update(0, _L("Loading user preset")); ProgressFn progressFn;
m_agent->get_setting_list(version, WasCancelledFn cancelFn;
[this, &dlg](int percent){ std::function<void()> finishFn;
dlg.Update(percent, _L("Loading user preset"));
}, if (with_progress_dlg) {
[this, &dlg]() { auto dlg = new ProgressDialog(_L("Loading"), "", 100, this->mainframe, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
dlg.GetValue(); dlg->Update(0, _L("Loading user preset"));
bool cont = dlg.Update(dlg.GetValue(), _L("Loading user preset")); progressFn = [this, dlg](int percent) {
return !cont; CallAfter([=]{
}); dlg->Update(percent, _L("Loading user preset"));
} else { });
m_agent->get_setting_list(version); };
} cancelFn = [dlg]() {
GUI::wxGetApp().reload_settings(); return dlg->WasCanceled();
};
finishFn = [this, dlg] {
CallAfter([=]{
dlg->Destroy();
reload_settings();
});
};
}
else {
finishFn = [this] {
CallAfter([=] {
reload_settings();
});
};
} }
BOOST_LOG_TRIVIAL(info) << "start_sync_service..."; BOOST_LOG_TRIVIAL(info) << "start_sync_service...";
//BBS //BBS
enable_sync = true; enable_sync = true;
m_sync_update_thread = Slic3r::create_thread( m_sync_update_thread = Slic3r::create_thread(
[this] { [this, progressFn, cancelFn, finishFn] {
// get setting list, update setting list
std::string version = preset_bundle->get_vendor_profile_version(PresetBundle::BBL_BUNDLE).to_string();
m_agent->get_setting_list(version, progressFn, cancelFn);
finishFn();
int count = 0, sync_count = 0; int count = 0, sync_count = 0;
std::vector<Preset> presets_to_sync; std::vector<Preset> presets_to_sync;
while (enable_sync) { while (enable_sync) {
@ -4111,6 +4129,7 @@ void GUI_App::start_sync_user_preset(bool with_progress_dlg)
void GUI_App::stop_sync_user_preset() void GUI_App::stop_sync_user_preset()
{ {
remove_user_presets();
enable_user_preset_folder(false); enable_user_preset_folder(false);
if (!enable_sync) if (!enable_sync)

View file

@ -421,7 +421,7 @@ public:
void reload_settings(); void reload_settings();
void remove_user_presets(); void remove_user_presets();
void sync_preset(Preset* preset); void sync_preset(Preset* preset);
void start_sync_user_preset(bool with_progress_dlg = false); void start_sync_user_preset(bool load_immediately = false, bool with_progress_dlg = false);
void stop_sync_user_preset(); void stop_sync_user_preset();
static bool catch_error(std::function<void()> cb, const std::string& err); static bool catch_error(std::function<void()> cb, const std::string& err);

View file

@ -3139,7 +3139,7 @@ void MainFrame::on_select_default_preset(SimpleEvent& evt)
{ {
case wxID_YES: { case wxID_YES: {
wxGetApp().app_config->set_bool("sync_user_preset", true); wxGetApp().app_config->set_bool("sync_user_preset", true);
wxGetApp().start_sync_user_preset(true); wxGetApp().start_sync_user_preset(true, true);
break; break;
} }
case wxID_NO: case wxID_NO:

View file

@ -608,6 +608,8 @@ bool ProgressDialog::Pulse(const wxString &newmsg, bool *skip)
return m_state != Canceled; return m_state != Canceled;
} }
bool ProgressDialog::WasCanceled() const { return m_state == Canceled; }
bool ProgressDialog::DoBeforeUpdate(bool *skip) bool ProgressDialog::DoBeforeUpdate(bool *skip)
{ {
// we have to yield because not only we want to update the display but // we have to yield because not only we want to update the display but

View file

@ -36,6 +36,7 @@ public:
virtual bool Update(int value, const wxString &newmsg = wxEmptyString, bool *skip = NULL); virtual bool Update(int value, const wxString &newmsg = wxEmptyString, bool *skip = NULL);
virtual bool Pulse(const wxString &newmsg = wxEmptyString, bool *skip = NULL); virtual bool Pulse(const wxString &newmsg = wxEmptyString, bool *skip = NULL);
bool WasCanceled() const;
virtual void Resume(); virtual void Resume();