Skip to content

Commit

Permalink
refactor: list items #2
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasGrandl committed Mar 6, 2024
1 parent b99b6f9 commit 3365480
Show file tree
Hide file tree
Showing 15 changed files with 636 additions and 644 deletions.
109 changes: 49 additions & 60 deletions src/commands/bitwarden/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
commands::{RootCommand, RootCommandBuilder},
components::{
form::{Form, Input, InputKind},
list::{Accessory, Item, ListBuilder, ListItem},
list::{Accessory, Item, ItemBuilder, ListBuilder, ListItem},
shared::{Icon, Img},
},
state::{Action, Shortcut, StateModel, StateViewBuilder, StateViewContext},
Expand Down Expand Up @@ -227,71 +227,60 @@ impl StateViewBuilder for BitwardenAccountListBuilder {
.into_iter()
.map(|account| {
let account = account.contents;
Item::new(
account.id.clone(),
vec![account.id.clone()],
cx.new_view({
let id = account.id.clone();
let instance = account.instance.clone();
move |_| {
ListItem::new(
Some(Img::list_icon(Icon::User, None)),
id,
None,
vec![Accessory::new(instance, None)],
)
}
})
.into(),
None,
vec![
Action::new(
Img::list_icon(Icon::Pen, None),
"Edit",
None,
{
// TODO:
move |actions, cx| {
actions.toast.error("Not implemented", cx);
ItemBuilder::new(account.id.clone(), {
let id = account.id.clone();
let instance = account.instance.clone();
ListItem::new(
Some(Img::list_icon(Icon::User, None)),
id,
None,
vec![Accessory::new(instance, None)],
)
})
.keywords(vec![account.id.clone()])
.actions(vec![
Action::new(
Img::list_icon(Icon::Pen, None),
"Edit",
None,
{
// TODO:
move |actions, cx| {
actions.toast.error("Not implemented", cx);
}
},
false,
),
Action::new(
Img::list_icon(Icon::Delete, None),
"Delete",
None,
{
//
let path = account.path();
let id = account.id.clone();
move |actions, cx| {
if let Err(err) = fs::remove_dir_all(path.clone()) {
error!("Failed to delete account: {}", err);
actions.toast.error("Failed to delete account", cx);
}
},
false,
),
Action::new(
Img::list_icon(Icon::Delete, None),
"Delete",
None,
{
//
let path = account.path();
let id = account.id.clone();
move |actions, cx| {
if let Err(err) = fs::remove_dir_all(path.clone()) {
if let Some(account) =
BitwardenAccount::get(&id, db()).unwrap()
{
if let Err(err) = account.delete(db()) {
error!("Failed to delete account: {}", err);
actions
.toast
.error("Failed to delete account", cx);
}
if let Some(account) =
BitwardenAccount::get(&id, db()).unwrap()
{
if let Err(err) = account.delete(db()) {
error!("Failed to delete account: {}", err);
actions
.toast
.error("Failed to delete account", cx);
}
};
StateModel::update(|this, cx| this.reset(cx), cx);
}
},
false,
),
],
None,
None,
None,
)
};
StateModel::update(|this, cx| this.reset(cx), cx);
}
},
false,
),
])
.build()
})
.collect();
Ok(Some(items))
Expand Down
25 changes: 10 additions & 15 deletions src/commands/bitwarden/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use url::Url;
use crate::{
commands::{RootCommand, RootCommandBuilder},
components::{
list::{Accessory, AsyncListItems, Item, ListBuilder, ListItem},
list::{Accessory, AsyncListItems, Item, ItemBuilder, ListBuilder, ListItem},
shared::{Icon, Img},
},
db::Db,
Expand Down Expand Up @@ -464,25 +464,20 @@ impl RootCommandBuilder for BitwardenCommandBuilder {
// StateItem::init(BitwardenAccountListBuilder, false, cx)
// }).ok();
actions.append(&mut login.get_actions(&id, &account));
items.push(Item::new(
id.clone(),
keywords,
cx.new_view(|_| {
items.push(
ItemBuilder::new(
id.clone(),
ListItem::new(
Some(img),
name.clone(),
None,
vec![Accessory::new(login.username.clone(), None)],
)
})
.unwrap()
.into(),
None,
actions,
None,
None,
None,
));
),
)
.keywords(keywords)
.actions(actions)
.build(),
);
}
}
} else {
Expand Down
169 changes: 75 additions & 94 deletions src/commands/clipboard/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tz::TimeZone;
use crate::{
commands::{RootCommand, RootCommandBuilder},
components::{
list::{AsyncListItems, Item, ListBuilder, ListItem},
list::{AsyncListItems, Item, ItemBuilder, ListBuilder, ListItem},
shared::{Icon, Img, ImgMask, ImgSize, ImgSource},
},
db::Db,
Expand Down Expand Up @@ -91,15 +91,7 @@ impl StateViewBuilder for ClipboardListBuilder {
items.get(&t).cloned().unwrap_or_default()
};

items.sort_by_key(|item| {
Reverse(
item.meta
.value()
.downcast_ref::<ClipboardListItem>()
.unwrap()
.copied_last,
)
});
items.sort_by_key(|item| Reverse(item.get_meta::<OffsetDateTime>()));
Ok(Some(items))
},
None,
Expand Down Expand Up @@ -199,103 +191,92 @@ impl ClipboardListItem {
item
}
fn get_item(&self, cx: &mut ViewContext<AsyncListItems>) -> Item {
Item::new(
ItemBuilder::new(
self.id,
vec![self.title.clone()],
cx.new_view(|_| {
ListItem::new(
match self.kind.clone() {
ClipboardListItemKind::Image { thumbnail } => {
Some(Img::list_file(thumbnail))
ListItem::new(
match self.kind.clone() {
ClipboardListItemKind::Image { thumbnail } => Some(Img::list_file(thumbnail)),
_ => Some(Img::list_icon(Icon::File, None)),
},
self.title.clone(),
None,
vec![],
),
)
.keywords(vec![self.title.clone()])
.preview(0.66, {
let id = self.id;
move |cx| StateItem::init(ClipboardPreview::init(id, cx), false, cx)
})
.actions({
let mut actions = vec![
Action::new(
Img::list_icon(Icon::ClipboardPaste, None),
"Paste",
None,
{
let id = self.id;
move |_, cx| {
let detail = ClipboardDetail::get(&id, db_detail()).unwrap().unwrap();
let _ = cx.update_window(cx.window_handle(), |_, cx| {
match detail.contents.kind.clone() {
ClipboardKind::Text { text, .. } => {
close_and_paste(text.as_str(), false, cx);
}
ClipboardKind::Image { path, .. } => {
close_and_paste_file(&path, cx);
}
}
});
}
_ => Some(Img::list_icon(Icon::File, None)),
},
self.title.clone(),
false,
),
Action::new(
Img::list_icon(Icon::Trash, None),
"Delete",
None,
vec![],
)
})
.into(),
Some((
0.66,
Box::new({
let id = self.id;
move |cx| StateItem::init(ClipboardPreview::init(id, cx), false, cx)
}),
)),
{
let mut actions = vec![
Action::new(
Img::list_icon(Icon::ClipboardPaste, None),
"Paste",
None,
{
let id = self.id;
move |_, cx| {
let detail =
ClipboardDetail::get(&id, db_detail()).unwrap().unwrap();
let _ =
cx.update_window(cx.window_handle(), |_, cx| {
match detail.contents.kind.clone() {
ClipboardKind::Text { text, .. } => {
close_and_paste(text.as_str(), false, cx);
}
ClipboardKind::Image { path, .. } => {
close_and_paste_file(&path, cx);
}
}
});
{
let self_clone = self.clone();
let view = cx.view().clone();
move |actions, cx| {
if let Err(err) = self_clone.delete(view.downgrade(), cx) {
error!("Failed to delete clipboard entry: {:?}", err);
actions.toast.error("Failed to delete clipboard entry", cx);
} else {
actions
.toast
.success("Successfully deleted clipboard entry", cx);
}
},
false,
),
}
},
false,
),
];
if let ClipboardListItemKind::Image { thumbnail } = self.kind.clone() {
actions.insert(
1,
Action::new(
Img::list_icon(Icon::Trash, None),
"Delete",
Img::list_icon(Icon::ScanEye, None),
"Copy Text to Clipboard",
None,
{
let self_clone = self.clone();
let view = cx.view().clone();
let mut path = thumbnail.clone();
path.pop();
path = path.join(format!("{}.png", self.id));
move |actions, cx| {
if let Err(err) = self_clone.delete(view.downgrade(), cx) {
error!("Failed to delete clipboard entry: {:?}", err);
actions.toast.error("Failed to delete clipboard entry", cx);
} else {
actions
.toast
.success("Successfully deleted clipboard entry", cx);
}
get_text_from_image(&path);
actions.toast.success("Copied Text to Clipboard", cx);
}
},
false,
),
];
if let ClipboardListItemKind::Image { thumbnail } = self.kind.clone() {
actions.insert(
1,
Action::new(
Img::list_icon(Icon::ScanEye, None),
"Copy Text to Clipboard",
None,
{
let mut path = thumbnail.clone();
path.pop();
path = path.join(format!("{}.png", self.id));
move |actions, cx| {
get_text_from_image(&path);
actions.toast.success("Copied Text to Clipboard", cx);
}
},
false,
),
)
}
actions
},
None,
Some(Box::new(self.clone())),
None,
)
)
}
actions
})
.meta(self.copied_last)
.build()
}
fn delete(&self, view: WeakView<AsyncListItems>, cx: &mut WindowContext) -> anyhow::Result<()> {
let _ = view.update(cx, |view, cx| {
Expand Down
Loading

0 comments on commit 3365480

Please sign in to comment.