Skip to content

Commit

Permalink
fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Dec 16, 2024
1 parent a746ad6 commit a8c8341
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,16 @@ pub trait DbTrait: Sized {

fn get_from_id(&self, id: EntryId) -> Option<&Self::Entry>;

fn iter(&self) -> Box<dyn Iterator<Item = &'_ Self::Entry> + '_>;
fn iter(&self) -> impl Iterator<Item = &'_ Self::Entry>;

fn search_iter(&self) -> impl Iterator<Item = &'_ Self::Entry>;

fn either_iter(
&self,
) -> itertools::Either<
impl Iterator<Item = &'_ Self::Entry>,
impl Iterator<Item = &'_ Self::Entry>,
>;

fn len(&self) -> usize;

Expand Down
42 changes: 27 additions & 15 deletions src/db/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ impl DbTrait for DbSqlite {
}
})
.collect::<Vec<_>>();
dbg!(&self.filtered);
}
}

Expand Down Expand Up @@ -654,23 +655,34 @@ impl DbTrait for DbSqlite {
self.iter().nth(index)
}

fn iter(&self) -> Box<dyn Iterator<Item = &'_ Self::Entry> + '_> {
if self.is_search_active() {
Box::new(self.filtered.iter().map(|id| &self.entries[id]))
} else {
Box::new(
self.favorites
.fav()
.iter()
fn iter(&self) -> impl Iterator<Item = &'_ Self::Entry> {
self.favorites
.fav()
.iter()
.map(|id| &self.entries[id])
.chain(
self.times
.values()
.map(|id| &self.entries[id])
.chain(
self.times
.values()
.map(|id| &self.entries[id])
.filter(|e| !e.is_favorite)
.rev(),
),
.filter(|e| !e.is_favorite)
.rev(),
)
}

fn search_iter(&self) -> impl Iterator<Item = &'_ Self::Entry> {
self.filtered.iter().map(|id| &self.entries[id])
}

fn either_iter(
&self,
) -> itertools::Either<
impl Iterator<Item = &'_ Self::Entry>,
impl Iterator<Item = &'_ Self::Entry>,
> {
if self.is_search_active() {
itertools::Either::Left(self.search_iter())
} else {
itertools::Either::Right(self.iter())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<Db: DbTrait> AppState<Db> {

let entries_view: Vec<_> = self
.db
.iter()
.either_iter()
.enumerate()
.get(range)
.filter_map(|(pos, data)| {
Expand Down

0 comments on commit a8c8341

Please sign in to comment.