Only expand one album at a time

This commit is contained in:
Mikko Ahlroth 2023-11-13 20:26:03 +02:00
parent 16214369f5
commit ee70f6742c
4 changed files with 37 additions and 14 deletions

View file

@ -13,6 +13,9 @@ pub type Library {
)
}
/// An ID that can never match any library item
pub const invalid_id = -1
/// Gets an empty library for use as a default value.
pub fn empty() {
Library(albums: map.new(), artists: map.new(), tracks: map.new())

View file

@ -0,0 +1,10 @@
//// Miscellaneous utilities that do not fit anywhere else.
/// Toggle a value with a new value - if the new value is the same as the
/// previous, returns the "null" value. Otherwise returns the new value.
pub fn toggle(prev: a, new: a, null: a) {
case new == prev {
True -> null
False -> new
}
}

View file

@ -3,7 +3,6 @@
import gleam/string
import gleam/list
import gleam/map
import gleam/set
import gleam/option
import gleam/dynamic
import gleam/result
@ -11,7 +10,7 @@ import lustre
import lustre/effect
import lustre/attribute
import lustre/element
import elekf/utils/set as set_utils
import elekf/utils/misc
import elekf/library.{type Library}
import elekf/library/album.{type Album}
import elekf/library/album_utils
@ -23,7 +22,7 @@ import elekf/web/common
const component_name = "albums-view"
type Model {
Model(library_view: library_view.Model(Album), expanded_albums: set.Set(Int))
Model(library_view: library_view.Model(Album), expanded_album: Int)
}
type Msg {
@ -62,7 +61,7 @@ fn init() {
library_view.init(library.empty(), data_getter, shuffler)
#(
Model(library_view: lib_m, expanded_albums: set.new()),
Model(library_view: lib_m, expanded_album: library.invalid_id),
effect.map(lib_e, LibraryViewMsg),
)
}
@ -73,7 +72,11 @@ fn update(model: Model, msg) {
#(
Model(
..model,
expanded_albums: set_utils.toggle(model.expanded_albums, album.0),
expanded_album: misc.toggle(
model.expanded_album,
album.0,
library.invalid_id,
),
),
effect.none(),
)
@ -116,6 +119,6 @@ fn view(model: Model, item: LibraryItem(Album)) {
model.library_view.library,
model.library_view.settings,
item,
set.contains(model.expanded_albums, item.0),
item.0 == model.expanded_album,
)
}

View file

@ -3,7 +3,6 @@
import gleam/string
import gleam/list
import gleam/map
import gleam/set
import gleam/option
import gleam/dynamic
import gleam/result
@ -12,7 +11,7 @@ import lustre/element/html.{div, header}
import lustre/element.{text}
import lustre/attribute
import lustre/effect
import elekf/utils/set as set_utils
import elekf/utils/misc
import elekf/library.{type Library}
import elekf/library/album.{type Album}
import elekf/library/album_utils
@ -28,7 +27,7 @@ type Model {
Model(
library_view: library_view.Model(Album),
artist: option.Option(LibraryItem(Artist)),
expanded_albums: set.Set(Int),
expanded_album: Int,
)
}
@ -85,7 +84,11 @@ fn init() {
)
#(
Model(artist: option.None, expanded_albums: set.new(), library_view: lib_m),
Model(
artist: option.None,
expanded_album: library.invalid_id,
library_view: lib_m,
),
effect.map(lib_e, LibraryViewMsg),
)
}
@ -95,7 +98,7 @@ fn update(model: Model, msg) {
ArtistUpdated(option.Some(artist)) -> #(
Model(
artist: option.Some(artist),
expanded_albums: set.new(),
expanded_album: library.invalid_id,
library_view: library_view.Model(
..model.library_view,
data: data_getter(model.library_view.library, artist),
@ -106,7 +109,7 @@ fn update(model: Model, msg) {
ArtistUpdated(option.None) -> #(
Model(
artist: option.None,
expanded_albums: set.new(),
expanded_album: library.invalid_id,
library_view: library_view.Model(..model.library_view, data: []),
),
effect.none(),
@ -114,7 +117,11 @@ fn update(model: Model, msg) {
LibraryViewMsg(AlbumExpandToggled(album)) -> #(
Model(
..model,
expanded_albums: set_utils.toggle(model.expanded_albums, album.0),
expanded_album: misc.toggle(
model.expanded_album,
album.0,
library.invalid_id,
),
),
effect.none(),
)
@ -186,7 +193,7 @@ fn album_view(model: Model, item: LibraryItem(Album)) {
model.library_view.library,
model.library_view.settings,
item,
set.contains(model.expanded_albums, item.0),
item.0 == model.expanded_album,
)
}