Skip to content

Commit

Permalink
Merge pull request #20 from lusingander/details
Browse files Browse the repository at this point in the history
Fix to load version when opening tab for the first time
  • Loading branch information
lusingander authored Sep 5, 2024
2 parents f856064 + dcdf03d commit edfe1f3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 119 deletions.
87 changes: 68 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
error::{AppError, Result},
event::{
AppEventType, CompleteDownloadObjectResult, CompleteInitializeResult,
CompleteLoadObjectResult, CompleteLoadObjectsResult, CompletePreviewObjectResult, Sender,
CompleteLoadObjectDetailResult, CompleteLoadObjectVersionsResult,
CompleteLoadObjectsResult, CompletePreviewObjectResult, Sender,
},
file::{copy_to_clipboard, save_binary, save_error_log},
if_match,
Expand Down Expand Up @@ -180,20 +181,18 @@ impl App {
ObjectItem::File { name, .. } => {
let current_object_key = &self.current_object_key_with_name(name.to_string());
let detail = self.app_objects.get_object_detail(current_object_key);
let versions = self.app_objects.get_object_versions(current_object_key);

if let (Some(detail), Some(versions)) = (detail, versions) {
// object has been already loaded
if let Some(detail) = detail {
// object detail has been already loaded
let object_detail_page = Page::of_object_detail(
detail.clone(),
versions.clone(),
object_page.object_list(),
object_page.list_state(),
self.tx.clone(),
);
self.page_stack.push(object_detail_page);
} else {
self.tx.send(AppEventType::LoadObject);
self.tx.send(AppEventType::LoadObjectDetail);
self.app_view_state.is_loading = true;
}
}
Expand Down Expand Up @@ -253,7 +252,7 @@ impl App {
self.app_view_state.is_loading = false;
}

pub fn load_object(&self) {
pub fn load_object_detail(&self) {
let object_page = self.page_stack.current_page().as_object_list();

if let ObjectItem::File {
Expand All @@ -274,28 +273,21 @@ impl App {
let detail = client
.load_object_detail(&bucket, &key, &name, size_byte)
.await;
let versions = client.load_object_versions(&bucket, &key).await;
let result = CompleteLoadObjectResult::new(detail, versions, map_key);
tx.send(AppEventType::CompleteLoadObject(result));
let result = CompleteLoadObjectDetailResult::new(detail, map_key);
tx.send(AppEventType::CompleteLoadObjectDetail(result));
});
}
}

pub fn complete_load_object(&mut self, result: Result<CompleteLoadObjectResult>) {
pub fn complete_load_object_detail(&mut self, result: Result<CompleteLoadObjectDetailResult>) {
match result {
Ok(CompleteLoadObjectResult {
detail,
versions,
map_key,
}) => {
self.app_objects
.set_object_details(map_key, *detail.clone(), versions.clone());
Ok(CompleteLoadObjectDetailResult { detail, map_key }) => {
self.app_objects.set_object_detail(map_key, *detail.clone());

let object_page = self.page_stack.current_page().as_object_list();

let object_detail_page = Page::of_object_detail(
*detail.clone(),
versions.clone(),
object_page.object_list(),
object_page.list_state(),
self.tx.clone(),
Expand All @@ -309,6 +301,63 @@ impl App {
self.app_view_state.is_loading = false;
}

pub fn open_object_versions_tab(&mut self) {
let object_detail_page = self.page_stack.current_page().as_object_detail();
let name = &object_detail_page.current_object_detail().name;

let current_object_key = self.current_object_key_with_name(name.to_string());
let versions = self.app_objects.get_object_versions(&current_object_key);

if let Some(versions) = versions {
// object versions has been already loaded
let result =
CompleteLoadObjectVersionsResult::new(Ok(versions.clone()), current_object_key);
self.tx
.send(AppEventType::CompleteLoadObjectVersions(result));
} else {
self.tx.send(AppEventType::LoadObjectVersions);
self.app_view_state.is_loading = true;
}
}

pub fn load_object_versions(&self) {
let object_detail_page = self.page_stack.current_page().as_object_detail();
let name = &object_detail_page.current_object_detail().name;

let bucket = self.current_bucket();
let prefix = self.current_object_prefix();
let key = format!("{}{}", prefix, name);

let map_key = self.current_object_key_with_name(name.to_string());

let (client, tx) = self.unwrap_client_tx();
spawn(async move {
let versions = client.load_object_versions(&bucket, &key).await;
let result = CompleteLoadObjectVersionsResult::new(versions, map_key);
tx.send(AppEventType::CompleteLoadObjectVersions(result));
});
}

pub fn complete_load_object_versions(
&mut self,
result: Result<CompleteLoadObjectVersionsResult>,
) {
match result {
Ok(CompleteLoadObjectVersionsResult { versions, map_key }) => {
self.app_objects
.set_object_versions(map_key, versions.clone());

let object_detail_page = self.page_stack.current_page_mut().as_mut_object_detail();
object_detail_page.set_versions(versions);
object_detail_page.select_versions_tab();
}
Err(e) => {
self.tx.send(AppEventType::NotifyError(e));
}
}
self.app_view_state.is_loading = false;
}

pub fn open_help(&mut self) {
let helps = match self.page_stack.current_page() {
Page::Initializing(_) | Page::Help(_) => {
Expand Down
36 changes: 24 additions & 12 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ pub enum AppEventType {
CompleteInitialize(Result<CompleteInitializeResult>),
LoadObjects,
CompleteLoadObjects(Result<CompleteLoadObjectsResult>),
LoadObject,
CompleteLoadObject(Result<CompleteLoadObjectResult>),
LoadObjectDetail,
CompleteLoadObjectDetail(Result<CompleteLoadObjectDetailResult>),
LoadObjectVersions,
CompleteLoadObjectVersions(Result<CompleteLoadObjectVersionsResult>),
DownloadObject(FileDetail, Option<String>),
DownloadObjectAs(FileDetail, String, Option<String>),
CompleteDownloadObject(Result<CompleteDownloadObjectResult>),
Expand All @@ -31,6 +33,7 @@ pub enum AppEventType {
ObjectListMoveDown,
ObjectListMoveUp,
BackToBucketList,
OpenObjectVersionsTab,
OpenPreview(FileDetail, Option<String>),
DetailDownloadObject(FileDetail, Option<String>),
DetailDownloadObjectAs(FileDetail, String, Option<String>),
Expand Down Expand Up @@ -74,25 +77,34 @@ impl CompleteLoadObjectsResult {
}

#[derive(Debug)]
pub struct CompleteLoadObjectResult {
pub struct CompleteLoadObjectDetailResult {
pub detail: Box<FileDetail>, // to avoid "warning: large size difference between variants" for AppEventType
pub versions: Vec<FileVersion>,
pub map_key: ObjectKey,
}

impl CompleteLoadObjectResult {
impl CompleteLoadObjectDetailResult {
pub fn new(
detail: Result<FileDetail>,
versions: Result<Vec<FileVersion>>,
map_key: ObjectKey,
) -> Result<CompleteLoadObjectResult> {
) -> Result<CompleteLoadObjectDetailResult> {
let detail = Box::new(detail?);
Ok(CompleteLoadObjectDetailResult { detail, map_key })
}
}

#[derive(Debug)]
pub struct CompleteLoadObjectVersionsResult {
pub versions: Vec<FileVersion>,
pub map_key: ObjectKey,
}

impl CompleteLoadObjectVersionsResult {
pub fn new(
versions: Result<Vec<FileVersion>>,
map_key: ObjectKey,
) -> Result<CompleteLoadObjectVersionsResult> {
let versions = versions?;
Ok(CompleteLoadObjectResult {
detail,
versions,
map_key,
})
Ok(CompleteLoadObjectVersionsResult { versions, map_key })
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ impl AppObjects {
self.versions_map.get(key)
}

pub fn set_object_details(
&mut self,
key: ObjectKey,
detail: FileDetail,
versions: Vec<FileVersion>,
) {
self.detail_map.insert(key.to_owned(), detail);
self.versions_map.insert(key.to_owned(), versions);
pub fn set_object_detail(&mut self, key: ObjectKey, detail: FileDetail) {
self.detail_map.insert(key, detail);
}

pub fn set_object_versions(&mut self, key: ObjectKey, versions: Vec<FileVersion>) {
self.versions_map.insert(key, versions);
}
}

Expand Down
Loading

0 comments on commit edfe1f3

Please sign in to comment.