From 0aa624ca05ddd4bf160a52eb298d43dbd7bab14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulysse=20G=C3=A9rard?= Date: Mon, 20 May 2024 11:25:49 +0200 Subject: [PATCH] enable order selection --- bin/db_worker/db_worker.ml | 4 +++- bin/main.ml | 31 ++++++++++++++++++++----------- lib/db/view.ml | 11 +++++++++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/bin/db_worker/db_worker.ml b/bin/db_worker/db_worker.ml index 149518c..c7ef045 100644 --- a/bin/db_worker/db_worker.ml +++ b/bin/db_worker/db_worker.ml @@ -182,7 +182,9 @@ module Worker () = struct Array.map indexes ~f:(fun index -> try let index = index + view.start_offset in - let index = Db.View.Order.apply order index in + let index = + Db.View.Order.apply ~size:view.item_count order index + in (* This could be optimize when access is sequential *) let key = keys.(index) in let open Fut.Syntax in diff --git a/bin/main.ml b/bin/main.ml index 6dcd061..e8e121e 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -52,13 +52,20 @@ let app = let open Brr_lwd_ui.Field_select in let options = Lwd.pure - (Lwd_seq.of_list - [ - ("date_added", "Date added"); ("name", "Name"); ("random", "Random"); - ]) + (Lwd_seq.of_list [ ("date_added", "Date added"); ("name", "Name") ]) in make { name = "view-sort"; default = "date_added"; label = [] } options in + let f_order = + let open Brr_lwd_ui.Field_select in + let options = + Lwd.pure + (Lwd_seq.of_list + [ ("asc", "Asc"); ("desc", "Desc"); ("random", "Random") ]) + in + make { name = "view-order"; default = "desc"; label = [] } options + in + let f_sort_order = Lwd.pair f_sort.value f_order.value in let filters, f_value = let f_libraries = let open Brr_lwd_ui.Field_checkboxes in @@ -76,12 +83,18 @@ let app = make { name = "pouet"; desc = Lwd.join choices } in let filters = - Elwd.div [ `R f_sort.field; `R f_search.field; `R f_libraries.field ] + Elwd.div + [ + `R f_sort.field; + `R f_order.field; + `R f_search.field; + `R f_libraries.field; + ] in (filters, f_libraries.value) in let main_view = - Ui_utils.map3 f_value f_search.value f_sort.value ~f:(fun l t s -> + Ui_utils.map3 f_value f_search.value f_sort_order ~f:(fun l t (s, o) -> let filters = Option.map (fun s -> [ Db.View.Search s ]) t in Console.log [ @@ -98,11 +111,7 @@ let app = (Create_view Db.View.(req Audio ~src_views:(Only l) ~sort ?filters ())) in - let order = - match s with - | "random" -> View.Order.random ~size:view.item_count - | _ -> View.Order.Initial - in + let order = View.Order.of_string ~size:view.item_count o in { View.view; first = 0; last = view.item_count; order }) in diff --git a/lib/db/view.ml b/lib/db/view.ml index b8f6976..28623b7 100644 --- a/lib/db/view.ml +++ b/lib/db/view.ml @@ -9,9 +9,16 @@ module Order = struct let () = Array.shuffle tbl in Custom tbl - let apply t i = + let of_string ~size = function + | "random" -> random ~size + | "asc" -> Asc + | "desc" -> Desc + | _ -> Initial + + let apply t ~size i = match t with - | Initial | Asc | Desc -> i + | Initial | Asc -> i + | Desc -> size - 1 - i | Custom a -> (* todo check bounds *) a.(i) end