Skip to content

Commit 20e57be

Browse files
committed
Pre 0.14 update commit
1 parent 72cecb4 commit 20e57be

File tree

7 files changed

+54
-54
lines changed

7 files changed

+54
-54
lines changed

wrapper/src/app.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* other in this repository, all of which is reserved by Essential.
1414
*/
1515

16-
use crate::download::download_java;
1716
use crate::file::extract_java_task;
1817
use crate::installer::{run_installer_task};
19-
use crate::java::get_java_executable_in_downloaded_jre;
18+
use crate::java::{fetch_java_url_task, get_java_executable_in_downloaded_jre};
2019
use crate::logging::get_logging_file_from_temp_directory;
2120
use crate::{fonts, WrapperInfo, BRAND};
2221
use arboard::Clipboard;
@@ -30,12 +29,12 @@ use iced::window::Mode;
3029
use iced::{
3130
color,
3231
widget::{progress_bar, text},
33-
window, Alignment, Background, Border, Color, Element, Length, Padding, Pixels, Shadow, Size,
34-
Subscription, Task, Vector,
32+
window, Alignment, Background, Border, Color, Element, Length, Padding, Pixels, Shadow, Size, Task, Vector,
3533
};
3634
use log::{debug, error, warn};
3735
use std::fs::read_to_string;
3836
use std::path::PathBuf;
37+
use crate::download::download_java_task;
3938

4039
pub fn start_app(app: WrapperApp, decorations: bool) {
4140
iced::application(
@@ -56,7 +55,6 @@ pub fn start_app(app: WrapperApp, decorations: bool) {
5655
.ok(),
5756
..Default::default()
5857
})
59-
.subscription(WrapperApp::subscription)
6058
.theme(move |_| AppTheme::default())
6159
.run_with(|| WrapperApp::new(app))
6260
.unwrap();
@@ -72,6 +70,8 @@ pub struct WrapperApp {
7270
pub enum AppState {
7371
#[default]
7472
Nothing,
73+
FetchingURL,
74+
StartDownloading(String),
7575
Downloading(String, f32),
7676
DownloadFinished(PathBuf),
7777
ExtractingJava(PathBuf),
@@ -94,7 +94,7 @@ static BODY_TEXT_SIZE: Pixels = Pixels(16.);
9494

9595
impl WrapperApp {
9696
fn new(flags: WrapperApp) -> (WrapperApp, Task<AppMessage>) {
97-
(flags, fonts::load_fonts())
97+
(flags, Task::batch(vec![fonts::load_fonts(), fetch_java_url_task()]))
9898
}
9999

100100
fn update(&mut self, message: AppMessage) -> Task<AppMessage> {
@@ -164,6 +164,10 @@ impl WrapperApp {
164164
let state = self.app_state.clone();
165165
window::get_oldest().and_then(move |id| match &state {
166166
AppState::Downloading(_, _) => window::change_mode(id, Mode::Windowed),
167+
AppState::StartDownloading(url) => Task::batch(vec![
168+
window::change_mode(id, Mode::Windowed),
169+
download_java_task(url.clone(), cache_dir.clone()),
170+
]),
167171
AppState::ExtractingJava(download_path) => Task::batch(vec![
168172
window::change_mode(id, Mode::Windowed),
169173
extract_java_task(cache_dir.clone(), download_path.clone()),
@@ -286,15 +290,6 @@ impl WrapperApp {
286290
.into()
287291
}
288292

289-
fn subscription(&self) -> Subscription<AppMessage> {
290-
debug!("Getting subscription {}", self.app_state);
291-
match &self.app_state {
292-
AppState::Downloading(url, _) => {
293-
download_java(url.clone(), self.wrapper_info.cache_dir.clone())
294-
}
295-
_ => Subscription::none(),
296-
}
297-
}
298293
}
299294

300295
// Visual stuff

wrapper/src/download.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,32 @@ use crate::java::get_java_zip_path_from_cache_dir;
1818
use crate::util::TaskError;
1919
use iced::futures::{SinkExt, Stream, StreamExt};
2020
use iced::stream::try_channel;
21-
use iced::Subscription;
21+
use iced::Task;
2222
use log::{debug, error, info, warn};
2323
use std::path::PathBuf;
2424
use std::sync::Arc;
2525
use std::time::Duration;
2626
use std::{fs, fs::File, io::Write};
2727

28-
pub fn download_java<T: ToString>(url: T, cache_dir: PathBuf) -> Subscription<AppMessage> {
29-
Subscription::run_with_id(
30-
"download-java",
31-
download_java_task(url.to_string(), cache_dir),
32-
)
33-
.map(|t| {
34-
if let Err(e) = t {
28+
pub fn download_java_task(url: String, cache_dir: PathBuf) -> Task<AppMessage> {
29+
Task::run(download_java_stream(url, cache_dir), move |r| {
30+
if let Err(e) = r {
3531
error!("Error when downloading java: {}", e);
3632
AppMessage::UpdateState(AppState::Errored(format!("Error downloading java: {}", e)))
3733
} else {
38-
AppMessage::UpdateState(t.unwrap())
34+
debug!("Mapping: {}", r.clone().unwrap());
35+
AppMessage::UpdateState(r.unwrap())
3936
}
4037
})
4138
}
4239

43-
pub fn download_java_task(
40+
pub fn download_java_stream(
4441
url: String,
4542
cache_dir: PathBuf,
4643
) -> impl Stream<Item = Result<AppState, TaskError>> {
47-
try_channel(100000, move |mut output| async move {
44+
try_channel(10000, move |mut output| async move {
45+
output.send(AppState::Downloading(url.clone(), 0.0)).await?;
46+
4847
info!("Starting download from {}", url);
4948
let download_path = get_java_zip_path_from_cache_dir(&cache_dir);
5049
info!("Temporary jre zip file: {}", download_path.display());
@@ -80,8 +79,6 @@ pub fn download_java_task(
8079
.content_length()
8180
.ok_or(TaskError::NoContentLength())?;
8281

83-
output.send(AppState::Downloading(url.clone(), 0.0)).await?;
84-
8582
let mut buffer = vec![0u8; total as usize];
8683
let mut byte_stream = response.bytes_stream();
8784
let mut downloaded = 0;
@@ -92,14 +89,10 @@ pub fn download_java_task(
9289
downloaded += bytes.len();
9390
buffer[previously_downloaded..downloaded].copy_from_slice(&bytes);
9491
let progress = 100.0 * downloaded as f32 / total as f32;
95-
if let Err(e) = output.try_send(AppState::Downloading(url.clone(), progress)) {
96-
debug!("Channel full!")
97-
}
92+
output.send(AppState::Downloading(url.clone(), progress)).await?;
9893
}
9994

100-
if let Err(e) = output.try_send(AppState::Downloading(url.clone(), 100f32)) {
101-
debug!("Channel full! {}", e)
102-
}
95+
output.send(AppState::Downloading(url.clone(), 100f32)).await?;
10396

10497
info!("Finished downloading!");
10598

@@ -112,9 +105,7 @@ pub fn download_java_task(
112105
file.write_all(&buffer[..downloaded])?;
113106

114107
info!("Written to file!");
115-
if let Err(e) = output.try_send(AppState::DownloadFinished(download_path.clone())) {
116-
debug!("Channel full! {}", e)
117-
}
108+
output.send(AppState::DownloadFinished(download_path.clone())).await?;
118109
Ok(())
119110
})
120111
}

wrapper/src/file.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{app, WrapperInfo, BRAND};
1919
use app::AppState;
2020
use iced::futures::channel::oneshot;
2121
use iced::Task;
22-
use log::{info, warn};
22+
use log::{debug, info, warn};
2323
use std::fs::File;
2424
use std::path::PathBuf;
2525
use std::time::{SystemTime, UNIX_EPOCH};
@@ -96,15 +96,18 @@ pub fn extract_zip(zip_path: PathBuf, target_path: PathBuf) -> ZipResult<()> {
9696
}
9797

9898
pub fn extract_java_task(cache_dir: PathBuf, download_path: PathBuf) -> Task<AppMessage> {
99+
debug!("Extracting java task requested.");
99100
Task::perform(
100101
async move {
102+
info!("Extracting java...");
101103
let (tx, rx) = oneshot::channel();
102104
std::thread::spawn(move || {
103105
let extract_path = get_java_extract_folder_from_cache_dir(&cache_dir);
104106
info!("Temporary jre folder: {}", extract_path.display());
105107
let res = extract_zip(download_path, extract_path)
106108
.map(|_| AppState::ExtractFinished)
107109
.unwrap_or_else(|e| AppState::Errored(format!("Error extracting java: {e}")));
110+
info!("Finished extracting java! {}", res);
108111
let _ = tx.send(res);
109112
});
110113
rx.await.unwrap_or_else(|_| AppState::Errored("Extraction thread dropped".into()))

wrapper/src/installer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ pub fn run_installer_task(
191191
let res = rx
192192
.await
193193
.unwrap_or_else(|_| AppState::Errored("Extraction thread dropped".into()));
194-
info!("Result 2: {}", res);
195194
res
196195
},
197196
AppMessage::UpdateState,

wrapper/src/java.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* other in this repository, all of which is reserved by Essential.
1414
*/
1515

16+
use crate::app::{AppMessage, AppState};
1617
use crate::process::run_process;
18+
use iced::Task;
1719
use log::info;
1820
use serde::Deserialize;
1921
use std::fs;
2022
use std::path::{Path, PathBuf};
2123

22-
pub fn get_java_download_url() -> Result<String, String> {
24+
pub async fn get_java_download_url() -> Result<String, String> {
2325
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
2426
let api = "https://api.azul.com/metadata/v1/zulu/packages/?java_version=17&os=macos&arch=x64&archive_type=zip&java_package_type=jre&javafx_bundled=false&crac_supported=false&crs_supported=false&support_term=lts&distro_version=17&release_status=ga&availability_types=CA&certifications=tck&page=1&page_size=100";
2527
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
@@ -35,9 +37,9 @@ pub fn get_java_download_url() -> Result<String, String> {
3537

3638
info!("Calling API to get java download! {}", api);
3739

38-
let packages: Vec<Package> = reqwest::blocking::get(api)
40+
let packages: Vec<Package> = reqwest::get(api).await
3941
.map_err(|e| e.to_string())?
40-
.json()
42+
.json().await
4143
.map_err(|e| e.to_string())?;
4244

4345
packages
@@ -46,6 +48,23 @@ pub fn get_java_download_url() -> Result<String, String> {
4648
.ok_or("No packages returned".to_string())
4749
}
4850

51+
pub fn fetch_java_url_task() -> Task<AppMessage> {
52+
Task::perform(
53+
async move {
54+
info!("Fetching java url");
55+
let url = get_java_download_url().await;
56+
info!("Fetched!");
57+
if let Err(e) = url {
58+
AppState::Errored(format!("Error when fetching Java download URL: {}", e))
59+
} else {
60+
info!("Successfully fetched Java url: {}", url.clone().unwrap());
61+
AppState::StartDownloading(url.unwrap())
62+
}
63+
},
64+
AppMessage::UpdateState,
65+
)
66+
}
67+
4968
pub fn get_java_zip_path_from_cache_dir(cache_dir: &Path) -> PathBuf {
5069
cache_dir.join("wrapper-jre.zip")
5170
}
@@ -105,7 +124,7 @@ pub fn find_java(cache_dir: &Path) -> Vec<String> {
105124
str_path, file_name
106125
))
107126
}
108-
}
127+
}
109128
});
110129
}
111130

wrapper/src/main.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use crate::app::{start_app, AppState, WrapperApp};
2020
use crate::file::{delete_temp_dir, get_cache_directory, get_invalid_path, get_temp_directory};
2121
use crate::installer::run_installer_with_permissive_error_handling;
22-
use crate::java::{find_java, get_java_download_url};
22+
use crate::java::find_java;
2323
use crate::logging::setup_logging;
2424
use log::info;
2525
use std::iter::Iterator;
@@ -85,18 +85,9 @@ pub fn main() {
8585

8686
info!("Downloading java");
8787

88-
let url = get_java_download_url()
89-
.inspect_err(|e| {
90-
show_error!(
91-
format!("Error when fetching Java download URL: {}", e),
92-
wrapper_info.clone()
93-
);
94-
})
95-
.unwrap();
96-
9788
start_app(
9889
WrapperApp {
99-
app_state: AppState::Downloading(url, 0.),
90+
app_state: AppState::FetchingURL,
10091
wrapper_info: wrapper_info.clone(),
10192
},
10293
false,

wrapper/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ impl Display for AppState {
2323
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2424
match self {
2525
AppState::Nothing => write!(f, "Nothing"),
26+
AppState::FetchingURL => write!(f, "FetchingURL"),
27+
AppState::StartDownloading(url) => write!(f, "StartDownloading({})", url),
2628
AppState::Downloading(url, percent) => write!(f, "Downloading({}, {})", url, percent),
2729
AppState::DownloadFinished(_) => write!(f, "DownloadFinished"),
2830
AppState::ExtractingJava(_) => write!(f, "ExtractingJava"),

0 commit comments

Comments
 (0)