Model -> store in appropriate places

This commit is contained in:
Sumner Evans
2019-08-27 21:34:46 -06:00
parent 044282e58c
commit 314569b357
5 changed files with 38 additions and 33 deletions

View File

@@ -69,7 +69,7 @@ class ApplicationState:
def load_from_json(self, json_object):
current_song_id = json_object.get('current_song') or None
if current_song_id:
if current_song_id and CacheManager.cache:
self.current_song = CacheManager.cache['song_details'].get(
current_song_id)
else:

View File

@@ -453,7 +453,7 @@ class AlbumWithSongs(Gtk.Box):
margin_left=10,
))
self.album_songs_model = Gtk.ListStore(
self.album_song_store = Gtk.ListStore(
str, # cache status
str, # title
str, # duration
@@ -480,7 +480,7 @@ class AlbumWithSongs(Gtk.Box):
album_details.add(self.loading_indicator)
self.album_songs = Gtk.TreeView(
model=self.album_songs_model,
model=self.album_song_store,
name='album-songs-list',
headers_visible=False, # TODO use the config value for this
margin_top=15,
@@ -518,9 +518,9 @@ class AlbumWithSongs(Gtk.Box):
def on_song_activated(self, treeview, idx, column):
# The song ID is in the last column of the model.
song_id = self.album_songs_model[idx][-1]
song_id = self.album_song_store[idx][-1]
self.emit('song-clicked', song_id,
[m[-1] for m in self.album_songs_model])
[m[-1] for m in self.album_song_store])
def on_song_button_press(self, tree, event):
if event.button == 3: # Right click
@@ -540,7 +540,7 @@ class AlbumWithSongs(Gtk.Box):
paths = [clicked_path[0]]
allow_deselect = True
song_ids = [self.album_songs_model[p][-1] for p in paths]
song_ids = [self.album_song_store[p][-1] for p in paths]
# Used to adjust for the header row.
bin_coords = tree.convert_tree_to_bin_window_coords(
@@ -572,12 +572,12 @@ class AlbumWithSongs(Gtk.Box):
self,
album: Union[AlbumWithSongsID3, Child, Directory],
):
new_model = [[
new_store = [[
util.get_cached_status_icon(CacheManager.get_cached_status(song)),
util.esc(song.title),
util.format_song_duration(song.duration),
song.id,
] for song in album.get('child', album.get('song', []))]
util.diff_model(self.album_songs_model, new_model)
util.diff_store(self.album_song_store, new_store)
self.loading_indicator.hide()

View File

@@ -24,7 +24,7 @@ class CoverArtGrid(Gtk.ScrolledWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# This is the root model. It stores the master list.
# This is the master list.
self.list_store = Gio.ListStore()
self.selected_list_store_index = None
@@ -100,7 +100,9 @@ class CoverArtGrid(Gtk.ScrolledWindow):
print('fail', e)
return
currently_selected = self.grid_top.get_selected_children()
selection = self.grid_top.get_selected_children()
if selection:
self.selected_list_store_index = selection[0].get_index()
self.list_store.remove_all()
for el in result:
@@ -170,14 +172,17 @@ class CoverArtGrid(Gtk.ScrolledWindow):
entries_before_fold = ((
(self.selected_list_store_index // covers_per_row) + 1)
* covers_per_row)
print(entries_before_fold)
# TODO should do diffing on the actual updates here:
# Split the list_store into top and bottom.
util.diff_model(self.list_store_top,
self.list_store[:entries_before_fold])
util.diff_model(self.list_store_bottom,
self.list_store[entries_before_fold:])
util.diff_store(
self.list_store_top,
self.list_store[:entries_before_fold],
)
util.diff_store(
self.list_store_bottom,
self.list_store[entries_before_fold:],
)
# Virtual Methods
# =========================================================================
@@ -213,5 +218,4 @@ class CoverArtGrid(Gtk.ScrolledWindow):
self.selected_list_store_index = (
child.get_index() + (0 if click_top else len(self.list_store_top)))
print('item clicked', self.list_store[self.selected_list_store_index])
self.reflow_grids()

View File

@@ -204,7 +204,7 @@ class PlaylistsPanel(Gtk.Paned):
column.set_expand(not width)
return column
self.playlist_song_model = Gtk.ListStore(
self.playlist_song_store = Gtk.ListStore(
str, # cache status
str, # title
str, # album
@@ -233,7 +233,7 @@ class PlaylistsPanel(Gtk.Paned):
return True
self.playlist_songs = Gtk.TreeView(
model=self.playlist_song_model,
model=self.playlist_song_store,
reorderable=True,
margin_top=15,
enable_search=True,
@@ -264,9 +264,9 @@ class PlaylistsPanel(Gtk.Paned):
# Set up drag-and-drop on the song list for editing the order of the
# playlist.
self.playlist_song_model.connect('row-inserted',
self.playlist_song_store.connect('row-inserted',
self.playlist_model_row_move)
self.playlist_song_model.connect('row-deleted',
self.playlist_song_store.connect('row-deleted',
self.playlist_model_row_move)
playlist_view_scroll_window.add(self.playlist_songs)
@@ -339,7 +339,7 @@ class PlaylistsPanel(Gtk.Paned):
# TODO: Only do this if it's the current playlist.
GLib.idle_add(self.update_playlist_view, playlist.id)
song_ids = [s[-1] for s in self.playlist_song_model]
song_ids = [s[-1] for s in self.playlist_song_store]
CacheManager.batch_download_songs(
song_ids,
before_download=download_state_change,
@@ -353,9 +353,9 @@ class PlaylistsPanel(Gtk.Paned):
def on_song_activated(self, treeview, idx, column):
# The song ID is in the last column of the model.
song_id = self.playlist_song_model[idx][-1]
song_id = self.playlist_song_store[idx][-1]
self.emit('song-clicked', song_id,
[m[-1] for m in self.playlist_song_model])
[m[-1] for m in self.playlist_song_store])
def on_song_button_press(self, tree, event):
if event.button == 3: # Right click
@@ -379,7 +379,7 @@ class PlaylistsPanel(Gtk.Paned):
paths = [clicked_path[0]]
allow_deselect = True
song_ids = [self.playlist_song_model[p][-1] for p in paths]
song_ids = [self.playlist_song_store[p][-1] for p in paths]
# Used to adjust for the header row.
bin_coords = tree.convert_tree_to_bin_window_coords(
@@ -545,7 +545,7 @@ class PlaylistsPanel(Gtk.Paned):
# update the list.
self.editing_playlist_song_list = True
new_model = [[
new_store = [[
util.get_cached_status_icon(CacheManager.get_cached_status(song)),
song.title,
song.album,
@@ -554,7 +554,7 @@ class PlaylistsPanel(Gtk.Paned):
song.id,
] for song in (playlist.entry or [])]
util.diff_model(self.playlist_song_model, new_model)
util.diff_store(self.playlist_song_store, new_store)
self.editing_playlist_song_list = False
self.set_playlist_view_loading(False)
@@ -576,7 +576,7 @@ class PlaylistsPanel(Gtk.Paned):
CacheManager.update_playlist(
playlist_id=playlist.id,
song_index_to_remove=list(range(playlist.songCount)),
song_id_to_add=[s[-1] for s in self.playlist_song_model],
song_id_to_add=[s[-1] for s in self.playlist_song_store],
)
self.update_playlist_song_list(playlist.id, force=True)

View File

@@ -81,11 +81,12 @@ def get_cached_status_icon(cache_status: SongCacheStatus):
return cache_icon[cache_status]
def diff_model(model_to_edit, new_model):
old_model = [row[:] for row in model_to_edit]
def diff_store(store_to_edit, new_store):
# old_store = [row[:] for row in store_to_edit]
old_store = [row for row in store_to_edit]
# Diff the lists to determine what needs to be changed.
diff = DeepDiff(old_model, new_model)
diff = DeepDiff(old_store, new_store)
changed = diff.get('values_changed', {})
added = diff.get('iterable_item_added', {})
removed = diff.get('iterable_item_removed', {})
@@ -96,14 +97,14 @@ def diff_model(model_to_edit, new_model):
for edit_location, diff in changed.items():
idx, field = parse_location(edit_location)
model_to_edit[idx][field] = diff['new_value']
store_to_edit[idx][field] = diff['new_value']
for add_location, value in added.items():
model_to_edit.append(value)
store_to_edit.append(value)
for remove_location, value in reversed(list(removed.items())):
remove_at = parse_location(remove_location)[0]
del model_to_edit[remove_at]
del store_to_edit[remove_at]
def show_song_popover(