Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/version fix #56

Merged
merged 7 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

![Github testing](https://github.com/mawilms/lembas/actions/workflows/testing.yml/badge.svg)
![GitHub issues](https://img.shields.io/github/issues/mawilms/lembas)
[![GitHub license](https://img.shields.io/github/license/mawilms/lembas)](https://github.com/mawilms/lembas/blob/main/LICENSE)
![GitHub downloads](https://img.shields.io/github/downloads/mawilms/lembas/total)
![GitHub Latest version](https://img.shields.io/github/v/release/mawilms/lembas?include_prereleases)

</div>

Expand Down
6 changes: 3 additions & 3 deletions src/core/api_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashMap;
pub trait APIOperations {
async fn fetch_plugins() -> Result<HashMap<String, BasePlugin>, APIError>;

async fn fetch_details(title: String) -> Result<DetailsPlugin, APIError>;
async fn fetch_details(plugin_id: i32) -> Result<DetailsPlugin, APIError>;
}

pub struct APIConnector {}
Expand All @@ -24,10 +24,10 @@ impl APIOperations for APIConnector {
}
}

async fn fetch_details(title: String) -> Result<DetailsPlugin, APIError> {
async fn fetch_details(plugin_id: i32) -> Result<DetailsPlugin, APIError> {
match reqwest::get(format!(
"https://lembas-backend.herokuapp.com/plugins/{}",
title.to_lowercase()
plugin_id
))
.await
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Installer {
.expect("Couldn't find your home directory")
.join("Documents")
.join("The Lord of the Rings Online")
.join("plugins_backup");
.join("PluginsBackup");

if !backup_path.exists() {
create_dir(&backup_path).unwrap();
Expand Down
12 changes: 5 additions & 7 deletions src/core/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ impl Synchronizer {
db_plugins: &HashMap<String, InstalledPlugin>,
) {
for (key, element) in local_plugins {
if let Ok(retrieved_plugin) = APIConnector::fetch_details(element.name.clone()).await {
if !db_plugins.contains_key(key) {
Self::delete_plugin(&element.name).unwrap();
} else if let Ok(retrieved_plugin) =
APIConnector::fetch_details(db_plugins.get(key).unwrap().plugin_id).await
{
if db_plugins.contains_key(key) {
let local_plugin = db_plugins.get(key).unwrap();
if local_plugin.latest_version != retrieved_plugin.base_plugin.latest_version {
Expand All @@ -48,12 +52,6 @@ impl Synchronizer {
}
}
}

for (key, element) in db_plugins {
if !local_plugins.contains_key(key) {
Self::delete_plugin(&element.title).unwrap();
}
}
}

pub fn search_local() -> Result<HashMap<String, Information>, Box<dyn Error>> {
Expand Down
29 changes: 13 additions & 16 deletions src/gui/views/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ impl Catalog {
state.plugins = filerted_plugins;
Command::none()
}
Message::Catalog(index, msg) => {
state.plugins[index].update(msg);
Command::none()
}
Message::Catalog(index, msg) => state.plugins[index]
.update(msg)
.map(move |msg| Message::Catalog(index, msg)),
Message::LoadPlugins => {
Command::perform(APIConnector::fetch_plugins(), Message::LoadedPlugins)
}
Expand Down Expand Up @@ -175,7 +174,7 @@ impl Catalog {
let search_plugins = TextInput::new(
input,
"Search plugins...",
&input_value,
input_value,
Message::CatalogInputChanged,
)
.padding(5);
Expand Down Expand Up @@ -308,7 +307,7 @@ impl PluginRow {
pub fn update(&mut self, message: RowMessage) -> Command<RowMessage> {
match message {
RowMessage::InstallPressed(plugin) => Command::perform(
APIConnector::fetch_details(plugin.title),
APIConnector::fetch_details(plugin.id),
RowMessage::DetailsFetched,
),

Expand All @@ -321,24 +320,22 @@ impl PluginRow {
Command::none()
}
RowMessage::DetailsFetched(fetched_plugin) => {
if fetched_plugin.is_ok() {
let plugin = fetched_plugin.unwrap();
if Installer::download(&plugin).is_ok() {
self.status = "Downloaded".to_string();
if Installer::extract(&plugin).is_ok() {
self.status = "Unpacked".to_string();
Installer::delete_cache_folder(&plugin);
if Synchronizer::insert_plugin(&plugin).is_ok() {
if let Ok(fetched_plugin) = fetched_plugin {
if Installer::download(&fetched_plugin).is_ok() {
if Installer::extract(&fetched_plugin).is_ok() {
Installer::delete_cache_folder(&fetched_plugin);
if Synchronizer::insert_plugin(&fetched_plugin).is_ok() {
self.status = "Installed".to_string();
self.current_version = fetched_plugin.base_plugin.latest_version;
} else {
self.status = "Installation failed".to_string();
}
} else {
self.status = "Unpacking failed".to_string();
}
} else {
self.status = "Download failed".to_string();
}
} else {
self.status = "Download failed".to_string();
}
Command::none()
}
Expand Down
3 changes: 1 addition & 2 deletions src/gui/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ impl Application for Lembas {
Command::none()
}
Message::PluginAction(msg) => {
state.plugins_view.update(msg);
Command::none()
state.plugins_view.update(msg).map(Message::PluginAction)
}
Message::CatalogAction(msg) => {
state.catalog_view.update(msg).map(Message::CatalogAction)
Expand Down
61 changes: 30 additions & 31 deletions src/gui/views/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl Plugins {
match self {
Plugins::Loaded(state) => match message {
PluginMessage::Plugin(index, msg) => {
if let Event::Synchronize = state.plugins[index].update(msg) {
let update_event = state.plugins[index].update(msg);
if let Event::Synchronize = update_event.0 {
let mut plugins: Vec<PluginRow> = Vec::new();
let mut all_plugins: Vec<InstalledPlugin> =
Synchronizer::get_plugins().values().cloned().collect();
Expand All @@ -98,7 +99,9 @@ impl Plugins {
}
state.plugins = plugins;
}
Command::none()
update_event
.1
.map(move |msg| PluginMessage::Plugin(index, msg))
}

PluginMessage::RefreshPressed => {
Expand Down Expand Up @@ -127,6 +130,7 @@ impl Plugins {
&plugin.latest_version,
&plugin.folder,
));
plugins.sort_by(|a, b| a.title.to_lowercase().cmp(&b.title.to_lowercase()));
}
state.plugins = plugins;
Command::none()
Expand Down Expand Up @@ -347,66 +351,61 @@ impl PluginRow {
}
}

pub fn update(&mut self, message: RowMessage) -> Event {
pub fn update(&mut self, message: RowMessage) -> (Event, Command<RowMessage>) {
match message {
RowMessage::ToggleView => {
self.opened = !self.opened;
Event::Nothing
}
RowMessage::UpdatePressed(plugin) => {
Command::perform(
APIConnector::fetch_details(plugin.title),
RowMessage::Updating,
);
Event::Nothing
}
RowMessage::DeletePressed(row) => {
Command::perform(APIConnector::fetch_details(row.title), RowMessage::Deleting);
Event::Nothing
(Event::Nothing, Command::none())
}
RowMessage::UpdatePressed(plugin) => (
Event::Nothing,
Command::perform(APIConnector::fetch_details(plugin.id), RowMessage::Updating),
),
RowMessage::DeletePressed(row) => (
Event::Nothing,
Command::perform(APIConnector::fetch_details(row.id), RowMessage::Deleting),
),
RowMessage::WebsitePressed(id, title) => {
webbrowser::open(&format!(
"https://www.lotrointerface.com/downloads/info{}-{}.html",
id, title,
))
.unwrap();
Event::Nothing
(Event::Nothing, Command::none())
}
RowMessage::Updating(fetched_plugin) => {
if let Ok(fetched_plugin) = fetched_plugin {
if Installer::download(&fetched_plugin).is_ok() {
self.status = "Downloaded".to_string();
if Installer::delete(
&fetched_plugin.base_plugin.folder,
&fetched_plugin.files,
)
.is_ok()
{
if Installer::extract(&fetched_plugin).is_ok() {
self.status = "Unpacked".to_string();
Installer::delete_cache_folder(&fetched_plugin);
if Synchronizer::insert_plugin(&fetched_plugin).is_ok() {
self.status = "Installed".to_string();
Event::Synchronize
self.status = "Updated".to_string();
(Event::Synchronize, Command::none())
} else {
self.status = "Install failed".to_string();
Event::Nothing
self.status = "Update failed".to_string();
(Event::Nothing, Command::none())
}
} else {
self.status = "Unpacking failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
} else {
self.status = "Installation failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
} else {
self.status = "Download failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
} else {
self.status = "Download failed".to_string();
Event::Nothing
self.status = "Update failed".to_string();
(Event::Nothing, Command::none())
}
}
RowMessage::Deleting(fetched_plugin) => {
Expand All @@ -416,18 +415,18 @@ impl PluginRow {
{
if Synchronizer::delete_plugin(&fetched_plugin.base_plugin.title).is_ok() {
self.status = "Deleted".to_string();
Event::Synchronize
(Event::Synchronize, Command::none())
} else {
self.status = "Delete failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
} else {
self.status = "Delete failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
} else {
self.status = "Delete failed".to_string();
Event::Nothing
(Event::Nothing, Command::none())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::too_many_lines)]
//#![windows_subsystem = "windows"]
#![windows_subsystem = "windows"]

mod core;
mod gui;
Expand Down