Files
SubMusic/source/View/SubMusicMenuPlaylistsRemoteToggle.mc
memen45 93f0966582 fix race condition (error now shows immediately instead of after Back)
fix Subsonic Unknown Api error: now reports BADRESPONSE
fixed interrupted sync due to max time (serialisation speed improved)
fixed issue with error handling
fixed "No remote playlists", when actually still loading
2021-11-07 15:22:32 +01:00

153 lines
5.1 KiB
MonkeyC

using Toybox.WatchUi;
module SubMusic {
module Menu {
class PlaylistsRemoteToggle extends MenuBase {
private var d_provider = SubMusic.Provider.get();
private var d_loading = false;
function initialize(title) {
MenuBase.initialize(title, false);
d_provider.setFallback(method(:onError));
// items are lazy loaded by a menu loader
}
// function getItem(idx) {
//
// if (items() == null) {
// return null;
// }
//
// // check if out of bounds
// if (idx >= items().size()) {
// return null;
// }
//
// // load playlist from array
// var playlist = items()[idx];
//
// // create checkbox menuitem
// var label = playlist.name();
// var sublabel = playlist.count().toString() + " songs";
// if (!playlist.remote()) {
// sublabel += " - local only";
// }
// var enabled = playlist.local();
// return new WatchUi.ToggleMenuItem(label, sublabel, playlist, enabled, {});
// }
function playlist_to_item(playlist) {
var label = playlist.name();
var sublabel = playlist.count().toString() + " songs";
if (!playlist.remote()) {
sublabel += " - local only";
}
var enabled = playlist.local();
return {
LABEL => label,
SUBLABEL => sublabel,
METHOD => playlist, // identify by playlist
OPTION => enabled, // is Boolean to make it Toggle item
};
}
function load() {
// if already loading, do nothing, wait for response
if (d_loading) {
return false;
}
// load locals (skip for now, as loading menu can only be done once
// (if old menuview is hidden, it will remove the items)
var ids = PlaylistStore.getIds();
for (var idx = 0; idx != ids.size(); ++idx) {
//items().add(playlist_to_item(new IPlaylist(ids[idx])));
}
// start loading
d_loading = true;
d_provider.getAllPlaylists(method(:onGetAllPlaylists));
return false;
}
function onGetAllPlaylists(playlists) {
var ids = PlaylistStore.getIds();
var items = [];
var items_remote = [];
// iterate over remotes first
for (var idx = 0; idx < playlists.size(); ++idx) {
var playlist = playlists[idx];
var id = playlist.id();
// nothing to update if not stored locally
if (ids.indexOf(id) < 0) {
items_remote.add(playlist_to_item(playlist)); // add to the remote items list
continue;
}
// if stored, update
var iplaylist = new IPlaylist(id);
iplaylist.setRemote(playlist.remote());
items.add(playlist_to_item(iplaylist)); // add to the items list
ids.remove(id); // this id is updated already, so remove from the list
}
// update remote state if not found on remote lists
for (var idx = 0; idx < ids.size(); ++idx) {
var iplaylist = new IPlaylist(ids[idx]);
iplaylist.setRemote(false);
items.add(playlist_to_item(iplaylist)); // add to the items list
}
// append remotes to the stored ones
items.addAll(items_remote);
// load items
MenuBase.load(items);
// update the view accordingly
d_loading = false;
MenuBase.onLoaded(null);
}
function placeholder() {
if (d_loading) {
return WatchUi.loadResource(Rez.Strings.fetchingPlaylists);
}
return WatchUi.loadResource(Rez.Strings.placeholder_noRemotePlaylists);
}
function onError(error) {
// loading finished
d_loading = false;
MenuBase.onLoaded(error);
}
function onPlaylistToggle(item) {
var playlist = item.getId();
var id = playlist.id();
var iplaylist = new IPlaylist(id);
// update local
iplaylist.setLocal(item.isEnabled());
if (item.isEnabled()) {
iplaylist.updateMeta(playlist);
}
}
function delegate() {
return new MenuDelegate(method(:onPlaylistToggle), null);
}
}
}
}